神马笔记创建对话笔记

版权声明:个人原创,欢迎转载。 https://blog.csdn.net/chuyangchangxi/article/details/90711500

在《Android实现聊天型笔记编辑器》开发过程中,实现了对话编辑器。

这回的开发目标是将对话编辑器整合到神马笔记中,从而创建一种新形式笔记——对话笔记。

一、目标

整合对话编辑器到神马笔记中,实现新的笔记形式——对话笔记。
在这里插入图片描述

二、开发过程

1. 新的笔记形式

原先的神马笔记只有一种文章笔记,需要在原来的基础上添加新的笔记形式。

因此在RecordEntry添加一个新的字段,以区分不同的笔记形式。

  • style属性
属性值 描述
chat 对话笔记
letter 书信笔记(未来)
article 文章笔记
  • 实现代码
public class RecordEntry extends BaseEntry {

  	...

    @SerializedName("style")
    String style;

  	...
}

2. 创建新的笔记

原先创建笔记,直接创建新的文章笔记。添加对话笔记后,将弹出对话框供用户选择。

  • 效果图
    在这里插入图片描述

  • 实现代码

final void requestCompose() {
  MenuItemClickListener listener = new MenuItemClickListener();

  {
    listener.put(R.id.menu_chat, e ->
                 OfficialHelper.test(this, OfficialHelper.ID_CHAT, () -> { 
                   requestCompose(RecordEntity.STYLE_CHAT);
                 })
                );

    listener.put(R.id.menu_article, e -> {
      requestCompose(RecordEntity.STYLE_ARTICLE);
    });
  }

  {
    PopupMenuDialogFragment dialogFragment = new PopupMenuDialogFragment();
    dialogFragment.setTheme(R.style.DialogDimTheme);
    dialogFragment.setMenuResource(R.menu.menu_create_note);
    dialogFragment.setOnMenuItemClickListener(listener);

    dialogFragment.showNow(getFragmentManager(), "create");
  }

}

3. 保存对话笔记

对话笔记和文章笔记保存在同级目录下。后缀调整为.chat以区分文章笔记。

  • 笔记储存结构
类型 后缀 笔记
对话笔记 {uuid}.chat chat.json
文章笔记 {uuid}.note article.json
  • 实现代码

Message转换为Chat,然后使用Gson保存到Json文件。

public long save() {
  if (readOnly) {
    return 0;
  }

  for (MessageEntity e : list) {
    e.save();
  }

  chat.clear();
  for (MessageEntity entity : list) {
    chat.add(entity.getEntry());
  }

  return getManager().save(this);
}

4. 读取对话笔记

读取过程分为2个过程,从Json文件创建Chat对象,然后转换为Message对象。

  • 创建Chat对象
Chat createChat(String id) {
  Chat ds = null;

  File file = getFile(id, TYPE_CHAT, URI_CHAT);
  if (file.exists()) {
    ds = GsonUtils.fromJson(file, Chat.class, new Pair(ChatEntry.class, this.getDeserializer()));
  }

  if (ds == null) {
    ds = new Chat();
  }

  return ds;
}
  • 转换为Message对象
public Message(Context context, String id, Chat chat) {
  this.context = context;
  this.id = id;
  this.chat = chat;

  final MessageManager.Factory factory = getManager().getFactory();
  this.list = chat.getList().stream()
    .map(e -> factory.create(Message.this, e))
    .collect(Collectors.toList());
}

5. 编辑对话笔记

根据不同的笔记形式,跳转到相对应的编辑器。

private class RequestComposeDelegate extends BaseRequestDelegate {

  RecordEntity entity;

  public RequestComposeDelegate(Fragment f, RecordEntity entity) {
    super(f);

    this.entity = entity;
  }

  @Override
  public boolean request() {
    boolean result = false;

    String id = entity.getId();
    String style = entity.getStyle();
    switch (style) {
      case RecordEntity.STYLE_CHAT: {
        ComposeChatActivity.startForResult(parent, id, requestCode);
        result = true;
        break;
      }
      case RecordEntity.STYLE_ARTICLE: {
        ComposeArticleActivity.startForResult(parent, id, requestCode);
        result = true;
        break;
      }
    }


    return result;
  }

  @Override
  public void accept(Integer resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK || data == null) {
      return;
    }

    String id = data.getStringExtra("id");
    if (TextUtils.isEmpty(id)) {
      return;
    }

    pendingId = Optional.ofNullable(id);
  }
}

6. 保存对话草稿

关闭对话编辑器时,用户输入的内容将自动保存为草稿。不用担心内容丢失。

  • 效果图
    在这里插入图片描述

  • 实现代码

public class Chat extends BaseTable<ChatEntry> {

    @SerializedName("draft")
    String draft;

    public Chat() {

    }

    public String getDraft() {
        return draft;
    }

    public void setDraft(String draft) {
        this.draft = draft;
    }
}

三、技术细节

1. 编辑框滚动到最后位置

对话包含草稿时,需要将草稿内容滚动到末尾。

  • 实现代码
void setDraft(String text) {
  editText.setText(text);

  if (!TextUtils.isEmpty(text)) {
    editText.setSelection(text.length());

    editContainer.post(() -> {
      editContainer.fullScroll(NestedScrollView.FOCUS_DOWN);

      editText.clearFocus();
    });


  }
}

四、开发过程回顾

从定义新的笔记形式开始,到启动对话编辑器加载、编辑、保存笔记,并添加了草稿功能。

最终完整了神马笔记新的笔记形式——对话笔记。

五、接下来

当前的对话笔记只支持2种笔记元素。

  1. 文本
  2. 日期——自动生成

下一版本,将增加另外一种笔记元素——图片。

六、Finally

~应如是知~如是见~如是信解~不生法相~

猜你喜欢

转载自blog.csdn.net/chuyangchangxi/article/details/90711500
今日推荐