Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
KunMinX committed Sep 14, 2023
1 parent e7beeb5 commit b9d42f3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

 
Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

 
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +23,7 @@ public class MviDispatcher<T> extends ViewModel implements DefaultLifecycleObser
private final HashMap<Integer, LifecycleOwner> mOwner = new HashMap<>();
private final HashMap<Integer, LifecycleOwner> mFragmentOwner = new HashMap<>();
private final HashMap<Integer, Observer<T>> mObservers = new HashMap<>();
private final FixedLengthList<MutableResult<T>> mResults = new FixedLengthList<>();
private final FixedLengthList<OneTimeMessage<T>> mResults = new FixedLengthList<>();

protected int initQueueMaxLength() {
return DEFAULT_QUEUE_LENGTH;
Expand All @@ -45,7 +45,7 @@ public final void output(@NonNull Fragment fragment, @NonNull Observer<T> observ
private void outputTo(Integer identityId, LifecycleOwner owner, Observer<T> observer) {
this.mOwner.put(identityId, owner);
this.mObservers.put(identityId, observer);
for (MutableResult<T> result : mResults) {
for (OneTimeMessage<T> result : mResults) {
result.observe(owner, observer);
}
}
Expand All @@ -58,16 +58,16 @@ protected final void sendResult(@NonNull T intent) {
}
});
boolean eventExist = false;
for (MutableResult<T> result : mResults) {
int id1 = System.identityHashCode(result.getValue());
for (OneTimeMessage<T> result : mResults) {
int id1 = System.identityHashCode(result.get());
int id2 = System.identityHashCode(intent);
if (id1 == id2) {
eventExist = true;
break;
}
}
if (!eventExist) {
MutableResult<T> result = new MutableResult<>(intent);
OneTimeMessage<T> result = new OneTimeMessage<>(intent);
for (Map.Entry<Integer, Observer<T>> entry : mObservers.entrySet()) {
Integer key = entry.getKey();
Observer<T> observer = entry.getValue();
Expand All @@ -78,16 +78,16 @@ protected final void sendResult(@NonNull T intent) {
mResults.add(result);
}

MutableResult<T> result = null;
for (MutableResult<T> r : mResults) {
int id1 = System.identityHashCode(r.getValue());
OneTimeMessage<T> result = null;
for (OneTimeMessage<T> 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) {
Expand All @@ -107,7 +107,7 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
Integer key = entry.getKey();
mOwner.remove(key);
if (isFragment) mFragmentOwner.remove(key);
for (MutableResult<T> mutableResult : mResults) {
for (OneTimeMessage<T> mutableResult : mResults) {
mutableResult.removeObserver(Objects.requireNonNull(mObservers.get(key)));
}
mObservers.remove(key);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +18,7 @@
* Create by KunMinX at 2022/8/16
*/

public abstract class Result<T> {
public class OneTimeMessage<T> {

private static final int START_VERSION = -1;
private static final Object NOT_SET = new Object();
Expand All @@ -35,16 +33,11 @@ public abstract class Result<T> {
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;
Expand Down Expand Up @@ -100,61 +93,28 @@ public void removeObserver(@NonNull final Observer<? super T> observer) {
}

@MainThread
public void removeObservers(@NonNull final LifecycleOwner owner) {
for (Map.Entry<Observer<? super T>, 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);
}

@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;
mActiveCount += 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;
}
Expand Down

0 comments on commit b9d42f3

Please sign in to comment.