-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go to next screen on onCreateAndPublishNewUserProfile completed.
- Loading branch information
1 parent
c374940
commit 51a63f1
Showing
2 changed files
with
53 additions
and
67 deletions.
There are no files selected for viewing
97 changes: 48 additions & 49 deletions
97
...Main/kotlin/network/bisq/mobile/presentation/ui/uicases/startup/CreateProfilePresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,83 @@ | ||
package network.bisq.mobile.presentation.ui.uicases.startup | ||
|
||
import androidx.navigation.NavController | ||
import co.touchlab.kermit.Logger | ||
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.BackgroundDispatcher | ||
import network.bisq.mobile.domain.data.repository.UserProfileRepository | ||
import network.bisq.mobile.domain.data.model.UserProfile | ||
import network.bisq.mobile.presentation.BasePresenter | ||
import network.bisq.mobile.presentation.ui.navigation.Routes | ||
import network.bisq.mobile.presentation.MainPresenter | ||
import network.bisq.mobile.presentation.ui.navigation.Routes | ||
|
||
open class CreateProfilePresenter( | ||
mainPresenter: MainPresenter, | ||
private val navController: NavController, | ||
private val userProfileRepository: UserProfileRepository | ||
) : BasePresenter(mainPresenter), ICreateProfilePresenter { | ||
) : BasePresenter(mainPresenter) { | ||
|
||
private val _profileName = MutableStateFlow("") | ||
override val profileName: StateFlow<String> = _profileName | ||
override val nym: StateFlow<String> = MutableStateFlow("") | ||
override val id: StateFlow<String> = MutableStateFlow("") | ||
private val log = Logger.withTag("CreateProfilePresenter") | ||
private val userProfileModel = userProfileRepository.model | ||
|
||
// TODO: Not working | ||
init { | ||
CoroutineScope(BackgroundDispatcher).launch { | ||
userProfileRepository.data.collectLatest { userProfile -> | ||
userProfile?.let { | ||
_profileName.value = it.name | ||
} | ||
} | ||
} | ||
} | ||
val nickName: StateFlow<String> = userProfileModel.nickName | ||
val nym: StateFlow<String> = userProfileModel.nym | ||
val id: StateFlow<String> = userProfileModel.id | ||
|
||
override fun onProfileNameChanged(newName: String) { | ||
_profileName.value = newName | ||
} | ||
|
||
override fun navigateToNextScreen() { | ||
if (_profileName.value.isNotEmpty()) { | ||
saveUserProfile() | ||
navController.navigate(Routes.TrustedNodeSetup.name) { | ||
popUpTo(Routes.CreateProfile.name) { inclusive = true } | ||
} | ||
} | ||
} | ||
|
||
override fun onGenerateKeyPair() { | ||
override fun onViewAttached() { | ||
onGenerateKeyPair() | ||
|
||
// Currently we just always show the create profile page. | ||
// We need to make the UI behaving to the intended use case. | ||
// 1. After loading screen -> check if there is an existing user profile by | ||
// calling `userProfileRepository.service.hasUserProfile()` | ||
// 1a. If there is an existing user profile, do not show create user profile screen, | ||
// but show user profile is some not yet defined way (right top corner in Desktop shows user profile). | ||
// `userProfileRepository.service.applySelectedUserProfile()` fills the user profile data to | ||
// userProfileRepository.model to be used in the UI. | ||
// 1b. If there is no existing user profile, show create profile screen and call | ||
// `onGenerateKeyPair()` when view is ready. | ||
} | ||
|
||
override fun onCreateAndPublishNewUserProfile() { | ||
this.navigateToNextScreen() | ||
fun onNickNameChanged(value: String) { | ||
userProfileRepository.model.setNickName(value) | ||
} | ||
|
||
fun saveUserProfile() { | ||
fun onGenerateKeyPair() { | ||
// takes 200 -1000 ms | ||
// todo start busy animation in UI | ||
CoroutineScope(BackgroundDispatcher).launch { | ||
val updatedProfile = UserProfile().apply { | ||
name = _profileName.value | ||
userProfileRepository.model.generateKeyPairInProgress.collect { inProgress -> | ||
if (!inProgress) { | ||
// todo stop busy animation in UI | ||
} | ||
} | ||
userProfileRepository.update(updatedProfile) | ||
} | ||
} | ||
|
||
/* | ||
fun onNicknameChanged(newNickname: String) { | ||
_nickname.value = newNickname | ||
CoroutineScope(BackgroundDispatcher).launch { | ||
userProfileRepository.service.generateKeyPair() | ||
} | ||
} | ||
|
||
fun navigateToNextScreen() { | ||
if (_nickname.value.isNotEmpty()) { | ||
navController.navigate(Routes.TrustedNodeSetup.name) { | ||
popUpTo(Routes.CreateProfile.name) { inclusive = true } | ||
fun onCreateAndPublishNewUserProfile() { | ||
// todo start busy animation in UI | ||
// We cannot use BackgroundDispatcher here as we get error: | ||
// `Method setCurrentState must be called on the main thread` | ||
CoroutineScope(Dispatchers.Main).launch { | ||
userProfileRepository.model.createAndPublishInProgress.collect { inProgress -> | ||
if (!inProgress) { | ||
// todo stop busy animation in UI | ||
navController.navigate(Routes.TrustedNodeSetup.name) { | ||
popUpTo(Routes.CreateProfile.name) { inclusive = true } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
CoroutineScope(BackgroundDispatcher).launch { | ||
userProfileRepository.service.createAndPublishNewUserProfile() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters