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