Line data Source code
1 : import 'package:chatwoot_client_sdk/data/chatwoot_repository.dart'; 2 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_contact_dao.dart'; 3 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_conversation_dao.dart'; 4 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_messages_dao.dart'; 5 : import 'package:chatwoot_client_sdk/data/local/dao/chatwoot_user_dao.dart'; 6 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_contact.dart'; 7 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_conversation.dart'; 8 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_message.dart'; 9 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_user.dart'; 10 : import 'package:chatwoot_client_sdk/data/local/local_storage.dart'; 11 : import 'package:chatwoot_client_sdk/data/remote/service/chatwoot_client_api_interceptor.dart'; 12 : import 'package:chatwoot_client_sdk/data/remote/service/chatwoot_client_auth_service.dart'; 13 : import 'package:chatwoot_client_sdk/data/remote/service/chatwoot_client_service.dart'; 14 : import 'package:chatwoot_client_sdk/chatwoot_parameters.dart'; 15 : import 'package:chatwoot_client_sdk/repository_parameters.dart'; 16 : import 'package:dio/dio.dart'; 17 : import 'package:hive_flutter/hive_flutter.dart'; 18 : import 'package:riverpod/riverpod.dart'; 19 : 20 : ///Provides an instance of [Dio] 21 2 : final unauthenticatedDioProvider = 22 3 : Provider.family.autoDispose<Dio, ChatwootParameters>((ref, params) { 23 3 : return Dio(BaseOptions(baseUrl: params.baseUrl)); 24 : }); 25 : 26 : ///Provides an instance of [ChatwootClientApiInterceptor] 27 2 : final chatwootClientApiInterceptorProvider = 28 1 : Provider.family<ChatwootClientApiInterceptor, ChatwootParameters>( 29 1 : (ref, params) { 30 3 : final localStorage = ref.read(localStorageProvider(params)); 31 3 : final authService = ref.read(chatwootClientAuthServiceProvider(params)); 32 1 : return ChatwootClientApiInterceptor( 33 1 : params.inboxIdentifier, localStorage, authService); 34 : }); 35 : 36 : ///Provides an instance of Dio with interceptors set to authenticate all requests called with this dio instance 37 2 : final authenticatedDioProvider = 38 3 : Provider.family.autoDispose<Dio, ChatwootParameters>((ref, params) { 39 3 : final authenticatedDio = Dio(BaseOptions(baseUrl: params.baseUrl)); 40 3 : final interceptor = ref.read(chatwootClientApiInterceptorProvider(params)); 41 2 : authenticatedDio.interceptors.add(interceptor); 42 : return authenticatedDio; 43 : }); 44 : 45 : ///Provides instance of chatwoot client auth service [ChatwootClientAuthService]. 46 2 : final chatwootClientAuthServiceProvider = 47 1 : Provider.family<ChatwootClientAuthService, ChatwootParameters>( 48 1 : (ref, params) { 49 3 : final unAuthenticatedDio = ref.read(unauthenticatedDioProvider(params)); 50 1 : return ChatwootClientAuthServiceImpl(dio: unAuthenticatedDio); 51 : }); 52 : 53 : ///Provides instance of chatwoot client api service [ChatwootClientService]. 54 0 : final chatwootClientServiceProvider = 55 : Provider.family<ChatwootClientService, ChatwootParameters>((ref, params) { 56 : final authenticatedDio = ref.read(authenticatedDioProvider(params)); 57 : return ChatwootClientServiceImpl(params.baseUrl, dio: authenticatedDio); 58 : }); 59 : 60 : ///Provides hive box to store relations between chatwoot client instance and contact object, 61 : ///which is used when persistence is enabled. Client instances are distinguished using baseurl and inboxIdentifier 62 4 : final clientInstanceToContactBoxProvider = Provider<Box<String>>((ref) { 63 2 : return Hive.box<String>( 64 1 : ChatwootContactBoxNames.CLIENT_INSTANCE_TO_CONTACTS.toString()); 65 : }); 66 : 67 : ///Provides hive box to store relations between chatwoot client instance and conversation object, 68 : ///which is used when persistence is enabled. Client instances are distinguished using baseurl and inboxIdentifier 69 4 : final clientInstanceToConversationBoxProvider = Provider<Box<String>>((ref) { 70 2 : return Hive.box<String>( 71 1 : ChatwootConversationBoxNames.CLIENT_INSTANCE_TO_CONVERSATIONS.toString()); 72 : }); 73 : 74 : ///Provides hive box to store relations between chatwoot client instance and messages, 75 : ///which is used when persistence is enabled. Client instances are distinguished using baseurl and inboxIdentifier 76 4 : final messageToClientInstanceBoxProvider = Provider<Box<String>>((ref) { 77 2 : return Hive.box<String>( 78 1 : ChatwootMessagesBoxNames.MESSAGES_TO_CLIENT_INSTANCE_KEY.toString()); 79 : }); 80 : 81 : ///Provides hive box to store relations between chatwoot client instance and user object, 82 : ///which is used when persistence is enabled. Client instances are distinguished using baseurl and inboxIdentifier 83 4 : final clientInstanceToUserBoxProvider = Provider<Box<String>>((ref) { 84 2 : return Hive.box<String>( 85 1 : ChatwootUserBoxNames.CLIENT_INSTANCE_TO_USER.toString()); 86 : }); 87 : 88 : ///Provides hive box for [ChatwootContact] object, which is used when persistence is enabled 89 4 : final contactBoxProvider = Provider<Box<ChatwootContact>>((ref) { 90 3 : return Hive.box<ChatwootContact>(ChatwootContactBoxNames.CONTACTS.toString()); 91 : }); 92 : 93 : ///Provides hive box for [ChatwootConversation] object, which is used when persistence is enabled 94 4 : final conversationBoxProvider = Provider<Box<ChatwootConversation>>((ref) { 95 2 : return Hive.box<ChatwootConversation>( 96 1 : ChatwootConversationBoxNames.CONVERSATIONS.toString()); 97 : }); 98 : 99 : ///Provides hive box for [ChatwootMessage] object, which is used when persistence is enabled 100 4 : final messagesBoxProvider = Provider<Box<ChatwootMessage>>((ref) { 101 2 : return Hive.box<ChatwootMessage>( 102 1 : ChatwootMessagesBoxNames.MESSAGES.toString()); 103 : }); 104 : 105 : ///Provides hive box for [ChatwootUser] object, which is used when persistence is enabled 106 4 : final userBoxProvider = Provider<Box<ChatwootUser>>((ref) { 107 3 : return Hive.box<ChatwootUser>(ChatwootUserBoxNames.USERS.toString()); 108 : }); 109 : 110 : ///Provides an instance of chatwoot user dao 111 : /// 112 : /// Creates an in memory storage if persistence isn't enabled in params else hive boxes are create to store 113 : /// chatwoot client's contact 114 2 : final chatwootContactDaoProvider = 115 2 : Provider.family<ChatwootContactDao, ChatwootParameters>((ref, params) { 116 1 : if (!params.isPersistenceEnabled) { 117 1 : return NonPersistedChatwootContactDao(); 118 : } 119 : 120 2 : final contactBox = ref.read(contactBoxProvider); 121 : final clientInstanceToContactBox = 122 2 : ref.read(clientInstanceToContactBoxProvider); 123 1 : return PersistedChatwootContactDao( 124 1 : contactBox, clientInstanceToContactBox, params.clientInstanceKey); 125 : }); 126 : 127 : ///Provides an instance of chatwoot user dao 128 : /// 129 : /// Creates an in memory storage if persistence isn't enabled in params else hive boxes are create to store 130 : /// chatwoot client's conversation 131 2 : final chatwootConversationDaoProvider = 132 2 : Provider.family<ChatwootConversationDao, ChatwootParameters>((ref, params) { 133 1 : if (!params.isPersistenceEnabled) { 134 1 : return NonPersistedChatwootConversationDao(); 135 : } 136 2 : final conversationBox = ref.read(conversationBoxProvider); 137 : final clientInstanceToConversationBox = 138 2 : ref.read(clientInstanceToConversationBoxProvider); 139 1 : return PersistedChatwootConversationDao(conversationBox, 140 1 : clientInstanceToConversationBox, params.clientInstanceKey); 141 : }); 142 : 143 : ///Provides an instance of chatwoot user dao 144 : /// 145 : /// Creates an in memory storage if persistence isn't enabled in params else hive boxes are create to store 146 : /// chatwoot client's messages 147 2 : final chatwootMessagesDaoProvider = 148 2 : Provider.family<ChatwootMessagesDao, ChatwootParameters>((ref, params) { 149 1 : if (!params.isPersistenceEnabled) { 150 1 : return NonPersistedChatwootMessagesDao(); 151 : } 152 2 : final messagesBox = ref.read(messagesBoxProvider); 153 : final messageToClientInstanceBox = 154 2 : ref.read(messageToClientInstanceBoxProvider); 155 1 : return PersistedChatwootMessagesDao( 156 1 : messagesBox, messageToClientInstanceBox, params.clientInstanceKey); 157 : }); 158 : 159 : ///Provides an instance of chatwoot user dao 160 : /// 161 : /// Creates an in memory storage if persistence isn't enabled in params else hive boxes are create to store 162 : /// user info 163 2 : final chatwootUserDaoProvider = 164 2 : Provider.family<ChatwootUserDao, ChatwootParameters>((ref, params) { 165 1 : if (!params.isPersistenceEnabled) { 166 1 : return NonPersistedChatwootUserDao(); 167 : } 168 2 : final userBox = ref.read(userBoxProvider); 169 2 : final clientInstanceToUserBoxBox = ref.read(clientInstanceToUserBoxProvider); 170 1 : return PersistedChatwootUserDao( 171 1 : userBox, clientInstanceToUserBoxBox, params.clientInstanceKey); 172 : }); 173 : 174 : ///Provides an instance of local storage 175 4 : final localStorageProvider = 176 3 : Provider.family<LocalStorage, ChatwootParameters>((ref, params) { 177 3 : final contactDao = ref.read(chatwootContactDaoProvider(params)); 178 3 : final conversationDao = ref.read(chatwootConversationDaoProvider(params)); 179 3 : final userDao = ref.read(chatwootUserDaoProvider(params)); 180 3 : final messagesDao = ref.read(chatwootMessagesDaoProvider(params)); 181 : 182 1 : return LocalStorage( 183 : contactDao: contactDao, 184 : conversationDao: conversationDao, 185 : userDao: userDao, 186 : messagesDao: messagesDao); 187 : }); 188 : 189 : ///Provides an instance of chatwoot repository 190 2 : final chatwootRepositoryProvider = 191 1 : Provider.family<ChatwootRepository, RepositoryParameters>( 192 0 : (ref, repoParams) { 193 0 : final localStorage = ref.read(localStorageProvider(repoParams.params)); 194 : final clientService = 195 0 : ref.read(chatwootClientServiceProvider(repoParams.params)); 196 : 197 0 : return ChatwootRepositoryImpl( 198 : clientService: clientService, 199 : localStorage: localStorage, 200 0 : streamCallbacks: repoParams.callbacks); 201 : });