Skip to content

Commit

Permalink
feat: NonNullLiveData
Browse files Browse the repository at this point in the history
  • Loading branch information
percula committed Oct 3, 2019
1 parent 414e524 commit d3fb8a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions library/src/main/java/dev/percula/ktx/MutableNonNullLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.percula.ktx

/**
* A MutableLiveData that enforces the generic type's nullability
*/
class MutableNonNullLiveData<T>(initialValue: T) : NonNullLiveData<T>(initialValue) {
public override fun setValue(value: T) {
super.setValue(value)
}
}
21 changes: 21 additions & 0 deletions library/src/main/java/dev/percula/ktx/NonNullLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.percula.ktx

import androidx.lifecycle.LiveData

/**
* A LiveData that enforces the generic type's nullability
*/
open class NonNullLiveData<T>(private val initialValue: T): LiveData<T>() {

init {
value = initialValue
}

/**
* If for some reason the value is null, just return the initial value
*/
override fun getValue(): T {
return super.getValue() ?: initialValue
}

}

0 comments on commit d3fb8a1

Please sign in to comment.