-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
library/src/main/java/dev/percula/ktx/MutableNonNullLiveData.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,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) | ||
} | ||
} |
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,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 | ||
} | ||
|
||
} |