Skip to content

Commit

Permalink
feat(ime): add delete all database beans menu action
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 22, 2022
1 parent 054da09 commit a34a9c3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ object CollectionHelper : CoroutineScope by CoroutineScope(SupervisorJob() + Dis

suspend fun delete(id: Int) = cltDao.delete(id)

suspend fun deleteAll() = cltDao.deleteAll()
suspend fun deleteAll(skipPinned: Boolean = true) {
if (skipPinned)
cltDao.deleteAllUnpinned()
else
cltDao.deleteAll()
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/FlexibleAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FlexibleAdapter(
b1.pinned && !b2.pinned -> -1
// 如果 b1 没置顶而 b2 置顶,则 b1 比 b2 大,排后面
!b1.pinned && b2.pinned -> 1
// 如果都置顶了或都没置顶,则比较 id,id 小的排前面
// 如果都置顶了或都没置顶,则比较 id,id 大的排前面
else -> b2.id.compareTo(b1.id)
}
}
Expand Down Expand Up @@ -138,6 +138,17 @@ class FlexibleAdapter(
true
}
}
if (beans.isNotEmpty()) {
add(R.string.delete_all).apply {
setIcon(R.drawable.ic_baseline_delete_sweep_24)
setOnMenuItemClickListener {
scope.launch {
listener.onDeleteAll()
}
true
}
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
menu.setForceShowIcon(true)
Expand Down Expand Up @@ -174,6 +185,8 @@ class FlexibleAdapter(
suspend fun onUnpin(bean: DatabaseBean)
suspend fun onDelete(bean: DatabaseBean)

suspend fun onDeleteAll()

val showCollectButton: Boolean
}

Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.osfans.trime.ime.symbol

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import androidx.core.view.setPadding
Expand Down Expand Up @@ -256,6 +257,39 @@ class LiquidKeyboard(private val context: Context) {
}
}

// FIXME: 这个方法可能实现得比较粗糙,需要日后改进
@SuppressLint("NotifyDataSetChanged")
override suspend fun onDeleteAll() {
if (beans.all { it.pinned }) {
// 如果没有未置顶的条目,则删除所有已置顶的条目
when (type) {
SymbolKeyboardType.CLIPBOARD -> ClipboardHelper.deleteAll(false)
SymbolKeyboardType.COLLECTION -> CollectionHelper.deleteAll(false)
SymbolKeyboardType.DRAFT -> DraftHelper.deleteAll(false)
else -> return
}
updateBeans(emptyList())
} else {
// 如果有已置顶的条目,则删除所有未置顶的条目
when (type) {
SymbolKeyboardType.CLIPBOARD -> {
ClipboardHelper.deleteAll()
updateBeans(ClipboardHelper.getAll())
}
SymbolKeyboardType.COLLECTION -> {
CollectionHelper.deleteAll()
updateBeans(CollectionHelper.getAll())
}
SymbolKeyboardType.DRAFT -> {
DraftHelper.deleteAll()
updateBeans(DraftHelper.getAll())
}
else -> return
}
}
notifyDataSetChanged()
}

override val showCollectButton: Boolean = type != SymbolKeyboardType.COLLECTION
})
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_delete_sweep_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:alpha="0.8" android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15,16h4v2h-4zM15,8h7v2h-7zM15,12h6v2h-6zM3,18c0,1.1 0.9,2 2,2h6c1.1,0 2,-0.9 2,-2L13,8L3,8v10zM14,5h-3l-1,-1L6,4L5,5L2,5v2h12z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@
<string name="other_collection">收藏夹</string>
<string name="simple_key_unpin">取消置顶</string>
<string name="simple_key_pin">置顶</string>
<string name="delete_all">删除全部</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,5 @@
<string name="other_collection">收藏夾</string>
<string name="simple_key_unpin">取消置頂</string>
<string name="simple_key_pin">置頂</string>
<string name="delete_all">刪除全部</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,5 @@
<string name="other_collection">Collection</string>
<string name="simple_key_unpin">Unpin</string>
<string name="simple_key_pin">Pin</string>
<string name="delete_all">Delete all</string>
</resources>

0 comments on commit a34a9c3

Please sign in to comment.