Skip to content

Commit

Permalink
[added] TrustedNodeSetupPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
nostrbuddha committed Nov 16, 2024
1 parent 7f5f89e commit 0ed838b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package network.bisq.mobile.domain.data.model

open class Settings : BaseModel() {
open var bisqUrl: String = ""
open var isConnected: Boolean = false
}

interface SettingsFactory {
fun createSettings(): Settings
}

class DefaultSettingsFactory : SettingsFactory {
override fun createSettings() = Settings()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ open class GreetingRepository<T: Greeting>: SingleObjectRepository<T>()
open class BisqStatsRepository<T: BisqStats>: SingleObjectRepository<T>()
open class BtcPriceRepository<T: BtcPrice>: SingleObjectRepository<T>()
open class UserProfileRepository<T: UserProfile>: SingleObjectRepository<T>()
open class SettingsRepository<T: Settings>: SingleObjectRepository<T>()
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ val domainModule = module {
single<BtcPriceRepository<BtcPrice>> { BtcPriceRepository() }
single<NetworkRepository<NetworkModel>> { NetworkRepository() }
single<UserProfileRepository<UserProfile>> { UserProfileRepository() }
single<SettingsRepository<Settings>> { SettingsRepository() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import network.bisq.mobile.presentation.ui.AppPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.ICreateProfilePresenter
import network.bisq.mobile.presentation.ui.uicases.startup.IOnboardingPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.ISplashPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.ITrustedNodeSetupPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.CreateProfilePresenter
import network.bisq.mobile.presentation.ui.uicases.startup.OnBoardingPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.SplashPresenter
import network.bisq.mobile.presentation.ui.uicases.startup.TrustedNodeSetupPresenter
import network.bisq.mobile.presentation.ui.uicases.GettingStartedPresenter
import network.bisq.mobile.presentation.ui.uicases.IGettingStarted
import org.koin.core.qualifier.named
Expand Down Expand Up @@ -48,4 +50,11 @@ val presentationModule = module {
userProfileRepository = get()
)
} bind ICreateProfilePresenter::class

single {
(navController: NavController) -> TrustedNodeSetupPresenter(
navController = navController,
settingsRepository = get()
)
} bind ITrustedNodeSetupPresenter::class
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open class CreateProfilePresenter(
private val _profileName = MutableStateFlow("")
override val profileName: StateFlow<String> = _profileName

// TODO: Not working
init {
CoroutineScope(Dispatchers.IO).launch {
userProfileRepository.data.collectLatest { userProfile ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package network.bisq.mobile.presentation.ui.uicases.startup

import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.collectLatest
import network.bisq.mobile.domain.data.model.Settings
import network.bisq.mobile.domain.data.repository.SettingsRepository
import network.bisq.mobile.presentation.BasePresenter
import network.bisq.mobile.presentation.ui.navigation.Routes
import kotlinx.coroutines.delay

class TrustedNodeSetupPresenter(
private val navController: NavController,
private val settingsRepository: SettingsRepository<Settings>
) : BasePresenter(), ITrustedNodeSetupPresenter {

private val _bisqUrl = MutableStateFlow("")
override val bisqUrl: StateFlow<String> = _bisqUrl

private val _isConnected = MutableStateFlow(false)
override val isConnected: StateFlow<Boolean> = _isConnected

override fun updateBisqUrl(newUrl: String) {
_bisqUrl.value = newUrl
}

override fun testConnection(isTested: Boolean) {
_isConnected.value = isTested

CoroutineScope(Dispatchers.IO).launch {
val updatedSettings = Settings().apply {
bisqUrl = _bisqUrl.value
isConnected = _isConnected.value
}

settingsRepository.update(updatedSettings)
}
}

override fun navigateToNextScreen() {
navController.navigate(Routes.TabContainer.name) {
popUpTo(Routes.TrustedNodeSetup.name) { inclusive = true }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -32,24 +33,39 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import org.koin.compose.koinInject
import org.koin.core.qualifier.named
import org.koin.core.parameter.parametersOf
import cafe.adriel.lyricist.LocalStrings

private lateinit var textState: MutableState<String>
import kotlinx.coroutines.flow.StateFlow

interface ITrustedNodeSetupPresenter {
val bisqUrl: StateFlow<String>
val isConnected: StateFlow<Boolean>

fun updateBisqUrl(newUrl: String)

fun testConnection(isTested: Boolean)

fun navigateToNextScreen()
}

//TODO: Rename this to BisqConnectScreen?
@OptIn(ExperimentalResourceApi::class)
@Composable
fun TrustedNodeSetupScreen(
) {
val strings = LocalStrings.current
val navController: NavHostController = koinInject(named("RootNavController"))
textState = remember { mutableStateOf("") }
val isConnected by remember { mutableStateOf(false) }
val presenter: ITrustedNodeSetupPresenter = koinInject { parametersOf(navController) }

val bisqUrl = presenter.bisqUrl.collectAsState().value
val isConnected = presenter.isConnected.collectAsState().value

BisqScrollLayout() {
BisqLogo()
Spacer(modifier = Modifier.height(24.dp))
Column(
verticalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxSize().padding(horizontal = 24.dp)
modifier = Modifier.fillMaxSize().padding(horizontal = 0.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
Expand All @@ -63,7 +79,7 @@ fun TrustedNodeSetupScreen(
Image(painterResource(Res.drawable.icon_question_mark), "Question mark")
}

MaterialTextField(textState.value, onValueChanged = { textState.value = it })
MaterialTextField(bisqUrl, onValueChanged = { presenter.updateBisqUrl(it) })

Row(
modifier = Modifier.fillMaxWidth(),
Expand Down Expand Up @@ -107,46 +123,41 @@ fun TrustedNodeSetupScreen(

Spacer(modifier = Modifier.height(56.dp))

var visible by remember {
mutableStateOf(false)
}

if (!visible) {
if (!isConnected) {
BisqButton(
text = "Test Connection",
color = if (textState.value.isEmpty()) BisqTheme.colors.grey1 else BisqTheme.colors.light1,
onClick = { visible = !visible },
color = if (bisqUrl.isEmpty()) BisqTheme.colors.grey1 else BisqTheme.colors.light1,
onClick = {
presenter.testConnection(true)
},
padding = PaddingValues(horizontal = 32.dp, vertical = 12.dp),
)
)
} else {
Row(modifier = Modifier.fillMaxWidth().padding(horizontal = 24.dp)) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth().padding(horizontal = 0.dp)
) {
AnimatedVisibility(
visible = visible,
visible = isConnected,
enter = slideInHorizontally(initialOffsetX = { it }, animationSpec = tween(700)),
) {
BisqButton(
text = "Test Connection",
color = if (textState.value.isEmpty()) BisqTheme.colors.grey1 else BisqTheme.colors.light1,
onClick = { },
color = if (bisqUrl.isEmpty()) BisqTheme.colors.grey1 else BisqTheme.colors.light1,
onClick = { presenter.testConnection(true) },
padding = PaddingValues(horizontal = 32.dp, vertical = 12.dp),
)
}
Spacer(modifier = Modifier.width(20.dp))
//Spacer(modifier = Modifier.width(20.dp))
AnimatedVisibility(
visible = visible,
visible = isConnected,
enter = fadeIn(animationSpec = tween(300)),

) {
BisqButton(
text = "Next",
color = BisqTheme.colors.light1,
onClick = {
navController.navigate(Routes.TabContainer.name) {
popUpTo(Routes.TrustedNodeSetup.name) {
inclusive = true
}
}
},
onClick = { presenter.navigateToNextScreen() },
padding = PaddingValues(horizontal = 32.dp, vertical = 12.dp),
)
}
Expand Down

0 comments on commit 0ed838b

Please sign in to comment.