From ce39a5fdf26985e14806dcbbd24937417f2bf4a2 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:54:10 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E4=BC=98=E5=8C=96=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/litepal.xml | 2 +- .../tieba/post/activities/ForumActivity.kt | 15 +-- .../tieba/post/activities/ThreadActivity.kt | 19 ++-- .../tieba/post/models/database/History.java | 91 ------------------- .../tieba/post/models/database/History.kt | 16 ++++ .../tieba/post/ui/page/forum/ForumPage.kt | 19 ++-- .../ui/page/history/list/HistoryListPage.kt | 7 +- .../page/history/list/HistoryListViewModel.kt | 4 +- .../tieba/post/utils/HistoryUtil.kt | 72 +++++++++++---- 9 files changed, 107 insertions(+), 138 deletions(-) delete mode 100644 app/src/main/java/com/huanchengfly/tieba/post/models/database/History.java create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/models/database/History.kt diff --git a/app/src/main/assets/litepal.xml b/app/src/main/assets/litepal.xml index f611974d..2ee086c1 100644 --- a/app/src/main/assets/litepal.xml +++ b/app/src/main/assets/litepal.xml @@ -1,7 +1,7 @@ - + diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/ForumActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/ForumActivity.kt index 12f4c811..d610d76c 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/ForumActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/ForumActivity.kt @@ -786,13 +786,14 @@ class ForumActivity : BaseActivity(), View.OnClickListener, OnRefreshedListener, } if (firstLoaded) { firstLoaded = false - HistoryUtil.writeHistory( - History() - .setTitle(getString(R.string.title_forum, forumName)) - .setTimestamp(System.currentTimeMillis()) - .setAvatar(forumPageBean.forum?.avatar) - .setType(HistoryUtil.TYPE_FORUM) - .setData(forumName) + if (forumName != null) HistoryUtil.saveHistory( + History( + title = getString(R.string.title_forum, forumName), + timestamp = System.currentTimeMillis(), + data = forumName!!, + type = HistoryUtil.TYPE_FORUM, + avatar = forumPageBean.forum?.avatar, + ) ) } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/ThreadActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/ThreadActivity.kt index 095cdbc5..26e2c9f6 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/ThreadActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/ThreadActivity.kt @@ -664,16 +664,15 @@ class ThreadActivity : BaseActivity(), View.OnClickListener, IThreadMenuFragment .setSeeLz(seeLz) .toString() } - val history = History() - .setData(threadId) - .setExtras(extras) - .setTitle(dataBean!!.thread?.title) - .setType(HistoryUtil.TYPE_THREAD) - if (dataBean!!.thread?.author != null) { - history.avatar = dataBean!!.thread?.author?.portrait - history.username = dataBean!!.thread?.author?.nameShow - } - HistoryUtil.writeHistory(history, async) + val history = History( + title = dataBean!!.thread?.title ?: "", + data = dataBean!!.thread?.id ?: threadId!!, + type = HistoryUtil.TYPE_THREAD, + extras = extras, + avatar = dataBean!!.thread?.author?.portrait, + username = dataBean!!.thread?.author?.nameShow + ) + HistoryUtil.saveHistory(history, async) } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.java b/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.java deleted file mode 100644 index adae1a02..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.huanchengfly.tieba.post.models.database; - -import org.litepal.crud.LitePalSupport; - -public class History extends LitePalSupport { - private int id; - private String avatar; - private String username; - private String data; - private String extras; - private String title; - private long timestamp; - private int count; - private int type; - - public String getUsername() { - return username; - } - - public History setUsername(String username) { - this.username = username; - return this; - } - - public String getAvatar() { - return avatar; - } - - public History setAvatar(String avatar) { - this.avatar = avatar; - return this; - } - - public String getExtras() { - return extras; - } - - public History setExtras(String extras) { - this.extras = extras; - return this; - } - - public int getId() { - return id; - } - - public String getData() { - return data; - } - - public History setData(String data) { - this.data = data; - return this; - } - - public String getTitle() { - return title; - } - - public History setTitle(String title) { - this.title = title; - return this; - } - - public long getTimestamp() { - return timestamp; - } - - public History setTimestamp(long timestamp) { - this.timestamp = timestamp; - return this; - } - - public int getCount() { - return count; - } - - public History setCount(int count) { - this.count = count; - return this; - } - - public int getType() { - return type; - } - - public History setType(int type) { - this.type = type; - return this; - } -} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.kt b/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.kt new file mode 100644 index 00000000..dabaeae8 --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/models/database/History.kt @@ -0,0 +1,16 @@ +package com.huanchengfly.tieba.post.models.database + +import org.litepal.crud.LitePalSupport + +data class History( + val title: String = "", + val data: String = "", + val type: Int = 0, + val timestamp: Long = 0, + val count: Int = 0, + val extras: String? = null, + val avatar: String? = null, + val username: String? = null, +) : LitePalSupport() { + val id: Long = 0L +} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt index 6ddb678f..2bd2002c 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt @@ -449,15 +449,16 @@ fun ForumPage( val unlikeDialogState = rememberDialogState() - if (forum != null) { - LaunchedEffect(forum) { - HistoryUtil.writeHistory( - History() - .setTitle(context.getString(R.string.title_forum, forumName)) - .setTimestamp(System.currentTimeMillis()) - .setAvatar(forum.avatar) - .setType(HistoryUtil.TYPE_FORUM) - .setData(forumName), + LaunchedEffect(forum) { + if (forum != null) { + HistoryUtil.saveHistory( + History( + title = context.getString(R.string.title_forum, forum.name), + timestamp = System.currentTimeMillis(), + avatar = forum.avatar, + type = HistoryUtil.TYPE_FORUM, + data = forum.name + ), true ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt index 3a64b476..d7ec6c67 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListPage.kt @@ -238,7 +238,12 @@ private fun HistoryItem( contentDescription = null ) }, - name = { Text(text = if (info.type == HistoryUtil.TYPE_THREAD) info.username else info.title) }, + name = { + Text( + text = (if (info.type == HistoryUtil.TYPE_THREAD) info.username else info.title) + ?: "" + ) + }, ) { Text( text = DateTimeUtils.getRelativeTimeString( diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt index 3c22b014..d33042e8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/history/list/HistoryListViewModel.kt @@ -114,7 +114,7 @@ sealed interface HistoryListUiIntent : UiIntent { data class LoadMore(val page: Int) : HistoryListUiIntent - data class Delete(val id: Int) : HistoryListUiIntent + data class Delete(val id: Long) : HistoryListUiIntent object DeleteAll : HistoryListUiIntent } @@ -189,7 +189,7 @@ sealed interface HistoryListPartialChange : PartialChange { } data class Success( - val id: Int + val id: Long ) : Delete() data class Failure( diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/HistoryUtil.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/HistoryUtil.kt index b4cc0a43..b51f1643 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/HistoryUtil.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/HistoryUtil.kt @@ -2,7 +2,6 @@ package com.huanchengfly.tieba.post.utils import com.huanchengfly.tieba.post.models.database.History import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn @@ -11,6 +10,7 @@ import org.litepal.LitePal.order import org.litepal.LitePal.where import org.litepal.crud.async.FindMultiExecutor import org.litepal.extension.find +import org.litepal.extension.findFirstAsync object HistoryUtil { const val PAGE_SIZE = 100 @@ -21,8 +21,12 @@ object HistoryUtil { } @JvmOverloads - fun writeHistory(history: History, async: Boolean = false) { - add(history, async) + fun saveHistory(history: History, async: Boolean = true) { + if (async) { + saveOrUpdateAsync(history) + } else { + saveOrUpdate(history) + } } val all: List @@ -50,14 +54,13 @@ object HistoryUtil { type: Int, page: Int ): Flow> { - return flow { - delay(100) + return flow> { emit( where("type = ?", "$type") .order("timestamp desc, count desc") .limit(PAGE_SIZE) .offset(page * 100) - .find() + .find() ) }.flowOn(Dispatchers.IO) } @@ -67,27 +70,62 @@ object HistoryUtil { History::class.java ) if (historyBean != null) { - historyBean.setTimestamp(System.currentTimeMillis()) - .setTitle(history.title) - .setExtras(history.extras) - .setAvatar(history.avatar) - .setUsername(history.username) - .setCount(historyBean.count + 1) - .update(historyBean.id.toLong()) + historyBean.copy( + timestamp = System.currentTimeMillis(), + title = history.title, + extras = history.extras, + avatar = history.avatar, + username = history.username, + count = historyBean.count + 1 + ).update(historyBean.id) return true } return false } - private fun add(history: History, async: Boolean = false) { + private fun updateAsync( + data: String, + callback: ((Boolean) -> Unit)? = null + ) { + where("data = ?", data).findFirstAsync() + .listen { + if (it == null) { + callback?.invoke(false) + } else { + it.copy( + timestamp = System.currentTimeMillis(), + count = it.count + 1 + ).updateAsync(it.id).listen { + callback?.invoke(true) + } + } + } + } + + private fun saveOrUpdate(history: History, async: Boolean = false) { if (update(history)) { return } - history.setCount(1).timestamp = System.currentTimeMillis() + val saveHistory = history.copy(count = 1, timestamp = System.currentTimeMillis()) if (async) { - history.saveAsync().listen(null) + saveHistory.saveAsync().listen(null) } else { - history.save() + saveHistory.save() + } + } + + private fun saveOrUpdateAsync( + history: History, + callback: ((Boolean) -> Unit)? = null + ) { + updateAsync(history.data) { success -> + if (!success) { + history.copy(count = 1, timestamp = System.currentTimeMillis()) + .saveAsync() + .listen { + callback?.invoke(it) + } + } } } } \ No newline at end of file