Android笔记之ViewModel的使用示例
依赖
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
android.arch.lifecycle:extensions:1.1.1用于引用ViewModelProviders
EyeKeyService.java
package com.bu_ish.viewmodeltest.service; import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; import io.reactivex.Single;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query; public interface EyeKeyService {
String BASE_URL = "http://api.eyekey.com/face/";
String APP_ID = "f89ae61fd63d4a63842277e9144a6bd2";
String APP_KEY = "af1cd33549c54b27ae24aeb041865da2"; @FormUrlEncoded
@POST("Check/checking")
Single<FaceCheckingReply> checkFace(@Field("app_id") String appId, @Field("app_key") String appKey, @Field("url") String url); @GET("Match/match_compare")
Single<FacesComparingReply> compareFaces(@Query("app_id") String appId, @Query("app_key") String appKey, @Query("face_id1") String faceId1, @Query("face_id2") String faceId2);
}
EyeKeyRepository.java
package com.bu_ish.viewmodeltest.view_model; import com.bu_ish.viewmodeltest.service.EyeKeyService;
import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory; public class EyeKeyRepository {
private static EyeKeyRepository instance = new EyeKeyRepository();
private EyeKeyService service; private EyeKeyRepository() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EyeKeyService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
service = retrofit.create(EyeKeyService.class);
} public static EyeKeyRepository getInstance() {
return instance;
} public Single<FaceCheckingReply> checkFace(String url) {
return setScheduler(service.checkFace(EyeKeyService.APP_ID, EyeKeyService.APP_KEY, url));
} public Single<FacesComparingReply> compareFaces(String faceId1, String faceId2) {
return setScheduler(service.compareFaces(EyeKeyService.APP_ID, EyeKeyService.APP_KEY, faceId1, faceId2));
} private Single setScheduler(Single single) {
return single.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
}
EyeKeyViewModel.java
package com.bu_ish.viewmodeltest.view_model; import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel; import com.bu_ish.viewmodeltest.observer.ReplyObserver;
import com.bu_ish.viewmodeltest.reply.FaceCheckingReply;
import com.bu_ish.viewmodeltest.reply.FacesComparingReply; public class EyeKeyViewModel extends ViewModel {
public MutableLiveData<FaceCheckingReply> faceCheckingReply1Data = new MutableLiveData<>();
public MutableLiveData<FaceCheckingReply> faceCheckingReply2Data = new MutableLiveData<>();
public MutableLiveData<FacesComparingReply> facesComparingReplyData = new MutableLiveData<>();
private DialogViewModel dialogViewModel;
private EyeKeyRepository eyeKeyRepository; public EyeKeyViewModel(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
eyeKeyRepository = EyeKeyRepository.getInstance();
} public void checkFace1(String url) {
eyeKeyRepository.checkFace(url).subscribe(new ReplyObserver<FaceCheckingReply>(dialogViewModel) {
@Override
public void onReply(FaceCheckingReply reply) {
faceCheckingReply1Data.setValue(reply);
}
});
} public void checkFace2(String url) {
eyeKeyRepository.checkFace(url).subscribe(new ReplyObserver<FaceCheckingReply>(dialogViewModel) {
@Override
public void onReply(FaceCheckingReply reply) {
faceCheckingReply2Data.setValue(reply);
}
});
} public void compareFaces(String faceId1, String faceId2) {
eyeKeyRepository.compareFaces(faceId1, faceId2).subscribe(new ReplyObserver<FacesComparingReply>(dialogViewModel) {
@Override
public void onReply(FacesComparingReply reply) {
facesComparingReplyData.setValue(reply);
}
});
}
}
ReplyObserver.java
package com.bu_ish.viewmodeltest.observer; import com.bu_ish.viewmodeltest.reply.BaseReply;
import com.bu_ish.viewmodeltest.view_model.DialogViewModel; import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable; public abstract class ReplyObserver<T extends BaseReply> implements SingleObserver<T> {
public abstract void onReply(T reply); private DialogViewModel dialogViewModel; public ReplyObserver(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
} @Override
public void onSubscribe(Disposable d) {
dialogViewModel.showProgressData.setValue(true);
} @Override
public void onSuccess(T reply) {
dialogViewModel.showProgressData.setValue(false);
String code = reply.getResCode();
if (code.equals("0000")) {
onReply(reply);
} else {
dialogViewModel.showErrorData.setValue(reply);
}
} @Override
public void onError(Throwable e) {
dialogViewModel.showProgressData.setValue(false);
dialogViewModel.showExceptionData.setValue(e);
}
}
EyeKeyViewModelFactory.java
package com.bu_ish.viewmodeltest.view_model; import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.support.annotation.NonNull; public class EyeKeyViewModelFactory implements ViewModelProvider.Factory {
private DialogViewModel dialogViewModel; public EyeKeyViewModelFactory(DialogViewModel dialogViewModel) {
this.dialogViewModel = dialogViewModel;
} @NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new EyeKeyViewModel(dialogViewModel);
}
}
DialogViewModel.java
package com.bu_ish.viewmodeltest.view_model; import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel; import com.bu_ish.viewmodeltest.reply.BaseReply; public class DialogViewModel extends ViewModel {
public MutableLiveData<Boolean> showProgressData = new MutableLiveData<>();
public MutableLiveData<BaseReply> showErrorData = new MutableLiveData<>();
public MutableLiveData<Throwable> showExceptionData = new MutableLiveData<>();
}
使用ViewModel的好处
即使发生配置变更(Configuration Change),ViewModel中的数据依然存在
所以,使用ViewModel后,也就不需要考虑onSaveInstanceState这类操作了^_^
参考博文
完整Demo
链接:https://pan.baidu.com/s/1TwY9OZTaQPk1eCn7vSz4fA,提取码:x0fy
Android笔记之ViewModel的使用示例的更多相关文章
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- Android笔记:触摸事件的分析与总结----TouchEvent处理机制
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320 ...
- [置顶]
Xamarin android如何调用百度地图入门示例(一)
在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...
- Android判断当前网络是否可用--示例代码
Android判断当前网络是否可用--示例代码 分类: *07 Android 2011-05-24 13:46 7814人阅读 评论(4) 收藏 举报 网络androiddialogmanagern ...
- Android 笔记之 R 文件
Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...
- Android 笔记之 Android 系统架构
Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...
- Android笔记之使用Glide加载网络图片、下载图片
Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...
- Android笔记--View绘制流程源码分析(二)
Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...
- Android笔记--View绘制流程源码分析(一)
Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...
随机推荐
- 渗透测试集成环境Faraday
渗透测试集成环境Faraday Kali Linux集成了海量的渗透测试工具.但是这些工具在使用的时候,还是分离的.虽然用户可以通过Shell.日志/报告导入导出功能等方式,进行整合,但是仍然不便 ...
- Codeforces 734C [水][暴力][贪心]
题意: 要生产n个物品,每个花费时间为x. 有两种魔法,每种最多使用1个. 其中第一种魔法可以使每个物品生产的花费时间变为ai,相应的花费是bi;第二种魔法可以减少ci个物品,相应的花费是di,并且保 ...
- 方程式组织EQUATION DRUG平台解析(提纲) —方程式组织系列分析报告之四
https://www.bleepingcomputer.com/news/security/shadow-brokers-release-new-files-revealing-windows-ex ...
- 如何在SQLite中创建自增字段
SQLite 简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加. 这里是详细的答案: 从 SQLite 的 2.3.4 版本开始,如果你将一个表中的一个字段声明为 ...
- html5扫面二维码逻辑
写在前面 项目中有这样的需求,在android端嵌入的html5应用中,需要扫描二维码,而一般的浏览器是不允许你调用摄像头的.最后时限方式是由app的webview进行扫描,将扫描结果返回,也就是js ...
- vs2012搭建OpenGL环境
1. 下载glut库 glut库地址为:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut全称为:OpenGL ...
- 2016.11.25 activiti的配置文件解析
参考来自activiti的用户手册. activiti的配置文件解析 1.processEngine的配置 注意,单独创建流程引擎与spring方式创建流程引擎是不一样的,区别在于:process ...
- linux中判断符号[]注意事项
1.中括号[]内的每个组件都需要有空格键来分割: 2.在中括号内的变量,最好都一双引号括号起来: 3.在中括号内的常量,最好都以单引号或双引号括号起来.
- Micro:Bit手柄试用之一MagicPad (解决蓝牙与gamePad包共存)
前言 原创文章,转载引用务必注明链接.由于本人初次接触Micro:Bit,水平有限,如有疏漏,欢迎指正. Micro:Bit真好玩! DFRobot的论坛相关资料算是国内比较丰富的了,个人感觉MB比A ...
- Vue 引入ElementUI 2.0.11:依赖未发现的问题
转自:https://blog.csdn.net/cslucifer/article/details/79019649