Line data Source code
1 : import 'package:hive_flutter/hive_flutter.dart'; 2 : 3 : import '../entity/chatwoot_contact.dart'; 4 : 5 : ///Data access object for retriving chatwoot contact from local storage 6 : abstract class ChatwootContactDao { 7 : Future<void> saveContact(ChatwootContact contact); 8 : ChatwootContact? getContact(); 9 : Future<void> deleteContact(); 10 : Future<void> onDispose(); 11 : Future<void> clearAll(); 12 : } 13 : 14 : //Only used when persistence is enabled 15 19 : enum ChatwootContactBoxNames { CONTACTS, CLIENT_INSTANCE_TO_CONTACTS } 16 : 17 : class PersistedChatwootContactDao extends ChatwootContactDao { 18 : //box containing all persisted contacts 19 : Box<ChatwootContact> _box; 20 : 21 : //_box with one to one relation between generated client instance id and conversation id 22 : final Box<String> _clientInstanceIdToContactIdentifierBox; 23 : 24 : final String _clientInstanceKey; 25 : 26 2 : PersistedChatwootContactDao(this._box, 27 : this._clientInstanceIdToContactIdentifierBox, this._clientInstanceKey); 28 : 29 : @override 30 1 : Future<void> deleteContact() async { 31 : final contactIdentifier = 32 3 : _clientInstanceIdToContactIdentifierBox.get(_clientInstanceKey); 33 4 : await _clientInstanceIdToContactIdentifierBox.delete(_clientInstanceKey); 34 3 : await _box.delete(contactIdentifier); 35 : } 36 : 37 : @override 38 1 : Future<void> saveContact(ChatwootContact contact) async { 39 3 : await _clientInstanceIdToContactIdentifierBox.put( 40 2 : _clientInstanceKey, contact.contactIdentifier!); 41 4 : await _box.put(contact.contactIdentifier, contact); 42 : } 43 : 44 1 : @override 45 : ChatwootContact? getContact() { 46 4 : if (_box.values.length == 0) { 47 : return null; 48 : } 49 : 50 : final contactIdentifier = 51 3 : _clientInstanceIdToContactIdentifierBox.get(_clientInstanceKey); 52 : 53 : if (contactIdentifier == null) { 54 : return null; 55 : } 56 : 57 2 : return _box.get(contactIdentifier, defaultValue: null); 58 : } 59 : 60 : @override 61 0 : Future<void> onDispose() async {} 62 : 63 1 : Future<void> clearAll() async { 64 3 : await _box.clear(); 65 3 : await _clientInstanceIdToContactIdentifierBox.clear(); 66 : } 67 : 68 3 : static Future<void> openDB() async { 69 9 : await Hive.openBox<ChatwootContact>( 70 3 : ChatwootContactBoxNames.CONTACTS.toString()); 71 9 : await Hive.openBox<String>( 72 3 : ChatwootContactBoxNames.CLIENT_INSTANCE_TO_CONTACTS.toString()); 73 : } 74 : } 75 : 76 : class NonPersistedChatwootContactDao extends ChatwootContactDao { 77 : ChatwootContact? _contact; 78 : 79 : @override 80 1 : Future<void> deleteContact() async { 81 1 : _contact = null; 82 : } 83 : 84 1 : @override 85 : ChatwootContact? getContact() { 86 1 : return _contact; 87 : } 88 : 89 : @override 90 1 : Future<void> onDispose() async { 91 1 : _contact = null; 92 : } 93 : 94 : @override 95 1 : Future<void> saveContact(ChatwootContact contact) async { 96 1 : _contact = contact; 97 : } 98 : 99 1 : Future<void> clearAll() async { 100 1 : _contact = null; 101 : } 102 : }