Transform LiveData
public class DataModel {
...
public MutableLiveData<List<Repo>> searchRepo(String query) {
final MutableLiveData<List<Repo>> repos = new MutableLiveData<>();
githubService.searchRepos(query)
.enqueue(new Callback<RepoSearchResponse>() {
@Override
public void onResponse(@NonNull Call<RepoSearchResponse> call, @NonNull Response<RepoSearchResponse> response) {
repos.setValue(response.body().getItems());
}
...
});
return repos;
}
}
public class RepoViewModel extends ViewModel {
...
MutableLiveData<List<Repo>> searchRepo(String query) {
// NO!
return dataModel.searchRepo(query);
}
}
public class RepoViewModel extends ViewModel {
...
private final MutableLiveData<String> query = new MutableLiveData<>();
private final LiveData<List<Repo>> repos;
public RepoViewModel(final DataModel dataModel) {
...
repos = Transformations.switchMap(query, new Function<String, LiveData<List<Repo>>>() {
@Override
public LiveData<List<Repo>> apply(String userInput) {
return dataModel.searchRepo(userInput);
}
});
}
...
void searchRepo(String userInput) {
query.setValue(userInput);
}
}
/**
* A LiveData class that has {@code null} value.
*/
public class AbsentLiveData extends LiveData {
private AbsentLiveData() {
postValue(null);
}
public static <T> LiveData<T> create() {
//noinspection unchecked
return new AbsentLiveData();
}
}
repos = Transformations.switchMap(query, new Function<String, LiveData<List<Repo>>>() {
@Override
public LiveData<List<Repo>> apply(String userInput) {
if (TextUtils.isEmpty(userInput)) {
return AbsentLiveData.create();
} else {
return dataModel.searchRepo(userInput);
}
}
});
MutableLiveData userIdLiveData = ...;
LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
repository.getUserById(id)); void setUserId(String userId) {
this.userIdLiveData.setValue(userId);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LiveData userLiveData = ...;
LiveData userName = Transformations.map(userLiveData, user -> {
return user.firstName + " " + user.lastName
});
Transform LiveData的更多相关文章
- CSS3 3D立方体效果-transform也不过如此
CSS3系列已经学习了一段时间了,第一篇文章写了一些css3的奇技淫巧,原文戳这里,还获得了较多网友的支持,在此谢过各位,你们的支持是我写文章最大的动力^_^. 那么这一篇文章呢,主要是通过一个3D立 ...
- 深入node之Transform
Transform流特性 在开发中直接接触Transform流的情况不是很多,往往是使用相对成熟的模块或者封装的API来完成流的处理,最为特殊的莫过于through2模块和gulp流操作.那么,Tra ...
- CSS 3 学习——transform 3D转换渲染
以下内容根据官方规范翻译,没有翻译关于SVG变换的内容和关于矩阵计算的内容. 一般情况下,元素在一个无景深无立体感的平面(flat plane)上渲染,这个平面就是其包含块所处的平面.同时,页面上的其 ...
- CSS 3学习——transform 2D转换
首先声明一点,transform属性不为none的元素是它的定位子元素(绝对定位和固定定位)的包含块,而且对内创建一个新的层叠上下文. 注意:可以通过 transform-box 属性指定元素的那个盒 ...
- Hilbert-Huang Transform(希尔伯特-黄变换)
在我们正式开始讲解Hilbert-Huang Transform之前,不妨先来了解一下这一伟大算法的两位发明人和这一算法的应用领域 Section I 人物简介 希尔伯特:公认的数学界“无冕之王”,1 ...
- 【CSS3动画】transform对文字及图片的旋转、缩放、倾斜和移动
前言:之前我有写过CSS3的transform这一这特性,对于它的用法,还不是很透彻,今天补充补充,呵呵 你懂的,小司机准备开车了. a)再提一提transform的四个属性 ①旋转--->ro ...
- CSS3和javascript中的transform
在javascript中,WebkitTransform 大概相当于 transform .transform 为标准,WebkitTransform 适用于Webkit浏览器.js中的WebkitT ...
- CALayer的transform属性
先来与View比较一下 View:transform -> CGAffineTransformRotate... layer:transform -> CATransform3DRotat ...
- rxjs5.X系列 —— transform系列 api 笔记
欢迎指导与讨论:) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第一篇 -- transform转换.如有错漏,希望大家指出提醒O(∩_∩)O.更详细的资料尽在rxj ...
- NDT(Normal Distribution Transform) 算法(与ICP对比)和一些常见配准算法
原文地址:http://ghx0x0.github.io/2014/12/30/NDT-match/ By GH 发表于 12月 30 2014 目前三维配准中用的较多的是ICP迭代算法,需要提供一个 ...
随机推荐
- langchain中的Document类
在Langchain-Chatchat的上传文档接口(upload_docs)中有个自定义的docs字段,用到了Document类.根据发现指的是from langchain.docstore.d ...
- 【MicroPython】用 c 添加接口 -- 添加 type
[来源]https://www.eemaker.com/micropython-type.html
- 【MicroPython】用 c 添加接口 -- 框架介绍
[来源]https://www.eemaker.com/micropython-c-kuangjia.html
- RSA趣题篇(简单型)
1.n与p的关系 题目 ('n=', 288990088827100766680640490138486855101396196362885475612662192799072729620922966 ...
- 基本操作Linux
基本操作Linux 关机,重启# 关机 shutdown -h now # 重启 shutdown -r now 查看系统,CPU信息# 查看系统内核信息 uname -a # 查看系统内核版本 ca ...
- 在TypeScript项目中搭配Axios封装后端接口调用
前言 本来是想发 next.js 开发笔记的,结果发现里面涉及了太多东西,还是拆分出来发吧~ 本文记录一下在 TypeScript 项目里封装 axios 的过程,之前在开发 StarBlog-Adm ...
- ext4 磁盘扩容
目录 ext4文件系统磁盘扩容 目标 途径 操作步骤 改变前的现状 操作和改变后的状态 ext4文件系统磁盘扩容 一个磁盘有多个分区,分别创建了物理卷.卷组.逻辑卷.通过虚拟机软件对虚拟机的磁盘/de ...
- [转帖]MioIO读/写性能测试
https://www.jianshu.com/p/a0a84f91b16f image.png COSBench是Intel团队基于java开发,是一个测试云对象存储系统的分布式基准测试工具,全 ...
- [转帖]iometer - 性能压力测试工具
<存储工具系列文章>主要介绍存储相关的测试和调试工具,包括不限于dd.fio.vdbench.iozone.iometer.cosbench等性能负载工具,及strace等调试工具. 1 ...
- 【转帖】Linux 调优篇:虚拟化调优(hugepage 大页内存)* 叁
一. 大页(HugePages)概念Hugepage的引入二. hugepages相关概念三.Regular Pages 与 HugePagesa.Regular Pagesb.Huge Pages四 ...