Line data Source code
1 : 2 : import 'package:chatwoot_client_sdk/chatwoot_callbacks.dart'; 3 : import 'package:chatwoot_client_sdk/data/chatwoot_repository.dart'; 4 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_user.dart'; 5 : import 'package:chatwoot_client_sdk/data/remote/requests/chatwoot_action_data.dart'; 6 : import 'package:chatwoot_client_sdk/data/remote/requests/chatwoot_new_message_request.dart'; 7 : import 'package:chatwoot_client_sdk/di/modules.dart'; 8 : import 'package:chatwoot_client_sdk/chatwoot_parameters.dart'; 9 : import 'package:chatwoot_client_sdk/repository_parameters.dart'; 10 : import 'package:riverpod/riverpod.dart'; 11 : 12 : import 'data/local/local_storage.dart'; 13 : 14 : 15 : /// Represents a chatwoot client instance 16 : /// 17 : /// Results from repository operations are passed through [callbacks] to be handled 18 : /// appropriately 19 : class ChatwootClient{ 20 : 21 : late final ChatwootRepository _repository; 22 : final ChatwootParameters _parameters; 23 : final ChatwootCallbacks? callbacks; 24 : final ChatwootUser? user; 25 : 26 3 : String get baseUrl => _parameters.baseUrl; 27 : 28 3 : String get inboxIdentifier => _parameters.inboxIdentifier; 29 : 30 : 31 1 : ChatwootClient._( 32 : this._parameters,{ 33 : this.user, 34 : this.callbacks 35 : }){ 36 4 : providerContainerMap.putIfAbsent(_parameters.clientInstanceKey, () => ProviderContainer()); 37 4 : final container = providerContainerMap[_parameters.clientInstanceKey]!; 38 2 : _repository = container.read( 39 2 : chatwootRepositoryProvider( 40 1 : RepositoryParameters( 41 1 : params: _parameters, 42 2 : callbacks: callbacks ?? ChatwootCallbacks() 43 : ) 44 : ) 45 : ); 46 : } 47 : 48 1 : Future<void> _init() async{ 49 4 : await _repository.initialize(user); 50 : } 51 : 52 1 : void loadMessages() async{ 53 2 : _repository.getPersistedMessages(); 54 3 : await _repository.getMessages(); 55 : } 56 : 57 1 : Future<void> sendMessage({ 58 : required String content, 59 : required String echoId 60 : }) async{ 61 1 : final request = ChatwootNewMessageRequest( 62 : content: content, 63 : echoId: echoId 64 : ); 65 3 : await _repository.sendMessage(request); 66 : } 67 : 68 1 : Future<void> sendAction(ChatwootActionType action) async{ 69 2 : _repository.sendAction(action); 70 : } 71 : 72 : 73 1 : dispose(){ 74 4 : final container = providerContainerMap[_parameters.clientInstanceKey]!; 75 2 : _repository.dispose(); 76 1 : container.dispose(); 77 4 : providerContainerMap.remove(_parameters.clientInstanceKey); 78 : } 79 : 80 1 : clearClientData(){ 81 4 : final container = providerContainerMap[_parameters.clientInstanceKey]!; 82 4 : final localStorage = container.read(localStorageProvider(_parameters)); 83 1 : localStorage.clear(clearChatwootUserStorage: false); 84 : } 85 : 86 : 87 : 88 : 89 : 90 1 : static Future<ChatwootClient> create({ 91 : required String baseUrl, 92 : required String inboxIdentifier, 93 : ChatwootUser? user, 94 : bool enableMessagesPersistence = false, 95 : ChatwootCallbacks? callbacks 96 : }) async { 97 : 98 : if(enableMessagesPersistence){ 99 1 : LocalStorage.openDB(); 100 : } 101 : 102 1 : final chatwootParams = ChatwootParameters( 103 2 : clientInstanceKey: getClientInstanceKey(baseUrl: baseUrl, inboxIdentifier: inboxIdentifier, userIdentifier: user?.identifier), 104 : isPersistenceEnabled: enableMessagesPersistence, 105 : baseUrl: baseUrl, 106 : inboxIdentifier: inboxIdentifier, 107 1 : userIdentifier: user?.identifier 108 : ); 109 : 110 1 : final client = ChatwootClient._( 111 : chatwootParams, 112 : callbacks: callbacks, 113 : user: user 114 : ); 115 : 116 2 : await client._init(); 117 : 118 : return client; 119 : } 120 : 121 : static final keySeparator= "|||"; 122 1 : static String getClientInstanceKey({ 123 : required String baseUrl, 124 : required String inboxIdentifier, 125 : String? userIdentifier 126 : }){ 127 1 : return "$baseUrl$keySeparator$userIdentifier$keySeparator$inboxIdentifier"; 128 : } 129 : 130 3 : static Map<String, ProviderContainer> providerContainerMap = Map(); 131 : 132 1 : static Future<void> clearData({ 133 : required String baseUrl, 134 : required String inboxIdentifier, 135 : String? userIdentifier 136 : }) async{ 137 : 138 1 : final clientInstanceKey = getClientInstanceKey( 139 : baseUrl: baseUrl, 140 : inboxIdentifier: inboxIdentifier, 141 : userIdentifier: userIdentifier 142 : ); 143 2 : providerContainerMap.putIfAbsent(clientInstanceKey, () => ProviderContainer()); 144 2 : final container = providerContainerMap[clientInstanceKey]!; 145 1 : final params = ChatwootParameters( 146 : isPersistenceEnabled: true, 147 : baseUrl: "", 148 : inboxIdentifier: "", 149 : clientInstanceKey: "" 150 : ); 151 : 152 3 : final localStorage = container.read(localStorageProvider(params)); 153 2 : await localStorage.clear(); 154 : 155 1 : localStorage.dispose(); 156 1 : container.dispose(); 157 2 : providerContainerMap.remove(clientInstanceKey); 158 : } 159 : 160 1 : static Future<void> clearAllData() async{ 161 2 : providerContainerMap.putIfAbsent("all", () => ProviderContainer()); 162 2 : final container = providerContainerMap["all"]!; 163 1 : final params = ChatwootParameters( 164 : isPersistenceEnabled: true, 165 : baseUrl: "", 166 : inboxIdentifier: "", 167 : clientInstanceKey: "" 168 : ); 169 : 170 3 : final localStorage = container.read(localStorageProvider(params)); 171 2 : await localStorage.clearAll(); 172 : 173 1 : localStorage.dispose(); 174 1 : container.dispose(); 175 : } 176 : 177 : 178 : }