Skip to content

Commit

Permalink
[Feat]: 优化绝交功能,过滤屏蔽用户数据
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Dec 21, 2023
1 parent 2c8f799 commit c948fd3
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class BlockViewModel : BaseListViewModel<BlockEntity>() {

// 刷新
refresh()

// 刷新屏蔽用户
UserHelper.refreshBlockUser()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class UiConfigActivity : BaseBindingActivity<ActivitySettingUiBinding>() {
AppUtils.relaunchApp(true)
}
})
binding.settingFilterDelete.bindBoolean(this, ConfigHelper::isFilterDeleteComment)
binding.settingBreakUp.bindBoolean(this, ConfigHelper::isFilterBreakUpComment)
}

override fun initListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_setting_ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@
android:clickable="true"
android:focusable="true"
android:text="动态主题" />

<com.xiaoyv.common.widget.setting.SettingItemView
android:id="@+id/setting_filter_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:text="过滤掉删除的评论或回复" />

<com.xiaoyv.common.widget.setting.SettingItemView
android:id="@+id/setting_break_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:text="过滤掉绝交者评论或回复" />
</androidx.appcompat.widget.LinearLayoutCompat>
</com.xiaoyv.common.widget.scroll.AnimeNestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -21,7 +25,19 @@ import org.jsoup.select.Elements
fun Element.parserBottomComment(): List<CommentTreeEntity> {
// 解析 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
)
}

/**
Expand All @@ -38,17 +54,28 @@ fun Element.parserReplyForm(): CommentFormEntity {

/**
* 解析评论
*
* @param gh 表单
* @param filterDeleteComment 是否过滤删除的评论
* @param filterBlockUserComment 是否过滤屏蔽用户评论
* @param blockUsers 屏蔽的用户
*/
private fun Elements.mapCommentItems(gh: String): List<CommentTreeEntity> {
private fun Elements.mapCommentItems(
gh: String,
filterDeleteComment: Boolean,
filterBlockUserComment: Boolean,
blockUsers: List<String>,
): List<CommentTreeEntity> {
val entities = arrayListOf<CommentTreeEntity>()

forEach { item ->
if (item.id().isBlank()) return@forEach

val entity = CommentTreeEntity()
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()
Expand All @@ -68,6 +95,17 @@ private fun Elements.mapCommentItems(gh: String): List<CommentTreeEntity> {
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ fun FragmentActivity.ignoreUser(
.apply {
require(status.equals("ok", true)) { "绝交失败" }
}
// 刷新屏蔽用户缓存
UserHelper.refreshBlockUser()
}
showToastCompat("绝交成功")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
16 changes: 16 additions & 0 deletions lib-common/src/main/java/com/xiaoyv/common/helper/ConfigHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
45 changes: 43 additions & 2 deletions lib-common/src/main/java/com/xiaoyv/common/helper/UserHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,10 +77,13 @@ class UserHelper private constructor() {

debugLog { "校验缓存用户:有效!" }
}

// 缓存绝交用户
cacheBreakUserIds()
}

/**
* 刷新用户信息
* 通过查询用户设置,刷新用户信息
*/
private suspend fun refresh(): List<SettingBaseEntity> {
return withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -125,6 +129,9 @@ class UserHelper private constructor() {
// 更新
onUserInfoLiveData.sendValue(newInfo)
userSp.put(KEY_USER_INFO, newInfo.toJson())

// 刷新绝交用户
cacheBreakUserIds()
}

/**
Expand All @@ -133,22 +140,43 @@ 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()

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<String> {
return userSp.getString(KEY_BLOCK_USER).fromJson<List<String>>().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)

/**
* 当前用户
Expand All @@ -162,6 +190,12 @@ class UserHelper private constructor() {
val isLogin: Boolean
get() = !currentUser.isEmpty

/**
* 获取绝交的用户
*/
val blockUsers: List<String>
get() = helper.breakUsers()

/**
* 当前用户的 FromHash
*/
Expand Down Expand Up @@ -223,6 +257,13 @@ class UserHelper private constructor() {
return helper.refresh()
}

/**
* 刷新屏蔽的用户缓存
*/
fun refreshBlockUser() {
return helper.cacheBreakUserIds()
}

/**
* 缓存用户名和密码
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class SettingItemView @JvmOverloads constructor(
/**
* 绑定 Boolean 类型开关
*/
fun bindBoolean(
inline fun bindBoolean(
activity: FragmentActivity,
property: KMutableProperty0<Boolean>,
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 {
Expand All @@ -75,7 +75,7 @@ class SettingItemView @JvmOverloads constructor(
refresh(property)
}

private fun refresh(property: KMutableProperty0<Boolean>) {
fun refresh(property: KMutableProperty0<Boolean>) {
desc = if (property.get()) "开启" else "关闭"
}
}

0 comments on commit c948fd3

Please sign in to comment.