feat: add block to post list; closes #339 (#343)

This commit is contained in:
Diego Beraldin 2023-12-22 13:48:34 +01:00 committed by GitHub
parent 4eb2c59117
commit 6fa13bcf5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 347 additions and 22 deletions

View File

@ -0,0 +1,24 @@
package com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui
import androidx.compose.runtime.Composable
import com.github.diegoberaldin.raccoonforlemmy.resources.MR
import dev.icerock.moko.resources.compose.stringResource
sealed interface BlockActionType {
data object User : BlockActionType
data object Community : BlockActionType
data object Instance : BlockActionType
}
fun Int.toBlockActionType(): BlockActionType = when (this) {
2 -> BlockActionType.Instance
1 -> BlockActionType.Community
else -> BlockActionType.User
}
@Composable
fun BlockActionType.toReadableName(): String = when (this) {
BlockActionType.Community -> stringResource(MR.strings.block_action_community)
BlockActionType.Instance -> stringResource(MR.strings.community_detail_block_instance)
BlockActionType.User -> stringResource(MR.strings.block_action_user)
}

View File

@ -43,10 +43,13 @@ kotlin {
implementation(projects.core.utils)
implementation(projects.core.appearance)
implementation(projects.core.commonui.components)
implementation(projects.core.commonui.lemmyui)
implementation(projects.core.navigation)
implementation(projects.core.persistence)
implementation(projects.core.notifications)
implementation(projects.domain.lemmy.data)
implementation(projects.resources)
}
}

View File

@ -0,0 +1,145 @@
package com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.core.screen.Screen
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.components.BottomSheetHandle
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.BlockActionType
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.toReadableName
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.di.getNavigationCoordinator
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterEvent
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.di.getNotificationCenter
import com.github.diegoberaldin.raccoonforlemmy.core.utils.compose.onClick
import com.github.diegoberaldin.raccoonforlemmy.core.utils.compose.rememberCallback
import com.github.diegoberaldin.raccoonforlemmy.resources.MR
import dev.icerock.moko.resources.compose.stringResource
class BlockBottomSheet(
private val userName: String? = null,
private val userId: Int? = null,
private val communityName: String? = null,
private val communityId: Int? = null,
private val instanceName: String? = null,
private val instanceId: Int? = null,
private val userInstanceName: String? = null,
private val userInstanceId: Int? = null,
) : Screen {
@Composable
override fun Content() {
val navigationCoordinator = remember { getNavigationCoordinator() }
val notificationCenter = remember { getNotificationCenter() }
Column(
modifier = Modifier.padding(
top = Spacing.s,
start = Spacing.s,
end = Spacing.s,
bottom = Spacing.m,
),
verticalArrangement = Arrangement.spacedBy(Spacing.s),
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
BottomSheetHandle()
Text(
modifier = Modifier.padding(start = Spacing.s, top = Spacing.s),
text = stringResource(MR.strings.community_detail_block),
style = MaterialTheme.typography.titleLarge,
color = MaterialTheme.colorScheme.onBackground,
)
Column(
modifier = Modifier.fillMaxWidth().verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(Spacing.xxxs),
) {
val values: List<Triple<BlockActionType, Int, String>> = buildList {
if (userName != null && userId != null) {
this += Triple(
BlockActionType.User,
userId,
userName,
)
}
if (communityName != null && communityId != null) {
this += Triple(
BlockActionType.Community,
communityId,
communityName,
)
}
if (instanceName != null && instanceId != null) {
this += Triple(
BlockActionType.Instance,
instanceId,
instanceName,
)
}
if (userInstanceName != null && userInstanceId != null && userInstanceName != instanceName) {
this += Triple(
BlockActionType.Instance,
userInstanceId,
userInstanceName,
)
}
}
for (value in values) {
Row(
modifier = Modifier.padding(
horizontal = Spacing.s,
vertical = Spacing.m,
).fillMaxWidth().onClick(
onClick = rememberCallback {
val event = when (value.first) {
BlockActionType.Community -> NotificationCenterEvent.BlockActionSelected(
communityId = value.second
)
BlockActionType.Instance ->
NotificationCenterEvent.BlockActionSelected(
instanceId = value.second
)
BlockActionType.User -> NotificationCenterEvent.BlockActionSelected(
userId = value.second
)
}
notificationCenter.send(event)
navigationCoordinator.hideBottomSheet()
},
),
) {
val valueText = buildString {
append(value.first.toReadableName())
val additionalText = value.third
if (additionalText.isNotEmpty()) {
append("\n")
append("(")
append(additionalText)
append(")")
}
}
Text(
text = valueText,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onBackground,
)
}
}
}
}
}
}
}

View File

@ -53,4 +53,9 @@ sealed interface NotificationCenterEvent {
data class UserBannedPost(val postId: Int, val user: UserModel) : NotificationCenterEvent
data class UserBannedComment(val commentId: Int, val user: UserModel) : NotificationCenterEvent
data class ChangeCommentBarTheme(val value: CommentBarTheme) : NotificationCenterEvent
data class BlockActionSelected(
val userId: Int? = null,
val communityId: Int? = null,
val instanceId: Int? = null,
) : NotificationCenterEvent
}

View File

@ -278,4 +278,6 @@
<string name="modlog_item_comment_removed">تمت إزالته من التعليقات</string>
<string name="modlog_item_comment_restored">تمت استعادته في تعليقات</string>
<string name="modlog_item_community_transfer">تم نقل المجتمع إلى</string>
<string name="block_action_user">مستخدم محضور</string>
<string name="block_action_community">منع المجتمع</string>
</resources>

View File

@ -309,4 +309,6 @@
<string name="modlog_item_comment_removed">was removed from the comments of</string>
<string name="modlog_item_comment_restored">was restored in the comments of</string>
<string name="modlog_item_community_transfer">the community was transferred to</string>
<string name="block_action_user">Block user</string>
<string name="block_action_community">Block community</string>
</resources>

View File

@ -289,4 +289,6 @@
<string name="modlog_item_comment_removed">беше премахнат от коментарите на</string>
<string name="modlog_item_comment_restored">беше възстановено в коментарите на</string>
<string name="modlog_item_community_transfer">общността беше прехвърлена на</string>
<string name="block_action_user">Блокиране на потребител</string>
<string name="block_action_community">Блокиране на общността</string>
</resources>

View File

@ -280,4 +280,6 @@
<string name="modlog_item_comment_removed">byl odstraněn z komentářů uživatele</string>
<string name="modlog_item_comment_restored">byl obnoven v komentářích</string>
<string name="modlog_item_community_transfer">komunita byla převedena do</string>
<string name="block_action_user">Blokovat uživatele</string>
<string name="block_action_community">Blokovat komunitu</string>
</resources>

View File

@ -280,4 +280,6 @@
<string name="modlog_item_comment_removed">blev fjernet fra kommentarerne til</string>
<string name="modlog_item_comment_restored">blev gendannet i kommentarerne til</string>
<string name="modlog_item_community_transfer">samfundet blev overført til</string>
<string name="block_action_user">Bloker bruger</string>
<string name="block_action_community">Bloker fællesskabet</string>
</resources>

View File

@ -288,4 +288,6 @@
<string name="modlog_item_comment_restored">wurde in den Kommentaren von wiederhergestellt
</string>
<string name="modlog_item_community_transfer">die Gemeinde wurde übertragen</string>
<string name="block_action_user">Benutzer blockieren</string>
<string name="block_action_community">Community blockieren</string>
</resources>

View File

@ -14,7 +14,7 @@
<string name="button_retry">Προσπάθησε ξανά</string>
<string name="comment_action_delete">Διάγραψε</string>
<string name="community_detail_block">Απόκλεισε</string>
<string name="community_detail_block_instance">Απόκλεισε διακομιστή</string>
<string name="community_detail_block_instance">Αποκλεισμός διακομιστή</string>
<string name="community_detail_info">Πληροφορίες κοινότητας</string>
<string name="community_detail_instance_info">Πληροφορίες διακομιστή</string>
<string name="community_info_comments">σχόλια</string>
@ -290,4 +290,6 @@
<string name="modlog_item_comment_removed">αφαιρέθηκε από τα σχόλια του</string>
<string name="modlog_item_comment_restored">αποκαταστάθηκε στα σχόλια του</string>
<string name="modlog_item_community_transfer">η κοινότητα μεταφέρθηκε σε</string>
<string name="block_action_user">Αποκλεισμός χρήστη</string>
<string name="block_action_community">Αποκλεισμός κοινότητας</string>
</resources>

View File

@ -279,4 +279,6 @@
<string name="modlog_item_comment_removed">estis forigita de la komentoj de</string>
<string name="modlog_item_comment_restored">estis restarigita en la komentoj de</string>
<string name="modlog_item_community_transfer">la komunumo estis translokigita al</string>
<string name="block_action_user">Bloki uzanton</string>
<string name="block_action_community">Bloki komunumon</string>
</resources>

View File

@ -286,4 +286,6 @@
<string name="modlog_item_comment_removed">se removió de los comentarios de</string>
<string name="modlog_item_comment_restored">se restauró en los comentarios de</string>
<string name="modlog_item_community_transfer">la comunidad se trasladó a</string>
<string name="block_action_user">Bloquear usuario</string>
<string name="block_action_community">Bloquear comunidad</string>
</resources>

View File

@ -280,4 +280,6 @@
<string name="modlog_item_comment_removed">eemaldati kasutaja kommentaaridest</string>
<string name="modlog_item_comment_restored">aasta kommentaarides taastati</string>
<string name="modlog_item_community_transfer">kogukond viidi üle</string>
<string name="block_action_user">Blokeeri kasutaja</string>
<string name="block_action_community">Blokeeri kogukond</string>
</resources>

View File

@ -280,4 +280,6 @@
<string name="modlog_item_comment_removed">poistettiin kommenteista</string>
<string name="modlog_item_comment_restored">palautettiin kommenteissa</string>
<string name="modlog_item_community_transfer">yhteisö siirrettiin</string>
<string name="block_action_user">Estä käyttäjä</string>
<string name="block_action_community">Estä yhteisö</string>
</resources>

View File

@ -286,4 +286,6 @@
<string name="modlog_item_comment_removed">a été supprimé des commentaires de</string>
<string name="modlog_item_comment_restored">a été restauré dans les commentaires de</string>
<string name="modlog_item_community_transfer">la communauté a été transférée à</string>
<string name="block_action_user">Bloquer l\'utilisateur</string>
<string name="block_action_community">Bloquer la communauté</string>
</resources>

View File

@ -289,4 +289,6 @@
<string name="modlog_item_comment_removed">baineadh de thuairimí ó</string>
<string name="modlog_item_comment_restored">a athchóiriú i dtuairimí ó</string>
<string name="modlog_item_community_transfer">aistríodh an pobal go</string>
<string name="block_action_user">Cuir bac ar úsáideoir</string>
<string name="block_action_community">Bloc pobail</string>
</resources>

View File

@ -285,4 +285,6 @@
<string name="modlog_item_comment_removed">uklonjen je iz komentara korisnika</string>
<string name="modlog_item_comment_restored">vraćeno je u komentarima</string>
<string name="modlog_item_community_transfer">zajednica je prebačena u</string>
<string name="block_action_user">Blokirati korisnika</string>
<string name="block_action_community">Blokiraj zajednicu</string>
</resources>

View File

@ -284,4 +284,6 @@
<string name="modlog_item_comment_removed">eltávolítva a megjegyzései közül</string>
<string name="modlog_item_comment_restored">megjegyzésében helyreállították</string>
<string name="modlog_item_community_transfer">a közösség átkerült</string>
<string name="block_action_user">Felhasználó letiltása</string>
<string name="block_action_community">Közösség letiltása</string>
</resources>

View File

@ -284,4 +284,6 @@
<string name="modlog_item_comment_removed">è stato rimosso dai commenti di</string>
<string name="modlog_item_comment_restored">è stato ripristinato nei commenti dif</string>
<string name="modlog_item_community_transfer">la comunità è stata trasferita a</string>
<string name="block_action_user">Blocca utente</string>
<string name="block_action_community">Blocca comunità</string>
</resources>

View File

@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">buvo pašalintas iš komentarų</string>
<string name="modlog_item_comment_restored">buvo atkurta komentaruose</string>
<string name="modlog_item_community_transfer">bendruomenė buvo perduota</string>
<string name="block_action_user">Blokuoti vartotoją</string>
<string name="block_action_community">Blokuoti bendruomenę</string>
</resources>

View File

@ -284,4 +284,6 @@
<string name="modlog_item_comment_removed">tika noņemts no komentāriem</string>
<string name="modlog_item_comment_restored">gada komentāros tika atjaunots</string>
<string name="modlog_item_community_transfer">kopiena tika pārcelta uz</string>
<string name="block_action_user">Bloķēt lietotāju</string>
<string name="block_action_community">Bloķēt kopienu</string>
</resources>

View File

@ -285,4 +285,6 @@
<string name="modlog_item_comment_removed">tneħħa mill-kummenti ta</string>
<string name="modlog_item_comment_restored">ġie restawrat fil-kummenti ta</string>
<string name="modlog_item_community_transfer">il-komunità ġiet trasferita</string>
<string name="block_action_user">Blokk utent</string>
<string name="block_action_community">Blokk komunità</string>
</resources>

View File

@ -284,4 +284,6 @@
<string name="modlog_item_comment_removed">werd verwijderd uit de reacties van</string>
<string name="modlog_item_comment_restored">werd hersteld in de opmerkingen van</string>
<string name="modlog_item_community_transfer">de gemeenschap werd overgedragen</string>
<string name="block_action_user">Blokkeer gebruiker</string>
<string name="block_action_community">Blokkeer gemeenschap</string>
</resources>

View File

@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">ble fjernet fra kommentarene til</string>
<string name="modlog_item_comment_restored">ble gjenopprettet i kommentarene til</string>
<string name="modlog_item_community_transfer">fellesskapet ble overført til</string>
<string name="block_action_user">Blokker bruker</string>
<string name="block_action_community">Blokker fellesskapet</string>
</resources>

View File

@ -14,7 +14,7 @@
<string name="button_retry">Próba</string>
<string name="comment_action_delete">Usunąć</string>
<string name="community_detail_block">Blok</string>
<string name="community_detail_block_instance">Instancja bloku</string>
<string name="community_detail_block_instance">Zablokui instancję</string>
<string name="community_detail_info">Informacje o społeczności</string>
<string name="community_detail_instance_info">Szczegóły instancji</string>
<string name="community_info_comments">Komentarze</string>
@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">został usunięty z komentarzy</string>
<string name="modlog_item_comment_restored">został przywrócony w komentarzach</string>
<string name="modlog_item_community_transfer">gmina została przeniesiona</string>
<string name="block_action_user">Zablokuj użytkownika</string>
<string name="block_action_community">Zablokuj społeczność</string>
</resources>

View File

@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">foi removido dos comentários de</string>
<string name="modlog_item_comment_restored">foi restaurado nos comentários de</string>
<string name="modlog_item_community_transfer">a comunidade foi transferida para</string>
<string name="block_action_user">Bloquear usuário</string>
<string name="block_action_community">Bloquear comunidade</string>
</resources>

View File

@ -281,4 +281,6 @@
<string name="modlog_item_comment_removed">a fost eliminat din comentariile de</string>
<string name="modlog_item_comment_restored">a fost restaurat în comentariile de</string>
<string name="modlog_item_community_transfer">comunitatea a fost transferată la</string>
<string name="block_action_user">Blochează utilizatorul</string>
<string name="block_action_community">Blochează comunitatea</string>
</resources>

View File

@ -13,7 +13,7 @@
<string name="button_retry">Повторить</string>
<string name="comment_action_delete">Удалить</string>
<string name="community_detail_block">Блок</string>
<string name="community_detail_block_instance">Экземпляр блока</string>
<string name="community_detail_block_instance">Заблокировать экземпляр</string>
<string name="community_detail_info">Информация о микрорайоне</string>
<string name="community_detail_instance_info">Сведения об экземпляре</string>
<string name="community_info_comments">Комментарии</string>
@ -284,4 +284,6 @@
<string name="modlog_item_comment_removed">удалили из комментариев</string>
<string name="modlog_item_comment_restored">восстановлено в комментариях</string>
<string name="modlog_item_community_transfer">община была переведена в</string>
<string name="block_action_user">Заблокировать пользователя</string>
<string name="block_action_community">Заблокировать сообщество</string>
</resources>

View File

@ -281,4 +281,6 @@
<string name="modlog_item_comment_removed">togs bort från kommentarerna till</string>
<string name="modlog_item_comment_restored">återställdes i kommentarerna till</string>
<string name="modlog_item_community_transfer">samhället överfördes till</string>
<string name="block_action_user">Blockera användare</string>
<string name="block_action_community">Blockera gemenskap</string>
</resources>

View File

@ -282,4 +282,6 @@
<string name="modlog_item_comment_removed">bol odstránený z komentárov používateľa</string>
<string name="modlog_item_comment_restored">bol obnovený v komentároch o</string>
<string name="modlog_item_community_transfer">komunita bola prevedená na</string>
<string name="block_action_user">Blokovať používateľa</string>
<string name="block_action_community">Blokovať komunitu</string>
</resources>

View File

@ -280,4 +280,6 @@
<string name="modlog_item_comment_removed">je bil odstranjen iz komentarjev osebe</string>
<string name="modlog_item_comment_restored">je bil obnovljen v komentarjih</string>
<string name="modlog_item_community_transfer">skupnost je bila prenesena na</string>
<string name="block_action_user">Blokiraj uporabnika</string>
<string name="block_action_community">Blokiraj skupnost</string>
</resources>

View File

@ -286,4 +286,6 @@
<string name="modlog_item_comment_removed">u hoq nga komentet e</string>
<string name="modlog_item_comment_restored">u rivendos në komentet e</string>
<string name="modlog_item_community_transfer">komuniteti u transferua në</string>
<string name="block_action_user">Blloko përdoruesin</string>
<string name="block_action_community">Blloko komunitetin</string>
</resources>

View File

@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">yorumlarından kaldırıldı</string>
<string name="modlog_item_comment_restored">yorumlarında geri yüklendi</string>
<string name="modlog_item_community_transfer">topluluk şuraya transfer edildi</string>
<string name="block_action_user">Kullanıcıyı engelle</string>
<string name="block_action_community">Topluluğu engelle</string>
</resources>

View File

@ -283,4 +283,6 @@
<string name="modlog_item_comment_removed">було видалено з коментарів користувача</string>
<string name="modlog_item_comment_restored">було відновлено в коментарях</string>
<string name="modlog_item_community_transfer">громаду було передано с</string>
<string name="block_action_user">Заблокувати користувача</string>
<string name="block_action_community">Заблокувати спільноту</string>
</resources>

View File

@ -66,6 +66,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.OptionId
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.PostCard
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.PostCardPlaceholder
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.di.getFabNestedScrollConnection
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.BlockBottomSheet
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.ListingTypeBottomSheet
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.RawContentDialog
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.modals.SortBottomSheet
@ -421,49 +422,55 @@ class PostListScreen : Screen {
add(
Option(
OptionId.Share,
stringResource(MR.strings.post_action_share)
)
stringResource(MR.strings.post_action_share),
),
)
if (uiState.isLogged) {
add(
Option(
OptionId.Hide,
stringResource(MR.strings.post_action_hide)
)
stringResource(MR.strings.post_action_hide),
),
)
add(
Option(
OptionId.Block,
stringResource(MR.strings.community_detail_block),
),
)
}
add(
Option(
OptionId.SeeRaw,
stringResource(MR.strings.post_action_see_raw)
)
stringResource(MR.strings.post_action_see_raw),
),
)
if (uiState.isLogged) {
add(
Option(
OptionId.CrossPost,
stringResource(MR.strings.post_action_cross_post)
)
stringResource(MR.strings.post_action_cross_post),
),
)
add(
Option(
OptionId.Report,
stringResource(MR.strings.post_action_report)
)
stringResource(MR.strings.post_action_report),
),
)
}
if (post.creator?.id == uiState.currentUserId) {
add(
Option(
OptionId.Edit,
stringResource(MR.strings.post_action_edit)
)
stringResource(MR.strings.post_action_edit),
),
)
add(
Option(
OptionId.Delete,
stringResource(MR.strings.comment_action_delete)
)
stringResource(MR.strings.comment_action_delete),
),
)
}
},
@ -511,6 +518,36 @@ class PostListScreen : Screen {
PostListMviModel.Intent.SharePost(post.id)
)
OptionId.Block -> {
val screen = BlockBottomSheet(
userName = buildString {
post.creator?.also {
append(it.name)
if (it.host.isNotEmpty()) {
append("@")
append(it.host)
}
}
},
userId = post.creator?.id,
communityName = buildString {
post.community?.also {
append(it.name)
if (it.host.isNotEmpty()) {
append("@")
append(it.host)
}
}
},
communityId = post.community?.id,
instanceName = post.community?.host,
instanceId = post.community?.instanceId,
userInstanceName = post.creator?.host,
userInstanceId = post.creator?.instanceId,
)
navigationCoordinator.showBottomSheet(screen)
}
else -> Unit
}
}

View File

@ -19,9 +19,11 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SortType
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.imageUrl
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toListingType
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toSortType
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.GetSortTypesUseCase
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.drop
@ -38,6 +40,8 @@ class PostListViewModel(
private val themeRepository: ThemeRepository,
private val shareHelper: ShareHelper,
private val settingsRepository: SettingsRepository,
private val userRepository: UserRepository,
private val communityRepository: CommunityRepository,
private val notificationCenter: NotificationCenter,
private val hapticFeedback: HapticFeedback,
private val zombieModeHelper: ZombieModeHelper,
@ -89,6 +93,7 @@ class PostListViewModel(
)
}
}.launchIn(this)
notificationCenter.subscribe(NotificationCenterEvent.PostUpdated::class)
.onEach { evt ->
handlePostUpdate(evt.model)
@ -101,11 +106,24 @@ class PostListViewModel(
.onEach { evt ->
applyListingType(evt.value)
}.launchIn(this)
notificationCenter.subscribe(NotificationCenterEvent.ChangeSortType::class)
.onEach { evt ->
applySortType(evt.value)
}.launchIn(this)
notificationCenter.subscribe(NotificationCenterEvent.Logout::class).onEach {
handleLogout()
}.launchIn(this)
notificationCenter.subscribe(NotificationCenterEvent.BlockActionSelected::class)
.onEach { evt ->
val userId = evt.userId
val communityId = evt.communityId
val instanceId = evt.instanceId
when {
userId != null -> blockUser(userId)
communityId != null -> blockCommunity(communityId)
instanceId != null -> blockInstance(instanceId)
}
}.launchIn(this)
zombieModeHelper.index.onEach { index ->
if (uiState.value.zombieModeActive) {
@ -113,10 +131,6 @@ class PostListViewModel(
}
}.launchIn(this)
notificationCenter.subscribe(NotificationCenterEvent.Logout::class).onEach {
handleLogout()
}.launchIn(this)
val auth = identityRepository.authToken.value.orEmpty()
val user = siteRepository.getCurrentUser(auth)
mvi.updateState { it.copy(currentUserId = user?.id ?: 0) }
@ -475,4 +489,37 @@ class PostListViewModel(
}
markAsRead(post)
}
private fun blockUser(userId: Int) {
mvi.scope?.launch(Dispatchers.IO) {
try {
val auth = identityRepository.authToken.value
userRepository.block(userId, true, auth).getOrThrow()
} catch (e: Throwable) {
e.printStackTrace()
}
}
}
private fun blockCommunity(communityId: Int) {
mvi.scope?.launch(Dispatchers.IO) {
try {
val auth = identityRepository.authToken.value
communityRepository.block(communityId, true, auth).getOrThrow()
} catch (e: Throwable) {
e.printStackTrace()
}
}
}
private fun blockInstance(instanceId: Int) {
mvi.scope?.launch(Dispatchers.IO) {
try {
val auth = identityRepository.authToken.value
siteRepository.block(instanceId, true, auth)
} catch (e: Throwable) {
e.printStackTrace()
}
}
}
}

View File

@ -15,6 +15,8 @@ val postListModule = module {
siteRepository = get(),
themeRepository = get(),
settingsRepository = get(),
userRepository = get(),
communityRepository = get(),
shareHelper = get(),
notificationCenter = get(),
hapticFeedback = get(),