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

Commit

Permalink
AccountTab: Show/Hide Account Functionality Added
Browse files Browse the repository at this point in the history
  • Loading branch information
shamim-emon committed Nov 4, 2024
1 parent bee9dda commit a44df3d
Show file tree
Hide file tree
Showing 33 changed files with 997 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ivy.accounts

import com.ivy.data.model.Account

sealed interface AccountsEvent {
data class OnReorder(val reorderedList: List<com.ivy.legacy.data.model.AccountData>) :
AccountsEvent
data class OnReorderModalVisible(val reorderVisible: Boolean) : AccountsEvent
data class OnVisibilityChange(val updatedList: List<com.ivy.legacy.data.model.AccountData>) :
data class OnVisibilityUpdate(val updatedList: List<Account>) :
AccountsEvent
data class OnHideModalVisible(val hideVisible: Boolean) : AccountsEvent
}
24 changes: 12 additions & 12 deletions screen/accounts/src/main/java/com/ivy/accounts/AccountsTab.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.ivy.accounts

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand Down Expand Up @@ -55,20 +51,16 @@ import com.ivy.navigation.navigation
import com.ivy.navigation.screenScopedViewModel
import com.ivy.ui.R
import com.ivy.ui.rememberScrollPositionListState
import com.ivy.wallet.ui.theme.GradientGreen
import com.ivy.wallet.ui.theme.Green
import com.ivy.wallet.ui.theme.GreenLight
import com.ivy.wallet.ui.theme.White
import com.ivy.wallet.ui.theme.components.BalanceRow
import com.ivy.wallet.ui.theme.components.BalanceRowMini
import com.ivy.wallet.ui.theme.components.CircleButtonFilled
import com.ivy.wallet.ui.theme.components.ItemIconSDefaultIcon
import com.ivy.wallet.ui.theme.components.IvyCircleButton
import com.ivy.wallet.ui.theme.components.ReorderButton
import com.ivy.wallet.ui.theme.components.ReorderModalSingleType
import com.ivy.wallet.ui.theme.dynamicContrast
import com.ivy.wallet.ui.theme.findContrastTextColor
import com.ivy.wallet.ui.theme.modal.IvyModal
import com.ivy.wallet.ui.theme.toComposeColor
import kotlinx.collections.immutable.persistentListOf
import java.util.UUID
Expand All @@ -92,7 +84,7 @@ private fun BoxWithConstraintsScope.UI(
val nav = navigation()
val ivyContext = com.ivy.legacy.ivyWalletCtx()
var listState = rememberLazyListState()
if (!state.accountsData.isEmpty()) {
if (state.accountsData.any { it.account.isVisible }) {
listState = rememberScrollPositionListState(
key = "accounts_lazy_column",
initialFirstVisibleItemIndex = ivyContext.accountsListState?.firstVisibleItemIndex ?: 0,
Expand Down Expand Up @@ -167,7 +159,7 @@ private fun BoxWithConstraintsScope.UI(
Spacer(Modifier.height(16.dp))
}
}
items(state.accountsData) {
items(state.accountsData.filter { it.account.isVisible }) {
Spacer(Modifier.height(16.dp))
AccountCard(
baseCurrency = state.baseCurrency,
Expand Down Expand Up @@ -199,8 +191,8 @@ private fun BoxWithConstraintsScope.UI(
HideAccountsModal(
visible = state.hideVisible,
initialItems = state.accountsData,
OnDismiss = { onEvent(AccountsEvent.OnHideModalVisible(hideVisible = false)) },
onDone = { onEvent(AccountsEvent.OnVisibilityChange(updatedList = it)) }
onDismiss = { onEvent(AccountsEvent.OnHideModalVisible(hideVisible = false)) },
onComplete = { onEvent(AccountsEvent.OnVisibilityUpdate(updatedList = it)) }
)
ReorderModalSingleType(
visible = state.reorderVisible,
Expand Down Expand Up @@ -371,6 +363,7 @@ private fun PreviewAccountsTabCompactModeDisabled(theme: Theme = Theme.LIGHT) {
icon = null,
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc2 = Account(
Expand All @@ -381,6 +374,7 @@ private fun PreviewAccountsTabCompactModeDisabled(theme: Theme = Theme.LIGHT) {
icon = null,
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc3 = Account(
Expand All @@ -391,6 +385,7 @@ private fun PreviewAccountsTabCompactModeDisabled(theme: Theme = Theme.LIGHT) {
icon = IconAsset.unsafe("revolut"),
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc4 = Account(
Expand All @@ -401,6 +396,7 @@ private fun PreviewAccountsTabCompactModeDisabled(theme: Theme = Theme.LIGHT) {
icon = IconAsset.unsafe("cash"),
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)
val state = AccountsState(
baseCurrency = "BGN",
Expand Down Expand Up @@ -459,6 +455,7 @@ private fun PreviewAccountsTabCompactModeEnabled(theme: Theme = Theme.LIGHT) {
icon = null,
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc2 = Account(
Expand All @@ -469,6 +466,7 @@ private fun PreviewAccountsTabCompactModeEnabled(theme: Theme = Theme.LIGHT) {
icon = null,
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc3 = Account(
Expand All @@ -479,6 +477,7 @@ private fun PreviewAccountsTabCompactModeEnabled(theme: Theme = Theme.LIGHT) {
icon = IconAsset.unsafe("revolut"),
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)

val acc4 = Account(
Expand All @@ -489,6 +488,7 @@ private fun PreviewAccountsTabCompactModeEnabled(theme: Theme = Theme.LIGHT) {
icon = IconAsset.unsafe("cash"),
includeInBalance = true,
orderNum = 0.0,
isVisible = true,
)
val state = AccountsState(
baseCurrency = "BGN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.ivy.base.time.TimeConverter
import com.ivy.base.time.TimeProvider
import com.ivy.data.DataObserver
import com.ivy.data.DataWriteEvent
import com.ivy.data.model.Account
import com.ivy.data.repository.AccountRepository
import com.ivy.domain.features.Features
import com.ivy.legacy.IvyWalletCtx
Expand Down Expand Up @@ -153,7 +154,10 @@ class AccountsViewModel @Inject constructor(
is AccountsEvent.OnReorder -> reorder(event.reorderedList)
is AccountsEvent.OnReorderModalVisible -> reorderModalVisible(event.reorderVisible)
is AccountsEvent.OnHideModalVisible -> hideModalVisible(event.hideVisible)
is AccountsEvent.OnVisibilityChange -> TODO()
is AccountsEvent.OnVisibilityUpdate -> {
updateVisibility(event.updatedList)
hideVisible = false
}
}
}
}
Expand All @@ -168,6 +172,16 @@ class AccountsViewModel @Inject constructor(
startInternally()
}

private suspend fun updateVisibility(accounts: List<Account>) {
ioThread {
accounts.forEach { acc ->
accountRepository.save(acc)
}
}

startInternally()
}

private fun onStart() {
viewModelScope.launch(Dispatchers.Default) {
startInternally()
Expand Down
61 changes: 29 additions & 32 deletions screen/accounts/src/main/java/com/ivy/accounts/HideAccountsModal.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ivy.accounts

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -18,10 +17,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -43,24 +39,44 @@ import com.ivy.wallet.ui.theme.modal.IvyModal
import java.util.UUID
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.ivy.wallet.ui.theme.components.ItemIconSDefaultIcon
import com.ivy.wallet.ui.theme.findContrastTextColor
import com.ivy.wallet.ui.theme.toComposeColor
import kotlinx.collections.immutable.ImmutableList

@Suppress("ModifierMissing")
@SuppressLint("ComposeModifierMissing")
@Composable
fun BoxScope.HideAccountsModal(
visible: Boolean,
initialItems: List<AccountData>,
OnDismiss: () -> Unit,
initialItems: ImmutableList<AccountData>,
onDismiss: () -> Unit,
id: UUID = UUID.randomUUID(),
onDone: (List<AccountData>) -> Unit
onComplete: (List<Account>) -> Unit
) {
val accounts: SnapshotStateList<Account> = remember(id) {
mutableStateListOf<Account>().apply {
addAll(initialItems.map { it.account })
}
}

val onUpdateItemVisibility: (AccountId, Boolean) -> Unit =
{ id, isVisible ->
var selectedIndex = -1
accounts.forEachIndexed { index, account ->
if (account.id == id) {
selectedIndex = index
}
}
accounts[selectedIndex] = accounts[selectedIndex].copy(isVisible = !isVisible)
}

IvyModal(
id = id,
visible = visible,
scrollState = null,
dismiss = OnDismiss,
dismiss = onDismiss,
PrimaryAction = {
IvyCircleButton(
modifier = Modifier
Expand All @@ -69,8 +85,7 @@ fun BoxScope.HideAccountsModal(
icon = R.drawable.ic_check,
tint = White
) {
//onUpdateItemVisibility()
OnDismiss.invoke()
onComplete.invoke(accounts)
}
}
) {
Expand All @@ -87,28 +102,12 @@ fun BoxScope.HideAccountsModal(

Spacer(Modifier.height(24.dp))

val accounts = remember {
mutableStateListOf<Account>().apply {
addAll(initialItems.map { it.account })
}
}
val onUpdateItemVisibility: (AccountId, Boolean) -> Unit =
{ id, isVisible ->
var selectedIndex = -1
accounts.forEachIndexed { index, account ->
if (account.id == id) {
selectedIndex = index
}
}
accounts[selectedIndex] = accounts[selectedIndex].copy(isVisible = !isVisible)
}

LazyColumn(
modifier = Modifier.fillMaxHeight(),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
items(accounts) { account ->
HideAccountsColumn(
HideAccountsRow(
account = account,
onClick = onUpdateItemVisibility
)
Expand All @@ -117,16 +116,16 @@ fun BoxScope.HideAccountsModal(
Spacer(Modifier.height(150.dp))
}
}

}
}

@Composable
private fun HideAccountsColumn(
private fun HideAccountsRow(
account: Account,
onClick: (AccountId, Boolean) -> Unit
) {
val contrastColor = findContrastTextColor(account.color.value.toComposeColor())

Row(
modifier = Modifier
.padding(horizontal = 16.dp)
Expand All @@ -135,7 +134,6 @@ private fun HideAccountsColumn(
.background(color = account.color.value.toComposeColor()),
verticalAlignment = Alignment.CenterVertically
) {

Spacer(Modifier.width(12.dp))

ItemIconSDefaultIcon(
Expand All @@ -162,7 +160,7 @@ private fun HideAccountsColumn(

IvyIconScaled(
modifier = Modifier
.size(size = 32.dp)
.size(size = 24.dp)
.clickable {
onClick.invoke(account.id, account.isVisible)
},
Expand All @@ -173,6 +171,5 @@ private fun HideAccountsColumn(
)

Spacer(Modifier.width(16.dp))

}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private fun BoxWithConstraintsScope.Preview(isDark: Boolean = false) {
dateTime = testDateTime,
description = null,
category = null,
account = Account(name = "phyre", Orange.toArgb()),
account = Account(name = "phyre", isVisible = true, color = Orange.toArgb()),
toAccount = null,
amount = 0.0,
dueDate = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ class CSVImporterV2 @Inject constructor(
),
color = colorArgb,
icon = icon,
orderNum = orderNum ?: accountDao.findMaxOrderNum().nextOrderNum()
orderNum = orderNum ?: accountDao.findMaxOrderNum().nextOrderNum(),
isVisible = true,
)
val domainAccount = newAccount.toDomainAccount(currencyRepository).getOrNull()
?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ private fun Preview() {
IvyWalletComponentPreview {
Suggestions(
suggestions = listOf(
Account("Cash", Green.toArgb()),
Account("Bank", Red.toArgb()),
Account("Revolut", Purple.toArgb())
Account("Cash", isVisible = true, color = Green.toArgb()),
Account("Bank", isVisible = true, color = Red.toArgb()),
Account("Revolut", isVisible = true, color = Purple.toArgb())
),
onAddSuggestion = { }
) {
Expand Down
Loading

0 comments on commit a44df3d

Please sign in to comment.