diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-03 17:49:16 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-04 11:54:35 +0000 |
| commit | 7cc0f0e80a13ed1e013e3a6fdfb7fcfc50d6b30b (patch) | |
| tree | 23c16d81e1846259712d54bb9c7b1beb90b5cbe1 /android | |
| parent | 6e985d41d7210260b926a3e220ee4e7aae114b1f (diff) | |
| download | mullvadvpn-7cc0f0e80a13ed1e013e3a6fdfb7fcfc50d6b30b.tar.xz mullvadvpn-7cc0f0e80a13ed1e013e3a6fdfb7fcfc50d6b30b.zip | |
Move controller code into `AccountLogin` widget
Diffstat (limited to 'android')
3 files changed, 119 insertions, 122 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/AccountInputController.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/AccountInputController.kt deleted file mode 100644 index 7bd35bbc35..0000000000 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/AccountInputController.kt +++ /dev/null @@ -1,118 +0,0 @@ -package net.mullvad.mullvadvpn.ui - -import android.content.Context -import android.view.View -import android.widget.ArrayAdapter -import android.widget.ListView -import kotlin.properties.Delegates.observable -import net.mullvad.mullvadvpn.R -import net.mullvad.mullvadvpn.ui.widget.AccountInput -import net.mullvad.mullvadvpn.ui.widget.AccountLoginBorder -import net.mullvad.mullvadvpn.ui.widget.AccountLoginBorder.BorderState - -class AccountInputController(val parentView: View, context: Context) { - private var inputHasFocus by observable(false) { _, _, hasFocus -> - updateBorder() - - if (hasFocus) { - shouldShowAccountHistory = true - } - } - - var state: LoginState by observable(LoginState.Initial) { _, _, newState -> - input.loginState = newState - - updateBorder() - - when (newState) { - LoginState.Initial -> {} - LoginState.InProgress -> loggingInState() - LoginState.Success -> successState() - LoginState.Failure -> {} - } - } - - val container: AccountLoginBorder = parentView.findViewById(R.id.account_input_container) - val accountHistoryList: ListView = parentView.findViewById(R.id.account_history_list) - - val input = parentView.findViewById<AccountInput>(R.id.account_input).apply { - onFocusChanged.subscribe(this) { hasFocus -> - inputHasFocus = hasFocus - } - - onTextChanged.subscribe(this) { _ -> - if (state == LoginState.Failure) { - state = LoginState.Initial - } - } - } - - var accountHistory: ArrayList<String>? = null - set(value) { - synchronized(this) { - field = value - updateAccountHistory() - } - } - - private var shouldShowAccountHistory = false - set(value) { - synchronized(this) { - field = value - updateAccountHistory() - } - } - - var onLogin: ((String) -> Unit)? - get() = input.onLogin - set(value) { input.onLogin = value } - - fun onDestroy() { - input.onFocusChanged.unsubscribe(this) - input.onTextChanged.unsubscribe(this) - } - - private fun loggingInState() { - accountHistoryList.visibility = View.INVISIBLE - } - - private fun successState() { - container.visibility = View.INVISIBLE - } - - private fun updateAccountHistory() { - accountHistory?.let { history -> - accountHistoryList.apply { - setAdapter( - ArrayAdapter( - context, - R.layout.account_history_entry, - R.id.account_history_entry_text_view, - history - ) - ) - - setOnItemClickListener { _, _, idx, _ -> - input.loginWith(history[idx]) - accountHistoryList.visibility = View.GONE - } - } - - if (shouldShowAccountHistory && accountHistoryList.visibility != View.VISIBLE) { - accountHistoryList.visibility = View.VISIBLE - accountHistoryList.translationY = -accountHistoryList.height.toFloat() - accountHistoryList.animate().translationY(0.0F).setDuration(350).start() - } - } - } - - private fun updateBorder() { - if (state == LoginState.Failure) { - container.borderState = BorderState.ERROR - } else if (inputHasFocus) { - container.borderState = BorderState.FOCUSED - } else { - container.borderState = BorderState.UNFOCUSED - } - } -} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/LoginFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/LoginFragment.kt index ec8da11abb..7bb365b096 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/LoginFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/LoginFragment.kt @@ -13,6 +13,7 @@ import kotlinx.coroutines.delay import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.model.GetAccountDataResult import net.mullvad.mullvadvpn.service.AccountCache +import net.mullvad.mullvadvpn.ui.widget.AccountLogin import net.mullvad.mullvadvpn.ui.widget.Button import org.joda.time.DateTime @@ -28,7 +29,7 @@ class LoginFragment : ServiceDependentFragment(OnNoService.GoToLaunchScreen) { private lateinit var loggingInStatus: View private lateinit var loggedInStatus: View private lateinit var loginFailStatus: View - private lateinit var accountInput: AccountInputController + private lateinit var accountInput: AccountLogin private lateinit var scrollArea: ScrollView private val loggedIn = CompletableDeferred<LoginResult>() @@ -46,7 +47,7 @@ class LoginFragment : ServiceDependentFragment(OnNoService.GoToLaunchScreen) { loggedInStatus = view.findViewById(R.id.logged_in_status) loginFailStatus = view.findViewById(R.id.login_fail_status) - accountInput = AccountInputController(view, parentActivity) + accountInput = view.findViewById(R.id.account_login) accountInput.onLogin = { accountToken -> login(accountToken) } view.findViewById<Button>(R.id.create_account) @@ -55,7 +56,7 @@ class LoginFragment : ServiceDependentFragment(OnNoService.GoToLaunchScreen) { scrollArea = view.findViewById(R.id.scroll_area) fetchHistory() - scrollToShow(accountInput.input) + scrollToShow(accountInput) return view } @@ -198,6 +199,6 @@ class LoginFragment : ServiceDependentFragment(OnNoService.GoToLaunchScreen) { accountInput.state = LoginState.Failure - scrollToShow(accountInput.input) + scrollToShow(accountInput) } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountLogin.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountLogin.kt index 2d60730d0e..c049f121c6 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountLogin.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountLogin.kt @@ -3,8 +3,14 @@ package net.mullvad.mullvadvpn.ui.widget import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater +import android.view.View +import android.widget.ArrayAdapter +import android.widget.ListView import android.widget.RelativeLayout +import kotlin.properties.Delegates.observable import net.mullvad.mullvadvpn.R +import net.mullvad.mullvadvpn.ui.LoginState +import net.mullvad.mullvadvpn.ui.widget.AccountLoginBorder.BorderState class AccountLogin : RelativeLayout { private val container = @@ -14,6 +20,51 @@ class AccountLogin : RelativeLayout { inflater.inflate(R.layout.account_login, this) } + private val border: AccountLoginBorder = container.findViewById(R.id.account_input_container) + private val accountHistoryList: ListView = container.findViewById(R.id.account_history_list) + private val input: AccountInput = container.findViewById(R.id.account_input) + + private var shouldShowAccountHistory = false + set(value) { + synchronized(this) { + field = value + updateAccountHistory() + } + } + + private var inputHasFocus by observable(false) { _, _, hasFocus -> + updateBorder() + + if (hasFocus) { + shouldShowAccountHistory = true + } + } + + var accountHistory: ArrayList<String>? = null + set(value) { + synchronized(this) { + field = value + updateAccountHistory() + } + } + + var state: LoginState by observable(LoginState.Initial) { _, _, newState -> + input.loginState = newState + + updateBorder() + + when (newState) { + LoginState.Initial -> {} + LoginState.InProgress -> loggingInState() + LoginState.Success -> successState() + LoginState.Failure -> {} + } + } + + var onLogin: ((String) -> Unit)? + get() = input.onLogin + set(value) { input.onLogin = value } + constructor(context: Context) : super(context) {} constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {} @@ -28,4 +79,67 @@ class AccountLogin : RelativeLayout { defaultStyleResource: Int ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) { } + + init { + input.apply { + onFocusChanged.subscribe(this) { hasFocus -> + inputHasFocus = hasFocus + } + + onTextChanged.subscribe(this) { _ -> + if (state == LoginState.Failure) { + state = LoginState.Initial + } + } + } + } + + fun onDestroy() { + input.onFocusChanged.unsubscribe(this) + input.onTextChanged.unsubscribe(this) + } + + private fun loggingInState() { + accountHistoryList.visibility = View.INVISIBLE + } + + private fun successState() { + visibility = View.INVISIBLE + } + + private fun updateAccountHistory() { + accountHistory?.let { history -> + accountHistoryList.apply { + setAdapter( + ArrayAdapter( + context, + R.layout.account_history_entry, + R.id.account_history_entry_text_view, + history + ) + ) + + setOnItemClickListener { _, _, idx, _ -> + input.loginWith(history[idx]) + accountHistoryList.visibility = View.GONE + } + } + + if (shouldShowAccountHistory && accountHistoryList.visibility != View.VISIBLE) { + accountHistoryList.visibility = View.VISIBLE + accountHistoryList.translationY = -accountHistoryList.height.toFloat() + accountHistoryList.animate().translationY(0.0F).setDuration(350).start() + } + } + } + + private fun updateBorder() { + if (state == LoginState.Failure) { + border.borderState = BorderState.ERROR + } else if (inputHasFocus) { + border.borderState = BorderState.FOCUSED + } else { + border.borderState = BorderState.UNFOCUSED + } + } } |
