Skip to content

Commit

Permalink
Feature/redirect to node setup when no connectivity (#165)
Browse files Browse the repository at this point in the history
* - code refactor cleanup

* - redirect to trusted node after no connectivity detected in splash
  • Loading branch information
rodvar authored Jan 16, 2025
1 parent 42894c4 commit 5edfc41
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ class NodeSplashPresenter(
userProfileService,
userRepository,
settingsRepository,
settingsServiceFacade
settingsServiceFacade,
null
) {

override fun doCustomNavigationLogic(settings: Settings, hasProfile: Boolean): Boolean {
navigateToCreateProfile()
// do nothing
return false
}

override suspend fun hasConnectivity(): Boolean {
// TODO implement for node
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import network.bisq.mobile.domain.utils.createUuid
class WebSocketClient(
private val httpClient: HttpClient,
val json: Json,
var host: String,
var port: Int
val host: String,
val port: Int
) : Logging {

private val webSocketUrl: String = "ws://$host:$port/websocket"
Expand All @@ -49,7 +49,7 @@ class WebSocketClient(
if (!isConnected) {
try {
session = httpClient.webSocketSession { url(webSocketUrl) }
if (session != null && session!!.isActive) {
if (session?.isActive == true) {
isConnected = true
CoroutineScope(BackgroundDispatcher).launch { startListening() }
connectionReady.complete(true)
Expand Down Expand Up @@ -128,9 +128,7 @@ class WebSocketClient(
log.i { "Send message $message" }
val jsonString: String = json.encodeToString(message)
log.i { "Send raw text $jsonString" }
if (session != null) {
session!!.send(Frame.Text(jsonString))
}
session?.send(Frame.Text(jsonString))
}

private suspend fun startListening() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ val presentationModule = module {
get(),
get(),
get(),
get(),
get()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ open class OnBoardingPresenter(

if (pagerState.currentPage == indexesToShow.lastIndex) {

// to ensure event propagation, probably need to change settings equals definition to avoid this
val updatedSettings = Settings().apply {
bisqApiUrl = settings?.bisqApiUrl ?: ""
firstLaunch = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import network.bisq.mobile.client.websocket.WebSocketClientProvider
import network.bisq.mobile.domain.data.model.Settings
import network.bisq.mobile.domain.data.model.User
import network.bisq.mobile.domain.data.repository.SettingsRepository
Expand All @@ -23,7 +24,8 @@ open class SplashPresenter(
private val userProfileService: UserProfileServiceFacade,
private val userRepository: UserRepository,
private val settingsRepository: SettingsRepository,
private val settingsServiceFacade: SettingsServiceFacade
private val settingsServiceFacade: SettingsServiceFacade,
private val webSocketClientProvider: WebSocketClientProvider?,
) : BasePresenter(mainPresenter) {

val state: StateFlow<String> = applicationBootstrapFacade.state
Expand Down Expand Up @@ -59,46 +61,28 @@ open class SplashPresenter(
CoroutineScope(Dispatchers.Main).launch {
val settings: Settings = settingsRepository.fetch() ?: Settings()
val user: User? = userRepository.fetch()
val hasProfile: Boolean = userProfileService.hasUserProfile()

// Scenario 1: All good and setup for both androidNode and xClients
if (user != null && hasProfile) {
// TOOD:
// 1) Is this the right condition?
// 2a) androidNode being able to connect with other peers and
// 2b) xClients being able to connect with remote instance happening successfuly as part of services init?
navigateToHome()
// Scenario 2: Loading up for first time for both androidNode and xClients
} else if (settings.firstLaunch) {
navigateToOnboarding()
// Scenario 3: Handle others based on app type

if (hasConnectivity()) {
// only fetch profile with connectivity
val hasProfile: Boolean = userProfileService.hasUserProfile()

// Scenario 1: All good and setup for both androidNode and xClients
if (user != null && hasProfile) {
// TOOD:
// 1) Is this the right condition?
// 2a) androidNode being able to connect with other peers and
// 2b) xClients being able to connect with remote instance happening successfuly as part of services init?
navigateToHome()
// Scenario 2: Loading up for first time for both androidNode and xClients
} else if (settings.firstLaunch) {
navigateToOnboarding()
// Scenario 3: Handle others based on app type
} else {
doCustomNavigationLogic(settings, hasProfile)
}
} else {
doCustomNavigationLogic(settings, hasProfile)
navigateToTrustedNodeSetup()
}


// if (user == null) {
// navigateToCreateProfile()
// } else {
// if (userProfileService.hasUserProfile()) {
// if (!doCustomNavigationLogic(settings)) {
// navigateToHome()
// }
// } else {
// // If firstTimeApp launch, goto Onboarding[clientMode] (androidNode / xClient)
// // If not, goto CreateProfile
// if (settings.firstLaunch) {
// // TODO after onboarding need to make sure the rest is configured?
// navigateTo(Routes.Onboarding.name) {
// popUpTo(Routes.Splash.name) { inclusive = true }
// }
// } else {
// navigateTo(Routes.CreateProfile.name) {
// popUpTo(Routes.Splash.name) { inclusive = true }
// }
// }
// }
// }
}
}

Expand Down Expand Up @@ -126,6 +110,12 @@ open class SplashPresenter(
}
}

open suspend fun hasConnectivity(): Boolean {
webSocketClientProvider?.get().takeIf { it != null }.let {
return webSocketClientProvider?.testClient(it!!.host, it.port) == true
}
}

/**
* Default implementation in shared is for xClients. Override on node to avoid this.
* @return true if handled, false otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TrustedNodeSetupPresenter(
val currentSettings = settingsRepository.fetch()
val updatedSettings = Settings().apply {
bisqApiUrl = _bisqApiUrl.value
firstLaunch = currentSettings?.firstLaunch ?: false
firstLaunch = currentSettings?.firstLaunch ?: true
}
settingsRepository.update(updatedSettings)
}
Expand Down

0 comments on commit 5edfc41

Please sign in to comment.