Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #457 from ILIYANGERMANOV/develop
Browse files Browse the repository at this point in the history
Date bugfixes and improvements
  • Loading branch information
ILIYANGERMANOV authored Jan 24, 2022
2 parents 42161be + 7ba21ff commit db2897c
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ivy.wallet.compose.scenario

import androidx.compose.ui.test.*
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import com.ivy.wallet.compose.IvyComposeTest
import com.ivy.wallet.compose.helpers.*
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down Expand Up @@ -67,8 +70,13 @@ class OperationsCoreTest : IvyComposeTest() {
category = "Investments"
)

composeTestRule.onNodeWithTag("transaction_card")
.assertIsDisplayed()
homeTab.dismissPrompt()

homeTab.clickTransaction(
amount = "5,000.00",
title = "Salary",
category = "Investments"
)
}

@Test
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/com/ivy/wallet/base/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,32 @@ fun LocalTime.convertLocalToUTC(): LocalTime {
return this.minusSeconds(offset)
}

fun LocalTime.convertUTCToLocal(): LocalTime {
val offset = timeNowLocal().atZone(ZoneOffset.systemDefault()).offset.totalSeconds.toLong()
return this.plusSeconds(offset)
}

fun LocalDateTime.convertLocalToUTC(): LocalDateTime {
val offset = timeNowLocal().atZone(ZoneOffset.systemDefault()).offset.totalSeconds.toLong()
return this.minusSeconds(offset)
}

// The timepicker returns time in UTC, but the date picker returns date in LocalTimeZone
// hence use this method to get both date & time in UTC
fun getTrueDate(date: LocalDate, time: LocalTime, convert: Boolean = true): LocalDateTime {
val timeLocal = if (convert) time.convertUTCToLocal() else time

return timeNowUTC()
.withYear(date.year)
.withMonth(date.monthValue)
.withDayOfMonth(date.dayOfMonth)
.withHour(timeLocal.hour)
.withMinute(timeLocal.minute)
.withSecond(0)
.withNano(0)
.convertLocalToUTC()
}


fun LocalDate.formatLocal(
pattern: String = "dd MMM yyyy",
Expand Down Expand Up @@ -230,10 +251,10 @@ fun LocalDateTime.timeLeft(
}

fun startOfMonth(date: LocalDate): LocalDateTime =
date.withDayOfMonth(1).atStartOfDay()
date.withDayOfMonth(1).atStartOfDay().convertLocalToUTC()

fun endOfMonth(date: LocalDate): LocalDateTime =
date.withDayOfMonth(date.lengthOfMonth()).atEndOfDay()
date.withDayOfMonth(date.lengthOfMonth()).atEndOfDay().convertLocalToUTC()

fun LocalDate.atEndOfDay(): LocalDateTime =
this.atTime(23, 59, 59)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.wallet.base.asLiveData
import com.ivy.wallet.base.dateNowUTC
import com.ivy.wallet.base.ioThread
import com.ivy.wallet.logic.PlannedPaymentsLogic
import com.ivy.wallet.logic.WalletLogic
Expand Down Expand Up @@ -62,18 +63,20 @@ class BalanceViewModel @Inject constructor(

fun nextMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
start(
period = month.incrementMonthPeriod(ivyContext, 1L),
period = month.incrementMonthPeriod(ivyContext, 1L, year = year),
)
}
}

fun previousMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
start(
period = month.incrementMonthPeriod(ivyContext, -1L),
period = month.incrementMonthPeriod(ivyContext, -1L, year = year),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding
import com.ivy.wallet.R
import com.ivy.wallet.base.convertUTCtoLocal
import com.ivy.wallet.base.getTrueDate
import com.ivy.wallet.base.onScreenStart
import com.ivy.wallet.base.timeNowLocal
import com.ivy.wallet.logic.model.CreateAccountData
Expand Down Expand Up @@ -242,10 +244,10 @@ private fun BoxWithConstraintsScope.UI(
dueDateTime = dueDate,
) {
ivyContext.datePicker(
initialDate = dateTime?.toLocalDate(),
initialDate = dateTime?.convertUTCtoLocal()?.toLocalDate(),
) { date ->
ivyContext.timePicker { time ->
onSetDateTime(date.atTime(time.hour, time.minute, time.second))
onSetDateTime(getTrueDate(date, time))
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/ivy/wallet/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.wallet.base.TestIdlingResource
import com.ivy.wallet.base.asLiveData
import com.ivy.wallet.base.dateNowUTC
import com.ivy.wallet.base.ioThread
import com.ivy.wallet.logic.CustomerJourneyLogic
import com.ivy.wallet.logic.PlannedPaymentsLogic
Expand Down Expand Up @@ -275,18 +276,20 @@ class HomeViewModel @Inject constructor(

fun nextMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
load(
period = month.incrementMonthPeriod(ivyContext, 1L),
period = month.incrementMonthPeriod(ivyContext, 1L, year = year),
)
}
}

fun previousMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
load(
period = month.incrementMonthPeriod(ivyContext, -1L),
period = month.incrementMonthPeriod(ivyContext, -1L, year = year),
)
}
}
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/com/ivy/wallet/ui/onboarding/model/TimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.time.LocalDateTime

data class TimePeriod(
val month: Month? = null,
val year: Int? = null,
val fromToRange: FromToTimeRange? = null,
val lastNRange: LastNTimeRange? = null,
) {
Expand Down Expand Up @@ -41,7 +42,8 @@ data class TimePeriod(
return TimePeriod(
month = Month.fromMonthValue(
periodDate.monthValue
)
),
year = periodDate.year
)
}
}
Expand All @@ -54,7 +56,7 @@ data class TimePeriod(
): FromToTimeRange {
return when {
month != null -> {
val date = month.toDate()
val date = if (year != null) month.toDate().withYear(year) else month.toDate()
val (from, to) = if (startDateOfMonth != 1) {
customStartDayOfMonthPeriodRange(
date = date,
Expand Down Expand Up @@ -95,14 +97,16 @@ data class TimePeriod(
val from = date
.withDayOfMonthSafe(startDateOfMonth)
.atStartOfDay()
.convertLocalToUTC()

val to = date
.withDayOfMonthSafe(startDateOfMonth)
//startDayOfMonth != 1 just shift N day the month forward so to should +1 month
.plusMonths(1)
.withDayOfMonthSafe(startDateOfMonth)
//e.g. Correct: 14.10-13.11 (Incorrect: 14.10-14.11)
.minusDays(1)
.atEndOfDay()
.convertLocalToUTC()

return Pair(from, to)
}
Expand All @@ -113,13 +117,11 @@ data class TimePeriod(
return when {
month != null -> {
if (startDateOfMonth == 1) {
month.name
displayMonthStartingOn1st(month = month)
} else {
val range = toRange(startDateOfMonth)
val pattern = "MMM dd"
//Don't use formatLocal() because .to is at 23:59:59 =>
// it may appear as +1 day in some timeZones when converted
"${range.from?.format(pattern)} - ${range.to?.format(pattern)}"
"${range.from?.formatLocal(pattern)} - ${range.to?.formatLocal(pattern)}"
}
}
fromToRange != null -> {
Expand All @@ -138,7 +140,7 @@ data class TimePeriod(
return when {
month != null -> {
if (startDateOfMonth == 1) {
month.name
displayMonthStartingOn1st(month = month)
} else {
toRange(startDateOfMonth).toDisplay()
}
Expand All @@ -153,6 +155,17 @@ data class TimePeriod(
toRange(startDateOfMonth).toDisplay()
}
}
}

private fun displayMonthStartingOn1st(month: Month): String {
val year = year
return if (year != null && dateNowUTC().year != year) {
//not this year
"${month.name}, $year"
} else {
//this year
month.name
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.wallet.base.asLiveData
import com.ivy.wallet.base.dateNowUTC
import com.ivy.wallet.base.ioThread
import com.ivy.wallet.logic.WalletCategoryLogic
import com.ivy.wallet.logic.WalletLogic
Expand Down Expand Up @@ -142,19 +143,21 @@ class PieChartStatisticViewModel @Inject constructor(

fun nextMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
load(
period = month.incrementMonthPeriod(ivyContext, 1L),
period = month.incrementMonthPeriod(ivyContext, 1L,year),
type = type.value!!
)
}
}

fun previousMonth() {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
load(
period = month.incrementMonthPeriod(ivyContext, -1L),
period = month.incrementMonthPeriod(ivyContext, -1L,year),
type = type.value!!
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package com.ivy.wallet.ui.statistic.level2
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.wallet.base.TestIdlingResource
import com.ivy.wallet.base.asLiveData
import com.ivy.wallet.base.ioThread
import com.ivy.wallet.base.isNotNullOrBlank
import com.ivy.wallet.base.*
import com.ivy.wallet.logic.*
import com.ivy.wallet.logic.currency.ExchangeRatesLogic
import com.ivy.wallet.model.TransactionHistoryItem
Expand Down Expand Up @@ -316,21 +313,23 @@ class ItemStatisticViewModel @Inject constructor(

fun nextMonth(screen: Screen.ItemStatistic) {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
start(
screen = screen,
period = month.incrementMonthPeriod(ivyContext, 1L),
period = month.incrementMonthPeriod(ivyContext, 1L,year),
reset = false
)
}
}

fun previousMonth(screen: Screen.ItemStatistic) {
val month = period.value?.month
val year = period.value?.year ?: dateNowUTC().year
if (month != null) {
start(
screen = screen,
period = month.incrementMonthPeriod(ivyContext, -1L),
period = month.incrementMonthPeriod(ivyContext, -1L,year),
reset = false
)
}
Expand Down
Loading

0 comments on commit db2897c

Please sign in to comment.