badoo / badoo/MVICore

What is the best way to handle N Features for a View?

Open
#54 2 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Kotlin
Stars
1.3k
Forks
88
PR merge metrics
No merged PRs in 30d

Description

I'm working on implementing MVICore on a complicated screen and have many Features that need to reduce down to one ViewModel. The demo app uses an example with two and a pair with an extension function wrapping `Observable.combineLatest()` I am finding this is not scalable enough for most situations. The code I am referring to is below:

https://github.com/badoo/MVICore/blob/2b9ecc58a09c6c45a2ebead9c33b9d58f21999ac/mvicore-demo/mvicore-demo-app/src/main/java/com/badoo/mvicoredemo/ui/main/MainActivityBindings.kt#L24

https://github.com/badoo/MVICore/blob/2b9ecc58a09c6c45a2ebead9c33b9d58f21999ac/mvicore-demo/mvicore-demo-app/src/main/java/com/badoo/mvicoredemo/utils/RxExtensions.kt#L7

https://github.com/badoo/MVICore/blob/2b9ecc58a09c6c45a2ebead9c33b9d58f21999ac/mvicore-demo/mvicore-demo-app/src/main/java/com/badoo/mvicoredemo/ui/main/viewmodel/ViewModelTransformer.kt#L7

I have attempted to use RxJava's `combineLatest` that takes a list and a `Function` [(docs)](http://reactivex.io/RxJava/javadoc/rx/Observable.html#combineLatest-java.util.List-rx.functions.FuncN-) but have run into an issue with generics and inheritance from the `Features`.

I have spent some time working on a solution, but have not yet found one.

I have created an extension function like so:

```kotlin
fun combineFeatures(
features: Array>,
combine: (Array) -> R
): ObservableSource =
Observable
.combineLatest(list) {
combine(it)
}
```

However in attempting to use this like:
```kotlin
binder.bind(combineFeatures(arrayOf(feature1, feature2) {
var combinedState = CombinedState() // Initial State
it.forEach { feature ->
when (feature) {
is Feature1 -> combinedState.copy(fromFeature1 = feature.data)
is Feature2 -> combinedState.copy(fromFeature2 = feature.data)
}
}
combinedState
} ) to this using ViewModelTransformer
```

and making the appropriate changes to the `ViewModelTransformer` to accept the `CombinedState` to then derive the data for the `View`. The generics/inheritance issues arise in the `arrayOf(feature1, feature2)` creation. Even if I define the `Any` type. (This looks like it _could_ be a limitation of the JVM)

What I'm hoping for here is to see if there is already an appropriate solution to the N-Features for 1-View problem.

Please let me know if there is any other information I can provide.

Contributor guide

No contributing guide indexed for this repository

Research direction

Read the demo references in MainActivityBindings.kt, RxExtensions.kt, and ViewModelTransformer.kt, starting with the existing two-feature combineLatest example. Investigate how an N-feature combination can satisfy the reported Kotlin generics and inheritance constraints; done means the project has a clear, supported approach for reducing multiple Features to one ViewModel.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
22/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.