diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-23 19:00:08 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-23 19:00:08 -0300 |
| commit | b968cd5b867eef95a133a7285aed21b246d30688 (patch) | |
| tree | ed0b3f60f0e1052b2095044e2e36fc154c84da68 /android/src | |
| parent | 8c954fecee3120027fd1b847fd914c8aba03da3f (diff) | |
| parent | 9c52334a80463218274e7ee66bffb08c039bf562 (diff) | |
| download | mullvadvpn-b968cd5b867eef95a133a7285aed21b246d30688.tar.xz mullvadvpn-b968cd5b867eef95a133a7285aed21b246d30688.zip | |
Merge branch 'use-recycler-view-for-account-history'
Diffstat (limited to 'android/src')
9 files changed, 113 insertions, 41 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ListItemDividerDecoration.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ListItemDividerDecoration.kt index 16e1caad17..55084a0ea2 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ListItemDividerDecoration.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/ListItemDividerDecoration.kt @@ -6,12 +6,30 @@ import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView.ItemDecoration import android.support.v7.widget.RecyclerView.State import android.view.View -import net.mullvad.mullvadvpn.R +import kotlin.properties.Delegates.observable -class ListItemDividerDecoration(private val context: Context) : ItemDecoration() { - private val dividerHeight = context.resources.getDimensionPixelSize(R.dimen.list_item_divider) +class ListItemDividerDecoration(context: Context) : ItemDecoration() { + private var bottomOffset = 0 + private var topOffset = 0 + + var bottomOffsetId by observable<Int?>(null) { _, _, id -> + if (id != null) { + bottomOffset = context.resources.getDimensionPixelSize(id) + } else { + bottomOffset = 0 + } + } + + var topOffsetId by observable<Int?>(null) { _, _, id -> + if (id != null) { + topOffset = context.resources.getDimensionPixelSize(id) + } else { + topOffset = 0 + } + } override fun getItemOffsets(offsets: Rect, view: View, parent: RecyclerView, state: State) { - offsets.bottom = dividerHeight + offsets.bottom = bottomOffset + offsets.top = topOffset } } diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SelectLocationFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SelectLocationFragment.kt index 5ea1c1bfc5..e35377c690 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SelectLocationFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SelectLocationFragment.kt @@ -74,7 +74,11 @@ class SelectLocationFragment : ServiceDependentFragment(OnNoService.GoToLaunchSc } } - addItemDecoration(ListItemDividerDecoration(parentActivity)) + addItemDecoration( + ListItemDividerDecoration(parentActivity).apply { + bottomOffsetId = R.dimen.list_item_divider + } + ) } return view diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SplitTunnelingFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SplitTunnelingFragment.kt index 3d915c9a6b..6294a2028a 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SplitTunnelingFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/SplitTunnelingFragment.kt @@ -75,7 +75,11 @@ class SplitTunnelingFragment : ServiceDependentFragment(OnNoService.GoToLaunchSc } } - addItemDecoration(ListItemDividerDecoration(parentActivity)) + addItemDecoration( + ListItemDividerDecoration(parentActivity).apply { + bottomOffsetId = R.dimen.list_item_divider + } + ) } return view diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryAdapter.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryAdapter.kt new file mode 100644 index 0000000000..53082c8ea2 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryAdapter.kt @@ -0,0 +1,30 @@ +package net.mullvad.mullvadvpn.ui.widget + +import android.support.v7.widget.RecyclerView.Adapter +import android.view.LayoutInflater +import android.view.ViewGroup +import kotlin.properties.Delegates.observable +import net.mullvad.mullvadvpn.R + +class AccountHistoryAdapter : Adapter<AccountHistoryHolder>() { + var accountHistory by observable(ArrayList<String>()) { _, _, _ -> + notifyDataSetChanged() + } + + var onSelectEntry: ((String) -> Unit)? = null + + override fun onCreateViewHolder(parentView: ViewGroup, type: Int): AccountHistoryHolder { + val inflater = LayoutInflater.from(parentView.context) + val view = inflater.inflate(R.layout.account_history_entry, parentView, false) + + return AccountHistoryHolder(view).apply { + onSelect = { account -> onSelectEntry?.invoke(account) } + } + } + + override fun onBindViewHolder(holder: AccountHistoryHolder, position: Int) { + holder.accountToken = accountHistory[position] + } + + override fun getItemCount() = accountHistory.size +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryHolder.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryHolder.kt new file mode 100644 index 0000000000..5a7a0b34da --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountHistoryHolder.kt @@ -0,0 +1,23 @@ +package net.mullvad.mullvadvpn.ui.widget + +import android.support.v7.widget.RecyclerView.ViewHolder +import android.view.View +import android.widget.TextView +import kotlin.properties.Delegates.observable +import net.mullvad.mullvadvpn.R + +class AccountHistoryHolder(view: View) : ViewHolder(view) { + private val label: TextView = view.findViewById(R.id.label) + + var accountToken by observable("") { _, _, account -> + label.text = account + } + + var onSelect: ((String) -> Unit)? = null + + init { + view.setOnClickListener { + onSelect?.invoke(accountToken) + } + } +} 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 131d9c0c65..838bd63379 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,17 +3,18 @@ package net.mullvad.mullvadvpn.ui.widget import android.animation.ValueAnimator import android.app.Activity import android.content.Context +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.View.OnLayoutChangeListener import android.view.ViewGroup.MarginLayoutParams import android.view.inputmethod.InputMethodManager -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.ListItemDividerDecoration import net.mullvad.mullvadvpn.ui.LoginState import net.mullvad.mullvadvpn.ui.widget.AccountLoginBorder.BorderState @@ -26,9 +27,13 @@ class AccountLogin : RelativeLayout { } private val border: AccountLoginBorder = container.findViewById(R.id.border) - private val accountHistoryList: ListView = container.findViewById(R.id.history) + private val accountHistoryList: RecyclerView = container.findViewById(R.id.history) private val input: AccountInput = container.findViewById(R.id.input) + private val historyAdapter = AccountHistoryAdapter().apply { + onSelectEntry = { account -> input.loginWith(account) } + } + private val dividerHeight = resources.getDimensionPixelSize(R.dimen.account_history_divider) private val historyEntryHeight = resources.getDimensionPixelSize(R.dimen.account_history_entry_height) @@ -81,7 +86,10 @@ class AccountLogin : RelativeLayout { val entryCount = history?.size ?: 0 historyHeight = entryCount * (historyEntryHeight + dividerHeight) - updateAccountHistory() + + if (history != null) { + historyAdapter.accountHistory = history + } } var state: LoginState by observable(LoginState.Initial) { _, _, newState -> @@ -133,6 +141,17 @@ class AccountLogin : RelativeLayout { } ) } + + accountHistoryList.apply { + layoutManager = LinearLayoutManager(context) + adapter = historyAdapter + + addItemDecoration( + ListItemDividerDecoration(context).apply { + topOffsetId = R.dimen.account_history_divider + } + ) + } } fun onDestroy() { @@ -140,25 +159,6 @@ class AccountLogin : RelativeLayout { input.onTextChanged.unsubscribe(this) } - 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]) - } - } - } - } - private fun updateBorder() { if (state == LoginState.Failure) { border.borderState = BorderState.ERROR diff --git a/android/src/main/res/drawable/account_history_list_divider.xml b/android/src/main/res/drawable/account_history_list_divider.xml deleted file mode 100644 index fa32039e1d..0000000000 --- a/android/src/main/res/drawable/account_history_list_divider.xml +++ /dev/null @@ -1,5 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<shape xmlns:android="http://schemas.android.com/apk/res/android" - android:shape="rectangle"> - <solid android:color="@color/darkBlue" /> -</shape> diff --git a/android/src/main/res/layout/account_history_entry.xml b/android/src/main/res/layout/account_history_entry.xml index 440cce957d..d168bc6cc1 100644 --- a/android/src/main/res/layout/account_history_entry.xml +++ b/android/src/main/res/layout/account_history_entry.xml @@ -2,7 +2,7 @@ android:layout_width="match_parent" android:layout_height="@dimen/account_history_entry_height" android:orientation="vertical"> - <TextView android:id="@+id/account_history_entry_text_view" + <TextView android:id="@+id/label" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/account_history_entry_background" diff --git a/android/src/main/res/layout/account_login.xml b/android/src/main/res/layout/account_login.xml index 43d76c89e6..d91222c73f 100644 --- a/android/src/main/res/layout/account_login.xml +++ b/android/src/main/res/layout/account_login.xml @@ -9,10 +9,8 @@ android:layout_height="48dp" android:layout_alignParentTop="true" android:orientation="horizontal" /> - <ListView android:id="@+id/history" - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:layout_below="@id/input" - android:divider="@drawable/account_history_list_divider" - android:dividerHeight="@dimen/account_history_divider" /> + <android.support.v7.widget.RecyclerView android:id="@+id/history" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/input" /> </merge> |
