Skip to content

Commit

Permalink
- fix running condition related to navhostcontrollers (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodvar authored Dec 17, 2024
1 parent 450d7dc commit 03acf7c
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import org.koin.core.qualifier.named
fun TakeOfferReviewTradeScreen() {
// TODO presenter for this? And call RememberLifecycle...

val navController: NavHostController = koinInject(named("RootNavController"))
TakeOfferScaffold {
TakeOfferScaffold {
Column(modifier = Modifier.padding(horizontal = 32.dp, vertical = 24.dp)) {
BisqText.h3Regular(
text = "Review trade",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package network.bisq.mobile.client

import kotlinx.coroutines.launch
import network.bisq.mobile.client.websocket.WebSocketClient
import network.bisq.mobile.domain.service.TrustedNodeService
import network.bisq.mobile.domain.service.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.domain.service.controller.NotificationServiceController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import network.bisq.mobile.utils.Logging
* Presenter methods accesible by all views. Views should extend this interface when defining the behaviour expected for their presenter.
*/
interface ViewPresenter {
/**
* @return root navigation controller
*/
fun getRootNavController(): NavHostController
/**
* @return main app tab nav controller
*/
fun getRootTabNavController(): NavHostController
/**
* This can be used as initialization method AFTER view gets attached (so view is available)
*/
Expand Down Expand Up @@ -65,6 +73,26 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
rootPresenter?.registerChild(child = this)
}

/**
* Default implementation assumes is a child presenter
*/
override fun getRootNavController(): NavHostController {
if (isRoot()) {
throw IllegalStateException("You need to redefine this method in your root presenter implementation")
}
return rootPresenter!!.getRootNavController()
}

/**
* Default implementation assumes is a child presenter
*/
override fun getRootTabNavController(): NavHostController {
if (isRoot()) {
throw IllegalStateException("You need to redefine this method in your root presenter implementation")
}
return rootPresenter!!.getRootNavController()
}

@CallSuper
override fun onViewAttached() {
log.i { "Lifecycle: View attached to presenter" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ open class MainPresenter(private val notificationServiceController: Notification
const val PUSH_DELAY = 60000L
}

lateinit var navController: NavHostController
private set

override fun setNavController(controller: NavHostController) {
navController = controller
}
override lateinit var navController: NavHostController
override lateinit var tabNavController: NavHostController

// Observable state
private val _isContentVisible = MutableStateFlow(false)
Expand Down Expand Up @@ -84,4 +80,12 @@ open class MainPresenter(private val notificationServiceController: Notification
return isIOS
}

override fun getRootNavController(): NavHostController {
return navController
}

override fun getRootTabNavController(): NavHostController {
return tabNavController
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ import org.koin.dsl.bind
import org.koin.dsl.module

val presentationModule = module {
single(named("RootNavController")) { getKoin().getProperty<NavHostController>("RootNavController") }
single(named("TabNavController")) { getKoin().getProperty<NavHostController>("TabNavController") }

single<MainPresenter> { ClientMainPresenter(get(), get(), get(), get(), get()) } bind AppPresenter::class

single<TopBarPresenter> { TopBarPresenter(get(), get()) } bind ITopBarPresenter::class
Expand Down Expand Up @@ -95,10 +92,9 @@ val presentationModule = module {

single{ ReviewTradePresenter(get(), get()) } bind ITakeOfferReviewTradePresenter::class

single { (navController: NavController, tabController: NavController) ->
single {
MyTradesPresenter(
get(),
tabController = tabController,
myTradesRepository = get()
)
} bind IMyTrades::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import network.bisq.mobile.presentation.ViewPresenter
import network.bisq.mobile.presentation.ui.components.SwipeBackIOSNavigationHandler
import network.bisq.mobile.presentation.ui.helpers.RememberPresenterLifecycle
import org.koin.compose.koinInject
import network.bisq.mobile.presentation.ui.navigation.Routes

import network.bisq.mobile.presentation.ui.navigation.graph.RootNavGraph
import network.bisq.mobile.presentation.ui.theme.BisqTheme
import org.koin.mp.KoinPlatform.getKoin

interface AppPresenter : ViewPresenter {
fun setNavController(controller: NavHostController)
var navController: NavHostController

var tabNavController: NavHostController

// Observables for state
val isContentVisible: StateFlow<Boolean>
Expand All @@ -43,9 +43,8 @@ fun App() {
val presenter: AppPresenter = koinInject()

RememberPresenterLifecycle(presenter, {
getKoin().setProperty("RootNavController", rootNavController)
getKoin().setProperty("TabNavController", tabNavController)
presenter.setNavController(rootNavController)
presenter.navController = rootNavController
presenter.tabNavController = tabNavController
isNavControllerSet = true
})

Expand All @@ -55,7 +54,7 @@ fun App() {
ProvideStrings(lyricist) {
if (isNavControllerSet) {
SwipeBackIOSNavigationHandler(rootNavController) {
RootNavGraph()
RootNavGraph(rootNavController)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fun TopBar(
isFlowScreen: Boolean = false,
stepText: String = ""
) {
val navController: NavHostController = koinInject(named("RootNavController"))
val presenter: ITopBarPresenter = koinInject()
val navController: NavHostController = presenter.getRootNavController()

val showBackButton = customBackButton == null && navController.previousBackStackEntry != null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ import network.bisq.mobile.presentation.ui.uicases.startup.OnBoardingScreen
import network.bisq.mobile.presentation.ui.uicases.startup.SplashScreen
import network.bisq.mobile.presentation.ui.uicases.startup.TrustedNodeSetupScreen
import network.bisq.mobile.presentation.ui.uicases.trades.TradeFlowScreen
import org.koin.compose.koinInject
import org.koin.core.qualifier.named

@Composable
fun RootNavGraph() {
val navController: NavHostController = koinInject(named("RootNavController"))

fun RootNavGraph(rootNavController: NavHostController) {
NavHost(
modifier = Modifier.background(color = BisqTheme.colors.backgroundColor),
navController = navController,
navController = rootNavController,
startDestination = Routes.Splash.name,
) {
composable(route = Routes.Splash.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import network.bisq.mobile.presentation.ui.AppPresenter
import network.bisq.mobile.presentation.ui.navigation.Routes
import network.bisq.mobile.presentation.ui.navigation.Graph
import network.bisq.mobile.presentation.ui.theme.BisqTheme
Expand All @@ -22,12 +23,12 @@ import org.koin.core.qualifier.named
@Composable
fun TabNavGraph() {

val navController: NavHostController = koinInject(named("TabNavController"))
val mainPresenter: AppPresenter = koinInject()
val selectedTab = remember { mutableStateOf(Routes.TabHome.name) }

NavHost(
modifier = Modifier.background(color = BisqTheme.colors.backgroundColor),
navController = navController,
navController = mainPresenter.getRootTabNavController(),
startDestination = Graph.MAIN_SCREEN_GRAPH_KEY,
) {
navigation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import bisqapps.shared.presentation.generated.resources.*
import bisqapps.shared.presentation.generated.resources.Res
import network.bisq.mobile.presentation.ui.AppPresenter
import network.bisq.mobile.presentation.ui.components.layout.BisqStaticScaffold
import network.bisq.mobile.presentation.ui.components.molecules.TopBar
import network.bisq.mobile.presentation.ui.composeModels.BottomNavigationItem
Expand All @@ -24,7 +25,8 @@ val navigationListItem = listOf(

@Composable
fun TabContainerScreen() {
val navController: NavHostController = koinInject(named("TabNavController"))
val mainPresenter: AppPresenter = koinInject()
val navController: NavHostController = mainPresenter.getRootTabNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute by remember(navBackStackEntry) {
derivedStateOf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ fun CreateProfileScreen(
) {
val strings = LocalStrings.current.application
val commonStrings = LocalStrings.current.common
val navController: NavHostController = koinInject(named("RootNavController"))
val presenter: CreateProfilePresenter = koinInject { parametersOf(navController) }
val presenter: CreateProfilePresenter = koinInject()
val navController: NavHostController = presenter.getRootNavController()

RememberPresenterLifecycle(presenter)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ interface ITrustedNodeSetupPresenter: ViewPresenter {
fun TrustedNodeSetupScreen(
) {
val strings = LocalStrings.current
val navController: NavHostController = koinInject(named("RootNavController"))
val presenter: ITrustedNodeSetupPresenter = koinInject { parametersOf(navController) }
val presenter: ITrustedNodeSetupPresenter = koinInject()
val navController: NavHostController = presenter.getRootNavController()

val bisqApiUrl = presenter.bisqApiUrl.collectAsState().value
val isConnected = presenter.isConnected.collectAsState().value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package network.bisq.mobile.presentation.ui.uicases.trades

import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -15,15 +14,14 @@ import network.bisq.mobile.presentation.ui.navigation.Routes

class MyTradesPresenter(
mainPresenter: MainPresenter,
private val tabController: NavController,
private val myTradesRepository: MyTradesRepository
) : BasePresenter(mainPresenter), IMyTrades {

private val _myTrades = MutableStateFlow<List<BisqOffer>>(emptyList())
override val myTrades: StateFlow<List<BisqOffer>> = _myTrades

override fun navigateToCurrencyList() {

val tabController = getRootTabNavController()
tabController.navigate(Routes.TabCurrencies.name) {
tabController.graph.startDestinationRoute?.let { route ->
popUpTo(route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import bisqapps.shared.presentation.generated.resources.Res
import bisqapps.shared.presentation.generated.resources.img_no_trades
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -27,8 +26,6 @@ import network.bisq.mobile.presentation.ui.helpers.RememberPresenterLifecycle
import network.bisq.mobile.presentation.ui.theme.BisqTheme
import org.jetbrains.compose.resources.painterResource
import org.koin.compose.koinInject
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named

interface IMyTrades : ViewPresenter {
val myTrades: StateFlow<List<BisqOffer>>
Expand All @@ -38,10 +35,7 @@ interface IMyTrades : ViewPresenter {

@Composable
fun MyTradesScreen() {
val navController: NavHostController = koinInject(named("RootNavController"))
val tabController: NavHostController = koinInject(named("TabNavController"))

val presenter: IMyTrades = koinInject { parametersOf(navController, tabController) }
val presenter: IMyTrades = koinInject()

val myTrades: List<BisqOffer> = presenter.myTrades.collectAsState().value

Expand Down

0 comments on commit 03acf7c

Please sign in to comment.