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

Commit

Permalink
Fix end-of-time handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Aug 29, 2024
1 parent 973f6ed commit 71f7aee
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 27 deletions.
6 changes: 0 additions & 6 deletions screen/budgets/src/main/java/com/ivy/budgets/BudgetScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ivy.base.legacy.Theme
import com.ivy.budgets.model.DisplayBudget
import com.ivy.design.api.LocalTimeFormatter
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
import com.ivy.legacy.data.model.Month
import com.ivy.legacy.data.model.TimePeriod
import com.ivy.legacy.datamodel.Budget
import com.ivy.legacy.legacy.ui.theme.components.BudgetBattery
import com.ivy.legacy.utils.clickableNoIndication
import com.ivy.legacy.utils.format
Expand All @@ -44,7 +39,6 @@ import com.ivy.wallet.ui.theme.components.IvyIcon
import com.ivy.wallet.ui.theme.components.ReorderButton
import com.ivy.wallet.ui.theme.components.ReorderModalSingleType
import com.ivy.wallet.ui.theme.wallet.AmountCurrencyB1
import kotlinx.collections.immutable.persistentListOf

@Composable
fun BoxWithConstraintsScope.BudgetScreen(screen: BudgetScreen) {
Expand Down
3 changes: 0 additions & 3 deletions screen/home/src/main/java/com/ivy/home/HomeHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ import com.ivy.wallet.ui.theme.components.IvyOutlinedButton
import com.ivy.wallet.ui.theme.wallet.AmountCurrencyB1
import kotlin.math.absoluteValue

private const val OverflowLengthOfBalance = 7
private const val OverflowLengthOfMonthRange = 12

@ExperimentalAnimationApi
@Composable
internal fun HomeHeader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.util.UUID

typealias LegacyTransaction = Transaction

@Suppress("DataClassDefaultValues")
@Deprecated("Legacy data model. Will be deleted")
@Immutable
data class Transaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.annotation.StringRes
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

@Suppress("UnnecessaryPassThroughClass")
class AndroidResourceProvider @Inject constructor(
@ApplicationContext
private val context: Context,
Expand Down
14 changes: 14 additions & 0 deletions shared/base/src/main/java/com/ivy/base/time/TimeRange.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ivy.base.time

import java.time.Instant
import java.util.concurrent.TimeUnit

const val SAFE_OFFSET_DAYS = 365 * 10L

val INSTANT_MIN_SAFE: Instant
get() = Instant.ofEpochMilli(Long.MIN_VALUE)
.plusSeconds(TimeUnit.DAYS.toSeconds(SAFE_OFFSET_DAYS))

val INSTANT_MAX_SAFE: Instant
get() = Instant.ofEpochMilli(Long.MAX_VALUE)
.minusSeconds(TimeUnit.DAYS.toSeconds(SAFE_OFFSET_DAYS))
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.ivy.base.time.impl

import com.ivy.base.time.INSTANT_MAX_SAFE
import com.ivy.base.time.INSTANT_MIN_SAFE
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import java.time.DateTimeException
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneOffset
import javax.inject.Inject

class StandardTimeConverter @Inject constructor(
Expand All @@ -18,9 +21,9 @@ class StandardTimeConverter @Inject constructor(
} catch (e: DateTimeException) {
// This happens when we overflow MIN/MAX for LocalDateTime
if (this > Instant.EPOCH) {
LocalDateTime.MAX.minusYears(10)
INSTANT_MAX_SAFE.atZone(ZoneOffset.UTC).toLocalDateTime()
} else {
LocalDateTime.MIN.plusYears(10)
INSTANT_MIN_SAFE.atZone(ZoneOffset.UTC).toLocalDateTime()
}
}

Expand All @@ -30,9 +33,9 @@ class StandardTimeConverter @Inject constructor(
} catch (e: DateTimeException) {
// This happens when we overflow MIN/MAX for LocalDate
if (this > Instant.EPOCH) {
LocalDate.MAX.minusYears(10)
INSTANT_MAX_SAFE.atZone(ZoneOffset.UTC).toLocalDate()
} else {
LocalDate.MIN.plusYears(10)
INSTANT_MIN_SAFE.atZone(ZoneOffset.UTC).toLocalDate()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.arbitrary.boolean
import io.kotest.property.arbitrary.instant
import io.kotest.property.arbitrary.list
import io.kotest.property.arbitrary.localDateTime
import io.kotest.property.arbitrary.map
import io.kotest.property.arbitrary.next
import io.kotest.property.arbitrary.string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fun IntervalType.forDisplay(intervalN: Int): String {
}
}

@Suppress("MagicNumber")
fun IntervalType.incrementDate(date: Instant, intervalN: Long): Instant {
return when (this) {
IntervalType.DAY -> date.plus(intervalN, ChronoUnit.DAYS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.ivy.wallet.domain.action.transaction
import com.ivy.base.legacy.Transaction
import com.ivy.base.legacy.TransactionHistoryItem
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.data.db.dao.read.AccountDao
import com.ivy.data.repository.AccountRepository
import com.ivy.data.repository.TagRepository
Expand All @@ -21,7 +20,6 @@ class TrnsWithDateDivsAct @Inject constructor(
private val exchangeAct: ExchangeAct,
private val tagRepository: TagRepository,
private val accountRepository: AccountRepository,
private val timeProvider: TimeProvider,
) : FPAction<TrnsWithDateDivsAct.Input, List<TransactionHistoryItem>>() {

override suspend fun Input.compose(): suspend () -> List<TransactionHistoryItem> = suspend {
Expand All @@ -31,7 +29,6 @@ class TrnsWithDateDivsAct @Inject constructor(
getTags = { tagIds -> tagRepository.findByIds(tagIds) },
getAccount = accountDao::findById then { it?.toLegacyDomain() },
accountRepository = accountRepository,
timeProvider = timeProvider,
exchange = ::actInput then exchangeAct
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PlannedPaymentsGenerator @Inject constructor(
}
}

@Suppress("MagicNumber")
private suspend fun generateRecurring(rule: PlannedPaymentRule) {
val startDate = rule.startDate!!
val endDate = startDate.plusSeconds(94_608_000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.ivy.frp.then
import com.ivy.legacy.datamodel.Account
import com.ivy.legacy.datamodel.temp.toImmutableLegacyTags
import com.ivy.legacy.datamodel.temp.toLegacyDomain
import com.ivy.legacy.utils.convertUTCtoLocal
import com.ivy.legacy.utils.toEpochSeconds
import com.ivy.wallet.domain.data.TransactionHistoryDateDivider
import com.ivy.wallet.domain.deprecated.logic.currency.ExchangeRatesLogic
Expand Down Expand Up @@ -67,8 +66,6 @@ suspend fun transactionsWithDateDividers(
transactions: List<Transaction>,
baseCurrencyCode: String,
accountRepository: AccountRepository,
timeProvider: TimeProvider,

@SideEffect
getAccount: suspend (accountId: UUID) -> Account?,
@SideEffect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ private fun ColumnScope.LastNPeriod(
}

@Composable
@Suppress("ParameterNaming")
@Suppress("ParameterNaming", "MagicNumber")
private fun ColumnScope.AllTime(
timeRange: FromToTimeRange?,
onSelected: (FromToTimeRange?) -> Unit,
Expand Down Expand Up @@ -555,6 +555,7 @@ private fun Preview_MonthSelected() {
}
}

@Suppress("MagicNumber")
@Preview
@Composable
private fun Preview_FromTo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data class LoanRecordModalData(
val id: UUID = UUID.randomUUID(),
)

@Suppress("CyclomaticComplexMethod", "LongMethod")
@SuppressLint("ComposeModifierMissing")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.UUID

@Suppress("CyclomaticComplexMethod")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Composable
fun TransactionCard(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.ivy.legacy.utils
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.ivy.base.legacy.stringRes
import com.ivy.base.time.INSTANT_MAX_SAFE
import com.ivy.base.time.INSTANT_MIN_SAFE
import com.ivy.base.time.TimeConverter
import com.ivy.frp.Total
import com.ivy.ui.R
Expand All @@ -14,7 +16,6 @@ import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.concurrent.TimeUnit

@Deprecated("Use the TimeProvider interface via DI")
fun timeNowLocal(): LocalDateTime = LocalDateTime.now()
Expand Down Expand Up @@ -269,11 +270,9 @@ fun endOfMonth(date: LocalDate, timeConverter: TimeConverter): Instant {
fun LocalDate.atEndOfDay(): LocalDateTime =
this.atTime(23, 59, 59)

fun ivyMinTime(): Instant = Instant.ofEpochMilli(Long.MIN_VALUE)
.plusSeconds(TimeUnit.DAYS.toSeconds(365 * 10))
fun ivyMinTime(): Instant = INSTANT_MIN_SAFE

fun ivyMaxTime(): Instant = Instant.ofEpochMilli(Long.MAX_VALUE)
.minusSeconds(TimeUnit.DAYS.toSeconds(365 * 10))
fun ivyMaxTime(): Instant = INSTANT_MAX_SAFE

fun LocalDate.withDayOfMonthSafe(targetDayOfMonth: Int): LocalDate {
val maxDayOfMonth = this.lengthOfMonth()
Expand Down
1 change: 0 additions & 1 deletion temp/old-design/src/main/java/com/ivy/design/api/IvyUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ val LocalTimeProvider = compositionLocalOf<TimeProvider> { error("No LocalTimePr
@Deprecated("Used only for time migration to Instant. Never use it in new code!")
val LocalTimeFormatter = compositionLocalOf<TimeFormatter> { error("No LocalTimeFormatter") }


@SuppressLint("ComposeModifierMissing")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Composable
Expand Down

0 comments on commit 71f7aee

Please sign in to comment.