This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for lifecycle-viewmodel-savedstate
- Loading branch information
1 parent
1d7f293
commit 0b4cfdd
Showing
22 changed files
with
399 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
16 changes: 15 additions & 1 deletion
16
...ain/java/org/rewedigital/katana/android/example/fragment/model/SecondFragmentViewModel.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,5 +1,19 @@ | ||
package org.rewedigital.katana.android.example.fragment.model | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
|
||
data class SecondFragmentViewModel(var message: String? = null) : ViewModel() | ||
data class SecondFragmentViewModel( | ||
private val state: SavedStateHandle | ||
) : ViewModel() { | ||
|
||
var message: String? | ||
get() = state.get(KEY_MESSAGE) | ||
set(value) { | ||
state.set(KEY_MESSAGE, value) | ||
} | ||
|
||
private companion object { | ||
private const val KEY_MESSAGE = "MESSAGE" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest | ||
package="org.rewedigital.katana.android"/> | ||
<manifest package="org.rewedigital.katana.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# AndroidX ViewModel Saved State extensions for Katana | ||
|
||
The extensions declared in this artifact provide support for injection of [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) | ||
with [Saved State module](https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate). Also see | ||
[katana-androidx-viewmodel](../androidx-viewmodel). | ||
|
||
Create a ViewModel binding inside a module: | ||
|
||
```kotlin | ||
data class MyViewModel( | ||
val state: SavedStateHandle, | ||
val someDependency: SomeDependency | ||
) : ViewModel() { | ||
|
||
var someValue: String? | ||
get() = state.get("SOME_VALUE") | ||
set(value) { | ||
state.set("SOME_VALUE", value) | ||
} | ||
} | ||
|
||
Module { | ||
|
||
viewModelSavedState { state -> MyViewModel(state, get()) } | ||
} | ||
``` | ||
|
||
Inject ViewModel in your `Activity` or `Fragment`: | ||
|
||
```kotlin | ||
class MyFragment : Fragment(), | ||
KatanaTrait { | ||
|
||
override val component = Component(...) | ||
|
||
private val viewModel by viewModelSavedState<MyViewModel, MyFragment>() | ||
} | ||
``` |
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 @@ | ||
plugins { | ||
`base-android-plugin` | ||
} | ||
|
||
configureBase( | ||
artifactName = "katana-androidx-viewmodel-savedstate", | ||
sourcePath = android.sourceSets["main"].java.srcDirs, | ||
publicationComponent = components["android"] | ||
) | ||
|
||
version = "1.7.0-alpha01" | ||
|
||
dependencies { | ||
api(project(":core")) | ||
api(project(":androidx-viewmodel")) { | ||
exclude(group = "androidx.lifecycle", module = "lifecycle-viewmodel") | ||
} | ||
api(Dependencies.androidXLifecycleViewModelSavedState) | ||
} |
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 @@ | ||
# Currently Katana does not require any specific ProGuard configuration |
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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="org.rewedigital.katana.androidx.viewmodel.savedstate"/> |
Oops, something went wrong.