gpt4 book ai didi

android - 如何使用 Java(不是 Kotlin)在 Android 中使用 RxJava 管理状态

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:28:01 25 4
gpt4 key购买 nike

我正在尝试根据 Jake Wharton 的以下演讲开发 Android 应用程序

The State of Managing State with RxJava
21 March 2017 – Devoxx (San Jose, CA, USA)

Jake promise 了我无法找到的第 2 部分和/或 GITHUB 示例(如果确实存在的话)

在高层次上,我可以理解/理解上述大部分内容。

但是我有以下问题。

我可以看到如何使用 UiEvent、UiModel、Action 和 Result 来分离关注点。

我感到困惑的是以下内容:-

幻灯片 194 上的图表显示了 Observable 的“流/流”

Android Device -----> Observable<UiEvent> -----> <application code> -----> Observable<Action>  -----> {Backend}
{Backend} -----> Observable<Result> -----> <application code> -----> Observable<UiModel> -----> Android Device

幻灯片 210 包含此代码 fragment ,展示了如何将结果流“扫描”到 UiModel 中

SubmitUiModel initialState = SubmitUiModel.idle();
Observable<Result> results = /* ... */;
Observable<SubmitUiModel> uiModels = results.scan(initialState, (state, result) -> {
if (result == CheckNameResult.IN_FLIGHT
|| result == SubmitResult.IN_FLIGHT)
return SubmitUiModel.inProgress();
if (result == CheckNameResult.SUCCESS)
return SubmitUiModel.idle();
if (result == SubmitResult.SUCCESS)
return SubmitUiModel.success();
// TODO handle check name and submit failures...
throw new IllegalArgumentException("Unknown result: " + result);
});

幻灯片 215 上的最终代码 fragment ,代码 fragment 类似于:-

ObservableTransformer<SubmitAction, SubmitResult> submit =
actions -> actions.flatMap(action -> service.setName(action.name)
.map(response -> SubmitResult.SUCCESS)
.onErrorReturn(t -> SubmitResult.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(SubmitResult.IN_FLIGHT));

ObservableTransformer<CheckNameAction, CheckNameResult> checkName =
actions -> actions.switchMap(action -> action
.delay(200, MILLISECONDS, AndroidSchedulers.mainThread())
.flatMap(action -> service.checkName(action.name))
.map(response -> CheckNameResult.SUCCESS)
.onErrorReturn(t -> CheckNameResult.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(CheckNameResult.IN_FLIGHT));

说明了从 Action(s) 到 Result(s) 的转换

关于如何将 UiEvent/UiModel 与 Action/Result 流相结合的演讲/幻灯片我错过了什么?

流由 UiEvents 驱动您如何完成从 UiEvent(s) 到 Action 再到 Result 然后最后是 UiModel 的流程?

更新使用 Star Wars API 我采取了以下方法我使用我的 UI 事件通过操作驱动 UI 事件到结果之间的转换,然后扫描结果以映射回 UI 模型。

这是我的类和代码:-

ACTION CLASSES
==============

public abstract class Action<T> {

Api service = Service.instance();

final T data;

public Action(final T data) {
this.data = data;
}

public T getData() {
return data;
}

public abstract Observable<Response<String>> execute();
}


public class CheckCharacterAction extends Action<String> {

public CheckCharacterAction(final String characterName) {
super(characterName);
}

@Override
public Observable<Response<String>> execute() {
return service.peopleSearch(getData());
}
}

public class CheckFilmAction extends Action<String> {
public CheckFilmAction(final String filmTitle) {
super(filmTitle);
}

@Override
public Observable<Response<String>> execute() {
return service.filmSearch(getData());
}
}

public class SearchAction extends Action<String> {
public SearchAction(final String search) {
super(search);
}

@Override
public Observable<Response<String>> execute() {
return service.filmSearch(getData());
}
}

EVENT CLASSES
=============
public abstract class UiEvent<T> {

private final T data;

public UiEvent(final T data) {
this.data = data;
}

public T getData() {
return data;
}
}

public class CharacterUiEvent extends UiEvent<String> {
public CharacterUiEvent(final String name) {
super(name);
}
}

public class FilmUiEvent extends UiEvent<String> {
public FilmUiEvent(final String title) {
super(title);
}
}

public class SearchUiEvent extends UiEvent<String> {
public SearchUiEvent(final String data) {
super(data);
}
}

UI MODEL CLASSES
================
public class UiModel<T> {

public final boolean isProgress;
public final String message;
public final boolean isSuccess;
public T data;

public UiModel(final boolean isProgress) {
this.isProgress = isProgress;
this.message = null;
this.isSuccess = false;
this.data = null;
}

public UiModel(final T data) {
this.isProgress = false;
this.message = null;
this.isSuccess = true;
this.data = data;
}

public UiModel(final String message) {
this.isProgress = false;
this.message = message;
this.isSuccess = false;
this.data = null;
}

public UiModel(final boolean isProgress, final String message, final boolean isSuccess, final T data) {
this.isProgress = isProgress;
this.message = message;
this.isSuccess = isSuccess;
this.data = data;
}
}

public class CharacterUiModel extends UiModel<JsonData> {

public CharacterUiModel(final boolean isProgress) {
super(isProgress);
}

public CharacterUiModel(final JsonData data) {
super(data);
}

public CharacterUiModel(final String message) {
super(message);
}

public CharacterUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}


public static CharacterUiModel inProgress() {
return new CharacterUiModel(true);
}

public static CharacterUiModel success(final JsonData data) {
return new CharacterUiModel(data);
}

public static CharacterUiModel failure(final String message) {
return new CharacterUiModel(message);
}

}

public class FilmUiModel extends UiModel<JsonData> {


public FilmUiModel(final boolean isProgress) {
super(isProgress);
}

public FilmUiModel(final JsonData data) {
super(data);
}

public FilmUiModel(final String message) {
super(message);
}

public FilmUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}


public static FilmUiModel inProgress() {
return new FilmUiModel(true);
}

public static FilmUiModel success(final JsonData data) {
return new FilmUiModel(data);
}

public static FilmUiModel failure(final String message) {
return new FilmUiModel(message);
}

}

public class SearchUiModel extends UiModel<JsonData> {

private SearchUiModel(final boolean isProgress) {
super(isProgress);
}

private SearchUiModel(final JsonData data) {
super(data);
}

private SearchUiModel(final String message) {
super(message);
}

private SearchUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}

public static SearchUiModel idle() {
return new SearchUiModel(false, null, false, null);
}

public static SearchUiModel inProgress() {
return new SearchUiModel(true);
}

public static SearchUiModel success(final JsonData data) {
return new SearchUiModel(data);
}

public static SearchUiModel failure(final String message) {
return new SearchUiModel(message);
}
}


RESULT CLASSES
==============

public abstract class Result<T> {

public enum LIFECYCLE {
DEPARTURE_LOUNGE,
IN_FLIGHT,
LANDED_SAFELY,
CRASHED_BURNED
}

final LIFECYCLE lifecycle;
final T data;
final String errorMessage;

public Result(final LIFECYCLE lifecycle, final T data, final String errorMessage) {
this.lifecycle = lifecycle;
this.data = data;
this.errorMessage = errorMessage;
}

public T getData() {
return data;
}

public String getErrorMessage() {
return errorMessage;
}

public LIFECYCLE getLifecycle() {
return lifecycle;
}
}

public class CharacterResult extends Result<JsonData> {

private CharacterResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}

private CharacterResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}

public static CharacterResult departureLounge() {
return new CharacterResult(LIFECYCLE.DEPARTURE_LOUNGE);
}

public static CharacterResult inflight() {
return new CharacterResult(LIFECYCLE.IN_FLIGHT);
}

public static CharacterResult landedSafely(final JsonData data) {
return new CharacterResult(LIFECYCLE.LANDED_SAFELY, data, null);
}

public static CharacterResult crashedBurned(final String errorMessage) {
return new CharacterResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}


public class FilmResult extends Result<JsonData> {

private FilmResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}

private FilmResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}

public static FilmResult departureLounge() {
return new FilmResult(LIFECYCLE.DEPARTURE_LOUNGE);
}

public static FilmResult inflight() {
return new FilmResult(LIFECYCLE.IN_FLIGHT);
}

public static FilmResult landedSafely(final JsonData data) {
return new FilmResult(LIFECYCLE.LANDED_SAFELY, data, null);
}

public static FilmResult crashedBurned(final String errorMessage) {
return new FilmResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}

public class SearchResult extends Result<JsonData> {

private SearchResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}

private SearchResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}

public static SearchResult departureLounge() {
return new SearchResult(LIFECYCLE.DEPARTURE_LOUNGE);
}

public static SearchResult inflight() {
return new SearchResult(LIFECYCLE.IN_FLIGHT);
}

public static SearchResult landedSafely(final JsonData data) {
return new SearchResult(LIFECYCLE.LANDED_SAFELY, data, null);
}

public static SearchResult crashedBurned(final String errorMessage) {
return new SearchResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}

然后我从我的 Activity onCreate() 方法中设置我的 Rx Streams:-

   final Observable<SearchUiEvent> searchEvents = RxView.clicks(activityMainBinding.searchButton)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.map(ignored -> new SearchUiEvent(activityMainBinding.filmTitle.getText().toString()));

final Observable<FilmUiEvent> filmEvents = RxTextView.afterTextChangeEvents(activityMainBinding.filmTitle)
.skipInitialValue()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.delay(1000, MILLISECONDS, AndroidSchedulers.mainThread())
.map(text -> new FilmUiEvent(text.view().getText().toString()));

final Observable<CharacterUiEvent> characterEvents = RxTextView.afterTextChangeEvents(activityMainBinding.people)
.skipInitialValue()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.delay(200, MILLISECONDS, AndroidSchedulers.mainThread())
.map(text -> new CharacterUiEvent(text.view().getText().toString()));

/**
*
*/
final Observable<UiEvent> uiEvents = Observable.merge(searchEvents, filmEvents, characterEvents);

/*********
*
*/

final ObservableTransformer<SearchUiEvent, SearchResult> searchAction =
events -> events.flatMap(event -> new SearchAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> SearchResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> SearchResult.crashedBurned(throwable.getMessage()))
.startWith(SearchResult.inflight());

final ObservableTransformer<FilmUiEvent, FilmResult> filmAction =
events -> events.flatMap(event -> new CheckFilmAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> FilmResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> FilmResult.crashedBurned(throwable.getMessage()))
.startWith(FilmResult.inflight());

final ObservableTransformer<CharacterUiEvent, CharacterResult> characterAction =
events -> events.flatMap(event -> new CheckCharacterAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> CharacterResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> CharacterResult.crashedBurned(throwable.getMessage()))
.startWith(CharacterResult.inflight());

final ObservableTransformer<UiEvent, ? extends Result> whatever = events -> events.publish(shared -> Observable.merge(
shared.ofType(SearchUiEvent.class).compose(searchAction),
shared.ofType(CharacterUiEvent.class).compose(characterAction),
shared.ofType(FilmUiEvent.class).compose(filmAction)));

/**
*
*/
final UiModel initialState = SearchUiModel.idle();

final Observable<? extends Result> results = uiEvents.compose(whatever).doOnSubscribe(COMPOSITE_DISPOSABLE::add);

final Observable<UiModel> models = results.scan(initialState, (state, result) -> {
Log.e(TAG, "scan() state = " + state + " result = " + result);
if (result.getLifecycle().equals(SearchResult.LIFECYCLE.DEPARTURE_LOUNGE) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.DEPARTURE_LOUNGE) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.DEPARTURE_LOUNGE)) {
return SearchUiModel.idle();
}

if (result.getLifecycle().equals(SearchResult.LIFECYCLE.IN_FLIGHT) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.IN_FLIGHT) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.IN_FLIGHT)) {
return SearchUiModel.inProgress();
}

if (result.getLifecycle().equals(SearchResult.LIFECYCLE.LANDED_SAFELY) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.LANDED_SAFELY) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.LANDED_SAFELY)) {
return SearchUiModel.success((JsonData) result.getData());
}

if (result.getLifecycle().equals(SearchResult.LIFECYCLE.CRASHED_BURNED) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.CRASHED_BURNED) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.CRASHED_BURNED)) {
return SearchUiModel.failure(result.getErrorMessage());
}


return null;

});

models.doOnSubscribe(COMPOSITE_DISPOSABLE::add).subscribe(model -> report(model), throwable -> error(throwable));

一旦我的 Activity 显示,我就会收到以下日志:-

2018-10-09 14:22:33.310 D/MainActivity: report() called with: model = [UiModel{isProgress=false, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.311 E/MainActivity: scan() state = UiModel{isProgress=false, message='null', isSuccess=false, data=null} result = SearchResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.311 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.313 E/MainActivity: scan() state = UiModel{isProgress=true, message='null', isSuccess=false, data=null} result = CharacterResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.313 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.313 E/MainActivity: scan() state = UiModel{isProgress=true, message='null', isSuccess=false, data=null} result = FilmResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.313 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]

我猜我得到这些 IN FLIGHT 结果是因为我的 .startWith() 语句。

当我单击搜索按钮或在 EditText View 中输入任何文本时,我会看到以下日志:-

2018-10-09 14:55:19.463 E/MainActivity: scan() state = UiModel{isProgress=false, message='null', isSuccess=true, data=com.test.model.JsonData@5e0b6f1} result = FilmResult{lifecycle=LANDED_SAFELY, data=com.test.model.JsonData@8ae4d86, errorMessage='null'}
2018-10-09 14:55:19.463 D/MainActivity: report() called with: model = [UiModel{isProgress=false, message='null', isSuccess=true, data=com.test.model.JsonData@8ae4d86}]

为什么我没有看到“IN FLIGHT”然后是“LANDED SAFELY”?

我只得到“安全着陆”

我在 UI 事件 -> 操作 -> 结果 -> UI 模型之间转换的方法是否与 J Wharton 先生所描述的接近?

我哪里做错了?

更新(二)

我的错误是没有在 .flatmap() 操作中包含我所有的下游 Rx。

澄清

这种 UI 事件 ---> 操作 ---> 结果 ---> UI 模型的模式是否仍然适用于没有“后端”的情况?例如主屏幕可以为用户提供许多选项(按钮)以导航到应用程序中的较低级别屏幕。 UI 事件将是“按钮单击”,UI 模型将返回关联的 Activity 类,以使用 startActivity() 方法调用。

如何将登录屏幕的 UI 输入事件合并为一个 UI 事件流,其中我有两个 EditText 字段(用户名和密码)和一个登录按钮。我希望按钮单击 UI 事件包含输入的用户名和用户密码。如果我使用 RxBinding 来处理 EditTexts 并点击登录按钮,我看不到如何将这三个 Observables 组合到我的 UI 事件流中,并验证 EditTexts 以确保它们输入了数据,然后将这个用户输入的数据传递给我结束登录 API(或者例如 Google 登录)

最佳答案

(我正在添加评论,但它太长了)

我无法帮助 Jake 介绍的会谈等内容。但是关于你的最后一个问题:

Does this pattern of UI Event ---> Action ---> Result ---> UI Model still apply for cases where there is no "Backend" as such?

确实如此,只是后端是您的应用程序状态存储库

在这种架构中,您的应用程序应该只有一个真实位置:后端、本地数据库、两者的组合或适合您的用例的任何解决方案。


考虑到这一点,您的Action 流 应该通过调用后端、将更改发布到数据库或在 sharedSetting 中写入元素来修改状态。同样,您状态的变化应该会触发将结果发送到您的流中。

具体细节取决于您将什么用作应用程序的真实来源。

关于android - 如何使用 Java(不是 Kotlin)在 Android 中使用 RxJava 管理状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52705186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com