-
Notifications
You must be signed in to change notification settings - Fork 664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MVI ViewModel #59
Merged
Merged
MVI ViewModel #59
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3b72d11
create abstraction fort the MovieDataSource
gbasile 8594308
split render and action provider out of a single interface
gbasile 24f4df2
use viewModel to allows state to survive device rotation
gbasile b65314b
handle loading
gbasile aea3e85
handling error states
gbasile 033fbe0
avoid unnecessary refreshes of the input field
gbasile eaceff7
Merge branch 'mvi_reducer' into mvi_view_model
gbasile df90942
renamed viewRender to Displayer
gbasile 2dfbca7
automatic wiring
gbasile f9a219e
Displayer implement actionProvider and render
gbasile 0e80ea2
scope types
gbasile 7c5a2ad
simplify VM logic
gbasile cb5648c
added import
gbasile bea331c
removed extra lines
gbasile efebcc1
Moved scope for State and Action into the ViewModel
gbasile 2c0d3e2
removed abstraction
gbasile 2e02c6c
reverted variable names
gbasile File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,7 @@ | |
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".presentation.SearchActivity" | ||
android:screenOrientation="portrait"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
<activity android:name=".presentation.SearchActivity" /> | ||
</application> | ||
|
||
</manifest> |
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
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
67 changes: 31 additions & 36 deletions
67
...iewIntentSample/search/src/main/java/com/novoda/movies/mvi/search/domain/SearchReducer.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,61 +1,56 @@ | ||
package com.novoda.movies.mvi.search.domain | ||
|
||
import com.novoda.movies.mvi.search.Reducer | ||
import com.novoda.movies.mvi.search.domain.SearchReducer.Changes.* | ||
import com.novoda.movies.mvi.search.presentation.SearchActivity.State | ||
import com.novoda.movies.mvi.search.presentation.SearchResultsConverter | ||
import com.novoda.movies.mvi.search.presentation.ViewSearchResults | ||
|
||
internal class SearchReducer( | ||
private val searchResultsConverter: SearchResultsConverter | ||
) : Reducer<ScreenState, ScreenStateChanges> { | ||
|
||
override fun reduce(state: ScreenState, change: ScreenStateChanges): ScreenState = | ||
when (change) { | ||
is ScreenStateChanges.ShowProgress -> state.showLoading() | ||
is ScreenStateChanges.HideProgress -> state.hideLoading() | ||
is ScreenStateChanges.AddResults -> state.addResults(change.results) | ||
is ScreenStateChanges.RemoveResults -> state.removeResults() | ||
is ScreenStateChanges.UpdateSearchQuery -> state.updateQuery(change.queryString) | ||
is ScreenStateChanges.HandleError -> state.toError(change.throwable) | ||
} | ||
|
||
private fun ScreenState.addResults(results: SearchResults): ScreenState { | ||
private val searchResultsConverter: SearchResultsConverter | ||
) : Reducer<State, SearchReducer.Changes> { | ||
|
||
override fun reduce(state: State, change: Changes): State = | ||
when (change) { | ||
is ShowProgress -> state.showLoading() | ||
is HideProgress -> state.hideLoading() | ||
is AddResults -> state.addResults(change.results) | ||
is RemoveResults -> state.removeResults() | ||
is UpdateSearchQuery -> state.updateQuery(change.queryString) | ||
is HandleError -> state.toError(change.throwable) | ||
} | ||
|
||
private fun State.addResults(results: SearchResults): State { | ||
return copy(results = searchResultsConverter.convert(results)) | ||
} | ||
|
||
sealed class Changes { | ||
object ShowProgress : Changes() | ||
object HideProgress : Changes() | ||
data class AddResults(val results: SearchResults) : Changes() | ||
object RemoveResults : Changes() | ||
data class HandleError(val throwable: Throwable) : Changes() | ||
data class UpdateSearchQuery(val queryString: String) : Changes() | ||
} | ||
|
||
} | ||
|
||
private fun ScreenState.removeResults(): ScreenState { | ||
private fun State.removeResults(): State { | ||
return copy(results = ViewSearchResults.emptyResults) | ||
} | ||
|
||
private fun ScreenState.toError(throwable: Throwable): ScreenState { | ||
private fun State.toError(throwable: Throwable): State { | ||
return copy(error = throwable) | ||
} | ||
|
||
private fun ScreenState.updateQuery(queryString: String): ScreenState { | ||
private fun State.updateQuery(queryString: String): State { | ||
return copy(queryString = queryString) | ||
} | ||
|
||
private fun ScreenState.hideLoading(): ScreenState { | ||
private fun State.hideLoading(): State { | ||
return copy(loading = false) | ||
} | ||
|
||
private fun ScreenState.showLoading(): ScreenState { | ||
private fun State.showLoading(): State { | ||
return copy(loading = true) | ||
} | ||
|
||
internal data class ScreenState( | ||
var queryString: String, | ||
var loading: Boolean = false, | ||
var results: ViewSearchResults, | ||
var error: Throwable? = null | ||
) | ||
|
||
sealed class ScreenStateChanges { | ||
|
||
object ShowProgress : ScreenStateChanges() | ||
object HideProgress : ScreenStateChanges() | ||
data class AddResults(val results: SearchResults) : ScreenStateChanges() | ||
object RemoveResults: ScreenStateChanges() | ||
data class HandleError(val throwable: Throwable) : ScreenStateChanges() | ||
data class UpdateSearchQuery(val queryString: String) : ScreenStateChanges() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do I need to have a consistent indenting with the rest of the project?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you import the novoda code styles from https://github.com/novoda/novoda/tree/master/ide-settings/intellij ?