diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 59cd447..8fb4f67 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,6 +1,7 @@ # Changelog ## [Development] +- LiveDataAction implementation. - ChangeLog updated for Bintray Download. - ChangeLog format fixed. diff --git a/README.MD b/README.MD index 58ebaad..02ffae9 100644 --- a/README.MD +++ b/README.MD @@ -21,6 +21,10 @@ 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. + Usage ----- @@ -57,8 +61,7 @@ Usage * **`nonNullObserve`** - LiveData that buffers all items it observes and replays them to any `Observer` that subscribes. - + observe method which eliminates nullable invocation of observers. ```kotlin val liveData = MutableLiveData() @@ -73,6 +76,24 @@ Usage } ``` + * **`LiveDataAction`** + + Object Type which enables invocation listener methods without parameter + + ```kotlin + + val liveData = MutableLiveData() + ... + + viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataAction) + + ... + + fun onLiveDataEvent(){ + //Do something really good!. + } + + ``` Download -------- diff --git a/src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt b/src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt index 85ccb6e..103ae81 100644 --- a/src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt +++ b/src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt @@ -1,10 +1,12 @@ package me.ibrahimyilmaz.arch_data +import androidx.annotation.MainThread import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer +object LiveDataAction fun mutableLiveDataOf(): MutableLiveData = MutableLiveData() @@ -17,3 +19,25 @@ fun LiveData.nonNullObserve(owner: LifecycleOwner, observer: (t: T) -> Un it?.let(observer) }) } + +fun LiveData.observe(owner: LifecycleOwner, observer: () -> Unit) { + this.observe(owner, Observer { + observer() + }) +} + +fun LiveData.nonNullObserve(owner: LifecycleOwner, observer: () -> Unit) { + this.observe(owner, Observer { + it?.let { observer() } + }) +} + + +fun MutableLiveData.postValue() { + this.postValue(LiveDataAction) +} + +@MainThread +fun MutableLiveData.sendValue() { + this.value = LiveDataAction +} \ No newline at end of file diff --git a/src/test/java/me/ibrahimyilmaz/arch_data/LiveDataExtensionsTest.kt b/src/test/java/me/ibrahimyilmaz/arch_data/LiveDataExtensionsTest.kt new file mode 100644 index 0000000..a5cce54 --- /dev/null +++ b/src/test/java/me/ibrahimyilmaz/arch_data/LiveDataExtensionsTest.kt @@ -0,0 +1,93 @@ +package me.ibrahimyilmaz.arch_data + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.MutableLiveData +import com.nhaarman.mockitokotlin2.any +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.times +import com.nhaarman.mockitokotlin2.verify +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TestRule +import org.junit.runner.RunWith +import org.mockito.junit.MockitoJUnitRunner + +@RunWith(MockitoJUnitRunner::class) +class LiveDataExtensionsTest { + + @get:Rule + var rule: TestRule = InstantTaskExecutorRule() + + private val lifecycleOwner = TestingLifeCycleOwner("lifecycleOwner") + + @Test + fun `nonNullObserve should fire method with non null parameter when it is fired with non null data `() { + //GIVEN + val testData = "Test" + val liveData = MutableLiveData() + val nonNullObserver = mock<(t: String) -> Unit>() + liveData.nonNullObserve(lifecycleOwner, nonNullObserver) + + //WHEN + liveData.value = testData + + //THEN + verify(nonNullObserver).invoke(testData) + } + + @Test + fun `nonNullObserve should not fire method with non null parameter when it is fired with null data `() { + //GIVEN + val liveData = MutableLiveData() + val nonNullObserver = mock<(t: String) -> Unit>() + liveData.nonNullObserve(lifecycleOwner, nonNullObserver) + + //WHEN + liveData.value = null + + //THEN + verify(nonNullObserver, times(0)).invoke(any()) + } + + @Test + fun `LiveData with LiveDataAction should fire observer without parameter`() { + //GIVEN + val liveData = MutableLiveData() + val observerWithoutParameter = mock<() -> Unit>() + liveData.observe(lifecycleOwner, observerWithoutParameter) + + //WHEN + liveData.value = null + + //THEN + verify(observerWithoutParameter).invoke() + } + + @Test + fun `LiveData with LiveDataAction should fire observer with nonNull value when it is observed by nonNullObserver`() { + //GIVEN + val liveData = MutableLiveData() + val observerWithoutParameter = mock<() -> Unit>() + liveData.nonNullObserve(lifecycleOwner, observerWithoutParameter) + + //WHEN + liveData.postValue() + + //THEN + verify(observerWithoutParameter).invoke() + } + + @Test + fun `LiveData with LiveDataAction should not fire observer with null value when it is observed by nonNullObserver`() { + //GIVEN + val liveData = MutableLiveData() + val observerWithoutParameter = mock<() -> Unit>() + liveData.nonNullObserve(lifecycleOwner, observerWithoutParameter) + + //WHEN + liveData.postValue(null) + + //THEN + verify(observerWithoutParameter, times(0)).invoke() + } +} \ No newline at end of file