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>>(), AccountsChangeListener {
    init {
        loadAccountsAsync()
    }

    private fun loadAccountsAsync() {
        launch(UI) {
            val accounts = bg {
                loadAccounts()
            }

            value = accounts.await()
        }
    }

    override fun onAccountsChanged() {
        loadAccountsAsync()
    }

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

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

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

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>>(), AccountsChangeListener {

    private fun loadAccountsAsync() {
        launch(UI) {
            val accounts = bg {
                loadAccounts()
            }

            value = accounts.await()
        }
    }

    override fun onAccountsChanged() {
        loadAccountsAsync()
    }

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

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

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

Fix crash of SettingsActivity on resume
Fix crash of SettingsActivity on resume

`SettingsActivity#populateSettingsList` listens for changes in accounts
using `observeNotNull` that throws when a `null` value is assigned.

This change leaves the old value when `onInactive` is called.

See: https://github.com/k9mail/k-9/pull/3819#issuecomment-447523394

k9mail/k-9,cketti/k-9,k9mail/k-9,cketti/k-9,cketti/k-9,k9mail/k-9,cketti/k-9