From 0ad0f7c911d0b4047dc4762e7f1921862140bb93 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Wed, 24 Mar 2021 22:34:49 -0700 Subject: [PATCH] Fix date range selection issue. - The issue was that the MaterialDatePicker uses the UTC time zone internally. We had to account for that to avoid showing an off by one wrong selection on the Filter Options date range and the Date Range picker (MaterialDatePicker). --- .../fragments/FilterOptionsDialog.kt | 2 ++ .../viewmodels/TransactionsViewModel.kt | 13 +++++++++++-- app/src/main/res/layout/dialog_filter_options.xml | 5 ++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt index e4bb5d1..c040519 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt @@ -99,6 +99,8 @@ class FilterOptionsDialog : DialogFragment() { binding.tvEndDate.setOnClickListener { showDateRangePicker() } + dateFormat.timeZone = TimeZone.getTimeZone("UTC") + updateDateTextViews() // Initialize Asset diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransactionsViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransactionsViewModel.kt index 5b0966c..7a1f952 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransactionsViewModel.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransactionsViewModel.kt @@ -2,6 +2,7 @@ package cy.agorise.bitsybitshareswallet.viewmodels import android.app.Application import androidx.lifecycle.* +import com.google.android.material.datepicker.MaterialDatePicker import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.models.FilterOptions import cy.agorise.bitsybitshareswallet.repositories.TransferDetailRepository @@ -11,6 +12,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.util.* + class TransactionsViewModel(application: Application) : AndroidViewModel(application) { companion object { const val TAG = "TransactionsViewModel" @@ -32,12 +34,19 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica init { // Initialize the start and end dates for the FilterOptions - val calendar = Calendar.getInstance() + val calendar = getClearedUtc() + calendar.timeInMillis = MaterialDatePicker.todayInUtcMilliseconds() mFilterOptions.endDate = calendar.timeInMillis - calendar.add(Calendar.MONTH, -2) + calendar.roll(Calendar.MONTH, -2) mFilterOptions.startDate = calendar.timeInMillis } + private fun getClearedUtc(): Calendar { + val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC")) + utc.clear() + return utc + } + internal fun getFilteredTransactions(userId: String): LiveData> { val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault()) transactions = mRepository.getAll(userId, currencyCode) diff --git a/app/src/main/res/layout/dialog_filter_options.xml b/app/src/main/res/layout/dialog_filter_options.xml index b20725c..efcb3a9 100644 --- a/app/src/main/res/layout/dialog_filter_options.xml +++ b/app/src/main/res/layout/dialog_filter_options.xml @@ -67,6 +67,7 @@ android:text="@string/text__date_range" android:textSize="16sp" app:layout_constraintTop_toTopOf="@id/cbDateRange" + app:layout_constraintBottom_toBottomOf="@id/cbDateRange" app:layout_constraintStart_toStartOf="parent"/> - \ No newline at end of file +