Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Add support for lifecycle-viewmodel-savedstate
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs committed Jun 5, 2019
1 parent 1d7f293 commit 0b4cfdd
Show file tree
Hide file tree
Showing 22 changed files with 399 additions and 24 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/sven.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cache:

deploy:
provider: script
script: ./gradlew :core:bintrayUpload :android:bintrayUpload :androidx-viewmodel:bintrayUpload
script: ./gradlew :core:bintrayUpload :android:bintrayUpload :androidx-viewmodel:bintrayUpload :androidx-viewmodel-savedstate:bintrayUpload
skip_cleanup: true
on:
branch: master
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Version 1.7.0

_2019-06-05_

* Add experimental support for ViewModel injection with [saved state](https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate).
Please use artifact `org.rewedigital.katana:katana-androidx-viewmodel-savedstate:1.7.0-alpha01`.

## Version 1.6.3

_2019-05-16_
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ a repository to your project. Then add the following dependencies:

```gradle
dependencies {
implementation 'org.rewedigital.katana:katana-core:1.6.3'
implementation 'org.rewedigital.katana:katana-core:1.7.0'
// Use this artifact for Katana on Android
implementation 'org.rewedigital.katana:katana-android:1.6.3'
implementation 'org.rewedigital.katana:katana-android:1.7.0'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.rewedigital.katana.android.example.fragment
import org.rewedigital.katana.Module
import org.rewedigital.katana.android.example.fragment.inject.Container
import org.rewedigital.katana.android.example.fragment.model.SecondFragmentViewModel
import org.rewedigital.katana.androidx.viewmodel.viewModel
import org.rewedigital.katana.androidx.viewmodel.savedstate.viewModelSavedState
import org.rewedigital.katana.dsl.compact.factory
import org.rewedigital.katana.dsl.get

Expand All @@ -15,5 +15,5 @@ val secondFragmentModule = Module {

factory { Container(get(SOME_DEPENDENCY), get(FRAGMENT_DEPENDENCY2)) }

viewModel { SecondFragmentViewModel() }
viewModelSavedState { state -> SecondFragmentViewModel(state) }
}
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.rewedigital.katana.android.example.fragment.inject.Container
import org.rewedigital.katana.android.example.fragment.model.SecondFragmentViewModel
import org.rewedigital.katana.android.example.fragment.secondFragmentModule
import org.rewedigital.katana.android.fragment.KatanaFragment
import org.rewedigital.katana.androidx.viewmodel.savedstate.viewModelSavedStateNow
import org.rewedigital.katana.androidx.viewmodel.viewModelNow
import org.rewedigital.katana.injectNow

Expand Down Expand Up @@ -48,7 +49,7 @@ class SecondFragment : KatanaFragment(), KatanaTrait {
component = (activity as KatanaTrait).component + secondFragmentModule

container = injectNow()
viewModel = viewModelNow()
viewModel = viewModelSavedStateNow()

updateMessage()
}
Expand Down
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
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"/>
38 changes: 38 additions & 0 deletions androidx-viewmodel-savedstate/README.md
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>()
}
```
19 changes: 19 additions & 0 deletions androidx-viewmodel-savedstate/build.gradle.kts
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)
}
1 change: 1 addition & 0 deletions androidx-viewmodel-savedstate/proguard-consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Currently Katana does not require any specific ProGuard configuration
2 changes: 2 additions & 0 deletions androidx-viewmodel-savedstate/src/main/AndroidManifest.xml
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"/>
Loading

0 comments on commit 0b4cfdd

Please sign in to comment.