Skip to content

Commit

Permalink
Remove domain models and use return values instead
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJannsen committed Nov 20, 2024
1 parent 1f9c774 commit 5778656
Show file tree
Hide file tree
Showing 25 changed files with 284 additions and 352 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package network.bisq.mobile.client.di

import network.bisq.mobile.android.node.main.bootstrap.ClientApplicationBootstrapFacade
import network.bisq.mobile.android.node.main.bootstrap.ClientApplicationBootstrapModel
import network.bisq.mobile.client.presentation.AndroidClientMainPresenter
import network.bisq.mobile.client.service.ApiRequestService
import network.bisq.mobile.domain.client.main.user_profile.ClientUserProfileModel
import network.bisq.mobile.domain.client.main.user_profile.ClientUserProfileServiceFacade
import network.bisq.mobile.domain.client.main.user_profile.UserProfileApiGateway
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapModel
import network.bisq.mobile.domain.user_profile.UserProfileModel
import network.bisq.mobile.domain.user_profile.UserProfileServiceFacade
import network.bisq.mobile.presentation.MainPresenter
import network.bisq.mobile.presentation.ui.AppPresenter
import org.koin.dsl.bind
import org.koin.dsl.module

val androidClientModule = module {
single<ApplicationBootstrapModel> { ClientApplicationBootstrapModel() }
single<ApplicationBootstrapFacade> { ClientApplicationBootstrapFacade(get()) }
single<ApplicationBootstrapFacade> { ClientApplicationBootstrapFacade() }

single<UserProfileModel> { ClientUserProfileModel() }
single { ApiRequestService(get(), "10.0.2.2") }
single { UserProfileApiGateway(get()) }
single<UserProfileServiceFacade> { ClientUserProfileServiceFacade(get(), get()) }

single<UserProfileServiceFacade> { ClientUserProfileServiceFacade(get()) }
single<MainPresenter> { AndroidClientMainPresenter(get()) } bind AppPresenter::class
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package network.bisq.mobile.client.presentation

import network.bisq.mobile.domain.data.repository.GreetingRepository
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.presentation.MainPresenter

@Suppress("UNCHECKED_CAST")
class AndroidClientMainPresenter(
private val applicationBootstrapFacade: ApplicationBootstrapFacade
) : MainPresenter(GreetingRepository()) {

// FIXME onViewAttached is called twice
var applicationServiceInited = false
override fun onViewAttached() {
super.onViewAttached()

if (!applicationServiceInited) {
applicationServiceInited = true
applicationBootstrapFacade.initialize()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package network.bisq.mobile.android.node.di

import network.bisq.mobile.android.node.AndroidApplicationService
import network.bisq.mobile.android.node.domain.data.repository.NodeGreetingRepository
import network.bisq.mobile.android.node.domain.user_profile.NodeUserProfileModel
import network.bisq.mobile.android.node.domain.user_profile.NodeUserProfileServiceFacade
import network.bisq.mobile.android.node.main.bootstrap.NodeApplicationBootstrapFacade
import network.bisq.mobile.android.node.main.bootstrap.NodeApplicationBootstrapModel
import network.bisq.mobile.android.node.presentation.NodeMainPresenter
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapModel
import network.bisq.mobile.domain.user_profile.UserProfileModel
import network.bisq.mobile.domain.user_profile.UserProfileServiceFacade
import network.bisq.mobile.presentation.MainPresenter
import network.bisq.mobile.presentation.ui.AppPresenter
Expand All @@ -22,11 +18,9 @@ val androidNodeModule = module {

single { AndroidApplicationService.Supplier() }

single<ApplicationBootstrapModel> { NodeApplicationBootstrapModel() }
single<ApplicationBootstrapFacade> { NodeApplicationBootstrapFacade(get(), get()) }
single<ApplicationBootstrapFacade> { NodeApplicationBootstrapFacade(get()) }

single<UserProfileModel> { NodeUserProfileModel() }
single<UserProfileServiceFacade> { NodeUserProfileServiceFacade(get(), get()) }
single<UserProfileServiceFacade> { NodeUserProfileServiceFacade(get()) }


// this line showcases both, the possibility to change behaviour of the app by changing one definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ package network.bisq.mobile.android.node.main.bootstrap
import bisq.application.State
import network.bisq.mobile.android.node.AndroidApplicationService
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapModel

class NodeApplicationBootstrapFacade(
override val model: ApplicationBootstrapModel,
private val applicationServiceSupplier: AndroidApplicationService.Supplier
private val supplier: AndroidApplicationService.Supplier
) :
ApplicationBootstrapFacade {
ApplicationBootstrapFacade() {

override fun initialize() {
applicationServiceSupplier.stateSupplier.get().addObserver { state: State ->
supplier.stateSupplier.get().addObserver { state: State ->
when (state) {
State.INITIALIZE_APP -> {
model.setState("Starting Bisq")
model.setProgress(0f)
setState("Starting Bisq")
setProgress(0f)
}

State.INITIALIZE_NETWORK -> {
model.setState("Initialize P2P network")
model.setProgress(0.5f)
setState("Initialize P2P network")
setProgress(0.5f)
}

// not used
State.INITIALIZE_WALLET -> {
}

State.INITIALIZE_SERVICES -> {
model.setState("Initialize services")
model.setProgress(0.75f)
setState("Initialize services")
setProgress(0.75f)
}

State.APP_INITIALIZED -> {
model.setState("Bisq started")
model.setProgress(1f)
setState("Bisq started")
setProgress(1f)
}

State.FAILED -> {
model.setState("Startup failed")
model.setProgress(0f)
setState("Startup failed")
setProgress(0f)
}
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package network.bisq.mobile.android.node.domain.user_profile
import bisq.common.encoding.Hex
import bisq.security.DigestUtil
import bisq.security.SecurityService
import bisq.security.pow.ProofOfWork
import bisq.user.UserService
import bisq.user.identity.NymIdGenerator
import bisq.user.identity.UserIdentity
import bisq.user.profile.UserProfile
import co.touchlab.kermit.Logger
import network.bisq.mobile.android.node.AndroidApplicationService
import network.bisq.mobile.domain.user_profile.UserProfileModel
import network.bisq.mobile.domain.user_profile.UserProfileServiceFacade
import java.security.KeyPair
import java.util.Random
import kotlin.math.max
import kotlin.math.min
Expand All @@ -21,10 +21,7 @@ import kotlin.math.min
* It uses in a in-memory model for the relevant data required for the presenter to reflect the domains state.
* Persistence is done inside the Bisq 2 libraries.
*/
class NodeUserProfileServiceFacade(
override val model: UserProfileModel,
private val applicationServiceSupplier: AndroidApplicationService.Supplier
) :
class NodeUserProfileServiceFacade(private val supplier: AndroidApplicationService.Supplier) :
UserProfileServiceFacade {

companion object {
Expand All @@ -33,99 +30,86 @@ class NodeUserProfileServiceFacade(

private val log = Logger.withTag(this::class.simpleName ?: "NodeUserProfileServiceFacade")

private var pubKeyHash: ByteArray? = null
private var keyPair: KeyPair? = null
private var proofOfWork: ProofOfWork? = null

private var nickName: String? = ""
private var id: String? = null
private var nym: String? = null

private val securityService: SecurityService
get() = applicationServiceSupplier.securityServiceSupplier.get()
get() = supplier.securityServiceSupplier.get()

private val userService: UserService
get() = applicationServiceSupplier.userServiceSupplier.get()
get() = supplier.userServiceSupplier.get()


override suspend fun hasUserProfile(): Boolean {
return userService.userIdentityService.userIdentities.isNotEmpty()
}

override suspend fun generateKeyPair() {
model as NodeUserProfileModel
model.setGenerateKeyPairInProgress(true)
val keyPair = securityService.keyBundleService.generateKeyPair()
model.keyPair = keyPair
val pubKeyHash = DigestUtil.hash(keyPair.public.encoded)
model.pubKeyHash = pubKeyHash
model.setId(Hex.encode(pubKeyHash))
override suspend fun generateKeyPair(result: (String, String) -> Unit) {
keyPair = securityService.keyBundleService.generateKeyPair()
pubKeyHash = DigestUtil.hash(keyPair!!.public.encoded)

val ts = System.currentTimeMillis()
val proofOfWork = userService.userIdentityService.mintNymProofOfWork(pubKeyHash)
proofOfWork = userService.userIdentityService.mintNymProofOfWork(pubKeyHash)
val powDuration = System.currentTimeMillis() - ts
log.i("Proof of work creation completed after $powDuration ms")
createSimulatedDelay(powDuration)
model.proofOfWork = proofOfWork
val powSolution = proofOfWork.solution
val nym = NymIdGenerator.generate(pubKeyHash, powSolution)
model.setNym(nym)

id = Hex.encode(pubKeyHash)
nym = NymIdGenerator.generate(pubKeyHash, proofOfWork!!.solution)

// CatHash is in desktop, needs to be reimplemented or the javafx part extracted and refactored into a non javafx lib
// Image image = CatHash.getImage(pubKeyHash,
// powSolution,
// CURRENT_AVATARS_VERSION,
// CreateProfileModel.CAT_HASH_IMAGE_SIZE);

model.setGenerateKeyPairInProgress(false)
result(id!!, nym!!)
}

override fun setNickName(value: String) {
this.nickName = value
}

override suspend fun createAndPublishNewUserProfile() {
model as NodeUserProfileModel
model.setCreateAndPublishInProgress(true) // UI should start busy animation based on that property
userService.userIdentityService.createAndPublishNewUserProfile(
model.nickName.value,
model.keyPair,
model.pubKeyHash,
model.proofOfWork,
AVATAR_VERSION,
"",
""
)
.whenComplete { userIdentity: UserIdentity?, throwable: Throwable? ->
// UI should stop busy animation and show `next` button
model.setCreateAndPublishInProgress(false)
}
nickName?.let {
userService.userIdentityService.createAndPublishNewUserProfile(
nickName,
keyPair,
pubKeyHash,
proofOfWork,
AVATAR_VERSION,
"",
""
)
}
}

override suspend fun getUserIdentityIds(): List<String> {
return userService.userIdentityService.userIdentities
.map { userIdentity -> userIdentity.id }
return userService.userIdentityService.userIdentities.map { userIdentity -> userIdentity.id }
}

override suspend fun applySelectedUserProfile() {
override suspend fun applySelectedUserProfile(result: (String?, String?, String?) -> Unit) {
val userProfile = getSelectedUserProfile()
if (userProfile != null) {
model.setNickName(userProfile.nickName)
model.setNym(userProfile.nym)
model.setId(userProfile.id)
nickName = userProfile.nickName
nym = userProfile.nym
id = userProfile.id

// reset
pubKeyHash = null
keyPair = null
proofOfWork = null
}
result(nickName, nym, id)
}

private fun getSelectedUserProfile(): UserProfile? {
val userIdentity = userService.userIdentityService.selectedUserIdentity ?: return null
return userIdentity.userProfile

}

private fun findUserProfile(id: String): UserProfileModel? {
return userService.userIdentityService.userIdentities
.map { userIdentity -> getNodeUserProfileModel(userIdentity) }
.find { model -> model.id.equals(id) }
}

private fun getNodeUserProfileModel(userIdentity: UserIdentity): UserProfileModel {
val userProfile = userIdentity.userProfile
val model = NodeUserProfileModel()
model.setNickName(userProfile.nickName)
model.setNym(userProfile.nym)
model.setId(userProfile.id)
model.keyPair = userIdentity.identity.keyBundle.keyPair
model.pubKeyHash = userIdentity.userProfile.pubKeyHash
model.proofOfWork = userIdentity.userProfile.proofOfWork
return model
return userService.userIdentityService.selectedUserIdentity?.userProfile
}

private fun createSimulatedDelay(powDuration: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ class NodeMainPresenter(
supplier.applicationService.shutdown()
super.onDestroying()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import network.bisq.mobile.domain.data.BackgroundDispatcher
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapFacade
import network.bisq.mobile.domain.data.repository.main.bootstrap.ApplicationBootstrapModel

class ClientApplicationBootstrapFacade(
override val model: ApplicationBootstrapModel
) :
ApplicationBootstrapFacade {
private val coroutineScope = CoroutineScope(BackgroundDispatcher)
class ClientApplicationBootstrapFacade() :
ApplicationBootstrapFacade() {
private val coroutineScope = CoroutineScope(BackgroundDispatcher)

override fun initialize() {
val model = model as ClientApplicationBootstrapModel
model.setState( "Dummy state 1")
model.setProgress(0f)
setState("Dummy state 1")
setProgress(0f)

// just dummy loading simulation, might be that there is no loading delay at the end...
coroutineScope.launch {
delay(500L)
model.setState( "Dummy state 2")
model.setProgress(0.25f)
setState("Dummy state 2")
setProgress(0.25f)

delay(500L)
model.setState( "Dummy state 3")
model.setProgress(0.5f)
setState("Dummy state 3")
setProgress(0.5f)

delay(500L)
model.setState( "Dummy state 4")
model.setProgress(1f)
setState("Dummy state 4")
setProgress(1f)
}
}
}

This file was deleted.

Loading

0 comments on commit 5778656

Please sign in to comment.