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).
This commit is contained in:
Severiano Jaramillo 2021-03-24 22:34:49 -07:00
parent 7e12224795
commit 0ad0f7c911
3 changed files with 17 additions and 3 deletions

View file

@ -99,6 +99,8 @@ class FilterOptionsDialog : DialogFragment() {
binding.tvEndDate.setOnClickListener { showDateRangePicker() } binding.tvEndDate.setOnClickListener { showDateRangePicker() }
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
updateDateTextViews() updateDateTextViews()
// Initialize Asset // Initialize Asset

View file

@ -2,6 +2,7 @@ package cy.agorise.bitsybitshareswallet.viewmodels
import android.app.Application import android.app.Application
import androidx.lifecycle.* import androidx.lifecycle.*
import com.google.android.material.datepicker.MaterialDatePicker
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.models.FilterOptions import cy.agorise.bitsybitshareswallet.models.FilterOptions
import cy.agorise.bitsybitshareswallet.repositories.TransferDetailRepository import cy.agorise.bitsybitshareswallet.repositories.TransferDetailRepository
@ -11,6 +12,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.util.* import java.util.*
class TransactionsViewModel(application: Application) : AndroidViewModel(application) { class TransactionsViewModel(application: Application) : AndroidViewModel(application) {
companion object { companion object {
const val TAG = "TransactionsViewModel" const val TAG = "TransactionsViewModel"
@ -32,12 +34,19 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
init { init {
// Initialize the start and end dates for the FilterOptions // Initialize the start and end dates for the FilterOptions
val calendar = Calendar.getInstance() val calendar = getClearedUtc()
calendar.timeInMillis = MaterialDatePicker.todayInUtcMilliseconds()
mFilterOptions.endDate = calendar.timeInMillis mFilterOptions.endDate = calendar.timeInMillis
calendar.add(Calendar.MONTH, -2) calendar.roll(Calendar.MONTH, -2)
mFilterOptions.startDate = calendar.timeInMillis 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<List<TransferDetail>> { internal fun getFilteredTransactions(userId: String): LiveData<List<TransferDetail>> {
val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault()) val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault())
transactions = mRepository.getAll(userId, currencyCode) transactions = mRepository.getAll(userId, currencyCode)

View file

@ -67,6 +67,7 @@
android:text="@string/text__date_range" android:text="@string/text__date_range"
android:textSize="16sp" android:textSize="16sp"
app:layout_constraintTop_toTopOf="@id/cbDateRange" app:layout_constraintTop_toTopOf="@id/cbDateRange"
app:layout_constraintBottom_toBottomOf="@id/cbDateRange"
app:layout_constraintStart_toStartOf="parent"/> app:layout_constraintStart_toStartOf="parent"/>
<CheckBox <CheckBox
@ -118,6 +119,7 @@
android:text="@string/text__asset" android:text="@string/text__asset"
android:textSize="16sp" android:textSize="16sp"
app:layout_constraintTop_toTopOf="@id/cbAsset" app:layout_constraintTop_toTopOf="@id/cbAsset"
app:layout_constraintBottom_toBottomOf="@id/cbAsset"
app:layout_constraintStart_toStartOf="parent"/> app:layout_constraintStart_toStartOf="parent"/>
<CheckBox <CheckBox
@ -158,6 +160,7 @@
android:text="@string/text__equivalent_value" android:text="@string/text__equivalent_value"
android:textSize="16sp" android:textSize="16sp"
app:layout_constraintTop_toTopOf="@id/cbEquivalentValue" app:layout_constraintTop_toTopOf="@id/cbEquivalentValue"
app:layout_constraintBottom_toBottomOf="@id/cbEquivalentValue"
app:layout_constraintStart_toStartOf="parent"/> app:layout_constraintStart_toStartOf="parent"/>
<CheckBox <CheckBox
@ -238,4 +241,4 @@
app:layout_constraintEnd_toStartOf="@id/btnFilter" app:layout_constraintEnd_toStartOf="@id/btnFilter"
android:textColor="@color/colorPrimary"/> android:textColor="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>