Skip to content

Commit

Permalink
Merge pull request #8 from liip/initial-value
Browse files Browse the repository at this point in the history
Initial value
  • Loading branch information
jschmid authored Jul 26, 2019
2 parents af3f8e5 + 1a4dae2 commit 6992c63
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class MainActivity : AppCompatActivity() {

// Set the value manually during creation
manualText.setText(viewModel.manualText)
manualValue.setText(viewModel.manualText)

// Observe the livedata
viewModel.liveDataText.observe(this, Observer {
liveDataText.setText(it)
liveDataValue.setText(it)
})

// Save the values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ class MainViewModel(handle: SavedStateHandle) : ViewModel() {

// MutableLiveData that is saved in the SavedState
val liveDataText by handle.livedata<String?>()

// Could be used with a default value
// val liveDataText by handle.livedata<String?>(initialValue = "Hello")
}
32 changes: 28 additions & 4 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="@string/editTextHint"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@id/liveDataText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/manualValue"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info" />

Expand All @@ -43,12 +43,12 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="@string/editTextHint"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/liveDataValue"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/manualText" />

Expand All @@ -63,4 +63,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/liveDataText" />

<TextView
android:id="@+id/manualValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/manualText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/manualText"
app:layout_constraintTop_toTopOf="@+id/manualText"
tools:text="TextView" />

<TextView
android:id="@+id/liveDataValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/liveDataText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/liveDataText"
app:layout_constraintTop_toTopOf="@+id/liveDataText"
tools:text="TextView" />

</android.support.constraint.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ inline fun <reified T> SavedStateHandle.delegate(key: String? = null): ReadWrite
}
}

inline fun <reified T> SavedStateHandle.livedata(key: String? = null) = object : ReadOnlyProperty<Any, MutableLiveData<T?>> {
inline fun <reified T> SavedStateHandle.livedata(key: String? = null, initialValue: T? = null) = object : ReadOnlyProperty<Any, MutableLiveData<T?>> {
override fun getValue(thisRef: Any, property: KProperty<*>): MutableLiveData<T?> {
val stateKey = key ?: property.name
return this@livedata.getLiveData(stateKey)
return if (initialValue == null) {
this@livedata.getLiveData(stateKey)
} else {
this@livedata.getLiveData(stateKey, initialValue)
}
}
}

0 comments on commit 6992c63

Please sign in to comment.