initialize method

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

Initializes solevato client repository

Implementation

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

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

    callbacks.onContactResolved?.call(contact);

    //refresh conversation
    final conversations = await clientService.getConversations();
    final persistedConversation =
        localStorage.conversationDao.getConversation();

    if (persistedConversation != null && conversations.length > 0) {
      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);
    }
    listenForEvents();
  } on SolevatoClientException catch (e) {
    callbacks.onError?.call(e);
  }
}