Line data Source code
1 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_contact_dao.dart'; 2 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_conversation_dao.dart'; 3 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_messages_dao.dart'; 4 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_user_dao.dart'; 5 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_conversation.dart'; 6 : import 'package:chatwoot_client_sdk/data/remote/responses/chatwoot_event.dart'; 7 : import 'package:hive_flutter/hive_flutter.dart'; 8 : 9 : import 'entity/chatwoot_contact.dart'; 10 : import 'entity/chatwoot_conversation.dart'; 11 : import 'entity/chatwoot_message.dart'; 12 : import 'entity/chatwoot_user.dart'; 13 : 14 : const CHATWOOT_CONTACT_HIVE_TYPE_ID = 0; 15 : const CHATWOOT_CONVERSATION_HIVE_TYPE_ID = 1; 16 : const CHATWOOT_MESSAGE_HIVE_TYPE_ID = 2; 17 : const CHATWOOT_USER_HIVE_TYPE_ID = 3; 18 : const CHATWOOT_EVENT_USER_HIVE_TYPE_ID = 4; 19 : 20 : class LocalStorage { 21 : ChatwootUserDao userDao; 22 : ChatwootConversationDao conversationDao; 23 : ChatwootContactDao contactDao; 24 : ChatwootMessagesDao messagesDao; 25 : 26 2 : LocalStorage({ 27 : required this.userDao, 28 : required this.conversationDao, 29 : required this.contactDao, 30 : required this.messagesDao, 31 : }); 32 : 33 2 : static Future<void> openDB({void Function()? onInitializeHive}) async { 34 : if (onInitializeHive == null) { 35 3 : await Hive.initFlutter(); 36 2 : if (!Hive.isAdapterRegistered(CHATWOOT_CONTACT_HIVE_TYPE_ID)) { 37 3 : Hive..registerAdapter(ChatwootContactAdapter()); 38 : } 39 2 : if (!Hive.isAdapterRegistered(CHATWOOT_CONVERSATION_HIVE_TYPE_ID)) { 40 3 : Hive..registerAdapter(ChatwootConversationAdapter()); 41 : } 42 2 : if (!Hive.isAdapterRegistered(CHATWOOT_MESSAGE_HIVE_TYPE_ID)) { 43 3 : Hive..registerAdapter(ChatwootMessageAdapter()); 44 : } 45 2 : if (!Hive.isAdapterRegistered(CHATWOOT_EVENT_USER_HIVE_TYPE_ID)) { 46 3 : Hive..registerAdapter(ChatwootEventMessageUserAdapter()); 47 : } 48 2 : if (!Hive.isAdapterRegistered(CHATWOOT_USER_HIVE_TYPE_ID)) { 49 3 : Hive..registerAdapter(ChatwootUserAdapter()); 50 : } 51 : } else { 52 1 : onInitializeHive(); 53 : } 54 : 55 4 : await PersistedChatwootContactDao.openDB(); 56 4 : await PersistedChatwootConversationDao.openDB(); 57 4 : await PersistedChatwootMessagesDao.openDB(); 58 4 : await PersistedChatwootUserDao.openDB(); 59 : } 60 : 61 1 : Future<void> clear({bool clearChatwootUserStorage = true}) async { 62 3 : await conversationDao.deleteConversation(); 63 3 : await contactDao.deleteContact(); 64 3 : await messagesDao.clear(); 65 : if (clearChatwootUserStorage) { 66 3 : await userDao.deleteUser(); 67 : } 68 : } 69 : 70 1 : Future<void> clearAll() async { 71 3 : await conversationDao.clearAll(); 72 3 : await contactDao.clearAll(); 73 3 : await messagesDao.clearAll(); 74 3 : await userDao.clearAll(); 75 : } 76 : 77 1 : dispose() { 78 2 : userDao.onDispose(); 79 2 : conversationDao.onDispose(); 80 2 : contactDao.onDispose(); 81 2 : messagesDao.onDispose(); 82 : } 83 : }