使用Mortar + Flow从对话框获取用户输入

时间:2014-05-02 10:32:48

标签: 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;
}
}

此类按预期工作。它使用SavedPage来确定在对话框中显示的内容,并在按下正确的按钮时使用PopupPresenter#onDismissed将用户输入返回到PopupPresenter

我的问题是编写用于呈现对话框的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!
    }
  }
}

正如评论所说,我没有对对话框中显示的SavedPage的引用。它存储在whatToShow的{​​{1}}字段中,但在调用PopupPresenter之前,此字段已被清除。好像我会不必要地重复自己来保留onPopupResult的其他副本。

1 个答案:

答案 0 :(得分:0)

PopupPresenterPopup尚未提供大量文档。我唯一看到的是示例项目中的一个基本示例。他们根据ConfirmerPopup对象中的数据创建Confirmation。 ConfirmerPopup的目的是根据类声明所见的Confirmation对象的标题/主体来捕获用户的布尔决策。

public class ConfirmerPopup implements Popup<Confirmation, Boolean> {

在您的情况下,您希望从用户捕获其他用户输入的文本。调用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;
  }
}  

您的PopupPresenter现在不需要了解Dialog的实现。

new PopupPresenter<SavedPage, SavedPageResults>() {
  @Override protected void onPopupResult(SavedPageResults result) {
    if (result.isPresent()) {
      updateUi(result.getSavedText());  
    }
  }
}