Skip to content

Commit

Permalink
- base presenter helper method for all presenters that need to passt…
Browse files Browse the repository at this point in the history
…hrough a view requirement of a field to the repository object. Every time the object gets updated the transform code will get executed and the resulting data passed to the view to update (#63)
  • Loading branch information
rodvar authored Nov 18, 2024
1 parent 6a534ab commit 89dc282
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import network.bisq.mobile.domain.data.model.BaseModel

/**
* Presenter for any type of view.
Expand Down Expand Up @@ -96,5 +101,33 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?) {
this.dependants!!.remove(child)
}

/**
* Pass through function that allows you to link the requested type the view is asking for in its view presenter interface
* with the field in the domain model of the repository.
*
* @param M base model from where to extract the requested type T
* @param T the requested view type T
* @param repositoryFlow
* @param transform the transformation function from the
* @param initialValue
* @param scope defaults to Coroutine.Main (view thread)
* @param started defaults to Lazy loading
*/
protected fun <M: BaseModel, T> stateFlowFromRepository(
repositoryFlow: StateFlow<M?>,
transform: (M?) -> T,
initialValue: T,
scope: CoroutineScope = CoroutineScope(Dispatchers.Main),
started: SharingStarted = SharingStarted.Lazily
): StateFlow<T> {
return repositoryFlow
.map { transform(it) }
.stateIn(
scope = scope,
started = started,
initialValue = initialValue
)
}

private fun isRoot() = rootPresenter == null
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ open class MainPresenter(private val greetingRepository: GreetingRepository<Gree
private val _isContentVisible = MutableStateFlow(false)
override val isContentVisible: StateFlow<Boolean> = _isContentVisible

// TODO remove we don't need all the gretting example stuff anymore - but this code to passthrough should be a helper for presentation
private val _greetingText: StateFlow<String> = greetingRepository.data
.map { it?.greet() ?: "" } // Transform Greeting to String, we don't want the null
.stateIn(
scope = CoroutineScope(Dispatchers.Main), // Use an appropriate CoroutineScope for lifecycle control
started = SharingStarted.Lazily,
initialValue = "Welcome!"
)
// passthrough example
private val _greetingText: StateFlow<String> = stateFlowFromRepository(
repositoryFlow = greetingRepository.data,
transform = { it?.greet() ?: "" },
initialValue = "Welcome!"
)

override val greetingText: StateFlow<String> = _greetingText

Expand Down

0 comments on commit 89dc282

Please sign in to comment.