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

fix-issue-3481 #3482

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/src/main/java/com/ivy/wallet/RootActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ import com.ivy.legacy.Constants
import com.ivy.legacy.IvyWalletCtx
import com.ivy.legacy.appDesign
import com.ivy.legacy.utils.activityForResultLauncher
import com.ivy.legacy.utils.convertLocalToUTC
import com.ivy.legacy.utils.sendToCrashlytics
import com.ivy.legacy.utils.simpleActivityForResultLauncher
import com.ivy.legacy.utils.timeNowLocal
import com.ivy.navigation.Navigation
import com.ivy.navigation.NavigationRoot
import com.ivy.ui.R
Expand Down Expand Up @@ -210,7 +208,7 @@ class RootActivity : AppCompatActivity(), RootScreen {
private fun setupTimePicker() {
ivyContext.onShowTimePicker = { initialTime,
onTimePicked ->
val nowLocal = initialTime ?: timeNowLocal().toLocalTime()
val nowLocal = initialTime ?: timeProvider.localTimeNow()
val is24Hour = android.text.format.DateFormat.is24HourFormat(this)
akashs056 marked this conversation as resolved.
Show resolved Hide resolved
val timeFormat = if (is24Hour) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H

Expand All @@ -223,7 +221,7 @@ class RootActivity : AppCompatActivity(), RootScreen {
picker.show(supportFragmentManager, "timePicker")
picker.addOnPositiveButtonClickListener {
onTimePicked(
LocalTime.of(picker.hour, picker.minute).convertLocalToUTC().withSecond(0)
LocalTime.of(picker.hour, picker.minute).withSecond(0)
akashs056 marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ sealed interface LoanScreenEvent {
data class OnReOrderModalShow(val show: Boolean) : LoanScreenEvent
data object OnAddLoan : LoanScreenEvent
data object OnLoanModalDismiss : LoanScreenEvent
data object OnChangeDate : LoanScreenEvent
data object OnChangeTime : LoanScreenEvent

/** Toggles paid off loans visibility */
data object OnTogglePaidOffLoanVisibility : LoanScreenEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.ivy.legacy.datamodel.Account
import com.ivy.loans.loan.data.DisplayLoan
import com.ivy.wallet.ui.theme.modal.LoanModalData
import kotlinx.collections.immutable.ImmutableList
import java.time.Instant

data class LoanScreenState(
val baseCurrency: String,
Expand All @@ -15,4 +16,5 @@ data class LoanScreenState(
val totalOweAmount: String,
val totalOwedAmount: String,
val paidOffLoanVisibility: Boolean,
val dateTime: Instant
)
62 changes: 61 additions & 1 deletion screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewModelScope
import com.ivy.base.legacy.SharedPrefs
import com.ivy.base.model.processByType
import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.data.db.dao.read.LoanRecordDao
import com.ivy.data.db.dao.read.SettingsDao
import com.ivy.data.db.dao.write.WriteLoanDao
Expand All @@ -20,6 +22,7 @@ import com.ivy.legacy.utils.getDefaultFIATCurrency
import com.ivy.legacy.utils.ioThread
import com.ivy.loans.loan.data.DisplayLoan
import com.ivy.ui.ComposeViewModel
import com.ivy.ui.time.impl.DateTimePicker
import com.ivy.wallet.domain.action.account.AccountsAct
import com.ivy.wallet.domain.action.loan.LoansAct
import com.ivy.wallet.domain.deprecated.logic.LoanCreator
Expand All @@ -33,6 +36,8 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.Instant
import java.time.LocalDateTime
import java.util.UUID
import javax.inject.Inject

Expand All @@ -48,6 +53,9 @@ class LoanViewModel @Inject constructor(
private val loansAct: LoansAct,
private val accountsAct: AccountsAct,
private val loanWriter: WriteLoanDao,
private val timeConverter: TimeConverter,
private val timeProvider: TimeProvider,
private val dateTimePicker: DateTimePicker,
) : ComposeViewModel<LoanScreenState, LoanScreenEvent>() {

private val baseCurrencyCode = mutableStateOf(getDefaultFIATCurrency().currencyCode)
Expand All @@ -56,6 +64,7 @@ class LoanViewModel @Inject constructor(
private val selectedAccount = mutableStateOf<Account?>(null)
private val loanModalData = mutableStateOf<LoanModalData?>(null)
private val reorderModalVisible = mutableStateOf(false)
private var dateTime = mutableStateOf<Instant>(timeProvider.utcNow())

/** If true paid off loans will be visible */
private val paidOffLoanVisibility = mutableStateOf(true)
Expand All @@ -81,7 +90,8 @@ class LoanViewModel @Inject constructor(
reorderModalVisible = getReorderModalVisible(),
totalOweAmount = getTotalOweAmount(totalOweAmount, defaultCurrencyCode),
totalOwedAmount = getTotalOwedAmount(totalOwedAmount, defaultCurrencyCode),
paidOffLoanVisibility = getPaidOffLoanVisibility()
paidOffLoanVisibility = getPaidOffLoanVisibility(),
dateTime = dateTime.value
)
}

Expand Down Expand Up @@ -126,6 +136,7 @@ class LoanViewModel @Inject constructor(

is LoanScreenEvent.OnLoanModalDismiss -> {
loanModalData.value = null
dateTime.value = timeProvider.utcNow()
}

is LoanScreenEvent.OnReOrderModalShow -> {
Expand All @@ -143,13 +154,22 @@ class LoanViewModel @Inject constructor(
LoanScreenEvent.OnTogglePaidOffLoanVisibility -> {
updatePaidOffLoanVisibility()
}

is LoanScreenEvent.OnChangeDate -> {
handleChangeDate()
}
is LoanScreenEvent.OnChangeTime -> {
handleChangeTime()
}
}
}

private fun start() {
viewModelScope.launch(Dispatchers.Default) {
TestIdlingResource.increment()

dateTime.value = timeProvider.utcNow()

defaultCurrencyCode = ioThread {
settingsDao.findFirst().currency
}.also {
Expand Down Expand Up @@ -226,6 +246,46 @@ class LoanViewModel @Inject constructor(
}
}

private fun handleChangeDate() {
dateTimePicker.pickDate(
initialDate = loanModalData.value?.loan?.dateTime?.let {
with(timeConverter) { it.toUTC() }
} ?: timeProvider.utcNow()
) { localDate ->
val localTime = loanModalData.value?.loan?.dateTime?.let {
with(timeConverter) { it.toLocalTime() }
} ?: timeProvider.localTimeNow()

updateDateTime(localDate.atTime(localTime))
}
}

private fun handleChangeTime() {
dateTimePicker.pickTime(
initialTime = loanModalData.value?.loan?.dateTime?.let {
with(timeConverter) { it.toLocalTime() }
} ?: timeProvider.localTimeNow()
) { localTime ->
val localDate = loanModalData.value?.loan?.dateTime?.let {
with(timeConverter) { it.toLocalDate() }
} ?: timeProvider.localDateNow()

updateDateTime(localDate.atTime(localTime))
}
}

private fun updateDateTime(newDateTime: LocalDateTime) {
val newDateTimeUtc = with(timeConverter) { newDateTime.toUTC() }
loanModalData.value?.let { currentData ->
loanModalData.value = currentData.copy(
loan = currentData.loan?.copy(
dateTime = newDateTime
)
)
dateTime.value = newDateTimeUtc
}
}

private fun createLoan(data: CreateLoanData) {
viewModelScope.launch {
TestIdlingResource.increment()
Expand Down
11 changes: 10 additions & 1 deletion screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.ivy.wallet.ui.theme.modal.LoanModal
import com.ivy.wallet.ui.theme.modal.LoanModalData
import com.ivy.wallet.ui.theme.toComposeColor
import kotlinx.collections.immutable.persistentListOf
import java.time.Instant
import java.time.LocalDateTime

@Composable
Expand Down Expand Up @@ -176,6 +177,13 @@ private fun BoxWithConstraintsScope.UI(
dismiss = {
onEventHandler.invoke(LoanScreenEvent.OnLoanModalDismiss)
},
dateTime = state.dateTime,
onSetDate = {
onEventHandler.invoke(LoanScreenEvent.OnChangeDate)
},
onSetTime = {
onEventHandler.invoke(LoanScreenEvent.OnChangeTime)
}
)
}

Expand Down Expand Up @@ -460,7 +468,8 @@ private fun Preview(theme: Theme = Theme.LIGHT) {
),
reorderModalVisible = false,
selectedAccount = null,
paidOffLoanVisibility = true
paidOffLoanVisibility = true,
dateTime = Instant.now()
)
IvyWalletPreview(theme) {
UI(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import com.ivy.legacy.utils.clickableNoIndication
import com.ivy.legacy.utils.drawColoredShadow
import com.ivy.legacy.utils.format
import com.ivy.legacy.utils.formatNicely
import com.ivy.legacy.utils.formatNicelyWithTime
import com.ivy.legacy.utils.isNotNullOrBlank
import com.ivy.legacy.utils.rememberInteractionSource
import com.ivy.legacy.utils.setStatusBarDarkTextCompat
Expand Down Expand Up @@ -86,6 +85,7 @@ import com.ivy.wallet.ui.theme.modal.LoanRecordModal
import com.ivy.wallet.ui.theme.modal.ProgressModal
import com.ivy.wallet.ui.theme.toComposeColor
import kotlinx.collections.immutable.persistentListOf
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.UUID
Expand All @@ -105,7 +105,7 @@ fun BoxWithConstraintsScope.LoanDetailsScreen(screen: LoanDetailsScreen) {
@Composable
private fun BoxWithConstraintsScope.UI(
state: LoanDetailsScreenState,
onEventHandler: (LoanDetailsScreenEvent) -> Unit = {},
onEventHandler: (LoanDetailsScreenEvent) -> Unit = {}
) {
val itemColor = state.loan?.color?.toComposeColor() ?: Gray

Expand Down Expand Up @@ -201,7 +201,8 @@ private fun BoxWithConstraintsScope.UI(
}
}

LoanModal(modal = state.loanModalData, onCreateLoan = {
LoanModal(
modal = state.loanModalData, onCreateLoan = {
// do nothing
}, onEditLoan = { loan, createLoanTransaction ->
onEventHandler.invoke(LoanModalEvent.OnEditLoanModal(loan, createLoanTransaction))
Expand All @@ -211,9 +212,17 @@ private fun BoxWithConstraintsScope.UI(
onEventHandler.invoke(LoanDetailsScreenEvent.OnCreateAccount(createAccountData))
}, accounts = state.accounts, onPerformCalculations = {
onEventHandler.invoke(LoanModalEvent.PerformCalculation)
})
}, dateTime = state.dateTime,
onSetDate = {
onEventHandler.invoke(LoanModalEvent.OnChangeDate)
},
onSetTime = {
onEventHandler.invoke(LoanModalEvent.OnChangeTime)
},
)

LoanRecordModal(modal = state.loanRecordModalData, onCreate = {
LoanRecordModal(
modal = state.loanRecordModalData, onCreate = {
onEventHandler.invoke(LoanRecordModalEvent.OnCreateLoanRecord(it))
}, onEdit = {
onEventHandler.invoke(LoanRecordModalEvent.OnEditLoanRecord(it))
Expand All @@ -223,7 +232,15 @@ private fun BoxWithConstraintsScope.UI(
onEventHandler.invoke(LoanRecordModalEvent.OnDismissLoanRecord)
}, onCreateAccount = { createAccountData ->
onEventHandler.invoke(LoanDetailsScreenEvent.OnCreateAccount(createAccountData))
})
},
dateTime = state.dateTime,
onSetDate = {
onEventHandler.invoke(LoanRecordModalEvent.OnChangeDate)
},
onSetTime = {
onEventHandler.invoke(LoanRecordModalEvent.OnChangeTime)
},
)

DeleteModal(
visible = state.isDeleteModalVisible,
Expand Down Expand Up @@ -793,12 +810,16 @@ private fun InitialRecordItem(
padding = 8.dp,
) {}

loan.dateTime?.formatNicelyWithTime(
noWeekDay = false
)?.let { nicelyFormattedDate ->
val timeFormatter = LocalTimeFormatter.current

loan.dateTime?.let { dateTime ->
Text(
modifier = Modifier.padding(horizontal = 24.dp),
text = nicelyFormattedDate.uppercase(),
text = with(timeFormatter) {
dateTime.format(
TimeFormatter.Style.DateAndTime(includeWeekDay = true)
).uppercase()
},
style = UI.typo.nC.style(
color = Gray,
fontWeight = FontWeight.Bold
Expand Down Expand Up @@ -892,7 +913,8 @@ private fun Preview_Empty() {
isDeleteModalVisible = false,
loanModalData = null,
loanRecordModalData = null,
waitModalVisible = false
waitModalVisible = false,
dateTime = Instant.now()
)
) {}
}
Expand Down Expand Up @@ -952,7 +974,8 @@ private fun Preview_Records(theme: Theme = Theme.LIGHT) {
isDeleteModalVisible = false,
loanModalData = null,
loanRecordModalData = null,
waitModalVisible = false
waitModalVisible = false,
dateTime = Instant.now()
)
) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ivy.loans.loan.data.DisplayLoanRecord
import com.ivy.wallet.ui.theme.modal.LoanModalData
import com.ivy.wallet.ui.theme.modal.LoanRecordModalData
import kotlinx.collections.immutable.ImmutableList
import java.time.Instant

data class LoanDetailsScreenState(
val baseCurrency: String,
Expand All @@ -20,5 +21,6 @@ data class LoanDetailsScreenState(
val loanModalData: LoanModalData?,
val loanRecordModalData: LoanRecordModalData?,
val waitModalVisible: Boolean,
val isDeleteModalVisible: Boolean
val isDeleteModalVisible: Boolean,
val dateTime: Instant
)
Loading