diff --git a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/block/BlockViewModel.kt b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/block/BlockViewModel.kt index 9408c643..0d962520 100644 --- a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/block/BlockViewModel.kt +++ b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/block/BlockViewModel.kt @@ -43,6 +43,9 @@ class BlockViewModel : BaseListViewModel() { // 刷新 refresh() + + // 刷新屏蔽用户 + UserHelper.refreshBlockUser() } ) } diff --git a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/ui/UiConfigActivity.kt b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/ui/UiConfigActivity.kt index 280c35a3..07dde0b8 100644 --- a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/ui/UiConfigActivity.kt +++ b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/setting/ui/UiConfigActivity.kt @@ -33,6 +33,8 @@ class UiConfigActivity : BaseBindingActivity() { AppUtils.relaunchApp(true) } }) + binding.settingFilterDelete.bindBoolean(this, ConfigHelper::isFilterDeleteComment) + binding.settingBreakUp.bindBoolean(this, ConfigHelper::isFilterBreakUpComment) } override fun initListener() { diff --git a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/user/UserViewModel.kt b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/user/UserViewModel.kt index ba86f565..3d74c8da 100644 --- a/app/src/main/java/com/xiaoyv/bangumi/ui/feature/user/UserViewModel.kt +++ b/app/src/main/java/com/xiaoyv/bangumi/ui/feature/user/UserViewModel.kt @@ -99,6 +99,9 @@ class UserViewModel : BaseViewModel() { withContext(Dispatchers.IO) { val referer = BgmApiManager.buildReferer(BgmPathType.TYPE_FRIEND, userId) BgmApiManager.bgmWebApi.postIgnoreUser(referer, userId, requireGh) + + // 刷新屏蔽用户缓存 + UserHelper.refreshBlockUser() } onActionResult.value = true UserHelper.notifyActionChange(BgmPathType.TYPE_USER) diff --git a/app/src/main/res/layout/activity_setting_ui.xml b/app/src/main/res/layout/activity_setting_ui.xml index f7fd3555..a222e397 100644 --- a/app/src/main/res/layout/activity_setting_ui.xml +++ b/app/src/main/res/layout/activity_setting_ui.xml @@ -78,6 +78,24 @@ android:clickable="true" android:focusable="true" android:text="动态主题" /> + + + + \ No newline at end of file diff --git a/lib-common/src/main/java/com/xiaoyv/common/api/parser/impl/CommentParser.kt b/lib-common/src/main/java/com/xiaoyv/common/api/parser/impl/CommentParser.kt index 2f97690d..1e733544 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/api/parser/impl/CommentParser.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/api/parser/impl/CommentParser.kt @@ -1,5 +1,6 @@ package com.xiaoyv.common.api.parser.impl +import com.xiaoyv.blueprint.kts.toJson import com.xiaoyv.common.api.parser.entity.CommentFormEntity import com.xiaoyv.common.api.parser.entity.CommentTreeEntity import com.xiaoyv.common.api.parser.fetchStyleBackgroundUrl @@ -9,6 +10,9 @@ import com.xiaoyv.common.api.parser.parseCount import com.xiaoyv.common.api.parser.parserFormHash import com.xiaoyv.common.api.parser.parserLikeParam import com.xiaoyv.common.api.parser.replaceSmiles +import com.xiaoyv.common.helper.ConfigHelper +import com.xiaoyv.common.helper.UserHelper +import com.xiaoyv.common.kts.debugLog import org.jsoup.nodes.Element import org.jsoup.select.Elements @@ -21,7 +25,19 @@ import org.jsoup.select.Elements fun Element.parserBottomComment(): List { // 解析 gh val gh = parserFormHash() - return select("#comment_list > div").mapCommentItems(gh) + val filterDeleteComment = ConfigHelper.isFilterDeleteComment + val isFilterBreakUpComment = ConfigHelper.isFilterBreakUpComment + val blockUsers = UserHelper.blockUsers + + debugLog { "绝交用户过滤:${isFilterBreakUpComment}:" + blockUsers.toJson(true) } + + // 解析评论 + return select("#comment_list > div").mapCommentItems( + gh, + filterDeleteComment, + isFilterBreakUpComment, + blockUsers + ) } /** @@ -38,9 +54,20 @@ fun Element.parserReplyForm(): CommentFormEntity { /** * 解析评论 + * + * @param gh 表单 + * @param filterDeleteComment 是否过滤删除的评论 + * @param filterBlockUserComment 是否过滤屏蔽用户评论 + * @param blockUsers 屏蔽的用户 */ -private fun Elements.mapCommentItems(gh: String): List { +private fun Elements.mapCommentItems( + gh: String, + filterDeleteComment: Boolean, + filterBlockUserComment: Boolean, + blockUsers: List, +): List { val entities = arrayListOf() + forEach { item -> if (item.id().isBlank()) return@forEach @@ -48,7 +75,7 @@ private fun Elements.mapCommentItems(gh: String): List { val topicSubReply = item.select(".topic_sub_reply").remove() if (topicSubReply.isNotEmpty()) { entity.topicSubReply = topicSubReply.select(".topic_sub_reply > div") - .mapCommentItems(gh) + .mapCommentItems(gh, filterDeleteComment, filterBlockUserComment, blockUsers) } entity.id = item.attr("id").parseCount().toString() entity.emojiParam = item.select(".like_dropdown").parserLikeParam() @@ -68,6 +95,17 @@ private fun Elements.mapCommentItems(gh: String): List { .ifEmpty { item.select(".inner > .cmt_sub_content") } .html().replaceSmiles() entity.gh = gh + + // 过滤删除数据 + if (filterDeleteComment && entity.replyContent.contains("删除了回复")) { + return@forEach + } + + // 过滤绝交用户数据 + if (filterBlockUserComment && blockUsers.contains(entity.userId)) { + debugLog { "过滤绝交用户(${entity.id})评论:" + entity.replyContent } + return@forEach + } entities.add(entity) } return entities diff --git a/lib-common/src/main/java/com/xiaoyv/common/helper/ActionHelper.kt b/lib-common/src/main/java/com/xiaoyv/common/helper/ActionHelper.kt index f417a96c..ee09109c 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/helper/ActionHelper.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/helper/ActionHelper.kt @@ -70,6 +70,8 @@ fun FragmentActivity.ignoreUser( .apply { require(status.equals("ok", true)) { "绝交失败" } } + // 刷新屏蔽用户缓存 + UserHelper.refreshBlockUser() } showToastCompat("绝交成功") } diff --git a/lib-common/src/main/java/com/xiaoyv/common/helper/CacheHelper.kt b/lib-common/src/main/java/com/xiaoyv/common/helper/CacheHelper.kt index d26c32b7..b2dfb598 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/helper/CacheHelper.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/helper/CacheHelper.kt @@ -43,10 +43,16 @@ object CacheHelper { }.getOrNull().orEmpty() } + /** + * 缓存翻译结果 + */ fun saveTranslate(cacheKey: String, text: String) { CacheDiskUtils.getInstance().put(cacheKey, text) } + /** + * 读取翻译缓存 + */ fun readTranslate(cacheKey: String): String { return CacheDiskUtils.getInstance().getString(cacheKey).orEmpty() } diff --git a/lib-common/src/main/java/com/xiaoyv/common/helper/ConfigHelper.kt b/lib-common/src/main/java/com/xiaoyv/common/helper/ConfigHelper.kt index 81a18d47..23cd41a5 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/helper/ConfigHelper.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/helper/ConfigHelper.kt @@ -21,6 +21,8 @@ object ConfigHelper { private const val KEY_GRID_ANIMATION = "grid-animation" private const val KEY_TIMELINE_TYPE = "timeline" private const val KEY_DYNAMIC_THEME = "dynamic-theme" + private const val KEY_FILTER_DELETE_COMMENT = "filter-delete-comment" + private const val KEY_FILTER_BREAK_UP_COMMENT = "filter-break-up-comment" private val KEY_VERSION_TIP get() = "version-tip-" + AppUtils.getAppVersionCode() @@ -100,4 +102,18 @@ object ConfigHelper { var isDynamicTheme: Boolean get() = SPStaticUtils.getBoolean(KEY_DYNAMIC_THEME, true) set(value) = SPStaticUtils.put(KEY_DYNAMIC_THEME, value) + + /** + * 是否过滤删除的回复 + */ + var isFilterDeleteComment: Boolean + get() = SPStaticUtils.getBoolean(KEY_FILTER_DELETE_COMMENT, true) + set(value) = SPStaticUtils.put(KEY_FILTER_DELETE_COMMENT, value) + + /** + * 是否过滤绝交者的回复 + */ + var isFilterBreakUpComment: Boolean + get() = SPStaticUtils.getBoolean(KEY_FILTER_BREAK_UP_COMMENT, true) + set(value) = SPStaticUtils.put(KEY_FILTER_BREAK_UP_COMMENT, value) } \ No newline at end of file diff --git a/lib-common/src/main/java/com/xiaoyv/common/helper/UserHelper.kt b/lib-common/src/main/java/com/xiaoyv/common/helper/UserHelper.kt index 0ed5807b..18403a34 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/helper/UserHelper.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/helper/UserHelper.kt @@ -12,6 +12,7 @@ import com.xiaoyv.common.api.exception.NeedLoginException import com.xiaoyv.common.api.parser.entity.SettingBaseEntity import com.xiaoyv.common.api.parser.hrefId import com.xiaoyv.common.api.parser.impl.LoginParser.parserCheckIsLogin +import com.xiaoyv.common.api.parser.impl.parserBlockUser import com.xiaoyv.common.api.parser.impl.parserSettingInfo import com.xiaoyv.common.api.parser.parserFormHash import com.xiaoyv.common.api.response.UserEntity @@ -76,10 +77,13 @@ class UserHelper private constructor() { debugLog { "校验缓存用户:有效!" } } + + // 缓存绝交用户 + cacheBreakUserIds() } /** - * 刷新用户信息 + * 通过查询用户设置,刷新用户信息 */ private suspend fun refresh(): List { return withContext(Dispatchers.IO) { @@ -125,6 +129,9 @@ class UserHelper private constructor() { // 更新 onUserInfoLiveData.sendValue(newInfo) userSp.put(KEY_USER_INFO, newInfo.toJson()) + + // 刷新绝交用户 + cacheBreakUserIds() } /** @@ -133,6 +140,7 @@ class UserHelper private constructor() { private fun clearUserInfo(clearEmailAndPassword: Boolean = false) { BgmApiManager.resetCookie() userSp.put(KEY_USER_INFO, "") + userSp.put(KEY_BLOCK_USER, "") // 是否清空账户和密码 if (clearEmailAndPassword) userSp.clear() @@ -140,15 +148,35 @@ class UserHelper private constructor() { onUserInfoLiveData.sendValue(empty) } + private fun cacheBreakUserIds() { + launchProcess(Dispatchers.IO) { + require(isLogin) + val blockUserIds = BgmApiManager.bgmWebApi.queryPrivacy() + .parserBlockUser() + .map { it.id } + + userSp.put(KEY_BLOCK_USER, blockUserIds.toJson()) + } + } + + /** + * 获取全部的绝交用户 + */ + fun breakUsers(): List { + return userSp.getString(KEY_BLOCK_USER).fromJson>().orEmpty() + } + companion object { + private const val NAME = "user" private const val KEY_USER_INFO = "user-info" + private const val KEY_BLOCK_USER = "user-block" private val helper by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { UserHelper() } private val userSp: SPUtils - get() = SPUtils.getInstance("user") + get() = SPUtils.getInstance(NAME) /** * 当前用户 @@ -162,6 +190,12 @@ class UserHelper private constructor() { val isLogin: Boolean get() = !currentUser.isEmpty + /** + * 获取绝交的用户 + */ + val blockUsers: List + get() = helper.breakUsers() + /** * 当前用户的 FromHash */ @@ -223,6 +257,13 @@ class UserHelper private constructor() { return helper.refresh() } + /** + * 刷新屏蔽的用户缓存 + */ + fun refreshBlockUser() { + return helper.cacheBreakUserIds() + } + /** * 缓存用户名和密码 */ diff --git a/lib-common/src/main/java/com/xiaoyv/common/widget/setting/SettingItemView.kt b/lib-common/src/main/java/com/xiaoyv/common/widget/setting/SettingItemView.kt index 2b85c102..93df46ca 100644 --- a/lib-common/src/main/java/com/xiaoyv/common/widget/setting/SettingItemView.kt +++ b/lib-common/src/main/java/com/xiaoyv/common/widget/setting/SettingItemView.kt @@ -44,12 +44,12 @@ class SettingItemView @JvmOverloads constructor( /** * 绑定 Boolean 类型开关 */ - fun bindBoolean( + inline fun bindBoolean( activity: FragmentActivity, property: KMutableProperty0, bindTitle: String? = null, - dialogTip: (Boolean) -> String? = { null }, - onChange: (Boolean) -> Unit = {}, + crossinline dialogTip: (Boolean) -> String? = { null }, + crossinline onChange: (Boolean) -> Unit = {}, ) { if (bindTitle != null) title = bindTitle setOnFastLimitClickListener { @@ -75,7 +75,7 @@ class SettingItemView @JvmOverloads constructor( refresh(property) } - private fun refresh(property: KMutableProperty0) { + fun refresh(property: KMutableProperty0) { desc = if (property.get()) "开启" else "关闭" } } \ No newline at end of file