-
Notifications
You must be signed in to change notification settings - Fork 500
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
Introduce Mavericks core module #635
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b369c85
Introduce Mavericks core module
sav007 e28cc73
Rename `MavericksSateModel` to `MavericksRepository`
sav007 129518c
Fix mutability checker
sav007 f238e31
Move some tests around
sav007 e06560e
Rename VM to Repository
sav007 3f76869
Feedback - remove `MavericksRepository.onExecute`
sav007 bc208a5
Lint
sav007 ae78fc0
Delegate over inheritance
sav007 e646426
Docs
sav007 e24b3fc
Remove `MavericksRepositoryConfigProvider`
sav007 1667d18
Update docs sample
sav007 8d5f167
Merge remote-tracking branch 'upstream/main' into is/mavericks-core
sav007 88df223
Unit tests
sav007 9b64797
Update docs
sav007 afc5b4e
Lint
sav007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# What's new in Mavericks 3.0 | ||
|
||
### Experimental MavericksRepository | ||
|
||
Mavericks 3.0 introduces an experimental module `mvrx-core` with new abstraction `MavericksRepository` designed to provide a base class for any statefull repository implementation that owns and manages its state. This module is pure Kotlin and has no Android dependencies. Primary goal of this module is to provide the same API and behaviour as `MavericksViewModel` in Android modules. | ||
|
||
|
||
### API Changes | ||
|
||
Mavericks 3.0 introduces one breaking change: `MavericksViewModelConfig.BlockExecutions` is extracted into `MavericksBlockExecutions`. In order to migrate, you need to update your code to use `MavericksBlockExecutions` instead of `MavericksViewModelConfig.BlockExecutions`. | ||
|
||
### New Functionality | ||
|
||
#### `MavericksRepository` | ||
|
||
`MavericksRepository` behaves exactly like `MavericksViewModel` except it doesn't have any Android dependencies. Even more, under the hood `MavericksViewModel` uses `MavericksRepository` to manage its state. You can find that `MavericksRepository` and `MavericksViewModel` are very similar in terms of API and behaviour. | ||
As this is experimental module you have to opt in to use it by `-Xopt-in=com.airbnb.mvrx.InternalMavericksApi` compilation argument. | ||
|
||
```kotlin | ||
data class Forecast( | ||
val temperature: Int, | ||
val precipitation: Int, | ||
val wind: Int, | ||
) | ||
|
||
data class WeatherForecastState( | ||
val forecasts: Async<List<Forecast>> = Uninitialized, | ||
) : MavericksState | ||
|
||
interface WeatherApi { | ||
suspend fun getForecasts(): List<Forecast> | ||
} | ||
|
||
class WeatherForecastRepository( | ||
scope: CoroutineScope, | ||
private val api: WeatherApi, | ||
) : MavericksRepository<WeatherForecastState>( | ||
initialState = WeatherForecastState(), | ||
coroutineScope = scope, | ||
performCorrectnessValidations = BuildConfig.DEBUG, | ||
) { | ||
init { | ||
suspend { api.getForecasts() }.execute { copy(forecasts = it) } | ||
} | ||
|
||
fun refresh() { | ||
suspend { api.getForecasts() }.execute { copy(forecasts = it) } | ||
} | ||
} | ||
``` | ||
|
||
#### `MavericksRepositoryConfig` | ||
|
||
In order to construct an instance of `MavericksRepository` you have to provide some configuration parameters, you can do that by: | ||
|
||
1. providing instance of `MavericksRepositoryConfig` | ||
```kotlin | ||
class WeatherForecastRepository( | ||
scope: CoroutineScope, | ||
private val api: WeatherApi, | ||
) : MavericksRepository<WeatherForecastState>( | ||
MavericksRepositoryConfig(...) | ||
``` | ||
|
||
2. or via constructor arguments | ||
```kotlin | ||
class WeatherForecastRepository( | ||
scope: CoroutineScope, | ||
private val api: WeatherApi, | ||
) : MavericksRepository<WeatherForecastState>( | ||
initialState = WeatherForecastState(), | ||
coroutineScope = scope, | ||
performCorrectnessValidations = BuildConfig.DEBUG, | ||
) | ||
``` | ||
|
||
**Note:** `performCorrectnessValidations` should be enabled in debug build only as it applies runtime checks to ensure the repository is used correctly. | ||
To avoid extra overhead this flag should be disabled in production build. | ||
|
||
Checkout out [integrate Mavericks into your app](/debug-checks) or docs for [MavericksRepositoryConfig](https://github.com/airbnb/mavericks/blob/main/mvrx-core/src/main/kotlin/com/airbnb/mvrx/MavericksRepositoryConfig.kt) for more info. |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id 'java-library' | ||
id 'org.jetbrains.kotlin.jvm' | ||
} | ||
|
||
tasks.withType(KotlinCompile).all { | ||
kotlinOptions { | ||
freeCompilerArgs += [ | ||
'-Xopt-in=kotlin.RequiresOptIn', | ||
'-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi', | ||
'-Xopt-in=com.airbnb.mvrx.InternalMavericksApi', | ||
'-Xopt-in=com.airbnb.mvrx.ExperimentalMavericksApi', | ||
] | ||
} | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
dependencies { | ||
api Libraries.kotlinCoroutines | ||
|
||
testImplementation TestLibraries.junit | ||
testImplementation TestLibraries.kotlinCoroutinesTest | ||
} |
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,4 @@ | ||
POM_NAME=Mavericks | ||
POM_ARTIFACT_ID=mavericks-core | ||
POM_PACKAGING=jar | ||
GROUP=com.airbnb.android |
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
9 changes: 9 additions & 0 deletions
9
mvrx-core/src/main/java/com/airbnb/mvrx/ExperimentalMavericksApi.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,9 @@ | ||
package com.airbnb.mvrx | ||
|
||
/** | ||
* Marks declarations that are still experimental in Mavericks API. | ||
* Marked declarations are subject to change their semantics or behaviours, that not backward compatible. | ||
*/ | ||
@Retention(value = AnnotationRetention.BINARY) | ||
@RequiresOptIn(level = RequiresOptIn.Level.WARNING) | ||
annotation class ExperimentalMavericksApi |
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
mvrx-core/src/main/java/com/airbnb/mvrx/MavericksBlockExecutions.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,19 @@ | ||
package com.airbnb.mvrx | ||
|
||
/** | ||
* Defines whether a [MavericksRepository.execute] invocation should not be run. | ||
*/ | ||
enum class MavericksBlockExecutions { | ||
/** Run the execute block normally. */ | ||
No, | ||
|
||
/** Block the execute call from having an impact. */ | ||
Completely, | ||
|
||
/** | ||
* Block the execute call from having an impact from values returned by the object | ||
* being executed, but perform one state callback to set the Async property to loading | ||
* as if the call is actually happening. | ||
*/ | ||
WithLoading | ||
} |
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
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.
seems like a good solution 👍