我使用MVVM架构设置了我的应用。我要解决的问题是基于两个查询参数(在本例中为起点和终点)触发api调用,此参数是动态的并实时更新。当更新这两个值中的任何一个时,都应检查两个值都不为null,然后根据这两个值进行api调用。
我一直在和MediatorLiveData
和Transformations.switchMap()
一起玩,但是还没有找到一个行之有效的解决方案。尝试使用MediatorLiveData:
class MyViewModel(val repository: AppRepository) : ViewModel() {
val origin = MutableLiveData<Location>()
val destination = MutableLiveData<Location>()
val directions = MediatorLiveData<DrivingDirections>()
init {
directions.addSource(origin) { origin ->
if (origin != null && destination.value != null) {
directions.value = // problem here
// I want to make a call to something like
// repository.getDirections(origin, destination), but this comes
// back as LiveData<Directions> so *can't* set it to directions.value
// I, in effect, want to make directions = repository.getDirections(origin, destination),
// but then I lose the Mediator functionality
}
}
directions.addSource(destination) {
// as above
}
}
}
因此,尝试使用switchMap,我创建了一个粗略的OriginAndDestination
对象,然后观察到对此的更改。
class myViewModel(val repository: AppRepository) : ViewModel() {
val originAndDestination = MutableLiveData<OriginAndDestination>()
val directions: LiveData<Directions>
init {
directions = Transformations.switchMap(originAndDestination) { originAndDestination ->
// probably should do some checks that both origin and destination are not null,
// so this switch map could return null? How do I guard against null in a switch map?
repository.getDirections(originAndDestination.origin, originAndDestination.destination)
}
}
fun setOrigin(location: Location) {
// bit of problem code here... need to retrieve the current value of
// originAndDestination.value, then update the 'origin' property,
// then set it to the liveData value again to trigger the switchMap, above... while checking that the value isn't null in the first place...
// something like:
val tempValue = originAndDestination.value
if (tempValue != null) {
// update tempValue.origin
} else {
// create a new OriginAndDestination object?
}
// just feels really messy
}
fun setDestination(location: Location) {
// As above
}
}
很抱歉,所有评论都在这里突出显示了一些痛点和挫折感。我会以错误的方式处理吗? Origin
和Destination
是在用户界面字段中设置的。
答案 0 :(得分:1)
这是比MVVM更多的MVI,但这是我的处理方式:
data class myViewState(
val origin: Location,
val destination: Location,
val directions: DrivingDirections
)
class myViewModel(val repository: AppRepository) : ViewModel() {
private val _states = MediatorLiveData<myViewState>()
val states: LiveData<myViewState> = _states
private val lastSource: LiveData<myViewState>? = null
fun updateLocations(origin: Location, destination: Location) {
lastSource?.let { _states.removeSource(lastSource) }
lastSource = repository.getDirections(origin, destination)
_states.addSource(lastSource) { directions ->
_states.value = myViewState(origin, destination, directions)
}
}
}
(幸运的是,我们可以摆脱lastSource
sometime in the future)
您的UI层然后观察states
并更新其UI的位置和方向。
这可能不适合您的体系结构,但可能会给您一些想法。