initialize method

Future<void> initialize(
  1. ChatwootUser? user
)
override

Initializes chatwoot client repository

Implementation

Future<void> initialize(ChatwootUser? user) async {
  try {
    if (user != null) {
      await localStorage.userDao.saveUser(user);
    }

    //refresh contact
    final contact = await clientService.getContact();
    localStorage.contactDao.saveContact(contact);

    //refresh conversation
    final conversations = await clientService.getConversations();
    final persistedConversation =
        localStorage.conversationDao.getConversation()!;
    final refreshedConversation = conversations.firstWhere(
        (element) => element.id == persistedConversation.id,
        orElse: () =>
            persistedConversation //highly unlikely orElse will be called but still added it just in case
        );
    localStorage.conversationDao.saveConversation(refreshedConversation);
  } on ChatwootClientException catch (e) {
    callbacks.onError?.call(e);
  }

  listenForEvents();
}