Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post removing #1324

Merged
merged 14 commits into from
Jan 21, 2024
Merged
7 changes: 7 additions & 0 deletions app/src/main/java/com/jerboa/JerboaAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import com.jerboa.ui.components.post.create.CreatePostReturn
import com.jerboa.ui.components.post.edit.PostEditReturn
import com.jerboa.ui.components.privatemessage.PrivateMessage
import com.jerboa.ui.components.remove.comment.CommentRemoveReturn
import com.jerboa.ui.components.remove.post.PostRemoveReturn
import it.vercruysse.lemmyapi.v0x19.datatypes.Comment
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentView
import it.vercruysse.lemmyapi.v0x19.datatypes.Community
import it.vercruysse.lemmyapi.v0x19.datatypes.CommunityView
import it.vercruysse.lemmyapi.v0x19.datatypes.Post
import it.vercruysse.lemmyapi.v0x19.datatypes.PostView
import it.vercruysse.lemmyapi.v0x19.datatypes.PrivateMessageView
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -70,6 +72,11 @@ class JerboaAppState(
navController.navigate(Route.PostReportArgs.makeRoute(id = "$id"))
}

fun toPostRemove(post: Post) {
sendReturnForwards(PostRemoveReturn.POST_SEND, post)
navController.navigate(Route.POST_REMOVE)
}

fun toCommentRemove(comment: Comment) {
sendReturnForwards(CommentRemoveReturn.COMMENT_SEND, comment)
navController.navigate(Route.COMMENT_REMOVE)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.jerboa.ui.components.post.edit.PostEditActivity
import com.jerboa.ui.components.privatemessage.CreatePrivateMessageActivity
import com.jerboa.ui.components.privatemessage.PrivateMessageReplyActivity
import com.jerboa.ui.components.remove.comment.CommentRemoveActivity
import com.jerboa.ui.components.remove.post.PostRemoveActivity
import com.jerboa.ui.components.report.comment.CreateCommentReportActivity
import com.jerboa.ui.components.report.post.CreatePostReportActivity
import com.jerboa.ui.components.settings.SettingsActivity
Expand Down Expand Up @@ -557,6 +558,15 @@ class MainActivity : AppCompatActivity() {
)
}

composable(
route = Route.POST_REMOVE,
) {
PostRemoveActivity(
appState = appState,
accountViewModel = accountViewModel,
)
}

composable(
route = Route.COMMENT_REMOVE,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.toApiState
import com.jerboa.ui.components.common.apiErrorToast
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentId
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentResponse
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentView
import it.vercruysse.lemmyapi.v0x19.datatypes.RemoveComment
import kotlinx.coroutines.launch

class RemoveViewModel : ViewModel() {
class CommentRemoveViewModel : ViewModel() {
dessalines marked this conversation as resolved.
Show resolved Hide resolved
var commentRemoveRes: ApiState<CommentResponse> by mutableStateOf(ApiState.Empty)
private set

fun removeOrRestoreComment(
commentId: Int,
commentId: CommentId,
removed: Boolean,
reason: String,
ctx: Context,
Expand Down
69 changes: 69 additions & 0 deletions app/src/main/java/com/jerboa/model/PostRemoveViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.jerboa.model

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.focus.FocusManager
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jerboa.R
import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.toApiState
import com.jerboa.ui.components.common.apiErrorToast
import it.vercruysse.lemmyapi.v0x19.datatypes.PostId
import it.vercruysse.lemmyapi.v0x19.datatypes.PostResponse
import it.vercruysse.lemmyapi.v0x19.datatypes.PostView
import it.vercruysse.lemmyapi.v0x19.datatypes.RemovePost
import kotlinx.coroutines.launch

class PostRemoveViewModel : ViewModel() {
var postRemoveRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private set

fun removeOrRestorePost(
postId: PostId,
removed: Boolean,
reason: String,
ctx: Context,
focusManager: FocusManager,
onSuccess: (PostView) -> Unit,
) {
viewModelScope.launch {
val form =
RemovePost(
post_id = postId,
removed = removed,
reason = reason,
)

postRemoveRes = ApiState.Loading
postRemoveRes = API.getInstance().removePost(form).toApiState()

when (val res = postRemoveRes) {
is ApiState.Failure -> {
Log.d("removePost", "failed", res.msg)
apiErrorToast(msg = res.msg, ctx = ctx)
}

is ApiState.Success -> {
val message =
if (removed) {
ctx.getString(R.string.remove_view_model_post_removed)
} else {
ctx.getString(R.string.remove_view_model_post_restored)
}
val postView = res.data.post_view
Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show()

focusManager.clearFocus()
onSuccess(postView)
}
else -> {}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object Route {
const val SITE_SIDEBAR = "siteSidebar"
const val COMMENT_EDIT = "commentEdit"
const val POST_EDIT = "postEdit"
const val POST_REMOVE = "postRemove"
const val PRIVATE_MESSAGE_REPLY = "privateMessageReply"
const val COMMENT_REMOVE = "commentRemove"
val CREATE_PRIVATE_MESSAGE = CreatePrivateMessageArgs.route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.jerboa.ui.components.common.isRefreshing
import com.jerboa.ui.components.post.PostListings
import com.jerboa.ui.components.post.PostViewReturn
import com.jerboa.ui.components.post.edit.PostEditReturn
import com.jerboa.ui.components.remove.post.PostRemoveReturn
import it.vercruysse.lemmyapi.dto.SubscribedType
import it.vercruysse.lemmyapi.v0x19.datatypes.BlockCommunity
import it.vercruysse.lemmyapi.v0x19.datatypes.CommunityId
Expand Down Expand Up @@ -96,6 +97,7 @@ fun CommunityActivity(
viewModel(factory = CommunityViewModel.Companion.Factory(communityArg))

appState.ConsumeReturn<PostView>(PostEditReturn.POST_VIEW, communityViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostRemoveReturn.POST_VIEW, communityViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostViewReturn.POST_VIEW, communityViewModel::updatePost)

val pullRefreshState =
Expand All @@ -106,6 +108,14 @@ fun CommunityActivity(
},
)

val moderators =
dessalines marked this conversation as resolved.
Show resolved Hide resolved
when (val communityRes = communityViewModel.communityRes) {
is ApiState.Success -> communityRes.data.moderators.toImmutableList()
else -> {
null
}
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
snackbarHost = { JerboaSnackbarHost(snackbarHostState) },
Expand Down Expand Up @@ -192,6 +202,8 @@ fun CommunityActivity(
is ApiState.Holder -> {
PostListings(
posts = postsRes.data.posts.toImmutableList(),
admins = siteViewModel.admins(),
moderators = moderators,
contentAboveListings = {
when (val communityRes = communityViewModel.communityRes) {
is ApiState.Success -> {
Expand Down Expand Up @@ -315,6 +327,9 @@ fun CommunityActivity(
onReportClick = { postView ->
appState.toPostReport(id = postView.post.id)
},
onRemoveClick = { pv ->
appState.toPostRemove(post = pv.post)
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.jerboa.ui.components.common.isRefreshing
import com.jerboa.ui.components.post.PostListings
import com.jerboa.ui.components.post.PostViewReturn
import com.jerboa.ui.components.post.edit.PostEditReturn
import com.jerboa.ui.components.remove.post.PostRemoveReturn
import it.vercruysse.lemmyapi.v0x19.datatypes.CreatePostLike
import it.vercruysse.lemmyapi.v0x19.datatypes.DeletePost
import it.vercruysse.lemmyapi.v0x19.datatypes.MarkPostAsRead
Expand Down Expand Up @@ -104,6 +105,7 @@ fun HomeActivity(
val snackbarHostState = remember(account) { SnackbarHostState() }

appState.ConsumeReturn<PostView>(PostEditReturn.POST_VIEW, homeViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostRemoveReturn.POST_VIEW, homeViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostViewReturn.POST_VIEW, homeViewModel::updatePost)

LaunchedEffect(account) {
Expand Down Expand Up @@ -251,6 +253,9 @@ fun MainPostListingsContent(

PostListings(
posts = posts,
admins = siteViewModel.admins(),
// No community moderators available here
moderators = null,
contentAboveListings = { if (taglines !== null) Taglines(taglines = taglines.toImmutableList()) },
onUpvoteClick = { postView ->
account.doIfReadyElseDisplayInfo(
Expand Down Expand Up @@ -335,6 +340,9 @@ fun MainPostListingsContent(
onReportClick = { postView ->
appState.toPostReport(id = postView.post.id)
},
onRemoveClick = { pv ->
appState.toPostRemove(post = pv.post)
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import com.jerboa.ui.components.post.PostListings
import com.jerboa.ui.components.post.PostViewReturn
import com.jerboa.ui.components.post.edit.PostEditReturn
import com.jerboa.ui.components.remove.comment.CommentRemoveReturn
import com.jerboa.ui.components.remove.post.PostRemoveReturn
import com.jerboa.ui.theme.MEDIUM_PADDING
import it.vercruysse.lemmyapi.v0x19.datatypes.BlockPerson
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentView
Expand Down Expand Up @@ -130,6 +131,7 @@ fun PersonProfileActivity(
viewModel(factory = PersonProfileViewModel.Companion.Factory(personArg, savedMode))

appState.ConsumeReturn<PostView>(PostEditReturn.POST_VIEW, personProfileViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostRemoveReturn.POST_VIEW, personProfileViewModel::updatePost)
appState.ConsumeReturn<CommentView>(CommentEditReturn.COMMENT_VIEW, personProfileViewModel::updateComment)
appState.ConsumeReturn<CommentView>(CommentRemoveReturn.COMMENT_VIEW, personProfileViewModel::updateComment)

Expand Down Expand Up @@ -453,6 +455,9 @@ fun UserTabs(
is ApiState.Holder -> {
PostListings(
posts = profileRes.data.posts.toImmutableList(),
admins = siteViewModel.admins(),
// No community moderators available here
moderators = null,
onUpvoteClick = { pv ->
account.doIfReadyElseDisplayInfo(
appState,
Expand Down Expand Up @@ -536,6 +541,9 @@ fun UserTabs(
onReportClick = { pv ->
appState.toPostReport(id = pv.post.id)
},
onRemoveClick = { pv ->
appState.toPostRemove(post = pv.post)
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import com.jerboa.ui.components.common.isRefreshing
import com.jerboa.ui.components.common.simpleVerticalScrollbar
import com.jerboa.ui.components.post.edit.PostEditReturn
import com.jerboa.ui.components.remove.comment.CommentRemoveReturn
import com.jerboa.ui.components.remove.post.PostRemoveReturn
import it.vercruysse.lemmyapi.dto.CommentSortType
import it.vercruysse.lemmyapi.v0x19.datatypes.*
import kotlinx.collections.immutable.toImmutableList
Expand Down Expand Up @@ -140,6 +141,7 @@ fun PostActivity(
val postViewModel: PostViewModel = viewModel(factory = PostViewModel.Companion.Factory(id))

appState.ConsumeReturn<PostView>(PostEditReturn.POST_VIEW, postViewModel::updatePost)
appState.ConsumeReturn<PostView>(PostRemoveReturn.POST_VIEW, postViewModel::updatePost)
appState.ConsumeReturn<CommentView>(CommentReplyReturn.COMMENT_VIEW, postViewModel::appendComment)
appState.ConsumeReturn<CommentView>(CommentEditReturn.COMMENT_VIEW, postViewModel::updateComment)
appState.ConsumeReturn<CommentView>(CommentRemoveReturn.COMMENT_VIEW, postViewModel::updateComment)
Expand Down Expand Up @@ -295,6 +297,8 @@ fun PostActivity(
item(key = "${postView.post.id}_listing", "post_listing") {
PostListing(
postView = postView,
admins = siteViewModel.admins(),
moderators = moderators,
onUpvoteClick = { pv ->
account.doIfReadyElseDisplayInfo(
appState,
Expand Down Expand Up @@ -388,6 +392,9 @@ fun PostActivity(
onReportClick = { pv ->
appState.toPostReport(id = pv.post.id)
},
onRemoveClick = { pv ->
appState.toPostRemove(post = pv.post)
},
onPersonClick = appState::toProfile,
showReply = true, // Do nothing
fullBody = true,
Expand Down
Loading