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

Commit

Permalink
WIP: Migrate LocalDateTime to Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Aug 28, 2024
1 parent c816c22 commit 619348e
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 48 deletions.
16 changes: 14 additions & 2 deletions app/src/main/java/com/ivy/wallet/RootActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import com.google.android.play.core.review.ReviewManagerFactory
import com.ivy.IvyNavGraph
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.design.api.IvyUI
import com.ivy.domain.RootScreen
import com.ivy.home.customerjourney.CustomerJourneyCardsProvider
Expand Down Expand Up @@ -66,6 +68,12 @@ class RootActivity : AppCompatActivity(), RootScreen {
@Inject
lateinit var customerJourneyLogic: CustomerJourneyCardsProvider

@Inject
lateinit var timeConverter: TimeConverter

@Inject
lateinit var timeProvider: TimeProvider

private lateinit var createFileLauncher: ActivityResultLauncher<String>
private lateinit var onFileCreated: (fileUri: Uri) -> Unit

Expand Down Expand Up @@ -110,7 +118,9 @@ class RootActivity : AppCompatActivity(), RootScreen {

true -> {
IvyUI(
design = appDesign(ivyContext)
design = appDesign(ivyContext),
timeConverter = timeConverter,
timeProvider = timeProvider,
) {
AppLockedScreen(
onShowOSBiometricsModal = {
Expand All @@ -129,7 +139,9 @@ class RootActivity : AppCompatActivity(), RootScreen {
NavigationRoot(navigation = navigation) { screen ->
IvyUI(
design = appDesign(ivyContext),
includeSurface = screen?.isLegacy ?: true
includeSurface = screen?.isLegacy ?: true,
timeConverter = timeConverter,
timeProvider = timeProvider,
) {
IvyNavGraph(screen)
}
Expand Down
4 changes: 2 additions & 2 deletions shared/base/src/main/java/com/ivy/base/di/BaseHiltBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.ivy.base.threading.IvyDispatchersProvider
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.base.time.impl.DeviceTimeProvider
import com.ivy.base.time.impl.StandardTimeConvert
import com.ivy.base.time.impl.StandardTimeConverter
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -21,5 +21,5 @@ interface BaseHiltBindings {
fun bindTimezoneProvider(impl: DeviceTimeProvider): TimeProvider

@Binds
fun bindTimeConverter(impl: StandardTimeConvert): TimeConverter
fun bindTimeConverter(impl: StandardTimeConverter): TimeConverter
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.time.LocalDate
import java.time.LocalDateTime
import javax.inject.Inject

class StandardTimeConvert @Inject constructor(
class StandardTimeConverter @Inject constructor(
private val timeZoneProvider: TimeProvider
) : TimeConverter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import io.mockk.mockk
import org.junit.Before
import org.junit.Test

class StandardTimeConvertPropertyTest {
class StandardTimeConverterPropertyTest {

private val timeProvider = mockk<TimeProvider>()

private lateinit var converter: TimeConverter

@Before
fun setup() {
converter = StandardTimeConvert(
converter = StandardTimeConverter(
timeZoneProvider = timeProvider
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import java.time.LocalDateTime
import java.time.ZoneOffset

@RunWith(TestParameterInjector::class)
class StandardTimeConvertTest {
class StandardTimeConverterTest {

private val timeProvider = mockk<TimeProvider>()

private lateinit var converter: TimeConverter

@Before
fun setup() {
converter = StandardTimeConvert(
converter = StandardTimeConverter(
timeZoneProvider = timeProvider
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ivy.wallet.ui.theme.modal

import android.annotation.SuppressLint
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -31,6 +33,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ivy.data.model.IntervalType
import com.ivy.design.api.LocalTimeConverter
import com.ivy.design.api.LocalTimeProvider
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
import com.ivy.legacy.IvyWalletPreview
Expand All @@ -44,7 +48,6 @@ import com.ivy.legacy.utils.addKeyboardListener
import com.ivy.legacy.utils.dateNowUTC
import com.ivy.legacy.utils.formatDateOnlyWithYear
import com.ivy.legacy.utils.onScreenStart
import com.ivy.legacy.utils.timeNowUTC
import com.ivy.ui.R
import com.ivy.wallet.ui.theme.Gradient
import com.ivy.wallet.ui.theme.GradientIvy
Expand All @@ -58,13 +61,15 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import java.util.UUID
import java.util.concurrent.TimeUnit

@Deprecated("Old design system. Use `:ivy-design` and Material3")
data class ChoosePeriodModalData(
val id: UUID = UUID.randomUUID(),
val period: TimePeriod
)

@SuppressLint("ComposeModifierMissing")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Suppress("ParameterNaming")
@Composable
Expand Down Expand Up @@ -156,7 +161,7 @@ fun BoxWithConstraintsScope.ChoosePeriodModal(

@Composable
@Suppress("ParameterNaming")
private fun ChooseMonth(
private fun ColumnScope.ChooseMonth(
selectedMonthYear: MonthYear?,
onSelected: (MonthYear) -> Unit,
) {
Expand Down Expand Up @@ -252,10 +257,10 @@ data class MonthYear(

@Composable
private fun MonthButton(
modifier: Modifier = Modifier,
selected: Boolean,
text: String,
onClick: () -> Unit
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
val background = if (selected) GradientIvy else Gradient.solid(UI.colors.medium)
Text(
Expand All @@ -282,9 +287,9 @@ private fun MonthButton(

@Composable
@Suppress("ParameterNaming")
private fun FromToRange(
timeRange: com.ivy.legacy.data.model.FromToTimeRange?,
onSelected: (com.ivy.legacy.data.model.FromToTimeRange?) -> Unit,
private fun ColumnScope.FromToRange(
timeRange: FromToTimeRange?,
onSelected: (FromToTimeRange?) -> Unit,
) {
Text(
modifier = Modifier
Expand All @@ -298,19 +303,20 @@ private fun FromToRange(

Spacer(Modifier.height(16.dp))

val converter = LocalTimeConverter.current
IntervalFromToDate(
border = IntervalBorder.FROM,
dateTime = timeRange?.from,
otherEndDateTime = timeRange?.to
dateTime = with(converter) { timeRange?.from?.toLocalDateTime() },
otherEndDateTime = with(converter) { timeRange?.to?.toLocalDateTime() }
) { from ->
onSelected(
if (from == null && timeRange?.to == null) {
null
} else {
timeRange?.copy(
from = from
) ?: com.ivy.legacy.data.model.FromToTimeRange(
from = from,
from = with(converter) { from?.toUTC() }
) ?: FromToTimeRange(
from = with(converter) { from?.toUTC() },
to = null
)
}
Expand All @@ -321,18 +327,18 @@ private fun FromToRange(

IntervalFromToDate(
border = IntervalBorder.TO,
dateTime = timeRange?.to,
otherEndDateTime = timeRange?.from
dateTime = with(converter) { timeRange?.to?.toLocalDateTime() },
otherEndDateTime = with(converter) { timeRange?.from?.toLocalDateTime() },
) { to ->
onSelected(
if (timeRange?.from == null && to == null) {
null
} else {
timeRange?.copy(
to = to?.plusDays(1)?.minusNanos(1)
) ?: com.ivy.legacy.data.model.FromToTimeRange(
to = with(converter) { to?.plusDays(1)?.minusNanos(1)?.toUTC() }
) ?: FromToTimeRange(
from = null,
to = to
to = with(converter) { to?.toUTC() }
)
}
)
Expand Down Expand Up @@ -435,11 +441,11 @@ private enum class IntervalBorder {

@Composable
@Suppress("ParameterNaming")
private fun LastNPeriod(
private fun ColumnScope.LastNPeriod(
modalScrollState: ScrollState,
lastNTimeRange: com.ivy.legacy.data.model.LastNTimeRange?,
lastNTimeRange: LastNTimeRange?,

onSelected: (com.ivy.legacy.data.model.LastNTimeRange) -> Unit
onSelected: (LastNTimeRange) -> Unit
) {
val rootView = LocalView.current
val coroutineScope = rememberCoroutineScope()
Expand Down Expand Up @@ -495,12 +501,13 @@ private fun LastNPeriod(

@Composable
@Suppress("ParameterNaming")
private fun AllTime(
timeRange: com.ivy.legacy.data.model.FromToTimeRange?,
onSelected: (com.ivy.legacy.data.model.FromToTimeRange?) -> Unit,
private fun ColumnScope.AllTime(
timeRange: FromToTimeRange?,
onSelected: (FromToTimeRange?) -> Unit,
) {
val timeProvider = LocalTimeProvider.current
val active = timeRange != null && timeRange.from == null &&
timeRange.to != null && timeRange.to!!.isAfter(timeNowUTC())
timeRange.to != null && timeRange.to!!.isAfter(timeProvider.utcNow())

Text(
modifier = Modifier
Expand All @@ -523,9 +530,9 @@ private fun AllTime(
if (active) {
null
} else {
com.ivy.legacy.data.model.FromToTimeRange(
FromToTimeRange(
from = null,
to = timeNowUTC().plusDays(1)
to = timeProvider.utcNow().plusSeconds(TimeUnit.HOURS.toSeconds(24L))
)
}
)
Expand Down Expand Up @@ -556,8 +563,9 @@ private fun Preview_FromTo() {
modal = ChoosePeriodModalData(
period = TimePeriod(
fromToRange = FromToTimeRange(
from = timeNowUTC(),
to = timeNowUTC().plusDays(35)
from = LocalTimeProvider.current.utcNow(),
to = LocalTimeProvider.current.utcNow()
.plusSeconds(TimeUnit.DAYS.toSeconds(36L))
)
)
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ivy.wallet.ui.theme.modal

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
Expand All @@ -22,13 +23,14 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
import com.ivy.design.utils.thenIf
import com.ivy.legacy.IvyWalletPreview
import com.ivy.legacy.data.model.Month
import com.ivy.legacy.data.model.Month.Companion.monthsList
import com.ivy.legacy.utils.dateNowUTC
import com.ivy.legacy.utils.drawColoredShadow
import com.ivy.legacy.utils.hideKeyboard
import com.ivy.legacy.utils.onScreenStart
import com.ivy.design.utils.thenIf
import com.ivy.ui.R
import com.ivy.wallet.ui.theme.Gradient
import com.ivy.wallet.ui.theme.Ivy
Expand All @@ -37,15 +39,16 @@ import com.ivy.wallet.ui.theme.findContrastTextColor
import java.time.LocalDate
import java.util.UUID

@SuppressLint("ComposeModifierMissing")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Suppress("ParameterNaming")
@Composable
fun BoxWithConstraintsScope.MonthPickerModal(
id: UUID = UUID.randomUUID(),
initialDate: LocalDate,
visible: Boolean,
dismiss: () -> Unit,
onMonthSelected: (Int) -> Unit
onMonthSelected: (Int) -> Unit,
id: UUID = UUID.randomUUID(),
) {
var selectedMonth by remember {
mutableStateOf(initialDate.monthValue)
Expand Down Expand Up @@ -112,7 +115,7 @@ private fun MonthPicker(

@Composable
private fun MonthButton(
month: com.ivy.legacy.data.model.Month,
month: Month,
selected: Boolean,
onClick: () -> Unit,
) {
Expand Down Expand Up @@ -159,8 +162,8 @@ private fun Preview() {
MonthPickerModal(
initialDate = dateNowUTC(),
visible = true,
dismiss = {}
) {
}
dismiss = {},
onMonthSelected = {}
)
}
}
14 changes: 14 additions & 0 deletions temp/old-design/src/main/java/com/ivy/design/api/IvyUI.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ivy.design.api

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -9,14 +10,25 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.design.IvyContext
import com.ivy.design.l0_system.IvyTheme

val LocalIvyContext = compositionLocalOf<IvyContext> { error("No LocalIvyContext") }

@Deprecated("Used only for time migration to Instant. Never use it in new code!")
val LocalTimeConverter = compositionLocalOf<TimeConverter> { error("No LocalTimeConverter") }
@Deprecated("Used only for time migration to Instant. Never use it in new code!")
val LocalTimeProvider = compositionLocalOf<TimeProvider> { error("No LocalTimeProvider") }


@SuppressLint("ComposeModifierMissing")
@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Composable
fun IvyUI(
timeConverter: TimeConverter,
timeProvider: TimeProvider,
design: IvyDesign,
includeSurface: Boolean = true,
content: @Composable BoxWithConstraintsScope.() -> Unit
Expand All @@ -25,6 +37,8 @@ fun IvyUI(

CompositionLocalProvider(
LocalIvyContext provides ivyContext,
LocalTimeConverter provides timeConverter,
LocalTimeProvider provides timeProvider,
) {
IvyTheme(
theme = ivyContext.theme,
Expand Down
Loading

0 comments on commit 619348e

Please sign in to comment.