@Override
  public void tearDown() {
    if (selectedConvSubscription != null) {
      selectedConvSubscription.cancel();
    }

    if (syncIndicator != null) {
      syncIndicator.removeUpdateListener(syncStateUpdateListener);
      syncIndicator = null;
    }

    if (conversationsList != null) {
      conversationsList.removeUpdateListener(conversationListUpdateListener);
      conversationsList = null;
    }

    if (inboxList != null) {
      inboxList.removeUpdateListener(inboxListUpdateListener);
      inboxList = null;
    }
    if (menuConversation != null) {
      menuConversation.removeUpdateListener(menuConversationUpdateListener);
      menuConversation = null;
    }

    establishedConversationsList = null;
    selectedConvSubscription = null;
    selectedConversation = null;
    conversationUiSignal = null;
  }
  public ScalaConversationStore(ZMessagingApi zMessagingApi) {
    conversationsList = zMessagingApi.getConversations();
    establishedConversationsList = conversationsList.getEstablishedConversations();
    inboxList = conversationsList.getIncomingConversations();
    conversationUiSignal = conversationsList.selectedConversation();
    selectedConvSubscription =
        conversationUiSignal.subscribe(
            new Subscriber<IConversation>() {
              @Override
              public void next(IConversation value) {
                IConversation prev = selectedConversation;
                selectedConversation = value;

                boolean changeSelectedConversation =
                    selectedConversation == null
                        && (conversationsList.size() > 0 || inboxList.size() > 0);
                if (conversationsList.isReady() && changeSelectedConversation) {
                  identifyCurrentConversation(prev);
                } else {
                  // TODO: Check with SE. In some cases like clicking on inapp-notification signal
                  // will also notify when conversation changes to another conversation
                  boolean conversationChanged =
                      (prev != null
                          && selectedConversation != null
                          && !prev.getId().equals(selectedConversation.getId()));
                  ConversationChangeRequester changeRequester =
                      conversationChanged
                          ? conversationChangeRequester
                          : ConversationChangeRequester.UPDATER;
                  notifyCurrentConversationHasChanged(prev, selectedConversation, changeRequester);
                }
              }
            });

    conversationsList.addUpdateListener(conversationListUpdateListener);
    conversationListUpdateListener.updated();
    conversationsList.onVerificationStateChange(verificationStateCallback);
    inboxList.addUpdateListener(inboxListUpdateListener);

    syncIndicator = conversationsList.getSyncIndicator();
    syncIndicator.addUpdateListener(syncStateUpdateListener);
  }
 @Override
 public void updated() {
   notifySyncChanged(syncIndicator.getState());
 }
 @Override
 public SyncState getConversationSyncingState() {
   return syncIndicator.getState();
 }