Skip to content

Commit

Permalink
feat: manually mark items as read or unread (#296)
Browse files Browse the repository at this point in the history
* add option identifier

* update post list

* update community detail

* update multi-community
  • Loading branch information
AkesiSeli authored Jan 24, 2025
1 parent fa3fcfa commit 17e29c8
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ sealed class OptionId(
data object Restore : OptionId(30)

data object ManageTags : OptionId(31)

data object ToggleRead : OptionId(32)
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ interface CommunityDetailMviModel :
data class RestorePost(
val id: Long,
) : Intent

data class ToggleRead(
val id: Long,
) : Intent
}

data class UiState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,11 @@ class CommunityDetailScreen(
LocalStrings.current.actionCopyClipboard,
)
if (uiState.isLogged && !isOnOtherInstance) {
this +=
Option(
OptionId.ToggleRead,
LocalStrings.current.actionToggleRead,
)
this +=
Option(
OptionId.Hide,
Expand Down Expand Up @@ -1332,6 +1337,14 @@ class CommunityDetailScreen(
)
}

OptionId.ToggleRead -> {
model.reduce(
CommunityDetailMviModel.Intent.ToggleRead(
post.id,
),
)
}

else -> Unit
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,35 @@ class CommunityDetailViewModel(
refresh()
}

is CommunityDetailMviModel.Intent.DownVotePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
}
is CommunityDetailMviModel.Intent.DownVotePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleDownVotePost(post)
}
}

is CommunityDetailMviModel.Intent.Share -> {
shareHelper.share(intent.url)
}

is CommunityDetailMviModel.Intent.SavePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
}
is CommunityDetailMviModel.Intent.Share -> shareHelper.share(intent.url)
is CommunityDetailMviModel.Intent.SavePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleSavePost(post)
}
}

is CommunityDetailMviModel.Intent.UpVotePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
}
is CommunityDetailMviModel.Intent.UpVotePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleUpVotePost(post)
}
}

CommunityDetailMviModel.Intent.HapticIndication -> hapticFeedback.vibrate()
CommunityDetailMviModel.Intent.Subscribe -> subscribe()
CommunityDetailMviModel.Intent.Unsubscribe -> unsubscribe()
is CommunityDetailMviModel.Intent.DeletePost -> handlePostDelete(intent.id)

CommunityDetailMviModel.Intent.Block -> blockCommunity()
CommunityDetailMviModel.Intent.BlockInstance -> blockInstance()
is CommunityDetailMviModel.Intent.MarkAsRead ->
Expand All @@ -305,27 +298,24 @@ class CommunityDetailViewModel(
}

CommunityDetailMviModel.Intent.ClearRead -> clearRead()
is CommunityDetailMviModel.Intent.Hide -> {
is CommunityDetailMviModel.Intent.Hide ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
hide(post)
}
}

CommunityDetailMviModel.Intent.PauseZombieMode -> {
CommunityDetailMviModel.Intent.PauseZombieMode ->
screenModelScope.launch {
updateState { it.copy(zombieModeActive = false) }
zombieModeHelper.pause()
}
}

is CommunityDetailMviModel.Intent.StartZombieMode -> {
is CommunityDetailMviModel.Intent.StartZombieMode ->
screenModelScope.launch {
updateState { it.copy(zombieModeActive = true) }
zombieModeHelper.start(
initialValue = intent.index,
interval = settingsRepository.currentSettings.value.zombieModeInterval,
)
}
)
}

is CommunityDetailMviModel.Intent.ModFeaturePost ->
Expand All @@ -349,25 +339,17 @@ class CommunityDetailViewModel(
lock(post)
}

is CommunityDetailMviModel.Intent.ModToggleModUser -> {
toggleModeratorStatus(intent.id)
}

CommunityDetailMviModel.Intent.ToggleFavorite -> {
toggleFavorite()
}

is CommunityDetailMviModel.Intent.ChangeSearching -> {
is CommunityDetailMviModel.Intent.ModToggleModUser -> toggleModeratorStatus(intent.id)
CommunityDetailMviModel.Intent.ToggleFavorite -> toggleFavorite()
is CommunityDetailMviModel.Intent.ChangeSearching ->
screenModelScope.launch {
updateState { it.copy(searching = intent.value) }
if (!intent.value) {
updateSearchText("")
}
updateSearchText("")
}
}

is CommunityDetailMviModel.Intent.SetSearch -> updateSearchText(intent.value)

is CommunityDetailMviModel.Intent.WillOpenDetail ->
screenModelScope.launch {
uiState.value.posts
Expand All @@ -379,21 +361,18 @@ class CommunityDetailViewModel(
}
}

CommunityDetailMviModel.Intent.UnhideCommunity -> {
unhideCommunity()
}

is CommunityDetailMviModel.Intent.SelectPreferredLanguage -> {
CommunityDetailMviModel.Intent.UnhideCommunity -> unhideCommunity()
is CommunityDetailMviModel.Intent.SelectPreferredLanguage ->
updatePreferredLanguage(intent.languageId)
}

CommunityDetailMviModel.Intent.DeleteCommunity -> {
deleteCommunity()
}

is CommunityDetailMviModel.Intent.RestorePost -> {
restorePost(intent.id)
}
CommunityDetailMviModel.Intent.DeleteCommunity -> deleteCommunity()
is CommunityDetailMviModel.Intent.RestorePost -> restorePost(intent.id)
is CommunityDetailMviModel.Intent.ToggleRead ->
screenModelScope.launch {
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
setRead(post = post, read = !post.read)
}
}
}
}

Expand Down Expand Up @@ -571,11 +550,18 @@ class CommunityDetailViewModel(
if (post.read) {
return
}
val newPost = post.copy(read = true)
setRead(post = post, read = true)
}

private suspend fun setRead(
post: PostModel,
read: Boolean,
) {
val newPost = post.copy(read = read)
try {
val auth = identityRepository.authToken.value.orEmpty()
postRepository.setRead(
read = true,
read = read,
postId = post.id,
auth = auth,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ interface MultiCommunityMviModel :
data class WillOpenDetail(
val id: Long,
) : Intent

data class ToggleRead(
val id: Long,
) : Intent
}

data class UiState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ class MultiCommunityScreen(
LocalStrings.current.actionCopyClipboard,
)
if (uiState.currentUserId != null) {
this +=
Option(
OptionId.ToggleRead,
LocalStrings.current.actionToggleRead,
)
this +=
Option(
OptionId.Hide,
Expand Down Expand Up @@ -505,6 +510,12 @@ class MultiCommunityScreen(
),
)

OptionId.ToggleRead -> {
model.reduce(
MultiCommunityMviModel.Intent.ToggleRead(post.id),
)
}

OptionId.Share -> {
val urls =
listOfNotNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,13 @@ class MultiCommunityViewModel(

override fun reduce(intent: MultiCommunityMviModel.Intent) {
when (intent) {
is MultiCommunityMviModel.Intent.DownVotePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
is MultiCommunityMviModel.Intent.DownVotePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleDownVote(post)
}
toggleDownVote(
post = uiState.value.posts.first { it.id == intent.id },
)
}

MultiCommunityMviModel.Intent.HapticIndication -> hapticFeedback.vibrate()
MultiCommunityMviModel.Intent.LoadNextPage ->
Expand All @@ -169,25 +168,23 @@ class MultiCommunityViewModel(
refresh()
}

is MultiCommunityMviModel.Intent.SavePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
is MultiCommunityMviModel.Intent.SavePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleSave(post)
}
toggleSave(
post = uiState.value.posts.first { it.id == intent.id },
)
}

is MultiCommunityMviModel.Intent.Share -> {
shareHelper.share(intent.url)
}
is MultiCommunityMviModel.Intent.Share -> shareHelper.share(intent.url)

is MultiCommunityMviModel.Intent.UpVotePost -> {
if (intent.feedback) {
hapticFeedback.vibrate()
is MultiCommunityMviModel.Intent.UpVotePost ->
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
if (intent.feedback) {
hapticFeedback.vibrate()
}
toggleUpVote(post)
}
toggleUpVote(uiState.value.posts.first { it.id == intent.id })
}

MultiCommunityMviModel.Intent.ClearRead -> clearRead()
is MultiCommunityMviModel.Intent.MarkAsRead ->
Expand All @@ -211,6 +208,13 @@ class MultiCommunityViewModel(
emitEffect(MultiCommunityMviModel.Effect.OpenDetail(post))
}
}

is MultiCommunityMviModel.Intent.ToggleRead ->
screenModelScope.launch {
uiState.value.posts.firstOrNull { it.id == intent.id }?.also { post ->
setRead(post = post, read = !post.read)
}
}
}
}

Expand Down Expand Up @@ -330,11 +334,18 @@ class MultiCommunityViewModel(
if (post.read) {
return
}
val newPost = post.copy(read = true)
setRead(post = post, read = true)
}

private suspend fun setRead(
post: PostModel,
read: Boolean,
) {
val newPost = post.copy(read = read)
try {
val auth = identityRepository.authToken.value.orEmpty()
postRepository.setRead(
read = true,
read = read,
postId = post.id,
auth = auth,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ interface PostListMviModel :
data class WillOpenDetail(
val id: Long,
) : Intent

data class ToggleRead(
val id: Long,
) : Intent
}

data class UiState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,11 @@ class PostListScreen : Screen {
LocalStrings.current.actionCopyClipboard,
)
if (uiState.isLogged) {
this +=
Option(
OptionId.ToggleRead,
LocalStrings.current.actionToggleRead,
)
this +=
Option(
OptionId.Hide,
Expand Down Expand Up @@ -747,6 +752,12 @@ class PostListScreen : Screen {
}
}

OptionId.ToggleRead -> {
model.reduce(
PostListMviModel.Intent.ToggleRead(post.id),
)
}

else -> Unit
}
},
Expand Down
Loading

0 comments on commit 17e29c8

Please sign in to comment.