optimize SingleLiveData

androidx
Zhanty 5 years ago
parent 8279f2f191
commit 7682edda28
  1. 61
      lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.java
  2. 36
      lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.kt
  3. 24
      lib_base/src/main/java/com/android/base/kotlin/MenuEx.kt
  4. 35
      lib_social/src/main/java/com/android/sdk/social/wechat/SingleLiveData.java

@ -0,0 +1,61 @@
package com.android.base.app.aac;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.NonNull;
import java.util.Map;
import java.util.WeakHashMap;
import timber.log.Timber;
public class SingleLiveData<T> extends MediatorLiveData<T> {
private int mVersion = 0;
private final Map<Observer<T>, Observer<T>> cache = new WeakHashMap<>();
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
super.observe(owner, getOrNewObserver(observer, mVersion));
}
@Override
public void observeForever(@NonNull Observer<T> observer) {
super.observeForever(getOrNewObserver(observer, mVersion));
}
@Override
public void setValue(T value) {
mVersion++;
super.setValue(value);
}
@Override
public void removeObserver(@NonNull Observer<T> observer) {
Observer<T> wrapper = cache.remove(observer);
Timber.d("removeObserver() called with: observer = [" + observer + "], wrapper = [" + wrapper + "]");
if (wrapper != null) {
super.removeObserver(wrapper);
}
}
private Observer<T> getOrNewObserver(@NonNull Observer<T> observer, int observerVersion) {
Observer<T> wrapper = cache.get(observer);
if (wrapper == null) {
wrapper = t -> {
if (observerVersion < mVersion) {
observer.onChanged(t);
}
};
cache.put(observer, wrapper);
}
Timber.d("getOrNewObserver() called with: observer = [" + observer + "], observerVersion = [" + observerVersion + "], wrapper = [" + wrapper + "]");
return wrapper;
}
}

@ -1,36 +0,0 @@
package com.android.base.app.aac
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.MediatorLiveData
import android.arch.lifecycle.Observer
/** https://github.com/Shopify/livedata-ktx */
class SingleLiveData<T> : MediatorLiveData<T>() {
private var _version = 0
private val version: Int get() = _version
override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
val observerVersion = version
super.observe(owner, Observer {
if (observerVersion < version) {
observer.onChanged(it)
}
})
}
override fun observeForever(observer: Observer<T>) {
val observeSinceVersion = version
super.observeForever {
if (version > observeSinceVersion) {
observer.onChanged(it)
}
}
}
override fun setValue(value: T?) {
_version++
super.setValue(value)
}
}

@ -1,7 +1,31 @@
package com.android.base.kotlin
import android.support.v7.widget.PopupMenu
import android.view.Menu
import android.view.MenuItem
fun PopupMenu.setMenus(items: Array<String>): PopupMenu {
for (i in 0 until items.size) {
menu.add(Menu.NONE, i, i, items[i])
}
return this
}
fun PopupMenu.setMenus(items: Array<String>, ids: IntArray): PopupMenu {
for (i in 0 until items.size) {
menu.add(Menu.NONE, ids[i], i, items[i])
}
return this
}
fun PopupMenu.onMenuItemClick(listener: (id: Int) -> Unit): PopupMenu {
setOnMenuItemClickListener {
listener(it.itemId)
true
}
return this
}
fun MenuItem.alwaysShow(): MenuItem {
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
return this

@ -5,32 +5,36 @@ import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.NonNull;
import java.util.WeakHashMap;
class SingleLiveData<T> extends MediatorLiveData<T> {
private int mVersion = 0;
private final WeakHashMap<Observer<T>, Observer<T>> cache = new WeakHashMap<>();
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
final int observerVersion = mVersion;
super.observe(owner, t -> {
if (observerVersion < mVersion) {
observer.onChanged(t);
}
});
super.observe(owner, getOrNewObserver(observer, mVersion));
}
@Override
public void observeForever(@NonNull Observer<T> observer) {
super.observeForever(getOrNewObserver(observer, mVersion));
}
final int observerVersion = mVersion;
private Observer<T> getOrNewObserver(@NonNull Observer<T> observer, int observerVersion) {
Observer<T> wrapper = cache.get(observer);
super.observeForever(t -> {
if (wrapper == null) {
wrapper = t -> {
if (observerVersion < mVersion) {
observer.onChanged(t);
}
});
};
cache.put(observer, wrapper);
}
return wrapper;
}
@Override
@ -39,4 +43,13 @@ class SingleLiveData<T> extends MediatorLiveData<T> {
super.setValue(value);
}
@Override
public void removeObserver(@NonNull Observer<T> observer) {
Observer<T> wrapper = cache.remove(observer);
if (wrapper != null) {
super.removeObserver(wrapper);
}
}
}
Loading…
Cancel
Save