package org.mercury_im.messenger.transport; import org.mercury_im.messenger.Messenger; import org.mercury_im.messenger.data.repository.AccountRepository; import org.mercury_im.messenger.entity.Account; import org.mercury_im.messenger.util.DiffUtil; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.inject.Inject; import io.reactivex.disposables.CompositeDisposable; public class AccountChangedHandler { private final Messenger messenger; private final AccountRepository accountRepository; private final Map accounts = new HashMap<>(); private final CompositeDisposable disposable = new CompositeDisposable(); @Inject public AccountChangedHandler(Messenger messenger, AccountRepository accountRepository) { this.messenger = messenger; this.accountRepository = accountRepository; //disposable.add(accountRepository.observeAllAccounts() // .scan(new ArrayList<>(), this::calculateChangeEvents) // .subscribe(this::onAccountsChanged)); } private HashMap calculateChangeEvents(List accounts, List newAccounts) { DiffUtil.Diff diff = DiffUtil.diff(accounts, newAccounts); return null; } private AccountChangedEvent calculateEvent(Account previous, Account next) { if (previous == null && next == null) { throw new IllegalArgumentException("Not both accounts can be null at the same time!"); } if (previous == null) { if (next.isEnabled()) { return new AccountChangedEvent(next, AccountChangedEvent.EventType.enabled); } else { return null; } } if (next == null) { if (previous.isEnabled()) { return new AccountChangedEvent(previous, AccountChangedEvent.EventType.disabled); } else { return null; } } if (previous.isEnabled()) { if (next.isEnabled()) { return null; } else { return new AccountChangedEvent(next, AccountChangedEvent.EventType.disabled); } } else { if (next.isEnabled()) { return new AccountChangedEvent(next, AccountChangedEvent.EventType.enabled); } else { return null; } } } private void onAccountsChanged(List accounts, List newAccounts) { DiffUtil.Diff diff = DiffUtil.diff(accounts, newAccounts); } }