diff --git a/README.md b/README.md index e35a348..7892807 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ MVI-Dispatcher 应运而生。 在本案例中,我将为您展示,MVI-Dispatcher 是如何将原本 "繁杂易出错" 消息分发流程,通过 **寥寥几行代码** 轻而易举完成。 ```Groovy -implementation 'com.kunminx.arch:mvi-dispatch:7.5.0' +implementation 'com.kunminx.arch:mvi-dispatch:7.6.0' //可选分支,简便安全完成 Config 读写 -implementation 'com.kunminx.arch:keyvalue-dispatch:7.5.0' +implementation 'com.kunminx.arch:keyvalue-dispatch:7.6.0' ```   diff --git a/README_EN.md b/README_EN.md index 67f2924..b867338 100644 --- a/README_EN.md +++ b/README_EN.md @@ -29,7 +29,7 @@ The author has long focused on the "business architecture" pattern and is commit In this case, I will show you how MVI-Dispatcher can easily accomplish the previously complicated and error-prone message distribution process with just a few lines of code. ```Groovy -implementation 'com.kunminx.arch:mvi-dispatch:7.5.0' +implementation 'com.kunminx.arch:mvi-dispatch:7.6.0' ```   diff --git a/build.gradle b/build.gradle index 42dfe09..1afaf92 100644 --- a/build.gradle +++ b/build.gradle @@ -7,8 +7,8 @@ buildscript { ext { appTargetSdk = 32 appMinSdk = 15 - appVersionCode = 7050000 - appVersionName = "7.5.0" + appVersionCode = 7060000 + appVersionName = "7.6.0" lifecycleVersion = "2.4.1" navigationVersion = "2.4.2" } diff --git a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/dispatch/MviDispatcher.java b/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/dispatch/MviDispatcher.java index b47acd9..744df78 100644 --- a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/dispatch/MviDispatcher.java +++ b/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/dispatch/MviDispatcher.java @@ -9,7 +9,7 @@ import androidx.lifecycle.ViewModel; import com.kunminx.architecture.domain.queue.FixedLengthList; -import com.kunminx.architecture.domain.result.MutableResult; +import com.kunminx.architecture.domain.result.OneTimeMessage; import java.util.HashMap; import java.util.Map; @@ -23,7 +23,7 @@ public class MviDispatcher extends ViewModel implements DefaultLifecycleObser private final HashMap mOwner = new HashMap<>(); private final HashMap mFragmentOwner = new HashMap<>(); private final HashMap> mObservers = new HashMap<>(); - private final FixedLengthList> mResults = new FixedLengthList<>(); + private final FixedLengthList> mResults = new FixedLengthList<>(); protected int initQueueMaxLength() { return DEFAULT_QUEUE_LENGTH; @@ -45,7 +45,7 @@ public final void output(@NonNull Fragment fragment, @NonNull Observer observ private void outputTo(Integer identityId, LifecycleOwner owner, Observer observer) { this.mOwner.put(identityId, owner); this.mObservers.put(identityId, observer); - for (MutableResult result : mResults) { + for (OneTimeMessage result : mResults) { result.observe(owner, observer); } } @@ -58,8 +58,8 @@ protected final void sendResult(@NonNull T intent) { } }); boolean eventExist = false; - for (MutableResult result : mResults) { - int id1 = System.identityHashCode(result.getValue()); + for (OneTimeMessage result : mResults) { + int id1 = System.identityHashCode(result.get()); int id2 = System.identityHashCode(intent); if (id1 == id2) { eventExist = true; @@ -67,7 +67,7 @@ protected final void sendResult(@NonNull T intent) { } } if (!eventExist) { - MutableResult result = new MutableResult<>(intent); + OneTimeMessage result = new OneTimeMessage<>(intent); for (Map.Entry> entry : mObservers.entrySet()) { Integer key = entry.getKey(); Observer observer = entry.getValue(); @@ -78,16 +78,16 @@ protected final void sendResult(@NonNull T intent) { mResults.add(result); } - MutableResult result = null; - for (MutableResult r : mResults) { - int id1 = System.identityHashCode(r.getValue()); + OneTimeMessage result = null; + for (OneTimeMessage r : mResults) { + int id1 = System.identityHashCode(r.get()); int id2 = System.identityHashCode(intent); if (id1 == id2) { result = r; break; } } - if (result != null) result.setValue(intent); + if (result != null) result.set(intent); } public final void input(T intent) { @@ -107,7 +107,7 @@ public void onDestroy(@NonNull LifecycleOwner owner) { Integer key = entry.getKey(); mOwner.remove(key); if (isFragment) mFragmentOwner.remove(key); - for (MutableResult mutableResult : mResults) { + for (OneTimeMessage mutableResult : mResults) { mutableResult.removeObserver(Objects.requireNonNull(mObservers.get(key))); } mObservers.remove(key); diff --git a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/MutableResult.java b/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/MutableResult.java deleted file mode 100644 index a934c29..0000000 --- a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/MutableResult.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.kunminx.architecture.domain.result; - -/** - * Create by KunMinX at 2022/8/17 - */ -public class MutableResult extends Result { - - public MutableResult(T value) { - super(value); - } - - public MutableResult() { - super(); - } - - @Override - public void setValue(T value) { - super.setValue(value); - } -} diff --git a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/Result.java b/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/OneTimeMessage.java similarity index 82% rename from mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/Result.java rename to mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/OneTimeMessage.java index 7669593..c2fdca4 100644 --- a/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/Result.java +++ b/mvi-dispatch/src/main/java/com/kunminx/architecture/domain/result/OneTimeMessage.java @@ -3,8 +3,6 @@ import static androidx.lifecycle.Lifecycle.State.DESTROYED; import static androidx.lifecycle.Lifecycle.State.STARTED; -import android.util.Log; - import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -20,7 +18,7 @@ * Create by KunMinX at 2022/8/16 */ -public abstract class Result { +public class OneTimeMessage { private static final int START_VERSION = -1; private static final Object NOT_SET = new Object(); @@ -35,16 +33,11 @@ public abstract class Result { private boolean mDispatchingValue; private boolean mDispatchInvalidated; - public Result(T value) { + public OneTimeMessage(T value) { mData = value; mVersion = START_VERSION + 1; } - public Result() { - mData = NOT_SET; - mVersion = START_VERSION; - } - @SuppressWarnings("unchecked") private void considerNotify(ObserverWrapper observer) { if (!observer.mActive) return; @@ -100,14 +93,7 @@ public void removeObserver(@NonNull final Observer observer) { } @MainThread - public void removeObservers(@NonNull final LifecycleOwner owner) { - for (Map.Entry, ObserverWrapper> entry : mObservers) { - if (entry.getValue().isAttachedTo(owner)) removeObserver(entry.getKey()); - } - } - - @MainThread - protected void setValue(T value) { + public void set(T value) { mVersion++; mData = value; dispatchingValue(null); @@ -115,32 +101,12 @@ protected void setValue(T value) { @SuppressWarnings("unchecked") @Nullable - public T getValue() { + public T get() { Object data = mData; if (data != NOT_SET) return (T) data; return null; } - int getVersion() { - return mVersion; - } - - protected void onActive() { - - } - - protected void onInactive() { - - } - - public boolean hasObservers() { - return mObservers.size() > 0; - } - - public boolean hasActiveObservers() { - return mActiveCount > 0; - } - @MainThread void changeActiveCounter(int change) { int previousActiveCount = mActiveCount; @@ -148,13 +114,7 @@ void changeActiveCounter(int change) { if (mChangingActiveState) return; mChangingActiveState = true; try { - while (previousActiveCount != mActiveCount) { - boolean needToCallActive = previousActiveCount == 0 && mActiveCount > 0; - boolean needToCallInactive = previousActiveCount > 0 && mActiveCount == 0; - previousActiveCount = mActiveCount; - if (needToCallActive) onActive(); - else if (needToCallInactive) onInactive(); - } + while (previousActiveCount != mActiveCount) previousActiveCount = mActiveCount; } finally { mChangingActiveState = false; }