Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e7d6f85e authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

add AccountsChangeListener to Preferences

parent 4160a1b4
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
package com.fsck.k9;


public interface AccountsChangeListener {
    void onAccountsChanged();
}
+23 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import java.util.Map;
import java.util.UUID;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;

@@ -48,7 +49,8 @@ public class Preferences {
    private Map<String, Account> accounts = null;
    private List<Account> accountsInOrder = null;
    private Account newAccount;
    private Context context;
    private final ArrayList<AccountsChangeListener> accountsChangeListeners = new ArrayList<>();
    private final Context context;
    private final LocalStoreProvider localStoreProvider;
    private final CoreResourceProvider resourceProvider;
    private final LocalKeyStoreManager localKeyStoreManager;
@@ -182,6 +184,8 @@ public class Preferences {
        if (newAccount == account) {
            newAccount = null;
        }

        notifyListeners();
    }

    /**
@@ -223,6 +227,8 @@ public class Preferences {
        StorageEditor editor = createStorageEditor();
        accountPreferenceSerializer.save(editor, storage, account);
        editor.commit();

        notifyListeners();
    }

    private void ensureAssignedAccountNumber(Account account) {
@@ -286,5 +292,21 @@ public class Preferences {
        accountPreferenceSerializer.move(storageEditor, account, storage, mUp);
        storageEditor.commit();
        loadAccounts();

        notifyListeners();
    }

    private void notifyListeners() {
        for (AccountsChangeListener listener : accountsChangeListeners) {
            listener.onAccountsChanged();
        }
    }

    public void addOnAccountsChangeListener(@NonNull AccountsChangeListener accountsChangeListener) {
        accountsChangeListeners.add(accountsChangeListener);
    }

    public void removeOnAccountsChangeListener(@NonNull AccountsChangeListener accountsChangeListener) {
        accountsChangeListeners.remove(accountsChangeListener);
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@ class NotificationChannelManager(
        MESSAGES, MISCELLANEOUS
    }

    init {
        preferences.addOnAccountsChangeListener(this::updateChannels)
    }

    fun updateChannels() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
+16 −1
Original line number Diff line number Diff line
@@ -2,12 +2,13 @@ package com.fsck.k9.ui.account

import android.arch.lifecycle.LiveData
import com.fsck.k9.Account
import com.fsck.k9.AccountsChangeListener
import com.fsck.k9.Preferences
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.coroutines.experimental.bg

class AccountsLiveData(val preferences: Preferences) : LiveData<List<Account>>() {
class AccountsLiveData(val preferences: Preferences) : LiveData<List<Account>>(), AccountsChangeListener {
    init {
        loadAccountsAsync()
    }
@@ -22,7 +23,21 @@ class AccountsLiveData(val preferences: Preferences) : LiveData<List<Account>>()
        }
    }

    override fun onAccountsChanged() {
        loadAccountsAsync()
    }

    private fun loadAccounts(): List<Account> {
        return preferences.accounts
    }

    override fun onActive() {
        super.onActive()
        preferences.addOnAccountsChangeListener(this)
    }

    override fun onInactive() {
        super.onInactive()
        preferences.removeOnAccountsChangeListener(this)
    }
}