diff options
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt new file mode 100644 index 0000000000..96d49df850 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt @@ -0,0 +1,67 @@ +package net.mullvad.mullvadvpn.ui.serviceconnection + +import android.os.Messenger +import net.mullvad.mullvadvpn.ipc.DispatchingHandler +import net.mullvad.mullvadvpn.ipc.Event +import net.mullvad.mullvadvpn.ipc.Request +import net.mullvad.mullvadvpn.model.LoginStatus +import net.mullvad.talpid.util.EventNotifier +import org.joda.time.DateTime + +class AccountCache(val connection: Messenger, eventDispatcher: DispatchingHandler<Event>) { + val onAccountNumberChange = EventNotifier<String?>(null) + val onAccountExpiryChange = EventNotifier<DateTime?>(null) + val onAccountHistoryChange = EventNotifier<List<String>>(listOf<String>()) + val onLoginStatusChange = EventNotifier<LoginStatus?>(null) + + private var accountHistory by onAccountHistoryChange.notifiable() + private var loginStatus by onLoginStatusChange.notifiable() + + init { + eventDispatcher.apply { + registerHandler(Event.AccountHistory::class) { event -> + accountHistory = event.history ?: listOf<String>() + } + + registerHandler(Event.LoginStatus::class) { event -> + loginStatus = event.status + + onAccountNumberChange.notifyIfChanged(loginStatus?.account) + onAccountExpiryChange.notifyIfChanged(loginStatus?.expiry) + } + } + } + + fun createNewAccount() { + connection.send(Request.CreateAccount.message) + } + + fun login(account: String) { + connection.send(Request.Login(account).message) + } + + fun logout() { + connection.send(Request.Logout.message) + } + + fun fetchAccountExpiry() { + connection.send(Request.FetchAccountExpiry.message) + } + + fun invalidateAccountExpiry(accountExpiryToInvalidate: DateTime) { + val request = Request.InvalidateAccountExpiry(accountExpiryToInvalidate) + + connection.send(request.message) + } + + fun removeAccountFromHistory(account: String) { + connection.send(Request.RemoveAccountFromHistory(account).message) + } + + fun onDestroy() { + onAccountNumberChange.unsubscribeAll() + onAccountExpiryChange.unsubscribeAll() + onAccountHistoryChange.unsubscribeAll() + onLoginStatusChange.unsubscribeAll() + } +} |
