Line data Source code
1 : 2 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_conversation.dart'; 3 : import 'package:hive_flutter/hive_flutter.dart'; 4 : 5 : 6 : abstract class ChatwootConversationDao{ 7 : Future<void> saveConversation(ChatwootConversation conversation); 8 : ChatwootConversation? getConversation(); 9 : Future<void> deleteConversation(); 10 : Future<void> onDispose(); 11 : Future<void> clearAll(); 12 : } 13 : 14 : //Only used when persistence is enabled 15 19 : enum ChatwootConversationBoxNames{ 16 : CONVERSATIONS, CLIENT_INSTANCE_TO_CONVERSATIONS 17 : } 18 : class PersistedChatwootConversationDao extends ChatwootConversationDao{ 19 : 20 : 21 : //box containing all persisted conversations 22 : Box<ChatwootConversation> _box; 23 : 24 : //box with one to one relation between generated client instance id and conversation id 25 : final Box<String> _clientInstanceIdToConversationIdentifierBox; 26 : 27 : final String _clientInstanceKey; 28 : 29 2 : PersistedChatwootConversationDao( 30 : this._box, 31 : this._clientInstanceIdToConversationIdentifierBox, 32 : this._clientInstanceKey 33 : ); 34 : 35 : @override 36 1 : Future<void> deleteConversation() async{ 37 2 : final conversationIdentifier = _clientInstanceIdToConversationIdentifierBox.get( 38 1 : _clientInstanceKey 39 : ); 40 3 : await _clientInstanceIdToConversationIdentifierBox.delete( 41 1 : _clientInstanceKey 42 : ); 43 3 : await _box.delete(conversationIdentifier); 44 : } 45 : 46 : @override 47 1 : Future<void> saveConversation(ChatwootConversation conversation) async{ 48 3 : await _clientInstanceIdToConversationIdentifierBox.put( 49 1 : _clientInstanceKey, 50 2 : conversation.id.toString() 51 : ); 52 4 : await _box.put(conversation.id, conversation); 53 : } 54 : 55 1 : @override 56 : ChatwootConversation? getConversation() { 57 4 : if(_box.values.length==0){ 58 : return null; 59 : } 60 : 61 2 : final conversationidentifierString = _clientInstanceIdToConversationIdentifierBox.get( 62 1 : _clientInstanceKey 63 : ); 64 1 : final conversationIdentifier = int.tryParse(conversationidentifierString ?? ""); 65 : 66 : if(conversationIdentifier == null){ 67 : return null; 68 : } 69 : 70 2 : return _box.get(conversationIdentifier); 71 : } 72 : 73 : @override 74 1 : Future<void> onDispose() async{ 75 3 : await _box.close(); 76 : } 77 : 78 : 79 3 : static Future<void> openDB() async{ 80 12 : await Hive.openBox<ChatwootConversation>(ChatwootConversationBoxNames.CONVERSATIONS.toString()); 81 12 : await Hive.openBox<String>(ChatwootConversationBoxNames.CLIENT_INSTANCE_TO_CONVERSATIONS.toString()); 82 : } 83 : 84 : @override 85 1 : Future<void> clearAll() async{ 86 3 : await _box.clear(); 87 3 : await _clientInstanceIdToConversationIdentifierBox.clear(); 88 : } 89 : 90 : } 91 : 92 : class NonPersistedChatwootConversationDao extends ChatwootConversationDao{ 93 : 94 : ChatwootConversation? _conversation; 95 : 96 : @override 97 1 : Future<void> deleteConversation() async{ 98 1 : _conversation = null; 99 : } 100 : 101 1 : @override 102 : ChatwootConversation? getConversation(){ 103 1 : return _conversation; 104 : } 105 : 106 : @override 107 1 : Future<void> onDispose() async{ 108 1 : _conversation = null; 109 : } 110 : 111 : @override 112 1 : Future<void> saveConversation(ChatwootConversation conversation) async{ 113 1 : _conversation = conversation; 114 : } 115 : 116 : @override 117 1 : Future<void> clearAll() async{ 118 1 : _conversation = null; 119 : } 120 : 121 : 122 : }