Skip to content

Commit

Permalink
Fix more content filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbwong committed Mar 19, 2023
1 parent 43ee68d commit cd88a38
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ContentFilter @Inject constructor(
private val siteStore: SiteStore,
@StackSharedPreferences private val preferences: SharedPreferences,
) {
val contentFilteredUpdated: LiveData<Unit>
val contentFilteredUpdated: LiveData<ContentFilterData>
get() = _contentFilterUpdated
private val _contentFilterUpdated = MutableLiveData<Unit>()
private val _contentFilterUpdated = MutableLiveData<ContentFilterData>()

val filteredQuestionIds: Set<Int>
get() = getFilteredContent(QUESTION_ID_CONTENT_FILTER)
Expand Down Expand Up @@ -117,15 +117,40 @@ class ContentFilter @Inject constructor(
preferences.edit()
.putStringSet(key, (currentIds + id).map { "${it},${siteStore.site}" }.toSet())
.apply()
_contentFilterUpdated.value = Unit
_contentFilterUpdated.value = ContentFilterData(
filteredQuestionIds = filteredQuestionIds,
filteredAnswerIds = filteredAnswerIds,
filteredCommentIds = filteredCommentIds,
filteredUserIds = filteredUserIds,
)
}
}

private fun clearFilteredContent(key: String) {
preferences.edit()
.putStringSet(key, emptySet())
.apply()
_contentFilterUpdated.value = Unit
_contentFilterUpdated.value = ContentFilterData(
filteredQuestionIds = filteredQuestionIds,
filteredAnswerIds = filteredAnswerIds,
filteredCommentIds = filteredCommentIds,
filteredUserIds = filteredUserIds,
)
}

data class ContentFilterData(
val filteredQuestionIds: Set<Int>,
val filteredAnswerIds: Set<Int>,
val filteredCommentIds: Set<Int>,
val filteredUserIds: Set<Int>,
) {
val isEmpty: Boolean
get() = listOf(
filteredQuestionIds,
filteredAnswerIds,
filteredCommentIds,
filteredUserIds,
).all { it.isEmpty() }
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,23 @@ class CommentsBottomSheetDialogFragment : BottomSheetDialogFragment() {
layoutManager = LinearLayoutManager(context)
}
binding.header.title.text = getString(R.string.comments)
viewModel.contentFilteredUpdated.observe(viewLifecycleOwner) {
viewModel.fetchComments()
viewModel.contentFilteredUpdated.observe(viewLifecycleOwner) { filterData ->
if (filterData.isEmpty) {
viewModel.fetchComments()
} else {
viewModel.data.value?.let {
adapter.submitList(
it.filter { item ->
if (item is CommentItem) {
item.comment.commentId !in filterData.filteredCommentIds &&
item.comment.owner.userId !in filterData.filteredUserIds
} else {
true
}
}
)
}
}
}
viewModel.refreshing.observe(viewLifecycleOwner) { isRefreshing ->
if (isRefreshing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CommentsViewModel @Inject constructor(
get() = _data
private val _data = MutableLiveData<List<DynamicItem>>()

internal val contentFilteredUpdated: LiveData<Unit>
internal val contentFilteredUpdated: LiveData<ContentFilter.ContentFilterData>
get() = contentFilter.contentFilteredUpdated

val errorToast: LiveData<CommentError?>
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/kotlin/me/tylerbwong/stack/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,19 @@ class HomeFragment : BaseFragment<HomeFragmentBinding>(
adapter.submitList(null)
viewModel.fetchQuestions()
}
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) {
viewModel.fetchQuestions()
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) { filterData ->
if (filterData.isEmpty) {
viewModel.fetchQuestions()
} else {
viewModel.questions.value?.let {
updateContent(
it.filter { question ->
question.questionId !in filterData.filteredQuestionIds &&
question.owner.userId !in filterData.filteredUserIds
}
)
}
}
}
viewModel.refreshing.observe(viewLifecycleOwner) {
binding.refreshLayout.isRefreshing = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class HomeViewModel @Inject constructor(
internal val siteLiveData: LiveData<String>
get() = siteRepository.siteLiveData

internal val contentFilterUpdated: LiveData<Unit>
internal val contentFilterUpdated: LiveData<ContentFilter.ContentFilterData>
get() = contentFilter.contentFilteredUpdated

@Sort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ class ProfileActivity : BaseActivity<ActivityProfileBinding>(ActivityProfileBind
}
}

viewModel.contentFilterUpdated.observe(this) {
viewModel.fetchProfileData()
}

viewModel.userData.observe(this) {
binding.toolbar.title = it.displayName.toHtml()
binding.profileHeader.setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ProfilePageFragment : BaseFragment<ProfilePageFragmentBinding>(
viewModel.refreshing.observe(viewLifecycleOwner) {
binding.refreshLayout.isRefreshing = it
}
viewModel.contentFilterUpdated.observe(viewLifecycleOwner) {
if (viewModel.userId !in it.filteredUserIds) {
viewModel.fetchProfileData()
}
}
viewModel.data.observe(viewLifecycleOwner) {
adapter.submitList(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProfileViewModel @Inject constructor(
get() = _data
private val _data = MutableLiveData<List<DynamicItem>>()

internal val contentFilterUpdated: LiveData<Unit>
internal val contentFilterUpdated: LiveData<ContentFilter.ContentFilterData>
get() = contentFilter.contentFilteredUpdated

internal fun fetchUserData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(

if (viewModel.questionId == -1) {
viewModel.questionId = intent.getIntExtra(QUESTION_ID, -1)
if (viewModel.questionId in contentFilter.filteredQuestionIds) {
Toast.makeText(this, R.string.hide_post_hidden, Toast.LENGTH_LONG).show()
finish()
}
checkContentFilter()
}

if (viewModel.answerId == -1) {
Expand Down Expand Up @@ -104,6 +101,11 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(
)
}

override fun onResume() {
super.onResume()
checkContentFilter()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(IS_IN_ANSWER_MODE, viewModel.isInAnswerMode)
Expand Down Expand Up @@ -169,6 +171,15 @@ class QuestionDetailActivity : BaseActivity<ActivityQuestionDetailBinding>(
binding.postAnswerButton.hide()
}

private fun checkContentFilter() {
val isQuestionIdHidden = viewModel.questionId in contentFilter.filteredQuestionIds
val isUserIdHidden = viewModel.question?.owner?.userId in contentFilter.filteredUserIds
if (isQuestionIdHidden || isUserIdHidden) {
Toast.makeText(this, R.string.hide_post_hidden, Toast.LENGTH_LONG).show()
finish()
}
}

companion object {
internal const val QUESTION_ID = "question_id"
internal const val ANSWER_ID = "answer_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SearchViewModel @Inject constructor(
internal val siteLiveData: LiveData<String>
get() = siteStore.siteLiveData

internal val contentFilteredUpdated: LiveData<Unit>
internal val contentFilteredUpdated: LiveData<ContentFilter.ContentFilterData>
get() = contentFilter.contentFilteredUpdated

internal var searchPayload = SearchPayload.empty()
Expand Down

0 comments on commit cd88a38

Please sign in to comment.