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