osfans / trime

同文安卓輸入法平臺3.x/Android-rime/Rime Input Method Engine for Android
http://osfans.github.io/trime/
GNU General Public License v3.0
3.08k stars 372 forks source link

关于long click to delete clipboard item删除功能 #810

Closed ywx closed 2 years ago

ywx commented 2 years ago

很荣幸成为Jul 2, 2022: long click to delete clipboard item 的白老鼠. 等待数日,还是没人报告或提交补丁.

当clipboard列表List中有: A B C D

After long click to delete A. 这时clipboard列表List会马上更新为: B C D D

当你继续long click 或 click 会得到意想不到的结果.试试就知道.

不过退出liquidKeyboard,重新进入就会得到更新,同时你会得到惊喜.

有删除功能是一个好的开端,后面有没有清除clipboard List和清除当前clipboard(置空)的功能,值得期待. 设置停用clipboard or draft并不能清除对应记录(db).重新启用功能,旧的记录仍在.

clipboard List的列表项数clipboardLimit默认值是否太大.很少人会处理那么长的clipboard or draft List.

ywx commented 2 years ago

关于long click to delete clipboard item相应UI更新的问题, 只要在trime\app\src\main\java\com\osfans\trime\ime\symbol\ClipboardAdapter.java public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int index)函数中添加一行代码:

            ClipboardDao.get().delete(list.get(position).getText());
+            list.remove(position);
            notifyItemRemoved(position);
ywx commented 2 years ago

关于清除clipboard List和清除当前clipboard(置空)的功能,可以在LiquidKeyboard注册新命令来实现. 首先,注册新命令 在trime\app\src\main\java\com\osfans\trime\ime\enums\KeyCommandType.kt中添加:

enum class KeyCommandType {
+    NULL, LEFT, RIGHT, EXIT, DEL_LEFT, DEL_RIGHT, UNDO, REDO, CLIPBOARD_CLEAN, CLIPBOARD_CLEAR_ALL, ;

在trime\app\src\main\java\com\osfans\trime\ime\symbol\LiquidKeyboard.java 和 TabView.java中实现功能:

+import android.content.ClipboardManager;
+import com.osfans.trime.data.db.clipboard.ClipboardDao;
+import com.osfans.trime.ime.core.Trime;

LiquidKeyboard.java

public void initFixData(int i) {
...
simpleAdapter.setOnItemClickListener(
...
          } else {
+            position = TabManager.getTagIndex( simpleKeyBeans.get(position).getText() );
            TabTag tag = TabManager.getTag(position);
            if (tag.type == SymbolKeyboardType.NO_KEY) {
              switch (tag.command) {
                case EXIT:
                  Trime.getService().selectLiquidKeyboard(-1);
                  break;
                  // TODO liquidKeyboard中除返回按钮外,其他按键均未实装
                case DEL_LEFT:
                case DEL_RIGHT:
                case REDO:
                case UNDO:
                  break;
+                case CLIPBOARD_CLEAN: {
+                  ClipboardManager cmb = (ClipboardManager) Trime.getService().getSystemService(Context.CLIPBOARD_SERVICE);
+                  cmb.setText("");
+                }
+                  break;
+                case CLIPBOARD_CLEAR_ALL:
+                  ClipboardDao.get().cleanRecord();
+                  break;
              }
            } else if (TabManager.get().isAfterTabSwitch(position)) {

TabView.java

public boolean onTouchEvent(@NonNull MotionEvent me) {
...
          if (tag.type == SymbolKeyboardType.NO_KEY) {
            switch (tag.command) {
              case EXIT:
                Trime.getService().selectLiquidKeyboard(-1);
                break;
                // TODO liquidKeyboard中除返回按钮外,其他按键均未实装
              case DEL_LEFT:
              case DEL_RIGHT:
              case REDO:
              case UNDO:
                break;
+              case CLIPBOARD_CLEAN: {
+                ClipboardManager cmb = (ClipboardManager) Trime.getService().getSystemService(Context.CLIPBOARD_SERVICE);
+                cmb.setText("");
+              }
+                break;
+              case CLIPBOARD_CLEAR_ALL:
+                ClipboardDao.get().cleanRecord();
+                break;
            }
          } else if (System.currentTimeMillis() - time0 < 500) {

在trime\app\src\main\java\com\osfans\trime\data\db\clipboard\ClipboardDao.java中实现功能:

+  /** 清空记录 * */
+  public void cleanRecord() {
+    helper = new DbHelper(Trime.getService(), "clipboard.db");
+    SQLiteDatabase db = helper.getWritableDatabase();
+    db.execSQL("delete from t_data;");
+    db.close();
+  }

为了使新命令出现在Tabs列表中 在trime\app\src\main\java\com\osfans\trime\ime\symbol\TabManager.java中修改

  public List<SimpleKeyBean> getTabSwitchData() {
    if (tabSwitchData.size() > 0) return tabSwitchData;

+    boolean addExit = true;
    for (TabTag tag : tabTags) {
      if (SymbolKeyboardType.Companion.hasKey(tag.type)) {
        tabSwitchData.add(new SimpleKeyBean(tag.text));
      }
+      else { // TABS or NEW_ROW or NO_KEY
+          if (tag.command == KeyCommandType.EXIT) {
+            if (addExit)  tabSwitchData.add(new SimpleKeyBean(tag.text));
+            addExit = false; // Only one
+          }
+          else if (tag.command == KeyCommandType.CLIPBOARD_CLEAN || tag.command == KeyCommandType.CLIPBOARD_CLEAR_ALL) {
+            tabSwitchData.add(new SimpleKeyBean(tag.text));
+          }
+          else if (tag.type == SymbolKeyboardType.NEW_ROW) {
+            tabSwitchData.add(new SimpleKeyBean("")); // TABS中显示时,产生换行效果
+          }
+      }
    }
    return tabSwitchData;
  }

重新compile打包,安装apk

在相应键盘主题...trime.yaml中添加

liquid_keyboard:
...
+  keyboards: [..., clipboard_clean, clipboard_clear, ...]  #tab列表
...
  clipboard:
    type: CLIPBOARD
    name: 剪贴板
+  clipboard_clean:
+    name: 清除剪贴板
+    type: NO_KEY
+    keys: CLIPBOARD_CLEAN
+  clipboard_clear:
+    name: 清空剪贴记录
+    type: NO_KEY
+    keys: CLIPBOARD_CLEAR_ALL
  draft:
    type: DRAFT
    name: 草稿

大致效果: tabs-demo-small

见笑了.

不过大致可实现"Add a button to clear trime clipboard. #739"要求的功能

ywx commented 2 years ago

关于修改clipboard List的列表项数clipboardLimit默认值,根据实际需要修改相应数值.

在trime\app\src\main\java\com\osfans\trime\data\AppPrefs.kt中

...
    class Other(private val prefs: AppPrefs) {
...
        var clipboardLimit: String
            get() = prefs.getPref(CLIPBOARD_LIMIT, "50")
            set(v) = prefs.setPref(CLIPBOARD_LIMIT, v)
        var draftLimit: String
            get() = prefs.getPref(DRAFT_LIMIT, "20")
            set(v) = prefs.setPref(DRAFT_LIMIT, v)

UI在trime\app\src\main\res\values\strings.xml 和 donottranslate.xml 中

    <string-array name="other__clipboard_manager_entries" >
        <item name="0">Disable</item>
        <item name="20">20</item>
        <item name="50">50</item>
        <item name="100">100</item>
        <item name="200">200</item>
        <item name="500">500</item>
        <item name="1000">1000</item>
        <item name="-1">All</item>
    </string-array>

在trime\app\src\main\res\values\donottranslate.xml 中

    <string-array name="other__clipboard_manager_values">
        <item>0</item>
        <item>20</item>
        <item>50</item>
        <item>100</item>
        <item>200</item>
        <item>500</item>
        <item>1000</item>
        <item>-1</item>
    </string-array>

在trime\app\src\main\res\values\values-zh-rCN\strings.xml 中

    <string-array name="other__clipboard_manager_entries" >
        <item name="0">停用</item>
        <item name="20">20</item>
        <item name="50">50</item>
        <item name="100">100</item>
        <item name="200">200</item>
        <item name="500">500</item>
        <item name="1000">1000</item>
        <item name="-1">全部</item>
    </string-array>

在trime\app\src\main\res\values\values-zh-rTW\strings.xml中

    <string-array name="other__clipboard_manager_entries" >
        <item name="0">停用</item>
        <item name="20">20</item>
        <item name="50">50</item>
        <item name="100">100</item>
        <item name="200">200</item>
        <item name="500">500</item>
        <item name="1000">1000</item>
        <item name="-1">全部</item>
    </string-array>
InfinityLoop1308 commented 2 years ago

Thanks for reporting the bug and provide a solution. I also noticed this bug, but I had no time to fix it these days. Now that you have provided the solution, I will make a PR to fix it in a few days.

InfinityLoop1308 commented 2 years ago

I really appreciate your effort adding these new features. No time to test it, but it looks good, except the UI may need some changes. You may make PRs directly or mention other contributors to discuss them if you need further insights.

Bambooin commented 2 years ago

Thanks for your hard work.

Please split your issues make pull requests for code review.

Your contribution are welcome!