ArchData provides two missing LiveData of Android Lifecycle Component and useful extension functions.
MutableLiveData
shares only last state to its new observers when they start to observe.
But in some cases, either these observers may want to do fresh start or they want to get all previous states.
PublishLiveData
and ReplayLiveData
types are extensions of MutableLiveData.
Basically:
PublishLiveData
isMutableLiveData
, which only notify its observers if there is an event after their starting of observation.ReplayLiveData
isMutableLiveData
, which only notify its observers for all previous events happened before their observation starts.
Extension Functions:
nonNullObserve
is extension function, which eliminates nullable invokes of Observer.
LiveDataAction:
LiveDataAction
is special object type for LiveData,which enables observer can listen LiveData object without parameter. It is useful when Action/Command use cases.
-
PublishLiveData
LiveData that, once an
Observer
has subscribed, emits all subsequently observed items to the subscriber.val liveData = PublishLiveData<String>(); // observer1 will receive all events liveData.observe(lifeCycleOwner,observer1) liveData.postValue("one") liveData.postValue("two") // observer2 will only receive "three" liveData.observe(lifeCycleOwner,observer2) liveData.postValue("three")
-
ReplayLiveData
LiveData that buffers all items it observes and replays them to any
Observer
that subscribes.val liveData = ReplayLiveData<String>() liveData.postValue("one") liveData.postValue("two") liveData.postValue("three") // both of the following will get the events from above liveData.observe(lifeCycleOwner,observer1) liveData.postValue(lifeCycleOwner,observer2)
-
nonNullObserve
observe method which eliminates nullable invocation of observers.
val liveData = MutableLiveData<String>() ... viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataEvent) ... fun onLiveDataEvent(event: String){ //Do something good!. }
-
LiveDataAction
Object Type which enables invocation listener methods without parameter
val liveData = MutableLiveData<LiveDataAction>() ... viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataAction) ... fun onLiveDataEvent(){ //Do something really good!. }
Maven:
<dependency>
<groupId>me.ibrahimyilmaz</groupId>
<artifactId>ArchData</artifactId>
<version>0.0.4</version>
<type>pom</type>
</dependency>
Gradle:
compile 'me.ibrahimyilmaz:ArchData:0.0.4'
MIT License
Copyright (c) [2019] [IBRAHIM YILMAZ | [email protected]]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.