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

View File

@ -786,13 +786,14 @@ class ForumActivity : BaseActivity(), View.OnClickListener, OnRefreshedListener,
} }
if (firstLoaded) { if (firstLoaded) {
firstLoaded = false firstLoaded = false
HistoryUtil.writeHistory( if (forumName != null) HistoryUtil.saveHistory(
History() History(
.setTitle(getString(R.string.title_forum, forumName)) title = getString(R.string.title_forum, forumName),
.setTimestamp(System.currentTimeMillis()) timestamp = System.currentTimeMillis(),
.setAvatar(forumPageBean.forum?.avatar) data = forumName!!,
.setType(HistoryUtil.TYPE_FORUM) type = HistoryUtil.TYPE_FORUM,
.setData(forumName) avatar = forumPageBean.forum?.avatar,
)
) )
} }
} }

View File

@ -664,16 +664,15 @@ class ThreadActivity : BaseActivity(), View.OnClickListener, IThreadMenuFragment
.setSeeLz(seeLz) .setSeeLz(seeLz)
.toString() .toString()
} }
val history = History() val history = History(
.setData(threadId) title = dataBean!!.thread?.title ?: "",
.setExtras(extras) data = dataBean!!.thread?.id ?: threadId!!,
.setTitle(dataBean!!.thread?.title) type = HistoryUtil.TYPE_THREAD,
.setType(HistoryUtil.TYPE_THREAD) extras = extras,
if (dataBean!!.thread?.author != null) { avatar = dataBean!!.thread?.author?.portrait,
history.avatar = dataBean!!.thread?.author?.portrait username = dataBean!!.thread?.author?.nameShow
history.username = dataBean!!.thread?.author?.nameShow )
} HistoryUtil.saveHistory(history, async)
HistoryUtil.writeHistory(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() val unlikeDialogState = rememberDialogState()
if (forum != null) {
LaunchedEffect(forum) { LaunchedEffect(forum) {
HistoryUtil.writeHistory( if (forum != null) {
History() HistoryUtil.saveHistory(
.setTitle(context.getString(R.string.title_forum, forumName)) History(
.setTimestamp(System.currentTimeMillis()) title = context.getString(R.string.title_forum, forum.name),
.setAvatar(forum.avatar) timestamp = System.currentTimeMillis(),
.setType(HistoryUtil.TYPE_FORUM) avatar = forum.avatar,
.setData(forumName), type = HistoryUtil.TYPE_FORUM,
data = forum.name
),
true true
) )
} }

View File

@ -238,7 +238,12 @@ private fun HistoryItem(
contentDescription = null 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(
text = DateTimeUtils.getRelativeTimeString( text = DateTimeUtils.getRelativeTimeString(

View File

@ -114,7 +114,7 @@ sealed interface HistoryListUiIntent : UiIntent {
data class LoadMore(val page: Int) : HistoryListUiIntent data class LoadMore(val page: Int) : HistoryListUiIntent
data class Delete(val id: Int) : HistoryListUiIntent data class Delete(val id: Long) : HistoryListUiIntent
object DeleteAll : HistoryListUiIntent object DeleteAll : HistoryListUiIntent
} }
@ -189,7 +189,7 @@ sealed interface HistoryListPartialChange : PartialChange<HistoryListUiState> {
} }
data class Success( data class Success(
val id: Int val id: Long
) : Delete() ) : Delete()
data class Failure( data class Failure(

View File

@ -2,7 +2,6 @@ package com.huanchengfly.tieba.post.utils
import com.huanchengfly.tieba.post.models.database.History import com.huanchengfly.tieba.post.models.database.History
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
@ -11,6 +10,7 @@ import org.litepal.LitePal.order
import org.litepal.LitePal.where import org.litepal.LitePal.where
import org.litepal.crud.async.FindMultiExecutor import org.litepal.crud.async.FindMultiExecutor
import org.litepal.extension.find import org.litepal.extension.find
import org.litepal.extension.findFirstAsync
object HistoryUtil { object HistoryUtil {
const val PAGE_SIZE = 100 const val PAGE_SIZE = 100
@ -21,8 +21,12 @@ object HistoryUtil {
} }
@JvmOverloads @JvmOverloads
fun writeHistory(history: History, async: Boolean = false) { fun saveHistory(history: History, async: Boolean = true) {
add(history, async) if (async) {
saveOrUpdateAsync(history)
} else {
saveOrUpdate(history)
}
} }
val all: List<History> val all: List<History>
@ -50,14 +54,13 @@ object HistoryUtil {
type: Int, type: Int,
page: Int page: Int
): Flow<List<History>> { ): Flow<List<History>> {
return flow { return flow<List<History>> {
delay(100)
emit( emit(
where("type = ?", "$type") where("type = ?", "$type")
.order("timestamp desc, count desc") .order("timestamp desc, count desc")
.limit(PAGE_SIZE) .limit(PAGE_SIZE)
.offset(page * 100) .offset(page * 100)
.find<History>() .find()
) )
}.flowOn(Dispatchers.IO) }.flowOn(Dispatchers.IO)
} }
@ -67,27 +70,62 @@ object HistoryUtil {
History::class.java History::class.java
) )
if (historyBean != null) { if (historyBean != null) {
historyBean.setTimestamp(System.currentTimeMillis()) historyBean.copy(
.setTitle(history.title) timestamp = System.currentTimeMillis(),
.setExtras(history.extras) title = history.title,
.setAvatar(history.avatar) extras = history.extras,
.setUsername(history.username) avatar = history.avatar,
.setCount(historyBean.count + 1) username = history.username,
.update(historyBean.id.toLong()) count = historyBean.count + 1
).update(historyBean.id)
return true return true
} }
return false 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)) { if (update(history)) {
return return
} }
history.setCount(1).timestamp = System.currentTimeMillis() val saveHistory = history.copy(count = 1, timestamp = System.currentTimeMillis())
if (async) { if (async) {
history.saveAsync().listen(null) saveHistory.saveAsync().listen(null)
} else { } 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)
}
}
} }
} }
} }