From 7233236d460f8e6e584d62e2bf11e6673ae83941 Mon Sep 17 00:00:00 2001 From: Schoolkid1 Date: Sun, 13 Aug 2023 19:34:41 +0200 Subject: [PATCH] Unblocking option #682 attempt 5 Rechecked all possible parameters, values, functions and API's for possible needs of change and errors. After tweaking I hope the code will be sufficient --- app/src/main/java/com/jerboa/Utils.kt | 17 ++++++- app/src/main/java/com/jerboa/api/Http.kt | 5 ++- .../datatypes/types/UnblockCommunity.kt | 11 +++++ .../types/UnblockCommunityRespone.kt | 10 +++++ .../com/jerboa/model/CommunityViewModel.kt | 20 +++++---- .../java/com/jerboa/model/HomeViewModel.kt | 12 +++++ .../jerboa/model/PersonProfileViewModel.kt | 14 +++++- .../java/com/jerboa/model/PostViewModel.kt | 13 ++++++ .../ui/components/community/Community.kt | 6 +++ .../components/community/CommunityActivity.kt | 45 +++++++++++++++++++ .../jerboa/ui/components/home/HomeActivity.kt | 20 +++++++++ .../person/PersonProfileActivity.kt | 15 ++++++- .../jerboa/ui/components/post/PostActivity.kt | 20 +++++++++ .../java/com/jerboa/util/Initializable.kt | 1 - 14 files changed, 195 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/jerboa/datatypes/types/UnblockCommunity.kt create mode 100644 app/src/main/java/com/jerboa/datatypes/types/UnblockCommunityRespone.kt diff --git a/app/src/main/java/com/jerboa/Utils.kt b/app/src/main/java/com/jerboa/Utils.kt index dda02b894..e29accdf0 100644 --- a/app/src/main/java/com/jerboa/Utils.kt +++ b/app/src/main/java/com/jerboa/Utils.kt @@ -1219,7 +1219,7 @@ fun showBlockPersonToast(blockPersonRes: ApiState, ctx: Con } } -fun showBlockCommunityToast(blockCommunityRes: ApiState, ctx: BlockCommunityResponse) { +fun showBlockCommunityToast(blockCommunityRes: ApiState, ctx: Context) { when (blockCommunityRes) { is ApiState.Success -> { Toast.makeText( @@ -1234,6 +1234,21 @@ fun showBlockCommunityToast(blockCommunityRes: ApiState, } } +fun showUnblockCommunityToast(unblockCommunityRes: ApiState, ctx: Context) { + when (unblockCommunityRes) { + is ApiState.Success -> { + Toast.makeText( + ctx, + "${unblockCommunityRes.data.community_view.community.name} Unblocked", + Toast.LENGTH_SHORT, + ) + .show() + } + + else -> {} + } +} + fun findAndUpdatePersonMention( mentions: List, updatedCommentView: CommentView, diff --git a/app/src/main/java/com/jerboa/api/Http.kt b/app/src/main/java/com/jerboa/api/Http.kt index 76ce195fb..911e5b5d4 100644 --- a/app/src/main/java/com/jerboa/api/Http.kt +++ b/app/src/main/java/com/jerboa/api/Http.kt @@ -231,7 +231,10 @@ interface API { * Block a community. */ @POST("community/block") - suspend fun blockCommunity(@Body form: BlockCommunityResponse): Response + suspend fun blockCommunity(@Body form: BlockCommunity): Response + + @POST("community/unblock") + suspend fun unblockCommunity(@Body form: UnblockCommunity): Response /** * Save your user settings. diff --git a/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunity.kt b/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunity.kt new file mode 100644 index 000000000..46017bc8d --- /dev/null +++ b/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunity.kt @@ -0,0 +1,11 @@ +package com.jerboa.datatypes.types + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class UnblockCommunity( + val community_id: CommunityId, + val block: Boolean, + val auth: String, +) : Parcelable diff --git a/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunityRespone.kt b/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunityRespone.kt new file mode 100644 index 000000000..59345f9c5 --- /dev/null +++ b/app/src/main/java/com/jerboa/datatypes/types/UnblockCommunityRespone.kt @@ -0,0 +1,10 @@ +package com.jerboa.datatypes.types + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class UnblockCommunityResponse( + val community_view: CommunityView, + val blocked: Boolean, +) : Parcelable diff --git a/app/src/main/java/com/jerboa/model/CommunityViewModel.kt b/app/src/main/java/com/jerboa/model/CommunityViewModel.kt index f37dea461..6ac5856df 100644 --- a/app/src/main/java/com/jerboa/model/CommunityViewModel.kt +++ b/app/src/main/java/com/jerboa/model/CommunityViewModel.kt @@ -29,11 +29,14 @@ import com.jerboa.datatypes.types.PostResponse import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType +import com.jerboa.datatypes.types.UnblockCommunity +import com.jerboa.datatypes.types.UnblockCommunityResponse import com.jerboa.findAndUpdatePost import com.jerboa.mergePosts import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast import com.jerboa.showBlockPersonToast +import com.jerboa.showUnblockCommunityToast import com.jerboa.util.Initializable import kotlinx.coroutines.launch @@ -54,6 +57,8 @@ class CommunityViewModel : ViewModel(), Initializable { private var deletePostRes: ApiState by mutableStateOf(ApiState.Empty) private var blockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) + private var unblockCommunityRes: ApiState by + mutableStateOf(ApiState.Empty) private var blockPersonRes: ApiState by mutableStateOf(ApiState.Empty) private var markPostRes: ApiState by mutableStateOf(ApiState.Empty) @@ -237,15 +242,14 @@ class CommunityViewModel : ViewModel(), Initializable { } } } - //private var blockCommunityRes: ApiState by - //mutableStateOf(ApiState.Empty) - fun unBlockCommunity(form: BlockCommunityResponse, ctx: BlockCommunityResponse){ + fun unblockCommunity(form: UnblockCommunity, ctx: Context){ viewModelScope.launch { - blockCommunityRes - apiWrapper(API.getInstance().blockCommunity(form)) - when (val blockCommunity = blockCommunityRes) { + unblockCommunityRes = ApiState.Loading + unblockCommunityRes + apiWrapper(API.getInstance().unblockCommunity(form)) + when (val unblockCommunity = unblockCommunityRes) { is ApiState.Success -> { - showBlockCommunityToast(blockCommunity, ctx) + showUnblockCommunityToast(unblockCommunity, ctx) when (val existing = communityRes) { is ApiState.Success -> { @@ -253,7 +257,7 @@ class CommunityViewModel : ViewModel(), Initializable { ApiState.Success( existing.data.copy( community_view = - blockCommunity.data.community_view, + unblockCommunity.data.community_view, ), ) communityRes = deleteRes diff --git a/app/src/main/java/com/jerboa/model/HomeViewModel.kt b/app/src/main/java/com/jerboa/model/HomeViewModel.kt index f05391000..863a51ec6 100644 --- a/app/src/main/java/com/jerboa/model/HomeViewModel.kt +++ b/app/src/main/java/com/jerboa/model/HomeViewModel.kt @@ -27,12 +27,15 @@ import com.jerboa.datatypes.types.PostResponse import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType +import com.jerboa.datatypes.types.UnblockCommunity +import com.jerboa.datatypes.types.UnblockCommunityResponse import com.jerboa.db.entity.Account import com.jerboa.findAndUpdatePost import com.jerboa.mergePosts import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast import com.jerboa.showBlockPersonToast +import com.jerboa.showUnblockCommunityToast import com.jerboa.util.Initializable import kotlinx.coroutines.launch @@ -48,6 +51,7 @@ class HomeViewModel : ViewModel(), Initializable { private var savePostRes: ApiState by mutableStateOf(ApiState.Empty) private var deletePostRes: ApiState by mutableStateOf(ApiState.Empty) private var blockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) + private var unblockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) private var blockPersonRes: ApiState by mutableStateOf(ApiState.Empty) private var markPostRes: ApiState by mutableStateOf(ApiState.Empty) @@ -176,6 +180,14 @@ class HomeViewModel : ViewModel(), Initializable { } } + fun unblockCommunity(form: UnblockCommunity, ctx: Context) { + viewModelScope.launch { + unblockCommunityRes = ApiState.Loading + unblockCommunityRes = + apiWrapper(API.getInstance().unblockCommunity(form)) + showUnblockCommunityToast(unblockCommunityRes, ctx) + } + } fun blockPerson(form: BlockPerson, ctx: Context) { viewModelScope.launch { blockPersonRes = ApiState.Loading diff --git a/app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt b/app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt index 0a5f7b1f4..b98cec2e1 100644 --- a/app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt +++ b/app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt @@ -31,11 +31,14 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType +import com.jerboa.datatypes.types.UnblockCommunity +import com.jerboa.datatypes.types.UnblockCommunityResponse import com.jerboa.findAndUpdateComment import com.jerboa.findAndUpdatePost import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast import com.jerboa.showBlockPersonToast +import com.jerboa.showUnblockCommunityToast import com.jerboa.util.Initializable import kotlinx.coroutines.launch @@ -54,6 +57,8 @@ class PersonProfileViewModel : ViewModel(), Initializable { private var deletePostRes: ApiState by mutableStateOf(ApiState.Empty) private var blockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) + private var unblockCommunityRes: ApiState by + mutableStateOf(ApiState.Empty) private var blockPersonRes: ApiState by mutableStateOf(ApiState.Empty) private var likeCommentRes: ApiState by mutableStateOf(ApiState.Empty) @@ -198,7 +203,14 @@ class PersonProfileViewModel : ViewModel(), Initializable { showBlockCommunityToast(blockCommunityRes, ctx) } } - + fun unblockCommunity(form: UnblockCommunity, ctx: Context) { + viewModelScope.launch { + unblockCommunityRes = ApiState.Loading + unblockCommunityRes = + apiWrapper(API.getInstance().unblockCommunity(form)) + showUnblockCommunityToast(unblockCommunityRes, ctx) + } + } fun blockPerson(form: BlockPerson, ctx: Context) { viewModelScope.launch { blockPersonRes = ApiState.Loading diff --git a/app/src/main/java/com/jerboa/model/PostViewModel.kt b/app/src/main/java/com/jerboa/model/PostViewModel.kt index 6fa5834c5..e568e9a11 100644 --- a/app/src/main/java/com/jerboa/model/PostViewModel.kt +++ b/app/src/main/java/com/jerboa/model/PostViewModel.kt @@ -34,11 +34,14 @@ import com.jerboa.datatypes.types.PostResponse import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost +import com.jerboa.datatypes.types.UnblockCommunity +import com.jerboa.datatypes.types.UnblockCommunityResponse import com.jerboa.db.entity.Account import com.jerboa.findAndUpdateComment import com.jerboa.serializeToMap import com.jerboa.showBlockCommunityToast import com.jerboa.showBlockPersonToast +import com.jerboa.showUnblockCommunityToast import com.jerboa.util.Initializable import kotlinx.coroutines.launch @@ -67,6 +70,7 @@ class PostViewModel : ViewModel(), Initializable { private var savePostRes: ApiState by mutableStateOf(ApiState.Empty) private var deletePostRes: ApiState by mutableStateOf(ApiState.Empty) private var blockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) + private var unblockCommunityRes: ApiState by mutableStateOf(ApiState.Empty) private var blockPersonRes: ApiState by mutableStateOf(ApiState.Empty) private var markPostRes: ApiState by mutableStateOf(ApiState.Empty) @@ -265,6 +269,15 @@ class PostViewModel : ViewModel(), Initializable { } } + fun unblockCommunity(form: UnblockCommunity, ctx: Context) { + viewModelScope.launch { + unblockCommunityRes = ApiState.Loading + unblockCommunityRes = + apiWrapper(API.getInstance().unblockCommunity(form)) + showUnblockCommunityToast(unblockCommunityRes, ctx) + } + } + fun blockPerson(form: BlockPerson, ctx: Context) { viewModelScope.launch { blockPersonRes = ApiState.Loading diff --git a/app/src/main/java/com/jerboa/ui/components/community/Community.kt b/app/src/main/java/com/jerboa/ui/components/community/Community.kt index fe79f8552..b8adb887d 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/Community.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/Community.kt @@ -130,6 +130,7 @@ fun CommunityHeader( communityName: String, onClickSortType: (SortType) -> Unit, onBlockCommunityClick: () -> Unit, + onUnblockCommunityClick: () -> Unit, onClickRefresh: () -> Unit, onClickPostViewMode: (PostViewMode) -> Unit, selectedSortType: SortType, @@ -226,6 +227,10 @@ fun CommunityHeader( showMoreOptions = false onBlockCommunityClick() }, + onUnblockCommunityClick = { + showMoreOptions = false + onUnblockCommunityClick() + }, onClickCommunityInfo = onClickCommunityInfo, ) } @@ -259,6 +264,7 @@ fun CommunityMoreDropdown( expanded: Boolean, onDismissRequest: () -> Unit, onBlockCommunityClick: () -> Unit, + onUnblockCommunityClick:() -> Unit, onClickRefresh: () -> Unit, onClickCommunityInfo: () -> Unit, onClickShowPostViewModeDialog: () -> Unit, diff --git a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt index 2fd8d774f..7afa9386c 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt @@ -50,6 +50,7 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType import com.jerboa.datatypes.types.SubscribedType +import com.jerboa.datatypes.types.UnblockCommunity import com.jerboa.db.entity.isAnon import com.jerboa.feat.doIfReadyElseDisplayInfo import com.jerboa.hostName @@ -233,6 +234,25 @@ fun CommunityActivity( ) } }, + onUnblockCommunityClick = { + account.doIfReadyElseDisplayInfo( + appState, + ctx, + snackbarHostState, + scope, + siteViewModel, + accountViewModel, + ) { + communityViewModel.unblockCommunity( + UnblockCommunity( + community_id = communityId, + auth = it.jwt, + block = !communityRes.data.community_view.blocked, + ), + ctx = ctx, + ) + } + }, onClickCommunityInfo = appState::toCommunitySideBar, onClickBack = appState::navigateUp, selectedPostViewMode = getPostViewMode(appSettingsViewModel), @@ -420,6 +440,31 @@ fun CommunityActivity( else -> {} } }, + onUnblockCommunityClick = { + when (val communityRes = communityViewModel.communityRes) { + is ApiState.Success -> { + account.doIfReadyElseDisplayInfo( + appState, + ctx, + snackbarHostState, + scope, + siteViewModel, + accountViewModel, + ) { + communityViewModel.unblockCommunity( + form = UnblockCommunity( + community_id = communityRes.data.community_view.community.id, + block = !communityRes.data.community_view.blocked, + auth = it.jwt, + ), + ctx = ctx, + ) + } + } + + else -> {} + } + }, onBlockCreatorClick = { person -> account.doIfReadyElseDisplayInfo( appState, diff --git a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt index 0f3a4c0bb..dcd447ac1 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt @@ -51,6 +51,7 @@ import com.jerboa.datatypes.types.MarkPostAsRead import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.Tagline +import com.jerboa.datatypes.types.UnblockCommunity import com.jerboa.db.entity.Account import com.jerboa.db.entity.isAnon import com.jerboa.db.entity.isReady @@ -343,6 +344,25 @@ fun MainPostListingsContent( ) } }, + onUnblockCommunityClick = { community -> + account.doIfReadyElseDisplayInfo( + appState, + ctx, + snackbarHostState, + scope, + siteViewModel + ) { + homeViewModel.unblockCommunity( + UnblockCommunity( + community_id = community.id, + auth = it.jwt, + block = false, + ), + ctx = ctx, + ) + } + }, + onBlockCreatorClick = { creator -> account.doIfReadyElseDisplayInfo( appState, diff --git a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt index 2d4462c95..dc54eca2f 100644 --- a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt @@ -69,6 +69,7 @@ import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost import com.jerboa.datatypes.types.SortType +import com.jerboa.datatypes.types.UnblockCommunity import com.jerboa.db.entity.Account import com.jerboa.db.entity.isAnon import com.jerboa.feat.doIfReadyElseDisplayInfo @@ -593,8 +594,18 @@ fun UserTabs( }, onUnblockCommunityClick = { community -> - account.doIfReadyElseDisplayInfo(appState, ctx, snackbarHostState, scope, loginAsToast = true) { - personProfileViewModel.blockCommunity(BlockCommunity(community_id = community.id, block = false, auth = it.jwt), ctx) + account.doIfReadyElseDisplayInfo( + appState, + ctx, + snackbarHostState, + scope, + loginAsToast = true) { + personProfileViewModel.unblockCommunity( + UnblockCommunity( + community_id = community.id, + block = false, + auth = it.jwt), + ctx) } }, diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt index bb7b71846..52dbc13d8 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt @@ -74,6 +74,7 @@ import com.jerboa.datatypes.types.PostId import com.jerboa.datatypes.types.PostView import com.jerboa.datatypes.types.SaveComment import com.jerboa.datatypes.types.SavePost +import com.jerboa.datatypes.types.UnblockCommunity import com.jerboa.db.entity.isAnon import com.jerboa.feat.doIfReadyElseDisplayInfo import com.jerboa.getCommentParentId @@ -450,6 +451,25 @@ fun PostActivity( ) } }, + onUnblockCommunityClick = { c -> + account.doIfReadyElseDisplayInfo( + appState, + ctx, + snackbarHostState, + scope, + siteViewModel, + accountViewModel, + ) { + postViewModel.unblockCommunity( + UnblockCommunity( + community_id = c.id, + block = false, + auth = it.jwt, + ), + ctx, + ) + } + }, onBlockCreatorClick = { person -> account.doIfReadyElseDisplayInfo( appState, diff --git a/app/src/main/java/com/jerboa/util/Initializable.kt b/app/src/main/java/com/jerboa/util/Initializable.kt index d64bf273b..bccedd3b9 100644 --- a/app/src/main/java/com/jerboa/util/Initializable.kt +++ b/app/src/main/java/com/jerboa/util/Initializable.kt @@ -6,7 +6,6 @@ import androidx.compose.runtime.LaunchedEffect import kotlinx.coroutines.CoroutineScope interface Initializable { - fun unblockCommunity(unBlockCommunity: Any, ctx: Context) var initialized: Boolean }