Skip to content

Commit

Permalink
Unblocking option LemmyNet#682 attempt 5
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Schoolkid1 committed Aug 13, 2023
1 parent 218e6e3 commit 7233236
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 14 deletions.
17 changes: 16 additions & 1 deletion app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ fun showBlockPersonToast(blockPersonRes: ApiState<BlockPersonResponse>, ctx: Con
}
}

fun showBlockCommunityToast(blockCommunityRes: ApiState<BlockCommunityResponse>, ctx: BlockCommunityResponse) {
fun showBlockCommunityToast(blockCommunityRes: ApiState<BlockCommunityResponse>, ctx: Context) {
when (blockCommunityRes) {
is ApiState.Success -> {
Toast.makeText(
Expand All @@ -1234,6 +1234,21 @@ fun showBlockCommunityToast(blockCommunityRes: ApiState<BlockCommunityResponse>,
}
}

fun showUnblockCommunityToast(unblockCommunityRes: ApiState<UnblockCommunityResponse>, 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<PersonMentionView>,
updatedCommentView: CommentView,
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/jerboa/api/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ interface API {
* Block a community.
*/
@POST("community/block")
suspend fun blockCommunity(@Body form: BlockCommunityResponse): Response<BlockCommunityResponse>
suspend fun blockCommunity(@Body form: BlockCommunity): Response<BlockCommunityResponse>

@POST("community/unblock")
suspend fun unblockCommunity(@Body form: UnblockCommunity): Response<UnblockCommunityResponse>

/**
* Save your user settings.
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/jerboa/datatypes/types/UnblockCommunity.kt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
20 changes: 12 additions & 8 deletions app/src/main/java/com/jerboa/model/CommunityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -54,6 +57,8 @@ class CommunityViewModel : ViewModel(), Initializable {
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockCommunityRes: ApiState<BlockCommunityResponse> by
mutableStateOf(ApiState.Empty)
private var unblockCommunityRes: ApiState<UnblockCommunityResponse> by
mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)
private var markPostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)

Expand Down Expand Up @@ -237,23 +242,22 @@ class CommunityViewModel : ViewModel(), Initializable {
}
}
}
//private var blockCommunityRes: ApiState<BlockCommunityResponse> 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 -> {
val deleteRes =
ApiState.Success(
existing.data.copy(
community_view =
blockCommunity.data.community_view,
unblockCommunity.data.community_view,
),
)
communityRes = deleteRes
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/jerboa/model/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -48,6 +51,7 @@ class HomeViewModel : ViewModel(), Initializable {
private var savePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockCommunityRes: ApiState<BlockCommunityResponse> by mutableStateOf(ApiState.Empty)
private var unblockCommunityRes: ApiState<UnblockCommunityResponse> by mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)
private var markPostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)

Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -54,6 +57,8 @@ class PersonProfileViewModel : ViewModel(), Initializable {
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockCommunityRes: ApiState<BlockCommunityResponse> by
mutableStateOf(ApiState.Empty)
private var unblockCommunityRes: ApiState<UnblockCommunityResponse> by
mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)

private var likeCommentRes: ApiState<CommentResponse> by mutableStateOf(ApiState.Empty)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/jerboa/model/PostViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,6 +70,7 @@ class PostViewModel : ViewModel(), Initializable {
private var savePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockCommunityRes: ApiState<BlockCommunityResponse> by mutableStateOf(ApiState.Empty)
private var unblockCommunityRes: ApiState<UnblockCommunityResponse> by mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)
private var markPostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fun CommunityHeader(
communityName: String,
onClickSortType: (SortType) -> Unit,
onBlockCommunityClick: () -> Unit,
onUnblockCommunityClick: () -> Unit,
onClickRefresh: () -> Unit,
onClickPostViewMode: (PostViewMode) -> Unit,
selectedSortType: SortType,
Expand Down Expand Up @@ -226,6 +227,10 @@ fun CommunityHeader(
showMoreOptions = false
onBlockCommunityClick()
},
onUnblockCommunityClick = {
showMoreOptions = false
onUnblockCommunityClick()
},
onClickCommunityInfo = onClickCommunityInfo,
)
}
Expand Down Expand Up @@ -259,6 +264,7 @@ fun CommunityMoreDropdown(
expanded: Boolean,
onDismissRequest: () -> Unit,
onBlockCommunityClick: () -> Unit,
onUnblockCommunityClick:() -> Unit,
onClickRefresh: () -> Unit,
onClickCommunityInfo: () -> Unit,
onClickShowPostViewModeDialog: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
},

Expand Down
Loading

0 comments on commit 7233236

Please sign in to comment.