Skip to content

Commit cd88a38

Browse files
committed
Fix more content filter logic
1 parent 43ee68d commit cd88a38

File tree

10 files changed

+83
-20
lines changed

10 files changed

+83
-20
lines changed

app/src/main/kotlin/me/tylerbwong/stack/data/content/ContentFilter.kt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class ContentFilter @Inject constructor(
1919
private val siteStore: SiteStore,
2020
@StackSharedPreferences private val preferences: SharedPreferences,
2121
) {
22-
val contentFilteredUpdated: LiveData<Unit>
22+
val contentFilteredUpdated: LiveData<ContentFilterData>
2323
get() = _contentFilterUpdated
24-
private val _contentFilterUpdated = MutableLiveData<Unit>()
24+
private val _contentFilterUpdated = MutableLiveData<ContentFilterData>()
2525

2626
val filteredQuestionIds: Set<Int>
2727
get() = getFilteredContent(QUESTION_ID_CONTENT_FILTER)
@@ -117,15 +117,40 @@ class ContentFilter @Inject constructor(
117117
preferences.edit()
118118
.putStringSet(key, (currentIds + id).map { "${it},${siteStore.site}" }.toSet())
119119
.apply()
120-
_contentFilterUpdated.value = Unit
120+
_contentFilterUpdated.value = ContentFilterData(
121+
filteredQuestionIds = filteredQuestionIds,
122+
filteredAnswerIds = filteredAnswerIds,
123+
filteredCommentIds = filteredCommentIds,
124+
filteredUserIds = filteredUserIds,
125+
)
121126
}
122127
}
123128

124129
private fun clearFilteredContent(key: String) {
125130
preferences.edit()
126131
.putStringSet(key, emptySet())
127132
.apply()
128-
_contentFilterUpdated.value = Unit
133+
_contentFilterUpdated.value = ContentFilterData(
134+
filteredQuestionIds = filteredQuestionIds,
135+
filteredAnswerIds = filteredAnswerIds,
136+
filteredCommentIds = filteredCommentIds,
137+
filteredUserIds = filteredUserIds,
138+
)
139+
}
140+
141+
data class ContentFilterData(
142+
val filteredQuestionIds: Set<Int>,
143+
val filteredAnswerIds: Set<Int>,
144+
val filteredCommentIds: Set<Int>,
145+
val filteredUserIds: Set<Int>,
146+
) {
147+
val isEmpty: Boolean
148+
get() = listOf(
149+
filteredQuestionIds,
150+
filteredAnswerIds,
151+
filteredCommentIds,
152+
filteredUserIds,
153+
).all { it.isEmpty() }
129154
}
130155

131156
companion object {

app/src/main/kotlin/me/tylerbwong/stack/ui/comments/CommentsBottomSheetDialogFragment.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,23 @@ class CommentsBottomSheetDialogFragment : BottomSheetDialogFragment() {
4545
layoutManager = LinearLayoutManager(context)
4646
}
4747
binding.header.title.text = getString(R.string.comments)
48-
viewModel.contentFilteredUpdated.observe(viewLifecycleOwner) {
49-
viewModel.fetchComments()
48+
viewModel.contentFilteredUpdated.observe(viewLifecycleOwner) { filterData ->
49+
if (filterData.isEmpty) {
50+
viewModel.fetchComments()
51+
} else {
52+
viewModel.data.value?.let {
53+
adapter.submitList(
54+
it.filter { item ->
55+
if (item is CommentItem) {
56+
item.comment.commentId !in filterData.filteredCommentIds &&
57+
item.comment.owner.userId !in filterData.filteredUserIds
58+
} else {
59+
true
60+
}
61+
}
62+
)
63+
}
64+
}
5065
}
5166
viewModel.refreshing.observe(viewLifecycleOwner) { isRefreshing ->
5267
if (isRefreshing) {

app/src/main/kotlin/me/tylerbwong/stack/ui/comments/CommentsViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CommentsViewModel @Inject constructor(
3131
get() = _data
3232
private val _data = MutableLiveData<List<DynamicItem>>()
3333

34-
internal val contentFilteredUpdated: LiveData<Unit>
34+
internal val contentFilteredUpdated: LiveData<ContentFilter.ContentFilterData>
3535
get() = contentFilter.contentFilteredUpdated
3636

3737
val errorToast: LiveData<CommentError?>

app/src/main/kotlin/me/tylerbwong/stack/ui/home/HomeFragment.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ class HomeFragment : BaseFragment<HomeFragmentBinding>(
4848
adapter.submitList(null)
4949
viewModel.fetchQuestions()
5050
}
51-
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) {
52-
viewModel.fetchQuestions()
51+
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) { filterData ->
52+
if (filterData.isEmpty) {
53+
viewModel.fetchQuestions()
54+
} else {
55+
viewModel.questions.value?.let {
56+
updateContent(
57+
it.filter { question ->
58+
question.questionId !in filterData.filteredQuestionIds &&
59+
question.owner.userId !in filterData.filteredUserIds
60+
}
61+
)
62+
}
63+
}
5364
}
5465
viewModel.refreshing.observe(viewLifecycleOwner) {
5566
binding.refreshLayout.isRefreshing = it

app/src/main/kotlin/me/tylerbwong/stack/ui/home/HomeViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal class HomeViewModel @Inject constructor(
2626
internal val siteLiveData: LiveData<String>
2727
get() = siteRepository.siteLiveData
2828

29-
internal val contentFilterUpdated: LiveData<Unit>
29+
internal val contentFilterUpdated: LiveData<ContentFilter.ContentFilterData>
3030
get() = contentFilter.contentFilteredUpdated
3131

3232
@Sort

app/src/main/kotlin/me/tylerbwong/stack/ui/profile/ProfileActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ class ProfileActivity : BaseActivity<ActivityProfileBinding>(ActivityProfileBind
3838
}
3939
}
4040

41-
viewModel.contentFilterUpdated.observe(this) {
42-
viewModel.fetchProfileData()
43-
}
44-
4541
viewModel.userData.observe(this) {
4642
binding.toolbar.title = it.displayName.toHtml()
4743
binding.profileHeader.setContent {

app/src/main/kotlin/me/tylerbwong/stack/ui/profile/ProfilePageFragment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ProfilePageFragment : BaseFragment<ProfilePageFragmentBinding>(
3636
viewModel.refreshing.observe(viewLifecycleOwner) {
3737
binding.refreshLayout.isRefreshing = it
3838
}
39+
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) {
40+
if (viewModel.userId !in it.filteredUserIds) {
41+
viewModel.fetchProfileData()
42+
}
43+
}
3944
viewModel.data.observe(viewLifecycleOwner) {
4045
adapter.submitList(it)
4146
}

app/src/main/kotlin/me/tylerbwong/stack/ui/profile/ProfileViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ProfileViewModel @Inject constructor(
3535
get() = _data
3636
private val _data = MutableLiveData<List<DynamicItem>>()
3737

38-
internal val contentFilterUpdated: LiveData<Unit>
38+
internal val contentFilterUpdated: LiveData<ContentFilter.ContentFilterData>
3939
get() = contentFilter.contentFilteredUpdated
4040

4141
internal fun fetchUserData() {

app/src/main/kotlin/me/tylerbwong/stack/ui/questions/detail/QuestionDetailActivity.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(
4040

4141
if (viewModel.questionId == -1) {
4242
viewModel.questionId = intent.getIntExtra(QUESTION_ID, -1)
43-
if (viewModel.questionId in contentFilter.filteredQuestionIds) {
44-
Toast.makeText(this, R.string.hide_post_hidden, Toast.LENGTH_LONG).show()
45-
finish()
46-
}
43+
checkContentFilter()
4744
}
4845

4946
if (viewModel.answerId == -1) {
@@ -104,6 +101,11 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(
104101
)
105102
}
106103

104+
override fun onResume() {
105+
super.onResume()
106+
checkContentFilter()
107+
}
108+
107109
override fun onSaveInstanceState(outState: Bundle) {
108110
super.onSaveInstanceState(outState)
109111
outState.putBoolean(IS_IN_ANSWER_MODE, viewModel.isInAnswerMode)
@@ -169,6 +171,15 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(
169171
binding.postAnswerButton.hide()
170172
}
171173

174+
private fun checkContentFilter() {
175+
val isQuestionIdHidden = viewModel.questionId in contentFilter.filteredQuestionIds
176+
val isUserIdHidden = viewModel.question?.owner?.userId in contentFilter.filteredUserIds
177+
if (isQuestionIdHidden || isUserIdHidden) {
178+
Toast.makeText(this, R.string.hide_post_hidden, Toast.LENGTH_LONG).show()
179+
finish()
180+
}
181+
}
182+
172183
companion object {
173184
internal const val QUESTION_ID = "question_id"
174185
internal const val ANSWER_ID = "answer_id"

app/src/main/kotlin/me/tylerbwong/stack/ui/search/SearchViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SearchViewModel @Inject constructor(
3636
internal val siteLiveData: LiveData<String>
3737
get() = siteStore.siteLiveData
3838

39-
internal val contentFilteredUpdated: LiveData<Unit>
39+
internal val contentFilteredUpdated: LiveData<ContentFilter.ContentFilterData>
4040
get() = contentFilter.contentFilteredUpdated
4141

4242
internal var searchPayload = SearchPayload.empty()

0 commit comments

Comments
 (0)