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

Commit

Permalink
Fix issue 2625 (#2793)
Browse files Browse the repository at this point in the history
* Migrate screen-onboarding to ComposeViewModel and new architecture

* Fix and format the code

* Incorporate suggested changes
  • Loading branch information
beradeep authored Oct 17, 2023
1 parent 929bc21 commit c166a17
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 308 deletions.
4 changes: 2 additions & 2 deletions config/detekt/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ complexity:
ignoreOverloaded: false
CyclomaticComplexMethod:
active: true
threshold: 15
threshold: 20
ignoreSingleWhenExpression: false
ignoreSimpleWhenEntries: false
ignoreNestingFunctions: false
Expand All @@ -128,7 +128,7 @@ complexity:
threshold: 600
LongMethod:
active: true
threshold: 120
threshold: 150
LongParameterList:
active: true
functionThreshold: 12
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ivy.onboarding

import androidx.compose.runtime.Immutable
import com.ivy.legacy.data.model.AccountBalance
import com.ivy.legacy.datamodel.Category
import com.ivy.wallet.domain.data.IvyCurrency
import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData
import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData
import kotlinx.collections.immutable.ImmutableList

@Immutable
data class OnboardingDetailState(
val currency: IvyCurrency,
val accounts: ImmutableList<AccountBalance>,
val accountSuggestions: ImmutableList<CreateAccountData>,
val categories: ImmutableList<Category>,
val categorySuggestions: ImmutableList<CreateCategoryData>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ivy.onboarding

import com.ivy.legacy.datamodel.Account
import com.ivy.legacy.datamodel.Category
import com.ivy.wallet.domain.data.IvyCurrency
import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData
import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData

sealed interface OnboardingEvent {

data object LoginWithGoogle : OnboardingEvent
data object LoginOfflineAccount : OnboardingEvent
data object StartImport : OnboardingEvent
data object ImportSkip : OnboardingEvent
data class ImportFinished(val success: Boolean) : OnboardingEvent
data object StartFresh : OnboardingEvent
data class SetBaseCurrency(val baseCurrency: IvyCurrency) : OnboardingEvent
data class EditAccount(val account: Account, val newBalance: Double) : OnboardingEvent
data class CreateAccount(val data: CreateAccountData) : OnboardingEvent
data object OnAddAccountsDone : OnboardingEvent
data object OnAddAccountsSkip : OnboardingEvent
data class EditCategory(val updatedCategory: Category) : OnboardingEvent
data class CreateCategory(val data: CreateCategoryData) : OnboardingEvent
data object OnAddCategoriesDone : OnboardingEvent
data object OnAddCategoriesSkip : OnboardingEvent
}
137 changes: 37 additions & 100 deletions screen-onboarding/src/main/java/com/ivy/onboarding/OnboardingScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ivy.legacy.IvyWalletPreview
import com.ivy.legacy.data.model.AccountBalance
import com.ivy.legacy.datamodel.Category
import com.ivy.legacy.utils.onScreenStart
import com.ivy.navigation.OnboardingScreen
import com.ivy.onboarding.steps.OnboardingAccounts
import com.ivy.onboarding.steps.OnboardingCategories
Expand All @@ -18,27 +17,17 @@ import com.ivy.onboarding.steps.OnboardingSplashLogin
import com.ivy.onboarding.steps.OnboardingType
import com.ivy.onboarding.viewmodel.OnboardingViewModel
import com.ivy.wallet.domain.data.IvyCurrency
import com.ivy.legacy.datamodel.Account
import com.ivy.legacy.datamodel.Category
import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData
import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData
import com.ivy.legacy.utils.OpResult
import com.ivy.legacy.utils.onScreenStart
import kotlinx.collections.immutable.ImmutableList

@ExperimentalFoundationApi
@Composable
fun BoxWithConstraintsScope.OnboardingScreen(screen: OnboardingScreen) {
val viewModel: OnboardingViewModel = viewModel()

val state by viewModel.state.observeAsState(OnboardingState.SPLASH)
val currency by viewModel.currency.observeAsState(IvyCurrency.getDefault())
val opGoogleSign by viewModel.opGoogleSignIn.observeAsState()

val accountSuggestions by viewModel.accountSuggestions.observeAsState(emptyList())
val accounts by viewModel.accounts.observeAsState(listOf())

val categorySuggestions by viewModel.categorySuggestions.observeAsState(emptyList())
val categories by viewModel.categories.observeAsState(emptyList())
val state by viewModel.state
val uiState = viewModel.uiState()

val isSystemDarkTheme = isSystemInDarkTheme()
onScreenStart {
Expand All @@ -50,32 +39,16 @@ fun BoxWithConstraintsScope.OnboardingScreen(screen: OnboardingScreen) {

UI(
onboardingState = state,
currency = currency,
opGoogleSignIn = opGoogleSign,
currency = uiState.currency,

accountSuggestions = accountSuggestions,
accounts = accounts,
accountSuggestions = uiState.accountSuggestions,
accounts = uiState.accounts,

categorySuggestions = categorySuggestions,
categories = categories,
categorySuggestions = uiState.categorySuggestions,
categories = uiState.categories,

onLoginWithGoogle = viewModel::loginWithGoogle,
onSkip = viewModel::loginOfflineAccount,
onEvent = viewModel::onEvent

onStartImport = viewModel::startImport,
onStartFresh = viewModel::startFresh,

onSetCurrency = viewModel::setBaseCurrency,

onCreateAccount = viewModel::createAccount,
onEditAccount = viewModel::editAccount,
onAddAccountsDone = viewModel::onAddAccountsDone,
onAddAccountsSkip = viewModel::onAddAccountsSkip,

onCreateCategory = viewModel::createCategory,
onEditCategory = viewModel::editCategory,
onAddCategoryDone = viewModel::onAddCategoriesDone,
onAddCategorySkip = viewModel::onAddCategoriesSkip
)
}

Expand All @@ -84,54 +57,34 @@ fun BoxWithConstraintsScope.OnboardingScreen(screen: OnboardingScreen) {
private fun BoxWithConstraintsScope.UI(
onboardingState: OnboardingState,
currency: IvyCurrency,
opGoogleSignIn: OpResult<Unit>?,

accountSuggestions: List<CreateAccountData>,
accounts: List<AccountBalance>,

categorySuggestions: List<CreateCategoryData>,
categories: List<Category>,

onLoginWithGoogle: () -> Unit = {},
onSkip: () -> Unit = {},
accountSuggestions: ImmutableList<CreateAccountData>,
accounts: ImmutableList<AccountBalance>,

onStartImport: () -> Unit = {},
onStartFresh: () -> Unit = {},
categorySuggestions: ImmutableList<CreateCategoryData>,
categories: ImmutableList<Category>,

onSetCurrency: (IvyCurrency) -> Unit = {},

onCreateAccount: (CreateAccountData) -> Unit = { },
onEditAccount: (Account, Double) -> Unit = { _, _ -> },
onAddAccountsDone: () -> Unit = {},
onAddAccountsSkip: () -> Unit = {},

onCreateCategory: (CreateCategoryData) -> Unit = {},
onEditCategory: (Category) -> Unit = {},
onAddCategoryDone: () -> Unit = {},
onAddCategorySkip: () -> Unit = {},
onEvent: (OnboardingEvent) -> Unit = {}
) {
when (onboardingState) {
OnboardingState.SPLASH, OnboardingState.LOGIN -> {
OnboardingSplashLogin(
onboardingState = onboardingState,
opGoogleSignIn = opGoogleSignIn,

onLoginWithGoogle = onLoginWithGoogle,
onSkip = onSkip
onSkip = { onEvent(OnboardingEvent.LoginOfflineAccount) }
)
}

OnboardingState.CHOOSE_PATH -> {
OnboardingType(
onStartImport = onStartImport,
onStartFresh = onStartFresh
onStartImport = { onEvent(OnboardingEvent.StartImport) },
onStartFresh = { onEvent(OnboardingEvent.StartFresh) }
)
}

OnboardingState.CURRENCY -> {
OnboardingSetCurrency(
preselectedCurrency = currency,
onSetCurrency = onSetCurrency
onSetCurrency = { onEvent(OnboardingEvent.SetBaseCurrency(it)) }
)
}

Expand All @@ -141,11 +94,18 @@ private fun BoxWithConstraintsScope.UI(
suggestions = accountSuggestions,
accounts = accounts,

onCreateAccount = onCreateAccount,
onEditAccount = onEditAccount,

onDone = onAddAccountsDone,
onSkip = onAddAccountsSkip
onCreateAccount = { onEvent(OnboardingEvent.CreateAccount(it)) },
onEditAccount = { account, newBalance ->
onEvent(
OnboardingEvent.EditAccount(
account,
newBalance
)
)
},

onDone = { onEvent(OnboardingEvent.OnAddAccountsDone) },
onSkip = { onEvent(OnboardingEvent.OnAddAccountsSkip) }
)
}

Expand All @@ -154,35 +114,12 @@ private fun BoxWithConstraintsScope.UI(
suggestions = categorySuggestions,
categories = categories,

onCreateCategory = onCreateCategory,
onEditCategory = onEditCategory,
onCreateCategory = { onEvent(OnboardingEvent.CreateCategory(it)) },
onEditCategory = { onEvent(OnboardingEvent.EditCategory(it)) },

onDone = onAddCategoryDone,
onSkip = onAddCategorySkip
onDone = { onEvent(OnboardingEvent.OnAddCategoriesDone) },
onSkip = { onEvent(OnboardingEvent.OnAddCategoriesSkip) }
)
}
}
}

@ExperimentalFoundationApi
@Preview
@Composable
private fun PreviewOnboarding() {
IvyWalletPreview {
UI(
accountSuggestions = listOf(),
accounts = listOf(),

categorySuggestions = listOf(),
categories = listOf(),

onboardingState = OnboardingState.SPLASH,
currency = IvyCurrency.getDefault(),
opGoogleSignIn = null,

onLoginWithGoogle = {},
onSkip = {},
onSetCurrency = {},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.ivy.onboarding

import androidx.compose.runtime.Immutable

@Immutable
enum class OnboardingState {
SPLASH,
LOGIN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,12 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
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.legacy.Constants
import com.ivy.legacy.IvyWalletCtx
import com.ivy.legacy.IvyWalletPreview
import com.ivy.legacy.ivyWalletCtx
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
import com.ivy.onboarding.OnboardingState
import com.ivy.resources.R
import com.ivy.wallet.ui.theme.Gradient
import com.ivy.wallet.ui.theme.Gray
import com.ivy.wallet.ui.theme.Green
import com.ivy.wallet.ui.theme.components.IvyIcon
import com.ivy.legacy.utils.OpResult
import com.ivy.legacy.utils.clickableNoIndication
import com.ivy.legacy.utils.drawColoredShadow
import com.ivy.legacy.utils.lerp
Expand All @@ -64,14 +57,17 @@ import com.ivy.legacy.utils.springBounceSlow
import com.ivy.legacy.utils.thenIf
import com.ivy.legacy.utils.toDensityDp
import com.ivy.legacy.utils.toDensityPx
import com.ivy.onboarding.OnboardingState
import com.ivy.resources.R
import com.ivy.wallet.ui.theme.Gradient
import com.ivy.wallet.ui.theme.Gray
import com.ivy.wallet.ui.theme.Green
import com.ivy.wallet.ui.theme.components.IvyIcon
import kotlin.math.roundToInt

@Composable
fun BoxWithConstraintsScope.OnboardingSplashLogin(
onboardingState: OnboardingState,
opGoogleSignIn: OpResult<Unit>?,

onLoginWithGoogle: () -> Unit,
onSkip: () -> Unit,
) {
var internalSwitch by remember { mutableStateOf(true) }
Expand Down Expand Up @@ -234,9 +230,6 @@ fun BoxWithConstraintsScope.OnboardingSplashLogin(

LoginSection(
percentTransition = percentTransition,

opGoogleSignIn = opGoogleSignIn,
onLoginWithGoogle = onLoginWithGoogle,
onSkip = onSkip
)
}
Expand Down Expand Up @@ -264,9 +257,6 @@ private fun Modifier.animateXCenterToLeft(
@Composable
private fun LoginSection(
percentTransition: Float,
opGoogleSignIn: OpResult<Unit>?,

onLoginWithGoogle: () -> Unit,
onSkip: () -> Unit
) {
if (percentTransition > 0.01f) {
Expand Down Expand Up @@ -482,8 +472,6 @@ private fun Preview() {
IvyWalletPreview {
OnboardingSplashLogin(
onboardingState = OnboardingState.SPLASH,
opGoogleSignIn = null,
onLoginWithGoogle = {},
onSkip = {}
)
}
Expand Down
Loading

0 comments on commit c166a17

Please sign in to comment.