From 208d7ab1872cc7f5183a553d590ec4debc911292 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 2 Jan 2020 10:00:48 -0300 Subject: Hide detailed history events by default Also implement history changes after latest feedback --- app/build.gradle | 5 +- .../main/java/net/taler/wallet/WalletViewModel.kt | 32 ++++++++++-- .../java/net/taler/wallet/history/HistoryEvent.kt | 7 ++- .../java/net/taler/wallet/history/WalletHistory.kt | 58 ++++++++++++---------- .../taler/wallet/history/WalletHistoryAdapter.kt | 15 ++++-- app/src/main/res/layout/fragment_show_history.xml | 39 ++++++++------- app/src/main/res/layout/history_payment_sent.xml | 11 ++-- app/src/main/res/layout/history_row.xml | 2 +- app/src/main/res/layout/history_withdrawn.xml | 25 +++++++--- app/src/main/res/menu/history.xml | 17 +++++-- app/src/main/res/values/strings.xml | 6 ++- app/src/main/res/values/styles.xml | 5 ++ 12 files changed, 146 insertions(+), 76 deletions(-) (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 7e0f2ce..c40ddef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,14 +45,15 @@ dependencies { implementation project(":akono") implementation 'com.google.guava:guava:28.0-android' - def nav_version = "2.2.0-rc03" + def nav_version = "2.2.0-rc04" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version" // ViewModel and LiveData - def lifecycle_version = "2.1.0" + def lifecycle_version = "2.2.0-rc03" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // QR codes implementation 'com.google.zxing:core:3.4.0' diff --git a/app/src/main/java/net/taler/wallet/WalletViewModel.kt b/app/src/main/java/net/taler/wallet/WalletViewModel.kt index f556ff3..f932cff 100644 --- a/app/src/main/java/net/taler/wallet/WalletViewModel.kt +++ b/app/src/main/java/net/taler/wallet/WalletViewModel.kt @@ -18,11 +18,15 @@ package net.taler.wallet import android.app.Application import android.util.Log -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.* import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.readValue +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.onStart import net.taler.wallet.backend.WalletBackendApi import net.taler.wallet.history.History import org.json.JSONObject @@ -61,6 +65,7 @@ open class WithdrawStatus { val tosText: String, val tosEtag: String ) : WithdrawStatus() + class Success : WithdrawStatus() data class ReceivedDetails( val talerWithdrawUri: String, @@ -81,6 +86,7 @@ open class PendingOperations( ) +@Suppress("EXPERIMENTAL_API_USAGE") class WalletViewModel(val app: Application) : AndroidViewModel(app) { private var initialized = false @@ -104,6 +110,18 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { value = PendingOperations(listOf()) } + private val mHistoryProgress = MutableLiveData() + val historyProgress: LiveData = mHistoryProgress + + val historyShowAll = MutableLiveData() + + val history: LiveData = historyShowAll.switchMap { showAll -> + loadHistory(showAll) + .onStart { mHistoryProgress.postValue(true) } + .onCompletion { mHistoryProgress.postValue(false) } + .asLiveData(Dispatchers.IO) + } + private var activeGetBalance = 0 private var activeGetPending = 0 @@ -188,15 +206,21 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { } } - fun getHistory(cb: (r: History) -> Unit) { + private fun loadHistory(showAll: Boolean) = callbackFlow { + mHistoryProgress.postValue(true) walletBackendApi.sendRequest("getHistory", null) { isError, result -> if (isError) { // TODO show error message in [WalletHistory] fragment + close() return@sendRequest } val history: History = mapper.readValue(result.getString("history")) - cb(history) + history.reverse() // show latest first + mHistoryProgress.postValue(false) + offer(if (showAll) history else history.filter { it.showToUser } as History) + close() } + awaitClose() } fun withdrawTestkudos() { diff --git a/app/src/main/java/net/taler/wallet/history/HistoryEvent.kt b/app/src/main/java/net/taler/wallet/history/HistoryEvent.kt index 31473f6..b21147a 100644 --- a/app/src/main/java/net/taler/wallet/history/HistoryEvent.kt +++ b/app/src/main/java/net/taler/wallet/history/HistoryEvent.kt @@ -80,7 +80,7 @@ class ReserveShortInfo( val reserveCreationDetail: ReserveCreationDetail ) -class History : ArrayList() +typealias History = ArrayList @JsonTypeInfo( use = NAME, @@ -109,7 +109,8 @@ abstract class HistoryEvent( @get:StringRes open val title: Int = 0, @get:DrawableRes - open val icon: Int = R.drawable.ic_account_balance + open val icon: Int = R.drawable.ic_account_balance, + open val showToUser: Boolean = false ) @@ -178,6 +179,7 @@ class HistoryWithdrawnEvent( override val layout = R.layout.history_withdrawn override val title = R.string.history_event_withdrawn override val icon = R.drawable.history_withdrawn + override val showToUser = true } @JsonTypeName("order-accepted") @@ -232,6 +234,7 @@ class HistoryPaymentSentEvent( override val layout = R.layout.history_payment_sent override val title = R.string.history_event_payment_sent override val icon = R.drawable.ic_cash_usd_outline + override val showToUser = true } @JsonTypeName("refreshed") diff --git a/app/src/main/java/net/taler/wallet/history/WalletHistory.kt b/app/src/main/java/net/taler/wallet/history/WalletHistory.kt index cdc90ae..0b2e214 100644 --- a/app/src/main/java/net/taler/wallet/history/WalletHistory.kt +++ b/app/src/main/java/net/taler/wallet/history/WalletHistory.kt @@ -19,11 +19,14 @@ package net.taler.wallet.history import android.os.Bundle import android.view.* +import android.view.View.INVISIBLE +import android.view.View.VISIBLE import androidx.fragment.app.Fragment -import androidx.lifecycle.ViewModelProviders +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.fragment_show_history.* import net.taler.wallet.R import net.taler.wallet.WalletViewModel @@ -34,64 +37,65 @@ import net.taler.wallet.WalletViewModel class WalletHistory : Fragment() { lateinit var model: WalletViewModel + private lateinit var showAllItem: MenuItem private val historyAdapter = WalletHistoryAdapter() - lateinit var historyPlaceholder: View override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) model = activity?.run { - ViewModelProviders.of(this)[WalletViewModel::class.java] + ViewModelProvider(this)[WalletViewModel::class.java] } ?: throw Exception("Invalid Activity") } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - activity?.menuInflater?.inflate(R.menu.history, menu) + inflater.inflate(R.menu.history, menu) + showAllItem = menu.findItem(R.id.show_all_history) } override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { + R.id.show_all_history -> { + item.isChecked = !item.isChecked + model.historyShowAll.value = item.isChecked + true + } R.id.reload_history -> { - updateHistory() + model.historyShowAll.value = showAllItem.isChecked true } else -> super.onOptionsItemSelected(item) } } - private fun updateHistory() { - model.getHistory { - if (it.isEmpty()) { - historyPlaceholder.visibility = View.VISIBLE - } - historyAdapter.update(it) - } - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - // Inflate the layout for this fragment - val view = inflater.inflate(R.layout.fragment_show_history, container, false) - val myLayoutManager = LinearLayoutManager(context).apply { - reverseLayout = true // show latest events first - } - val myItemDecoration = DividerItemDecoration(context, myLayoutManager.orientation) - view.findViewById(R.id.list_history).apply { + return inflater.inflate(R.layout.fragment_show_history, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + historyList.apply { + val myLayoutManager = LinearLayoutManager(context) + val myItemDecoration = DividerItemDecoration(context, myLayoutManager.orientation) layoutManager = myLayoutManager adapter = historyAdapter addItemDecoration(myItemDecoration) } - historyPlaceholder = view.findViewById(R.id.list_history_placeholder) - historyPlaceholder.visibility = View.GONE - - updateHistory() + model.historyProgress.observe(this, Observer { show -> + historyProgressBar.visibility = if (show) VISIBLE else INVISIBLE + }) + model.history.observe(this, Observer { history -> + historyEmptyState.visibility = if (history.isEmpty()) VISIBLE else INVISIBLE + historyAdapter.update(history) + }) - return view + // kicks off initial load, needs to be adapted if showAll state is ever saved + if (savedInstanceState == null) model.historyShowAll.value = false } companion object { diff --git a/app/src/main/java/net/taler/wallet/history/WalletHistoryAdapter.kt b/app/src/main/java/net/taler/wallet/history/WalletHistoryAdapter.kt index 14fc1e9..9d601e1 100644 --- a/app/src/main/java/net/taler/wallet/history/WalletHistoryAdapter.kt +++ b/app/src/main/java/net/taler/wallet/history/WalletHistoryAdapter.kt @@ -64,7 +64,7 @@ internal class WalletHistoryAdapter(private var history: History = History()) : internal abstract class HistoryEventViewHolder(protected val v: View) : ViewHolder(v) { private val icon: ImageView = v.findViewById(R.id.icon) - private val title: TextView = v.findViewById(R.id.title) + protected val title: TextView = v.findViewById(R.id.title) private val time: TextView = v.findViewById(R.id.time) @CallSuper @@ -77,7 +77,15 @@ internal abstract class HistoryEventViewHolder(protected val v: View) : ViewHold private fun getRelativeTime(timestamp: Long): CharSequence { val now = System.currentTimeMillis() - return getRelativeTimeSpanString(timestamp, now, MINUTE_IN_MILLIS, FORMAT_ABBREV_RELATIVE) + return if (now - timestamp > DAY_IN_MILLIS * 2) { + formatDateTime( + v.context, + timestamp, + FORMAT_SHOW_TIME or FORMAT_SHOW_DATE or FORMAT_ABBREV_MONTH or FORMAT_NO_YEAR + ) + } else { + getRelativeTimeSpanString(timestamp, now, MINUTE_IN_MILLIS, FORMAT_ABBREV_RELATIVE) + } } } @@ -132,7 +140,8 @@ internal class HistoryPaymentSentViewHolder(v: View) : HistoryEventViewHolder(v) super.bind(event) event as HistoryPaymentSentEvent - summary.text = event.orderShortInfo.summary + title.text = event.orderShortInfo.summary + summary.setText(event.title) amountPaidWithFees.text = parseAmount(event.amountPaidWithFees).toString() } diff --git a/app/src/main/res/layout/fragment_show_history.xml b/app/src/main/res/layout/fragment_show_history.xml index 6265a50..dc93889 100644 --- a/app/src/main/res/layout/fragment_show_history.xml +++ b/app/src/main/res/layout/fragment_show_history.xml @@ -1,28 +1,31 @@ - - + android:scrollbars="vertical" /> - + - - + - \ No newline at end of file + diff --git a/app/src/main/res/layout/history_payment_sent.xml b/app/src/main/res/layout/history_payment_sent.xml index 0c39133..cf03998 100644 --- a/app/src/main/res/layout/history_payment_sent.xml +++ b/app/src/main/res/layout/history_payment_sent.xml @@ -22,16 +22,15 @@ + app:layout_constraintTop_toTopOf="parent" + tools:text="Lots of books with very long titles" /> + tools:text="@string/history_event_payment_sent" /> + tools:text="http://taler.quite-long-exchange.url" /> + + + tools:text="23 min. ago" /> diff --git a/app/src/main/res/menu/history.xml b/app/src/main/res/menu/history.xml index c8fb3e7..8c2aa69 100644 --- a/app/src/main/res/menu/history.xml +++ b/app/src/main/res/menu/history.xml @@ -1,8 +1,15 @@ - + xmlns:app="http://schemas.android.com/apk/res-auto"> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1caf41e..6a92705 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,11 +20,15 @@ Exchange Added Exchange Updated Reserve Balance Updated - Payment Made + Payment Withdraw Order Confirmed Order Cancelled Refresh + Fee: + Show All + Reload History + The wallet history is empty Hello blank fragment diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 16dbab3..619c0c9 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -14,4 +14,9 @@ + -- cgit v1.2.3