pref: 优化历史记录保存

This commit is contained in:
HuanCheng65 2023-07-13 13:54:10 +08:00
parent db88b51b14
commit ce39a5fdf2
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
9 changed files with 107 additions and 138 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="tblite" />
<version value="32" />
<version value="33" />
<list>
<mapping class="com.huanchengfly.tieba.post.models.database.Account" />
<mapping class="com.huanchengfly.tieba.post.models.database.Draft" />

View File

@ -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,
)
)
}
}

View File

@ -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)
}
}

View File

@ -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;
}
}

View File

@ -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
}

View File

@ -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),
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
)
}

View File

@ -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(

View File

@ -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<HistoryListUiState> {
}
data class Success(
val id: Int
val id: Long
) : Delete()
data class Failure(

View File

@ -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<History>
@ -50,14 +54,13 @@ object HistoryUtil {
type: Int,
page: Int
): Flow<List<History>> {
return flow {
delay(100)
return flow<List<History>> {
emit(
where("type = ?", "$type")
.order("timestamp desc, count desc")
.limit(PAGE_SIZE)
.offset(page * 100)
.find<History>()
.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<History?>()
.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)
}
}
}
}
}