Edw*_*ale 5 java android mortar square-flow
我用Mortar + Flow构建我的应用程序.我正在试图找出显示弹出窗口的正确方法,该弹出窗口请求用户提供一些文本.我创建了这个弹出类:
public class SavedPageTitleInputPopup implements Popup<SavedPageTitleInput, Optional<String>> {
private final Context context;
private AlertDialog dialog;
public SavedPageTitleInputPopup(Context context) {
this.context = context;
}
@Override public Context getContext() {
return context;
}
@Override
public void show(final SavedPageTitleInput info, boolean withFlourish,
final PopupPresenter<SavedPageTitleInput, Optional<String>> presenter) {
if (dialog != null) throw new IllegalStateException("Already showing, can't show " + info);
final EditText input = new EditText(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setText(info.savedPage.getName());
dialog = new AlertDialog.Builder(context).setTitle(info.title)
.setView(input)
.setMessage(info.body)
.setPositiveButton(info.confirm, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface d, int which) {
dialog = null;
final String newTitle = Strings.emptyToNull(String.valueOf(input.getText()));
presenter.onDismissed(Optional.fromNullable(newTitle));
}
})
.setNegativeButton(info.cancel, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface d, int which) {
dialog = null;
presenter.onDismissed(Optional.<String>absent());
}
})
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface d) {
dialog = null;
presenter.onDismissed(Optional.<String>absent());
}
})
.show();
}
@Override public boolean isShowing() {
return dialog != null;
}
@Override public void dismiss(boolean withFlourish) {
dialog.dismiss();
dialog = null;
}
}
Run Code Online (Sandbox Code Playgroud)
这个类按预期工作.它使用它SavedPage来确定在对话框中显示的内容,并在按下正确的按钮时将用户输入返回到PopupPresenter使用PopupPresenter#onDismissed.
我的问题是编写PopupPresenter用于呈现对话框并处理输入的子类.这就是我现在所拥有的:
new PopupPresenter<SavedPage, Optional<String>>() {
@Override protected void onPopupResult(Optional<String> result) {
if (result.isPresent()) {
// The user entered something, so update the API
// Oh wait, I don't have a reference to the SavedPage
// that was displayed in the dialog!
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如评论所说,我没有参考SavedPage对话框中显示的内容.它存储在whatToShow字段中PopupPresenter,但是在onPopupResult调用之前该字段被清除.好像我会不必要地重复自己来保留一份额外的副本SavedPage.
目前还没有很多关于PopupPresenter和Popup 的文档。我唯一看到的是示例项目中的基本示例。他们根据Confirmation对象中的数据创建ConfirmerPopup。ConfirmerPopup 的目的是根据类声明中所看到的给予Confirmation 对象的标题/正文来捕获用户的布尔决策。
public class ConfirmerPopup implements Popup<Confirmation, Boolean> {
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您希望捕获用户输入的其他文本。当 PopupPresenter#onPopupResult 被调用时,结果对象应该包含 SavedPageTitleInputPopup 所需的所有数据。修改您的 SavedPageTitleInputPopup 如下
public class SavedPageTitleInputPopup implements Popup<SavedPage, SavedPageResults> {
private final Context context;
private AlertDialog dialog;
public SavedPageTitleInputPopup(Context context) {
this.context = context;
}
@Override public Context getContext() {
return context;
}
@Override
public void show(SavedPage info, boolean withFlourish, final PopupPresenter<SavedPage, SavedPageResults> presenter) {
if (dialog != null) throw new IllegalStateException("Already showing, can't show " + info);
// Create your Dialog but scrape all user data within OnClickListeners
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Anything else you need to do... .setView() or .setTitle() for example
builder.setPositiveButton(info.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
dialog = null;
//Save data to SavedPageResults
final SavedPageResults results = new SavedPageResults():
presenter.onDismissed(results);
}
});
builder.setNegativeButton(info.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
dialog = null;
final SavedPageResults results = new SavedPageResults();
presenter.onDismissed(results);
}
});
dialog = builder.show();
}
@Override public boolean isShowing() {
return dialog != null;
}
@Override public void dismiss(boolean withFlourish) {
dialog.dismiss();
dialog = null;
}
}
Run Code Online (Sandbox Code Playgroud)
您的 PopupPresenter 现在不需要了解有关 Dialog 实现的任何信息。
new PopupPresenter<SavedPage, SavedPageResults>() {
@Override protected void onPopupResult(SavedPageResults result) {
if (result.isPresent()) {
updateUi(result.getSavedText());
}
}
}
Run Code Online (Sandbox Code Playgroud)