Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set current tab group in add/edit screen, switch to tab where item added to #135

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import dev.arkbuilders.rate.core.presentation.utils.findActivity
import dev.arkbuilders.rate.core.presentation.utils.keyboardAsState
import dev.arkbuilders.rate.feature.pairalert.presentation.destinations.PairAlertConditionScreenDestination
import dev.arkbuilders.rate.feature.portfolio.presentation.destinations.PortfolioScreenDestination
import dev.arkbuilders.rate.feature.quick.presentation.add.AddQuickScreenArgs
import dev.arkbuilders.rate.feature.quick.presentation.destinations.AddQuickScreenDestination
import dev.arkbuilders.rate.feature.quick.presentation.destinations.QuickScreenDestination
import dev.arkbuilders.rate.feature.quickwidget.presentation.action.AddNewPairAction.Companion.ADD_NEW_PAIR
Expand All @@ -40,7 +41,6 @@ import dev.arkbuilders.rate.feature.settings.presentation.destinations.SettingsS
import dev.arkbuilders.rate.presentation.navigation.AnimatedRateBottomNavigation
import dev.arkbuilders.rate.presentation.navigation.NavGraphs
import kotlinx.coroutines.flow.drop
import timber.log.Timber

@Composable
fun MainScreen() {
Expand All @@ -58,7 +58,7 @@ fun MainScreen() {
val createNewPair = intent?.getStringExtra(ADD_NEW_PAIR) ?: ""
if (createNewPair.isNotEmpty()) {
val group = intent?.getStringExtra(ADD_NEW_PAIR_GROUP_KEY)
navController.navigate(AddQuickScreenDestination(group = group))
navController.navigate(AddQuickScreenDestination(AddQuickScreenArgs(group = group)))
intent?.removeExtra(ADD_NEW_PAIR_GROUP_KEY)
intent?.removeExtra(ADD_NEW_PAIR)
}
Expand All @@ -82,7 +82,6 @@ fun MainScreen() {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route ?: NavGraphs.root.startRoute.route

Timber.d("ALLO $currentRoute")
bottomBarVisible.value =
when {
currentRoute.startsWith(QuickScreenDestination.route) -> true
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.ksp)
alias(libs.plugins.kotlin.parcelize) apply false
id("com.google.gms.google-services") version "4.4.2" apply false
id("com.google.firebase.crashlytics") version "3.0.1" apply false
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ sealed class AppSharedFlow<T>(val flow: MutableSharedFlow<T>) {

data object ShowAddedSnackbarPairAlert :
AppSharedFlow<NotifyAddedSnackbarVisuals>(MutableSharedFlow())

data object SelectGroupQuick : AppSharedFlow<String?>(MutableSharedFlow())

data object SelectGroupPortfolio : AppSharedFlow<String?>(MutableSharedFlow())

data object SelectGroupPairAlert : AppSharedFlow<String?>(MutableSharedFlow())
}

enum class AppSharedFlowKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.pager.PagerState
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRowDefaults
Expand All @@ -29,11 +29,11 @@ import kotlinx.coroutines.launch
@Composable
fun GroupViewPager(
modifier: Modifier = Modifier,
pagerState: PagerState,
groups: List<String?>,
pageContent: @Composable (index: Int) -> Unit,
) {
val scope = rememberCoroutineScope()
val pagerState = rememberPagerState { groups.size }
Column(modifier = modifier) {
Box {
AppHorDiv(modifier = Modifier.align(Alignment.BottomCenter))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.arkbuilders.rate.core.presentation.ui

import androidx.compose.foundation.pager.PagerState

/** See:
* rememberPagerState()
* androidx.compose.foundation.pager.DefaultPagerState
*/

class RatePagerState(
currentPage: Int = 0,
currentPageOffsetFraction: Float = 0f,
private val updatedPageCount: () -> Int,
) : PagerState(currentPage, currentPageOffsetFraction) {
override val pageCount: Int get() = updatedPageCount()
}
1 change: 1 addition & 0 deletions feature/pairalert/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.ksp)
alias(libs.plugins.kotlin.parcelize)
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package dev.arkbuilders.rate.feature.pairalert.presentation.add

import android.os.Parcelable
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -61,14 +62,21 @@ import dev.arkbuilders.rate.core.presentation.ui.GroupSelectPopup
import dev.arkbuilders.rate.core.presentation.ui.NotifyAddedSnackbarVisuals
import dev.arkbuilders.rate.feature.pairalert.di.PairAlertComponentHolder
import dev.arkbuilders.rate.feature.search.presentation.destinations.SearchCurrencyScreenDestination
import kotlinx.parcelize.Parcelize
import org.orbitmvi.orbit.compose.collectAsState
import org.orbitmvi.orbit.compose.collectSideEffect
import dev.arkbuilders.rate.core.presentation.R as CoreR

@Parcelize
data class AddPairAlertScreenArgs(
val pairAlertId: Long? = null,
val group: String?,
) : Parcelable

@Destination
@Composable
fun AddPairAlertScreen(
pairAlertId: Long? = null,
args: AddPairAlertScreenArgs,
navigator: DestinationsNavigator,
) {
val ctx = LocalContext.current
Expand All @@ -78,7 +86,7 @@ fun AddPairAlertScreen(
}
val viewModel: AddPairAlertViewModel =
viewModel(
factory = component.addPairAlertVMFactory().create(pairAlertId),
factory = component.addPairAlertVMFactory().create(args),
)

val state by viewModel.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sealed class AddPairAlertScreenEffect {
}

class AddPairAlertViewModel(
private val pairAlertId: Long?,
private val args: AddPairAlertScreenArgs,
private val currencyRepo: CurrencyRepo,
private val pairAlertRepo: PairAlertRepo,
private val codeUseStatRepo: CodeUseStatRepo,
Expand All @@ -71,12 +71,12 @@ class AddPairAlertViewModel(
initOnCodeChange(newBase = it)
}.launchIn(viewModelScope)

pairAlertId?.let {
args.pairAlertId?.let {
setupFromExisting()
intent {
checkAboveNotBelow()
}
} ?: initOnCodeChange()
} ?: initOnCodeChange(firstInit = true)

intent {
val groups =
Expand All @@ -90,6 +90,7 @@ class AddPairAlertViewModel(
private fun initOnCodeChange(
newTarget: CurrencyCode? = null,
newBase: CurrencyCode? = null,
firstInit: Boolean = false,
) {
intent {
val target = newTarget ?: state.targetCode
Expand All @@ -104,6 +105,7 @@ class AddPairAlertViewModel(
currentPrice = currentPrice,
targetCode = target,
baseCode = base,
group = if (firstInit) args.group else state.group,
)
val priceOrPercent = calcNewPriceOrPercent(newState)

Expand Down Expand Up @@ -191,7 +193,7 @@ class AddPairAlertViewModel(

val percent = state.priceOrPercent.getOrNull()?.toDoubleArk()

val id = if (state.editExisting) pairAlertId!! else 0
val id = if (state.editExisting) args.pairAlertId!! else 0

val pairAlert =
PairAlert(
Expand All @@ -208,6 +210,7 @@ class AddPairAlertViewModel(
)
pairAlertRepo.insert(pairAlert)
codeUseStatRepo.codesUsed(pairAlert.baseCode, pairAlert.targetCode)
AppSharedFlow.SelectGroupPairAlert.flow.emit(state.group)
postSideEffect(AddPairAlertScreenEffect.NotifyPairAdded(pairAlert))
postSideEffect(AddPairAlertScreenEffect.NavigateBack)
}
Expand Down Expand Up @@ -296,7 +299,7 @@ class AddPairAlertViewModel(

private fun setupFromExisting() =
intent {
val pair = pairAlertRepo.getById(pairAlertId!!)!!
val pair = pairAlertRepo.getById(args.pairAlertId!!)!!

val priceOrPercent =
pair.percent?.let { percent ->
Expand All @@ -314,8 +317,8 @@ class AddPairAlertViewModel(
fromCode = pair.targetCode,
toCode = pair.baseCode,
)
val state =
AddPairAlertScreenState(
reduce {
state.copy(
targetCode = pair.targetCode,
baseCode = pair.baseCode,
priceOrPercent = priceOrPercent,
Expand All @@ -325,7 +328,7 @@ class AddPairAlertViewModel(
oneTimeNotRecurrent = pair.oneTimeNotRecurrent,
editExisting = true,
)
reduce { state }
}
}

private fun checkFinishEnabled() =
Expand Down Expand Up @@ -353,7 +356,7 @@ class AddPairAlertViewModel(
}

class AddPairAlertViewModelFactory @AssistedInject constructor(
@Assisted private val pairAlertId: Long?,
@Assisted private val args: AddPairAlertScreenArgs,
private val currencyRepo: CurrencyRepo,
private val pairAlertRepo: PairAlertRepo,
private val codeUseStatRepo: CodeUseStatRepo,
Expand All @@ -362,7 +365,7 @@ class AddPairAlertViewModelFactory @AssistedInject constructor(
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return AddPairAlertViewModel(
pairAlertId,
args,
currencyRepo,
pairAlertRepo,
codeUseStatRepo,
Expand All @@ -373,6 +376,6 @@ class AddPairAlertViewModelFactory @AssistedInject constructor(

@AssistedFactory
interface Factory {
fun create(pairAlertId: Long?): AddPairAlertViewModelFactory
fun create(args: AddPairAlertScreenArgs): AddPairAlertViewModelFactory
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
Expand Down Expand Up @@ -100,11 +101,7 @@ fun PairAlertConditionScreen(navigator: DestinationsNavigator) {
viewModel.collectSideEffect { effect ->
when (effect) {
is PairAlertEffect.NavigateToAdd ->
navigator.navigate(
AddPairAlertScreenDestination(
pairAlertId = effect.pairId,
),
)
navigator.navigate(AddPairAlertScreenDestination(effect.args))

PairAlertEffect.AskNotificationPermissionOnScreenOpen -> {
onScreenOpenNotificationPermissionLauncher
Expand Down Expand Up @@ -139,6 +136,8 @@ fun PairAlertConditionScreen(navigator: DestinationsNavigator) {
)
snackState.showSnackbar(visuals)
}

is PairAlertEffect.SelectGroup -> viewModel.pagerState.scrollToPage(effect.groupIndex)
}
}

Expand Down Expand Up @@ -176,6 +175,7 @@ fun PairAlertConditionScreen(navigator: DestinationsNavigator) {
Content(
component = component,
state = state,
pagerState = viewModel.pagerState,
onDelete = viewModel::onDelete,
onClick = { pair ->
viewModel.onNewPair(pair.id)
Expand All @@ -191,6 +191,7 @@ fun PairAlertConditionScreen(navigator: DestinationsNavigator) {
private fun Content(
component: PairAlertComponent,
state: PairAlertScreenState,
pagerState: PagerState,
onDelete: (PairAlert) -> Unit,
onClick: (PairAlert) -> Unit,
onEnableToggle: (PairAlert, Boolean) -> Unit,
Expand All @@ -207,6 +208,7 @@ private fun Content(
} else {
GroupViewPager(
modifier = Modifier.padding(top = 16.dp),
pagerState = pagerState,
groups = state.pages.map { it.group },
) { index ->
GroupPage(
Expand Down
Loading
Loading