refactor(mvvm): 重构MVVM架构,解耦ViewModel与View绑定
- 移除ViewModel对Activity Context、Lifecycle和DataBinding的直接依赖 - 新增BaseAndroidViewModel基类,通过Application获取Context避免内存泄漏 - 移除RxLifecycle依赖,改用CompositeDisposable统一管理RxJava订阅 - 将initData方法重命名为observeViewModel,明确职责语义 - 新增observeCommonState方法支持loading/error/success通用状态订阅 - 新增BaseMvvmDialogFragment基类支持对话框MVVM模式 - 重构BaseMvvmFragment增加onLoadData和createViewModel扩展点 - 重构BaseMvvmActivity生命周期流程,规范初始化顺序
This commit is contained in:
1 parent
5469505cc6
commit
ca5bde1be0
40 files changed
+1025
-1190
No files matched your search
@@ -19,6 +19,7 @@ import com.arialyy.aria.core.task.DownloadTask;
|
||||
import com.blankj.utilcode.util.ToastUtils;
|
||||
import com.fuying.sn.KeepAliveConnection;
|
||||
import com.fuying.sn.R;
|
||||
import com.fuying.sn.base.BaseService;
|
||||
import com.fuying.sn.gson.GsonUtils;
|
||||
import com.fuying.sn.service.main.MainService;
|
||||
import com.fuying.sn.utils.ApkUtils;
|
||||
@@ -26,8 +27,7 @@ import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
// 下载管理服务
|
||||
public class DownloadService extends Service {
|
||||
public class DownloadService extends BaseService {
|
||||
private static final String TAG = "DownloadService";
|
||||
|
||||
@Override
|
||||
@@ -63,35 +63,6 @@ public class DownloadService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
private void getFile(final Bundle bundle) {
|
||||
String url = bundle.getString("url");
|
||||
|
||||
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
|
||||
path.mkdirs();
|
||||
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
|
||||
|
||||
if (file.exists() && file.isFile()) {
|
||||
// AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
||||
// .setTitle("软件更新")
|
||||
// .setIcon(R.mipmap.ic_launcher)
|
||||
// .setCancelable(false)
|
||||
// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
|
||||
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface dialogInterface, int i) {
|
||||
ApkUtils.installApp(DownloadService.this, file.getAbsolutePath());
|
||||
// dialogInterface.dismiss();
|
||||
// }
|
||||
// });
|
||||
// AlertDialog ad = builder.create();
|
||||
// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||||
// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||||
// ad.show();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void update(final Bundle bundle) {
|
||||
String url = bundle.getString("url");
|
||||
|
||||
@@ -143,12 +114,10 @@ public class DownloadService extends Service {
|
||||
//在这里处理任务完成的状态
|
||||
ApkUtils.installApp(DownloadService.this, task.getFilePath());
|
||||
Log.e("taskComplete", task.getExtendField());
|
||||
// Aria.download(this).load(task.getDownloadEntity().getId()).cancel();
|
||||
}
|
||||
|
||||
@Download.onTaskFail
|
||||
void taskFail(DownloadTask task, Exception e) {
|
||||
// Aria.download(this).resumeAllTask();
|
||||
final String filepath = task.getFilePath();
|
||||
final String packageName = task.getExtendField();
|
||||
try {
|
||||
@@ -158,12 +127,5 @@ public class DownloadService extends Service {
|
||||
}
|
||||
Log.e(TAG, "taskFail: " + packageName + "filepath: " + filepath);
|
||||
Aria.download(this).load(task.getDownloadEntity().getId()).cancel(true);
|
||||
|
||||
// Aria.download(this)
|
||||
// .load(task.getDownloadEntity().getRealUrl()) //读取下载地址
|
||||
// .setFilePath(task.getFilePath())
|
||||
// .ignoreFilePathOccupy()
|
||||
// .setExtendField(task.getExtendField())
|
||||
// .create(); //启动下载}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import android.os.IBinder;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fuying.sn.base.BaseService;
|
||||
import com.fuying.sn.bean.BaseResponse;
|
||||
import com.fuying.sn.network.NetInterfaceManager;
|
||||
import com.fuying.sn.utils.Utils;
|
||||
@@ -32,7 +33,7 @@ import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
public class LogcatService extends Service {
|
||||
public class LogcatService extends BaseService {
|
||||
public final static String LOGCAT_START_ACTION = "START";
|
||||
public final static String LOGCAT_STOP_ACTION = "STOP";
|
||||
private static final String TAG = "LogcatService";
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.widget.TextView;
|
||||
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.fuying.sn.R;
|
||||
import com.fuying.sn.base.BaseService;
|
||||
import com.fuying.sn.bean.BaseResponse;
|
||||
import com.fuying.sn.config.CommonConfig;
|
||||
import com.fuying.sn.gson.GsonUtils;
|
||||
@@ -47,11 +48,7 @@ import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
|
||||
/**
|
||||
* @author jgy02
|
||||
*/
|
||||
public class ManagerService extends Service implements NetworkUtils.OnNetworkStatusChangedListener {
|
||||
public class ManagerService extends BaseService implements NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private static final String TAG = "ManagerService";
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
@@ -565,38 +562,6 @@ public class ManagerService extends Service implements NetworkUtils.OnNetworkSta
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void sendScreenState(int stateCode) {
|
||||
Log.e(TAG, "sendScreenState: code:" + stateCode);
|
||||
// Log.e(TAG, "sendScreenState: sn: " + Utils.getSerial());
|
||||
NetInterfaceManager.getInstance().setScreen()
|
||||
.setScreenState(Utils.getSerial(), stateCode)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("sendScreenState", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(BaseResponse baseResponse) {
|
||||
Log.e("sendScreenState", "onNext: " + baseResponse.msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
// TODO: 2022/7/11 接口本身问题
|
||||
// Log.e("sendScreenState", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("sendScreenState", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ApkInstallReceiver apKinstallReceiver;
|
||||
|
||||
private void registAppReceive() {
|
||||
|
||||
@@ -28,6 +28,7 @@ import androidx.lifecycle.ViewModelStoreOwner;
|
||||
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.base.BaseService;
|
||||
import com.fuying.sn.bean.BaseResponse;
|
||||
import com.fuying.sn.bean.SnInfo;
|
||||
import com.fuying.sn.config.CommonConfig;
|
||||
@@ -60,7 +61,7 @@ import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
|
||||
public class MainService extends LifecycleService implements ViewModelStoreOwner, NetworkUtils.OnNetworkStatusChangedListener {
|
||||
public class MainService extends BaseService implements ViewModelStoreOwner, NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private static final String TAG = "MainService";
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,8 +26,6 @@ import com.fuying.sn.utils.ServiceAliveUtils;
|
||||
import com.fuying.sn.utils.Utils;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.trello.rxlifecycle4.RxLifecycle;
|
||||
import com.trello.rxlifecycle4.android.ActivityEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
Reference in new issue
Block a user