-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🏗️ simplify viewmodel state (#26)
- apply state on UI layer for search - apply state on UI layer for suggestions - little changes and package movements
- Loading branch information
1 parent
a00ed21
commit 306f3ae
Showing
14 changed files
with
140 additions
and
112 deletions.
There are no files selected for viewing
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
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
4 changes: 1 addition & 3 deletions
4
app/src/main/java/es/ffgiraldez/comicsearch/platform/ViewBinding.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
32 changes: 32 additions & 0 deletions
32
app/src/main/java/es/ffgiraldez/comicsearch/query/base/presentation/QueryStateViewModel.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package es.ffgiraldez.comicsearch.query.base.presentation | ||
|
||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import es.ffgiraldez.comicsearch.platform.plus | ||
import es.ffgiraldez.comicsearch.platform.toFlowable | ||
import io.reactivex.Flowable | ||
import io.reactivex.disposables.CompositeDisposable | ||
import org.reactivestreams.Publisher | ||
|
||
open class QueryStateViewModel<T>( | ||
transformer: (Flowable<String>) -> Publisher<QueryViewState<T>> | ||
) : ViewModel() { | ||
|
||
private val _state: MutableLiveData<QueryViewState<T>> = MutableLiveData() | ||
val state: LiveData<QueryViewState<T>> | ||
get() = _state | ||
val query: MutableLiveData<String> = MutableLiveData() | ||
|
||
private val disposable: CompositeDisposable = CompositeDisposable() | ||
|
||
init { | ||
disposable + query.toFlowable() | ||
.compose { transformer(it) } | ||
.subscribe { _state.postValue(it) } | ||
} | ||
|
||
override fun onCleared(): Unit = disposable.clear() | ||
} | ||
|
41 changes: 0 additions & 41 deletions
41
app/src/main/java/es/ffgiraldez/comicsearch/query/base/presentation/QueryViewModel.kt
This file was deleted.
Oops, something went wrong.
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
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
24 changes: 24 additions & 0 deletions
24
app/src/main/java/es/ffgiraldez/comicsearch/query/base/ui/QueryViewStateExt.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package es.ffgiraldez.comicsearch.query.base.ui | ||
|
||
import arrow.core.Option | ||
import arrow.core.toOption | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.query.base.presentation.QueryViewState | ||
|
||
val <T>QueryViewState<T>.error: Option<ComicError> | ||
get() = when (this) { | ||
is QueryViewState.Error -> _error.toOption() | ||
else -> Option.empty() | ||
} | ||
|
||
val <T>QueryViewState<T>.results: List<T> | ||
get() = when (this) { | ||
is QueryViewState.Result -> _results | ||
else -> emptyList() | ||
} | ||
|
||
val <T>QueryViewState<T>.loading: Boolean | ||
get() = when (this) { | ||
is QueryViewState.Loading -> true | ||
else -> false | ||
} |
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
6 changes: 3 additions & 3 deletions
6
app/src/main/java/es/ffgiraldez/comicsearch/query/search/presentation/SearchViewModel.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
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
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
31 changes: 14 additions & 17 deletions
31
app/src/main/java/es/ffgiraldez/comicsearch/query/sugestion/ui/SuggestionBindingAdapters.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,36 +1,33 @@ | ||
package es.ffgiraldez.comicsearch.query.sugestion.ui | ||
|
||
import androidx.databinding.BindingAdapter | ||
import arrow.core.Option | ||
import com.arlib.floatingsearchview.FloatingSearchView | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.platform.safe | ||
import es.ffgiraldez.comicsearch.query.base.presentation.QueryViewState | ||
import es.ffgiraldez.comicsearch.query.base.ui.QuerySearchSuggestion.ErrorSuggestion | ||
import es.ffgiraldez.comicsearch.query.base.ui.QuerySearchSuggestion.ResultSuggestion | ||
import es.ffgiraldez.comicsearch.query.base.ui.error | ||
import es.ffgiraldez.comicsearch.query.base.ui.loading | ||
import es.ffgiraldez.comicsearch.query.base.ui.results | ||
import es.ffgiraldez.comicsearch.query.base.ui.toHumanResponse | ||
|
||
@BindingAdapter("on_change") | ||
fun bindQueryChangeListener(search: FloatingSearchView, listener: FloatingSearchView.OnQueryChangeListener) = | ||
search.setOnQueryChangeListener(listener) | ||
|
||
@BindingAdapter("suggestions", "error") | ||
fun bindSuggestions( | ||
fun bindQueryChangeListener( | ||
search: FloatingSearchView, | ||
resultData: List<String>?, | ||
errorData: Option<ComicError>? | ||
) = safe(errorData, resultData) { error, results -> | ||
listener: FloatingSearchView.OnQueryChangeListener | ||
): Unit = search.setOnQueryChangeListener(listener) | ||
|
||
@BindingAdapter("state_change") | ||
fun bindSuggestions(search: FloatingSearchView, data: QueryViewState<String>?): Unit? = data?.run { | ||
search.toggleProgress(loading) | ||
error.fold({ | ||
results.map { ResultSuggestion(it) } | ||
}, { | ||
listOf(ErrorSuggestion(it.toHumanResponse())) | ||
}).let { search.swapSuggestions(it) } | ||
} | ||
|
||
@BindingAdapter("show_progress") | ||
fun bindLoading(search: FloatingSearchView, liveData: Boolean?) = liveData?.let { | ||
when (it) { | ||
true -> search.showProgress() | ||
false -> search.hideProgress() | ||
} | ||
private fun FloatingSearchView.toggleProgress(show: Boolean): Unit = when (show) { | ||
true -> showProgress() | ||
false -> hideProgress() | ||
} | ||
|
Oops, something went wrong.