Skip to content

Commit

Permalink
Fix CSVScreen.kt success navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 17, 2023
1 parent 7986998 commit a358f6d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/java/com/ivy/wallet/ui/RootActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class RootActivity : AppCompatActivity() {
is ImagesScreen -> ImagesScreen(screen = screen)
is ExperimentalScreen -> ExperimentalScreen(screen = screen)
is DonateScreen -> DonateScreen(screen = screen)
is CSVScreen -> CSVScreen()
is CSVScreen -> CSVScreen(screen = screen)
null -> {
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/ivy/wallet/ui/Screens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ object Onboarding : Screen

object ServerStop : Screen

object CSVScreen : Screen
data class CSVScreen(
val launchedFromOnboarding: Boolean
) : Screen

data class EditTransaction(
val initialTransactionId: UUID?,
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVEvent.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ivy.wallet.ui.csv

import android.net.Uri
import com.ivy.wallet.ui.onboarding.viewmodel.OnboardingViewModel

sealed interface CSVEvent {
data class FilePicked(val uri: Uri) : CSVEvent
Expand All @@ -26,4 +27,8 @@ sealed interface CSVEvent {
object Continue : CSVEvent
object ResetState : CSVEvent

data class FinishImport(
val launchedFromOnboarding: Boolean,
val onboardingViewModel: OnboardingViewModel
) : CSVEvent
}
18 changes: 14 additions & 4 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,37 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.insets.systemBarsPadding
import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.colorAs
import com.ivy.frp.view.navigation.navigation
import com.ivy.wallet.ui.CSVScreen
import com.ivy.wallet.ui.csvimport.flow.ImportProcessing
import com.ivy.wallet.ui.csvimport.flow.ImportResultUI
import com.ivy.wallet.ui.ivyWalletCtx
import com.ivy.wallet.ui.onboarding.viewmodel.OnboardingViewModel
import com.ivy.wallet.utils.thenIf
import kotlin.math.abs

@Composable
fun CSVScreen() {
fun CSVScreen(
screen: CSVScreen
) {
val viewModel: CSVViewModel = viewModel()
val state = viewModel.uiState()
val nav = navigation()
val onboardingViewModel: OnboardingViewModel = viewModel()

when (val ui = state.uiState) {
UIState.Idle -> ImportUI(state = state, onEvent = viewModel::onEvent)
is UIState.Processing -> ImportProcessing(progressPercent = ui.percent)
is UIState.Result -> ImportResultUI(
result = ui.importResult,
isManualCsvImport = true,
launchedFromOnboarding = screen.launchedFromOnboarding,
onTryAgain = null,
onFinish = {
nav.back()
viewModel.onEvent(
CSVEvent.FinishImport(
launchedFromOnboarding = screen.launchedFromOnboarding,
onboardingViewModel = onboardingViewModel,
)
)
}
)
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/ivy/wallet/ui/csv/CSVViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.net.Uri
import androidx.compose.runtime.*
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.frp.view.navigation.Navigation
import com.ivy.wallet.domain.deprecated.logic.csv.IvyFileReader
import com.ivy.wallet.ui.csv.domain.*
import com.ivy.wallet.utils.uiThread
Expand All @@ -24,6 +25,7 @@ import kotlin.math.roundToInt
class CSVViewModel @Inject constructor(
private val fileReader: IvyFileReader,
private val csvImporter: CSVImporterV2,
private val nav: Navigation,
) : ViewModel() {

private var columns by mutableStateOf<CSVRow?>(null)
Expand Down Expand Up @@ -381,6 +383,9 @@ class CSVViewModel @Inject constructor(
CSVEvent.ResetState -> {
uiState = UIState.Idle
}
is CSVEvent.FinishImport -> {
handleFinishImport(event)
}
}
}

Expand Down Expand Up @@ -501,6 +506,25 @@ class CSVViewModel @Inject constructor(
}
}

private fun handleFinishImport(event: CSVEvent.FinishImport) {
if (event.launchedFromOnboarding) {
val importSuccess = (uiState as? UIState.Result)?.importResult
?.transactionsImported?.let { it > 0 } ?: false
event.onboardingViewModel.importFinished(
success = importSuccess
)
}

nav.back()
resetState()
}

private fun resetState() {
uiState = UIState.Idle
csv = null
columns = null
}


// region Boiler-plate
private val events = MutableSharedFlow<CSVEvent>(replay = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private fun BoxWithConstraintsScope.UI(
ImportStep.IMPORT_FROM -> {
ImportFrom(
hasSkip = screen.launchedFromOnboarding,
launchedFromOnboarding = screen.launchedFromOnboarding,
onSkip = onSkip,
onImportFrom = onChooseImportType
)
Expand All @@ -99,7 +100,8 @@ private fun BoxWithConstraintsScope.UI(
}
ImportStep.RESULT -> {
ImportResultUI(
result = importResult!!
result = importResult!!,
launchedFromOnboarding = screen.launchedFromOnboarding,
) {
onFinish()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.ivy.wallet.R
import com.ivy.wallet.domain.deprecated.logic.csv.model.ImportType
import com.ivy.wallet.ui.CSVScreen
import com.ivy.wallet.ui.IvyWalletPreview
import com.ivy.wallet.ui.ivyWalletCtx
import com.ivy.wallet.ui.onboarding.components.OnboardingToolbar
import com.ivy.wallet.ui.theme.components.GradientCutBottom
import com.ivy.wallet.ui.theme.components.IvyIcon
Expand All @@ -36,6 +35,7 @@ import com.ivy.wallet.ui.theme.components.IvyIcon
@Composable
fun BoxWithConstraintsScope.ImportFrom(
hasSkip: Boolean,
launchedFromOnboarding: Boolean,

onSkip: () -> Unit = {},
onImportFrom: (ImportType) -> Unit = {},
Expand Down Expand Up @@ -67,7 +67,7 @@ fun BoxWithConstraintsScope.ImportFrom(
.height(64.dp)
.padding(horizontal = 16.dp),
onClick = {
nav.navigateTo(CSVScreen)
nav.navigateTo(CSVScreen(launchedFromOnboarding))
}
) {
Text(text = "Manual CSV import")
Expand Down Expand Up @@ -151,6 +151,7 @@ private fun Preview() {
IvyWalletPreview {
ImportFrom(
hasSkip = true,
launchedFromOnboarding = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.ivy.wallet.utils.format
@Composable
fun ImportResultUI(
result: ImportResult,
launchedFromOnboarding: Boolean,
isManualCsvImport: Boolean = false,

onTryAgain: (() -> Unit)? = null,
Expand Down Expand Up @@ -161,7 +162,7 @@ fun ImportResultUI(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
text = "If this didn't work, Try manual CSV import.",
text = "If this didn't work, try manual CSV import.",
color = UI.colors.pureInverse,
)
Spacer(modifier = Modifier.height(8.dp))
Expand All @@ -171,7 +172,7 @@ fun ImportResultUI(
.height(52.dp)
.padding(horizontal = 16.dp),
onClick = {
nav.navigateTo(CSVScreen)
nav.navigateTo(CSVScreen(launchedFromOnboarding = launchedFromOnboarding))
}
) {
Text(text = "Manual CSV import")
Expand Down Expand Up @@ -223,7 +224,8 @@ private fun Preview() {
accountsImported = 4,
categoriesImported = 13,
failedRows = emptyList()
)
),
launchedFromOnboarding = false,
) {

}
Expand Down

0 comments on commit a358f6d

Please sign in to comment.