Improve last commit

This commit is contained in:
Paul Schaub 2019-12-21 03:13:00 +01:00
parent 749c4ec89c
commit 7d1714e47a
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
1 changed files with 9 additions and 15 deletions

View File

@ -3,9 +3,8 @@ package org.mercury_im.messenger;
import android.app.Application; import android.app.Application;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.util.Log;
import org.mercury_im.messenger.data.repository.Repositories; import org.mercury_im.messenger.data.repository.AccountRepository;
import org.mercury_im.messenger.di.component.AppComponent; import org.mercury_im.messenger.di.component.AppComponent;
import org.mercury_im.messenger.di.component.DaggerAppComponent; import org.mercury_im.messenger.di.component.DaggerAppComponent;
import org.mercury_im.messenger.di.module.AppModule; import org.mercury_im.messenger.di.module.AppModule;
@ -32,7 +31,7 @@ public class MercuryImApplication extends Application {
private final CompositeDisposable disposable = new CompositeDisposable(); private final CompositeDisposable disposable = new CompositeDisposable();
@Inject @Inject
Repositories repositories; AccountRepository accountRepository;
public static MercuryImApplication getApplication() { public static MercuryImApplication getApplication() {
return INSTANCE; return INSTANCE;
@ -49,7 +48,7 @@ public class MercuryImApplication extends Application {
registerActivityLifecycleCallbacks(clientStateHandler); registerActivityLifecycleCallbacks(clientStateHandler);
Notifications.initializeNotificationChannels(this); Notifications.initializeNotificationChannels(this);
observeAccounts(); subscribeForegroundServiceToActiveAccounts();
} }
/** /**
@ -69,31 +68,26 @@ public class MercuryImApplication extends Application {
return appComponent; return appComponent;
} }
private void observeAccounts() { private void subscribeForegroundServiceToActiveAccounts() {
disposable.add(repositories.getAccountRepository().observeAllAccounts() disposable.add(accountRepository.observeAllAccounts()
.map(this::listContainsActiveAccount) .map(this::listContainsActiveAccount)
.distinctUntilChanged() .distinctUntilChanged()
.subscribe(foregroundServiceNecessary -> { .subscribe(foregroundServiceNeeded -> {
if (foregroundServiceNecessary) { if (foregroundServiceNeeded) {
Log.d("MercuryIM", "Enable foreground service.");
startForegroundService(); startForegroundService();
} else { } else {
Log.d("MercuryIM", "Disable foreground service.");
stopForegroundService(); stopForegroundService();
} }
})); }));
} }
private boolean listContainsActiveAccount(List<Account> accounts) { private boolean listContainsActiveAccount(List<Account> accounts) {
Log.d("MercuryIM", "listContainsActiveAccount size: " + accounts.size());
boolean containsActiveAccount = false;
for (Account account : accounts) { for (Account account : accounts) {
if (account.isEnabled()) { if (account.isEnabled()) {
containsActiveAccount = true; return true;
break;
} }
} }
return containsActiveAccount; return false;
} }
private void startForegroundService() { private void startForegroundService() {