我正在尝试创建一个具有 SignaturePad 的仅发送视图,一旦用户单击保存按钮,就会触发保存意图并进行一些后台处理。
我有以下内容:
主持人:
@Override
protected void bindIntents() {
Observable<SignatureViewState> observable =
intent(SignatureView::saveSignature)
.switchMap(intent -> Observable.fromCallable(() ->
storage.createFile(intent.getFullPath(), intent.getName(), intent.getImage()))
.subscribeOn(Schedulers.from(threadExecutor)))
.map(SignatureViewState.SuccessState::new)
.cast(SignatureViewState.class)
.startWith(new SignatureViewState.LoadingState())
.onErrorReturn(SignatureViewState.ErrorState::new)
.observeOn(postExecutionThread.getScheduler());
subscribeViewState(observable, SignatureView::render);
}
签名片段:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
saveButtonClickObservable = RxView.clicks(saveBtn)
.share()
.map(bla -> true);
compositeDisposable.add(saveButtonClickObservable.subscribe());
...
@Override
public Observable<SaveSignatureIntent> saveSignature() {
Observable<SaveSignatureIntent> saveSignatureIntentObservable =
Observable.just(new SaveSignatureIntent(savePath, bookingId + ".png", null));
Observable<SaveSignatureIntent> saveSignatureIntent =
Observable.zip(signatureBitmapObservable, saveSignatureIntentObservable,
(signature, intent) -> new SaveSignatureIntent(intent.fullPath, intent.name, signature));
return saveButtonClickObservable
.flatMap(bla -> saveSignatureIntent);
}
@Override
public void render(SignatureViewState state) {
if(state instanceof SignatureViewState.LoadingState)
renderLoading();
else if (state instanceof SignatureViewState.SuccessState)
renderSuccess();
if(state instanceof SignatureViewState.ErrorState)
renderError();
}
最后是我的看法:
public interface SignatureView extends MvpView {
Observable<SignatureFragment.SaveSignatureIntent> saveSignature();
void render(SignatureViewState state);
}
问题是,一旦我的片段被创建, .startWith 就会被触发,而无需用户单击按钮。之后,如果用户点击按钮,加载状态永远不会被调用(.startwith again),而只会调用成功(或错误)。我在这里想念什么?
再次感谢 !
编辑:
signatureBitmapObservable = Observable.fromCallable(() -> signaturePad.getTransparentSignatureBitmap(true))
.subscribeOn(Schedulers.io())
.startWith(bla -> renderLoading());
另一个进程正在获取透明位图,但在添加 startWith 之后,我的可调用对象永远不会被调用。如果我把它拿出来,它就像一个魅力。