Skip to content

Commit

Permalink
- rootNavigator not accessible from child classes anymore (#152)
Browse files Browse the repository at this point in the history
- provided handy navigation methods on base presenter
 - applied to all usages
  • Loading branch information
rodvar authored Jan 10, 2025
1 parent 6d65a3b commit df4e259
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network.bisq.mobile.presentation
import androidx.compose.material3.SnackbarHostState
import androidx.annotation.CallSuper
import androidx.navigation.NavHostController
import androidx.navigation.NavOptionsBuilder
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -13,6 +14,7 @@ import network.bisq.mobile.domain.data.BackgroundDispatcher
import network.bisq.mobile.domain.data.model.BaseModel
import network.bisq.mobile.i18n.AppStrings
import network.bisq.mobile.domain.utils.Logging
import network.bisq.mobile.presentation.ui.navigation.Routes

/**
* Presenter methods accesible by all views. Views should extend this interface when defining the behaviour expected for their presenter.
Expand All @@ -39,7 +41,7 @@ interface ViewPresenter {
/**
* Navigate back in the stack
*/
fun goBack()
fun goBack(): Boolean

/**
* This can be used as initialization method AFTER view gets attached (so view is available)
Expand Down Expand Up @@ -78,7 +80,7 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
// Presenter is interactive by default
private val _isInteractive = MutableStateFlow(true)
override val isInteractive: StateFlow<Boolean> = _isInteractive
val snackbarHostState: SnackbarHostState = SnackbarHostState()
private val snackbarHostState: SnackbarHostState = SnackbarHostState()

override fun getSnackState(): SnackbarHostState {
return snackbarHostState
Expand All @@ -94,7 +96,7 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
* @throws IllegalStateException if this presenter has no root
* @return Nav controller for navigation from the root
*/
protected val rootNavigator: NavHostController
private val rootNavigator: NavHostController
get() = (rootPresenter ?: throw IllegalStateException("This presenter has no root")).navController


Expand Down Expand Up @@ -131,12 +133,48 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
return rootPresenter!!.getRootTabNavController()
}

override fun goBack() {
/**
* Navigate to given destination
*/
protected fun navigateTo(destination: Routes, customSetup: (NavOptionsBuilder) -> Unit = {}) {
rootNavigator.navigate(destination.name) {
customSetup(this)
}
}

protected fun navigateBack(): Boolean {
return goBack()
}

/**
* Back navigation poping back stack
*/
protected fun navigateBackTo(destination: Routes, shouldInclusive: Boolean = false, shouldSaveState: Boolean = false) {
rootNavigator.popBackStack(destination.name, inclusive = shouldInclusive, saveState = shouldSaveState)
}

/**
* Navigates to the given tab route inside the main presentation, with default parameters.
*/
protected fun navigateToTab(destination: Routes, saveStateOnPopUp: Boolean = true, shouldLaunchSingleTop: Boolean = true, shouldRestoreState: Boolean = true) {
getRootTabNavController().navigate(destination.name) {
getRootTabNavController().graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = saveStateOnPopUp
}
}
launchSingleTop = shouldLaunchSingleTop
restoreState = shouldRestoreState
}
}

override fun goBack(): Boolean {
try {
log.i { "goBack defaut implementation" }
rootNavigator.popBackStack()
return rootNavigator.popBackStack()
} catch (e: Exception) {
log.e(e) { "Faled to navigate back" }
return false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class TopBarPresenter(

override fun onAvatarClicked() {
enableInteractive(false)
getRootNavController().navigate(Routes.UserProfileSettings.name)
navigateTo(Routes.UserProfileSettings)
enableInteractive(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ open class GettingStartedPresenter(
}

private fun navigateToTradingTab() {
getRootTabNavController().navigate(Routes.TabCurrencies.name) {
getRootTabNavController().graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
navigateToTab(Routes.TabCurrencies)
}

override fun navigateLearnMore() {
Expand All @@ -60,7 +52,7 @@ open class GettingStartedPresenter(
}

fun navigateToChat() {
rootNavigator.navigate(Routes.ChatScreen.name)
navigateTo(Routes.ChatScreen)
}

private fun refresh() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TabContainerPresenter(

override fun createOffer() {
createOfferPresenter.onStartCreateOffer()
rootNavigator.navigate(Routes.CreateOfferDirection.name)
navigateTo(Routes.CreateOfferDirection)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class MarketListPresenter(

fun onSelectMarket(marketListItem: MarketListItem) {
offerbookServiceFacade.selectOfferbookMarket(marketListItem)
rootNavigator.navigate(Routes.Offerbook.name)
navigateTo(Routes.Offerbook)
}

override fun onViewAttached() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class OffersListPresenter(
takeOfferPresenter.selectOfferToTake(offer)

if (takeOfferPresenter.showAmountScreen()) {
rootNavigator.navigate(Routes.TakeOfferTradeAmount.name)
navigateTo(Routes.TakeOfferTradeAmount)
} else if (takeOfferPresenter.showPaymentMethodsScreen()) {
rootNavigator.navigate(Routes.TakeOfferPaymentMethod.name)
navigateTo(Routes.TakeOfferPaymentMethod)
} else {
rootNavigator.navigate(Routes.TakeOfferReviewTrade.name)
navigateTo(Routes.TakeOfferReviewTrade)
}
}

override fun createOffer() {
createOfferPresenter.onStartCreateOffer(offerbookServiceFacade.selectedOfferbookMarket.value.market)
rootNavigator.navigate(Routes.CreateOfferDirection.name)
navigateTo(Routes.CreateOfferDirection)
}

override fun chatForOffer(offer: OfferListItemVO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ class CreateOfferAmountPresenter(

fun onBack() {
commitToModel()
rootNavigator.popBackStack()
navigateBack()
}

fun onNext() {
commitToModel()
rootNavigator.navigate(Routes.CreateOfferPrice.name)
navigateTo(Routes.CreateOfferPrice)
}

private fun applyRangeAmountSliderValue(rangeSliderPosition: ClosedFloatingPointRange<Float>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CreateOfferDirectionPresenter(

fun onBack() {
commitToModel()
rootNavigator.popBackStack()
navigateBack()
}

fun onNext() {
Expand All @@ -44,9 +44,9 @@ class CreateOfferDirectionPresenter(
private fun navigateNext() {
commitToModel()
if (createOfferPresenter.createOfferModel.market == null) {
rootNavigator.navigate(Routes.CreateOfferMarket.name)
navigateTo(Routes.CreateOfferMarket)
} else {
rootNavigator.navigate(Routes.CreateOfferAmount.name)
navigateTo(Routes.CreateOfferAmount)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CreateOfferMarketPresenter(

fun onBack() {
commitToModel()
rootNavigator.popBackStack()
navigateBack()
}

fun onNext() {
Expand All @@ -54,7 +54,7 @@ class CreateOfferMarketPresenter(

private fun navigateNext() {
commitToModel()
rootNavigator.navigate(Routes.CreateOfferAmount.name)
navigateTo(Routes.CreateOfferAmount)
}

private fun commitToModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ class CreateOfferPaymentMethodPresenter(

fun onBack() {
commitToModel()
rootNavigator.popBackStack()
navigateBack()
}

fun onNext() {
if (isValid()) {
commitToModel()
rootNavigator.navigate(Routes.CreateOfferReviewOffer.name)
navigateTo(Routes.CreateOfferReviewOffer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ class CreateOfferPricePresenter(
if (isValid(percentagePriceValue)) {
commitToModel()
}
rootNavigator.popBackStack()
navigateBack()
}

fun onNext() {
if (isValid(percentagePriceValue)) {
commitToModel()
rootNavigator.navigate(Routes.CreateOfferPaymentMethod.name)
navigateTo(Routes.CreateOfferPaymentMethod)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CreateOfferReviewPresenter(
}

fun onBack() {
rootNavigator.popBackStack()
navigateBack()
}

fun onCreateOffer() {
Expand All @@ -116,6 +116,6 @@ class CreateOfferReviewPresenter(
}

fun onGoToOfferList() {
rootNavigator.popBackStack(Routes.Offerbook.name, false, false)
navigateBackTo(Routes.Offerbook)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ open class CreateProfilePresenter(
setCreateAndPublishInProgress(false)

// Skip for now the TrustedNodeSetup until its fully implemented with persisting the api URL.
/* rootNavigator.navigate(Routes.TrustedNodeSetup.name) {
popUpTo(Routes.CreateProfile.name) { inclusive = true }
/* navigateTo(Routes.TrustedNodeSetup) {
it.popUpTo(Routes.CreateProfile.name) { inclusive = true }
} */
rootNavigator.navigate(Routes.TabContainer.name) {
popUpTo(Routes.Onboarding.name) { inclusive = true }
navigateTo(Routes.TabContainer) {
it.popUpTo(Routes.Onboarding.name) { inclusive = true }
}
}.onFailure { e ->
// TODO give user feedback (we could have a general error screen covering usual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ open class OnBoardingPresenter(
}

protected fun navigateToCreateProfile() {
rootNavigator.navigate(Routes.CreateProfile.name)
navigateTo(Routes.CreateProfile)
}

protected fun navigateToTrustedNodeSetup() {
rootNavigator.navigate(Routes.TrustedNodeSetup.name)
navigateTo(Routes.TrustedNodeSetup)
}

open fun doCustomNavigationLogic(isBisqUrlSet: Boolean, hasProfile: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ open class SplashPresenter(
// // If not, goto CreateProfile
// if (settings.firstLaunch) {
// // TODO after onboarding need to make sure the rest is configured?
// rootNavigator.navigate(Routes.Onboarding.name) {
// navigateTo(Routes.Onboarding.name) {
// popUpTo(Routes.Splash.name) { inclusive = true }
// }
// } else {
// rootNavigator.navigate(Routes.CreateProfile.name) {
// navigateTo(Routes.CreateProfile.name) {
// popUpTo(Routes.Splash.name) { inclusive = true }
// }
// }
Expand All @@ -95,26 +95,26 @@ open class SplashPresenter(
}

private fun navigateToTrustedNodeSetup() {
rootNavigator.navigate(Routes.TrustedNodeSetup.name) {
popUpTo(Routes.Splash.name) { inclusive = true }
navigateTo(Routes.TrustedNodeSetup) {
it.popUpTo(Routes.Splash.name) { inclusive = true }
}
}

private fun navigateToOnboarding() {
rootNavigator.navigate(Routes.Onboarding.name) {
popUpTo(Routes.Splash.name) { inclusive = true }
navigateTo(Routes.Onboarding) {
it.popUpTo(Routes.Splash.name) { inclusive = true }
}
}

protected fun navigateToCreateProfile() {
rootNavigator.navigate(Routes.CreateProfile.name) {
popUpTo(Routes.Splash.name) { inclusive = true }
navigateTo(Routes.CreateProfile) {
it.popUpTo(Routes.Splash.name) { inclusive = true }
}
}

private fun navigateToHome() {
rootNavigator.navigate(Routes.TabContainer.name) {
popUpTo(Routes.Splash.name) { inclusive = true }
navigateTo(Routes.TabContainer) {
it.popUpTo(Routes.Splash.name) { inclusive = true }
}
}

Expand All @@ -130,7 +130,7 @@ open class SplashPresenter(
}
// when {
// settings.bisqApiUrl.isEmpty() -> {
// rootNavigator.navigate(Routes.TrustedNodeSetup.name) {
// navigateTo(Routes.TrustedNodeSetup.name) {
// popUpTo(Routes.Splash.name) { inclusive = true }
// }
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class TrustedNodeSetupPresenter(
}

override fun navigateToNextScreen() {
rootNavigator.navigate(Routes.CreateProfile.name)
navigateTo(Routes.CreateProfile)
}

override fun goBackToSetupScreen() {
rootNavigator.popBackStack()
navigateBack()
}
}
Loading

0 comments on commit df4e259

Please sign in to comment.