summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-12-17 18:10:34 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-31 14:36:30 +0000
commit42d599cd2d599e69f1e7023a56cc8b77c5366d7b (patch)
treead42974ae5305f1d7b53446afa3c42a58bf0b893 /android/src/main
parent7f0b81c12d54b78c4cc50b51aa02dd24a05a8857 (diff)
downloadmullvadvpn-42d599cd2d599e69f1e7023a56cc8b77c5366d7b.tar.xz
mullvadvpn-42d599cd2d599e69f1e7023a56cc8b77c5366d7b.zip
Create UI-side `AccountCache` class
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt67
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()
+ }
+}