Skip to content

Commit

Permalink
#update 切换LiveData为SingleLiveData
Browse files Browse the repository at this point in the history
  • Loading branch information
wumeng1 authored and wumeng1 committed Jun 17, 2022
1 parent 7447814 commit 73be1fb
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 145 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//是否使用本地lib
def useLocalModule = false
def useLocalModule = true
if (!useLocalModule) {
// implementation 'com.github.mirkowu:mvm:$ext.mvm_version' //可以直接依赖总仓库,也可以依赖指定库
implementation "com.github.mirkowu.mvm:lib_base:$ext.mvm_version"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/mirkowu/mvm/ui/mvvm/MVVMMediator.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mirkowu.mvm.ui.mvvm

import androidx.lifecycle.MutableLiveData
import com.mirkowu.lib_base.mediator.BaseMediator
import com.mirkowu.lib_base.util.RxLife
import com.mirkowu.lib_base.util.RxScheduler
Expand All @@ -10,6 +9,7 @@ import com.mirkowu.lib_network.ErrorType
import com.mirkowu.lib_network.state.ResponseData
import com.mirkowu.lib_network.state.ResponseLiveData
import com.mirkowu.lib_util.LogUtil
import com.mirkowu.lib_util.livedata.SingleLiveData
import com.mirkowu.lib_util.utilcode.util.NetworkUtils
import com.mirkowu.mvm.BizModel
import com.mirkowu.mvm.bean.GankBaseBean
Expand All @@ -22,14 +22,14 @@ import io.reactivex.rxjava3.core.Observable
open class MVVMMediator : BaseMediator<IBaseView?, BizModel?>() {
@JvmField
val mImageData = ResponseLiveData<List<RandomImageBean>>()
var mLiveData = MutableLiveData<Any>()
var mError = MutableLiveData<Throwable>()
var mLiveData = SingleLiveData<Any>()
var mError = SingleLiveData<Throwable>()

@JvmField
var mRequestImageListData = MutableLiveData<ResponseData<List<GankImageBean>>>()
var mRequestImageListData = SingleLiveData<ResponseData<List<GankImageBean>>>()

@JvmField
var mImageError = MutableLiveData<ErrorBean>()
var mImageError = SingleLiveData<ErrorBean>()

var mPingResult = ResponseLiveData<Boolean>()

Expand Down
38 changes: 19 additions & 19 deletions lib_base/src/main/java/com/mirkowu/lib_base/event/StateBean.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.mirkowu.lib_base.event;

import java.io.Serializable;

public class StateBean implements Serializable {
private static final long serialVersionUID = 330324159087062791L;
public int state;
public int code;
public String msg;

public StateBean() {
}

public StateBean(int state, int code, String msg) {
this.state = state;
this.code = code;
this.msg = msg;
}
}
//package com.mirkowu.lib_base.event;
//
//import java.io.Serializable;
//
//public class StateBean implements Serializable {
// private static final long serialVersionUID = 330324159087062791L;
// public int state;
// public int code;
// public String msg;
//
// public StateBean() {
// }
//
// public StateBean(int state, int code, String msg) {
// this.state = state;
// this.code = code;
// this.msg = msg;
// }
//}
226 changes: 113 additions & 113 deletions lib_base/src/main/java/com/mirkowu/lib_base/event/UiChangeEvent.java
Original file line number Diff line number Diff line change
@@ -1,127 +1,127 @@
package com.mirkowu.lib_base.event;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.mirkowu.lib_base.view.IBaseView;

public class UiChangeEvent {
private SingleLiveEvent<Boolean> mIsShowLoadingDialogEvent;
private SingleLiveEvent<Boolean> mIsEmptyViewEvent;
private SingleLiveEvent<Boolean> mIsDismissViewEvent;
private SingleLiveEvent<Boolean> mIsReloadViewEvent;
private SingleLiveEvent<String> mJumpPathEvent;
private SingleLiveEvent<Bundle> mJumpPathParamsEvent;
private SingleLiveEvent<StateBean> mStateViewEvent;
//显示 、隐藏
// private SingleLiveEvent<ApiException> mErrorEvent;//加载中、网络错误、服务器异常、(点击刷新)、空数据

public UiChangeEvent() {
}

public MutableLiveData<Boolean> getShowLoadingDialogEvent() {
return this.mIsShowLoadingDialogEvent = this.getLiveData(this.mIsShowLoadingDialogEvent);
}

public MutableLiveData<StateBean> getStateViewEvent() {
return this.mStateViewEvent = this.getLiveData(this.mStateViewEvent);
}

public MutableLiveData<Boolean> getEmptyViewEvent() {
return this.mIsEmptyViewEvent = this.getLiveData(this.mIsEmptyViewEvent);
}

public MutableLiveData<Boolean> getDismissViewEvent() {
return this.mIsDismissViewEvent = this.getLiveData(this.mIsDismissViewEvent);
}

public MutableLiveData<Boolean> getReloadViewEvent() {
return this.mIsReloadViewEvent = this.getLiveData(this.mIsReloadViewEvent);
}

public MutableLiveData<String> getJumpPagePathEvent() {
return this.mJumpPathEvent = this.getLiveData(this.mJumpPathEvent);
}

public MutableLiveData<Bundle> getJumpPageParamsEvent() {
return this.mJumpPathParamsEvent = this.getLiveData(this.mJumpPathParamsEvent);
}

private <T> SingleLiveEvent<T> getLiveData(SingleLiveEvent<T> mutableLiveData) {
if (mutableLiveData == null) {
mutableLiveData = new SingleLiveEvent<>();
}

return mutableLiveData;
}

public void unregisterEvent(@NonNull LifecycleOwner owner) {
getShowLoadingDialogEvent().removeObservers(owner);
getStateViewEvent().removeObservers(owner);
}

public void registerEvent(@NonNull LifecycleOwner owner, final IBaseView iBaseView) {
getShowLoadingDialogEvent().observe(owner, new Observer<Boolean>() {
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
iBaseView.showLoadingDialog();
} else {
iBaseView.hideLoadingDialog();
}
}
});


getStateViewEvent().observe(owner, new Observer<StateBean>() {
public void onChanged(StateBean stateBean) {

//stateBean.state==
// iBaseView.showStateView();


}
});
// getEmptyViewEvent().observe(owner, new Observer<Boolean>() {
// public void onChanged(Boolean aBoolean) {
// if (aBoolean) {
// iBaseView.showEmptyView();
// }
//package com.mirkowu.lib_base.event;
//
// }
// });
//import android.os.Bundle;
//
//import androidx.annotation.NonNull;
//import androidx.lifecycle.LifecycleOwner;
//import androidx.lifecycle.MutableLiveData;
//import androidx.lifecycle.Observer;
//
//import com.mirkowu.lib_base.view.IBaseView;
//
//public class UiChangeEvent {
// private SingleLiveEvent<Boolean> mIsShowLoadingDialogEvent;
// private SingleLiveEvent<Boolean> mIsEmptyViewEvent;
// private SingleLiveEvent<Boolean> mIsDismissViewEvent;
// private SingleLiveEvent<Boolean> mIsReloadViewEvent;
// private SingleLiveEvent<String> mJumpPathEvent;
// private SingleLiveEvent<Bundle> mJumpPathParamsEvent;
// private SingleLiveEvent<StateBean> mStateViewEvent;
// //显示 、隐藏
//// private SingleLiveEvent<ApiException> mErrorEvent;//加载中、网络错误、服务器异常、(点击刷新)、空数据
//
// public UiChangeEvent() {
// }
//
// public MutableLiveData<Boolean> getShowLoadingDialogEvent() {
// return this.mIsShowLoadingDialogEvent = this.getLiveData(this.mIsShowLoadingDialogEvent);
// }
//
// public MutableLiveData<StateBean> getStateViewEvent() {
// return this.mStateViewEvent = this.getLiveData(this.mStateViewEvent);
// }
//
// public MutableLiveData<Boolean> getEmptyViewEvent() {
// return this.mIsEmptyViewEvent = this.getLiveData(this.mIsEmptyViewEvent);
// }
//
// public MutableLiveData<Boolean> getDismissViewEvent() {
// return this.mIsDismissViewEvent = this.getLiveData(this.mIsDismissViewEvent);
// }
//
// getReloadViewEvent().observe(owner, new Observer<Boolean>() {
// public MutableLiveData<Boolean> getReloadViewEvent() {
// return this.mIsReloadViewEvent = this.getLiveData(this.mIsReloadViewEvent);
// }
//
// public MutableLiveData<String> getJumpPagePathEvent() {
// return this.mJumpPathEvent = this.getLiveData(this.mJumpPathEvent);
// }
//
// public MutableLiveData<Bundle> getJumpPageParamsEvent() {
// return this.mJumpPathParamsEvent = this.getLiveData(this.mJumpPathParamsEvent);
// }
//
// private <T> SingleLiveEvent<T> getLiveData(SingleLiveEvent<T> mutableLiveData) {
// if (mutableLiveData == null) {
// mutableLiveData = new SingleLiveEvent<>();
// }
//
// return mutableLiveData;
// }
//
// public void unregisterEvent(@NonNull LifecycleOwner owner) {
// getShowLoadingDialogEvent().removeObservers(owner);
// getStateViewEvent().removeObservers(owner);
// }
//
// public void registerEvent(@NonNull LifecycleOwner owner, final IBaseView iBaseView) {
// getShowLoadingDialogEvent().observe(owner, new Observer<Boolean>() {
// public void onChanged(Boolean aBoolean) {
// if (aBoolean) {
// iBaseView.showReloadView();
// iBaseView.showLoadingDialog();
// } else {
// iBaseView.hideLoadingDialog();
// }
//
// }
// });
//
// getJumpPagePathEvent().observe(owner, new Observer<String>() {
// public void onChanged(String path) {
// iBaseView.jumpPage(path);
// }
// });
//
// getStateViewEvent().observe(owner, new Observer<StateBean>() {
// public void onChanged(StateBean stateBean) {
//
// //stateBean.state==
// // iBaseView.showStateView();
//
// getJumpPageParamsEvent().observe(owner, new Observer<Bundle>() {
// public void onChanged(Bundle paramsMap) {
// if (paramsMap.containsKey("JUMP_PAGE_PATH_KEY")) {
// String path = paramsMap.getString("JUMP_PAGE_PATH_KEY");
// if (!TextUtils.isEmpty(path)) {
// paramsMap.remove("JUMP_PAGE_PATH_KEY");
// iBaseView.jumpPage(path, paramsMap);
// }
// }
//
// }
// });
}
}
//// getEmptyViewEvent().observe(owner, new Observer<Boolean>() {
//// public void onChanged(Boolean aBoolean) {
//// if (aBoolean) {
//// iBaseView.showEmptyView();
//// }
////
//// }
//// });
////
////
//// getReloadViewEvent().observe(owner, new Observer<Boolean>() {
//// public void onChanged(Boolean aBoolean) {
//// if (aBoolean) {
//// iBaseView.showReloadView();
//// }
////
//// }
//// });
////
//// getJumpPagePathEvent().observe(owner, new Observer<String>() {
//// public void onChanged(String path) {
//// iBaseView.jumpPage(path);
//// }
//// });
////
////
//// getJumpPageParamsEvent().observe(owner, new Observer<Bundle>() {
//// public void onChanged(Bundle paramsMap) {
//// if (paramsMap.containsKey("JUMP_PAGE_PATH_KEY")) {
//// String path = paramsMap.getString("JUMP_PAGE_PATH_KEY");
//// if (!TextUtils.isEmpty(path)) {
//// paramsMap.remove("JUMP_PAGE_PATH_KEY");
//// iBaseView.jumpPage(path, paramsMap);
//// }
//// }
////
//// }
//// });
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.mirkowu.lib_network.state

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.mirkowu.lib_network.ErrorBean
import com.mirkowu.lib_util.livedata.SingleLiveData


/**
Expand All @@ -29,4 +29,4 @@ inline fun <T> LiveData<ResponseData<T>>.observerRequest(owner: LifecycleOwner,
/**
* 提供类型别名,减少泛型嵌套
*/
typealias ResponseLiveData<T> = MutableLiveData<ResponseData<T>>
typealias ResponseLiveData<T> = SingleLiveData<ResponseData<T>>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.zxing.BarcodeFormat;
Expand All @@ -38,6 +37,7 @@
import com.king.zxing.manager.AmbientLightManager;
import com.king.zxing.manager.BeepManager;
import com.king.zxing.util.QrLogUtils;
import com.mirkowu.lib_util.livedata.SingleLiveData;

import java.util.concurrent.Executors;

Expand Down Expand Up @@ -83,7 +83,7 @@ public class DefaultCameraScan extends CameraScan {

private View flashlightView;

private MutableLiveData<Result> mResultLiveData;
private SingleLiveData<Result> mResultLiveData;

private OnScanResultCallback mOnScanResultCallback;

Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean onScale(ScaleGestureDetector detector) {
};

private void initData(){
mResultLiveData = new MutableLiveData<>();
mResultLiveData = new SingleLiveData<>();
mResultLiveData.observe(mLifecycleOwner, result -> {
if(result != null){
handleAnalyzeResult(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mirkowu.lib_base.event;
package com.mirkowu.lib_util.livedata;


import android.util.Log;
Expand All @@ -11,7 +11,7 @@

import java.util.concurrent.atomic.AtomicBoolean;

public class SingleLiveEvent<T> extends MutableLiveData<T> {
public class SingleLiveData<T> extends MutableLiveData<T> {

private static final String TAG = "SingleLiveEvent";

Expand Down

0 comments on commit 73be1fb

Please sign in to comment.