Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.idea/caches/build_file_checksums.ser
  • Loading branch information
apikyanrobert committed Oct 31, 2018
2 parents 241a375 + c2f6f92 commit 054d1b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package robertapikyan.com.lifecyclemvp.lifecycle
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.Observer
import android.os.Handler
import android.os.Looper
import robertapikyan.com.abstractmvp.presentation.view.*
import java.util.*
Expand All @@ -29,6 +30,8 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
private lateinit var viewActionObserver: IViewActionObserver<V>

private val pending = AtomicBoolean(false)
private val dispatching = AtomicBoolean(false)
private val uiHandler by lazy { Handler(Looper.getMainLooper()) }

private val pendingActions: Queue<IViewAction<V>>
by lazy { LinkedList<IViewAction<V>>() }
Expand All @@ -44,7 +47,8 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
val view = viewHolder.getView()
?: throw IllegalStateException("View is null in ViewActionDispatcherLiveData::setViewActionObserver")

if (view !is LifecycleOwner) throw IllegalArgumentException(view::class.java.canonicalName ?: "View"
if (view !is LifecycleOwner) throw IllegalArgumentException(view::class.java.canonicalName
?: "View"
+
" might be implemented by LifecycleOwner activity or fragment")

Expand All @@ -55,10 +59,10 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
it != null) {

if (it !is DisposableViewAction<V>)
viewActionObserver.onInvoke(it)
dispatchToObserver(it)

if (it is DisposableViewAction<V> && !it.isVersionChanged(currentVersion))
viewActionObserver.onInvoke(it)
dispatchToObserver(it)
}
})
}
Expand Down Expand Up @@ -91,6 +95,12 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
super.postValue(value)
}

private fun dispatchToObserver(it: IViewAction<V>) {
dispatching.set(true)
viewActionObserver.onInvoke(it)
dispatching.set(false)
}

private fun performPendingActions() {
if (pendingActions.isEmpty()) return

Expand All @@ -103,7 +113,11 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
}

private fun sendImmediate(viewAction: IViewAction<V>) {
if (!isMainThread()) {
if (dispatching.get()) {
uiHandler.post {
value = viewAction
}
} else if (!isMainThread()) {
postValue(viewAction)
} else {
value = viewAction
Expand All @@ -114,12 +128,16 @@ class ViewActionDispatcherLiveData<V : IView> : LiveData<IViewAction<V>>(),
if (isActive) {
sendImmediate(viewAction)
} else {
lock.lock()
pendingActions.add(viewAction)
lock.unlock()
addPendingAction(viewAction)
}
}

private fun addPendingAction(viewAction: IViewAction<V>) {
lock.lock()
pendingActions.add(viewAction)
lock.unlock()
}

private fun isMainThread() = Looper.getMainLooper().thread == Thread.currentThread()

private class DisposableViewAction<V : IView>(
Expand Down

0 comments on commit 054d1b3

Please sign in to comment.