Skip to content

Commit

Permalink
CommentReplyView refactored on its own viewmodel. Added mark replied …
Browse files Browse the repository at this point in the history
…message as read. Fixes #45
  • Loading branch information
dessalines committed Jan 20, 2022
1 parent 3dc8bca commit 26da8fd
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 74 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.jerboa.db.AppDB
import com.jerboa.ui.components.comment.edit.CommentEditActivity
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
import com.jerboa.ui.components.comment.reply.CommentReplyActivity
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.community.CommunityActivity
import com.jerboa.ui.components.community.CommunityViewModel
Expand Down Expand Up @@ -54,6 +55,7 @@ class MainActivity : ComponentActivity() {
private val inboxViewModel by viewModels<InboxViewModel>()
private val communityListViewModel by viewModels<CommunityListViewModel>()
private val createPostViewModel by viewModels<CreatePostViewModel>()
private val commentReplyViewModel by viewModels<CommentReplyViewModel>()
private val commentEditViewModel by viewModels<CommentEditViewModel>()
private val postEditViewModel by viewModels<PostEditViewModel>()

Expand Down Expand Up @@ -128,6 +130,7 @@ class MainActivity : ComponentActivity() {
homeViewModel = homeViewModel,
inboxViewModel = inboxViewModel,
commentEditViewModel = commentEditViewModel,
commentReplyViewModel = commentReplyViewModel,
postEditViewModel = postEditViewModel,
)
}
Expand Down Expand Up @@ -170,6 +173,7 @@ class MainActivity : ComponentActivity() {
accountViewModel = accountViewModel,
homeViewModel = homeViewModel,
commentEditViewModel = commentEditViewModel,
commentReplyViewModel = commentReplyViewModel,
)
}
composable(
Expand All @@ -181,6 +185,7 @@ class MainActivity : ComponentActivity() {
communityViewModel = communityViewModel,
personProfileViewModel = personProfileViewModel,
commentEditViewModel = commentEditViewModel,
commentReplyViewModel = commentReplyViewModel,
postEditViewModel = postEditViewModel,
navController = navController,
)
Expand All @@ -189,9 +194,11 @@ class MainActivity : ComponentActivity() {
route = "commentReply",
) {
CommentReplyActivity(
commentReplyViewModel = commentReplyViewModel,
postViewModel = postViewModel,
accountViewModel = accountViewModel,
personProfileViewModel = personProfileViewModel,
inboxViewModel = inboxViewModel,
navController = navController,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,14 @@ fun CommentFooterLine(
},
account = account
)
ActionBarButton(
icon = Icons.Filled.Reply,
onClick = { onReplyClick(commentView) },
account = account,
)
// Don't let you respond to your own comment.
if (commentView.creator.id != account?.id) {
ActionBarButton(
icon = Icons.Filled.Reply,
onClick = { onReplyClick(commentView) },
account = account,
)
}
ActionBarButton(
icon = Icons.Filled.MoreVert,
account = account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,48 @@ fun markPrivateMessageAsReadRoutine(
}

fun createCommentRoutine(
comments: MutableList<CommentView>? = null,
loading: MutableState<Boolean>,
form: CreateComment,
content: String,
parentCommentView: CommentView?,
postId: Int,
ctx: Context,
scope: CoroutineScope,
navController: NavController,
focusManager: FocusManager,
account: Account,
postViewModel: PostViewModel,
personProfileViewModel: PersonProfileViewModel,
inboxViewModel: InboxViewModel,
) {
scope.launch {
loading.value = true
val form = CreateComment(
content = content,
parent_id = parentCommentView?.comment?.id,
post_id = postId,
auth = account.jwt
)
val commentView = createCommentWrapper(form, ctx).comment_view
comments?.add(0, commentView)

loading.value = false
focusManager.clearFocus()

// Add to all the views which might have your comment
addCommentToMutableList(postViewModel.comments, commentView)

// Maybe a back button would view this page.
if (account.id == personProfileViewModel.personId.value) {
addCommentToMutableList(personProfileViewModel.comments, commentView)
}

// Mark as read if you replied to it, and the grandparent is you
parentCommentView?.also { pcv ->
if (listOf(pcv.comment.parent_id, pcv.post.creator_id).contains(account.id)) {
val readCommentView = pcv.copy(comment = pcv.comment.copy(read = true))
findAndUpdateComment(inboxViewModel.replies, readCommentView)
}
}

navController.navigateUp()
}
}
Expand Down Expand Up @@ -218,6 +246,13 @@ fun findAndUpdateComment(
}
}

fun addCommentToMutableList(
comments: MutableList<CommentView>,
newCommentView: CommentView
) {
comments.add(0, newCommentView)
}

fun findAndUpdateMention(
mentions: MutableList<PersonMentionView>,
updatedPersonMentionView: PersonMentionView?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,17 @@ fun PostReply(
}
}
}

fun commentReplyClickWrapper(
commentReplyViewModel: CommentReplyViewModel,
postId: Int,
parentCommentView: CommentView? = null,
postView: PostView? = null,
navController: NavController,
) {
// Post id is mandatory, but the other two only one must be set
commentReplyViewModel.setPostId(postId)
commentReplyViewModel.setCommentParentView(parentCommentView)
commentReplyViewModel.setPostView(postView)
navController.navigate("commentReply")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.navigation.NavController
import com.jerboa.api.uploadPictrsImage
import com.jerboa.appendMarkdownImage
import com.jerboa.datatypes.api.CreateComment
import com.jerboa.db.AccountViewModel
import com.jerboa.imageInputStreamFromUri
import com.jerboa.isModerator
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.inbox.InboxViewModel
import com.jerboa.ui.components.person.PersonProfileViewModel
import com.jerboa.ui.components.person.personClickWrapper
import com.jerboa.ui.components.post.PostViewModel
import kotlinx.coroutines.launch

// TODO this should probably be refactored to not rely on postViewModel, since you should be able
// to create comments from many other screens.
// It should have its own viewmodel
@Composable
fun CommentReplyActivity(
postViewModel: PostViewModel,
commentReplyViewModel: CommentReplyViewModel,
accountViewModel: AccountViewModel,
personProfileViewModel: PersonProfileViewModel,
postViewModel: PostViewModel,
inboxViewModel: InboxViewModel,
navController: NavController,
) {

Expand All @@ -50,28 +49,27 @@ fun CommentReplyActivity(
CommentReplyHeader(
navController = navController,
onSendClick = {
postViewModel.postView.value?.also { postView ->
account?.also { account ->
val parentId = postViewModel.replyToCommentParent?.comment?.id
val form =
CreateComment(
content = reply,
parent_id = parentId,
post_id = postView.post.id,
auth = account.jwt
)
postViewModel.createComment(form, ctx, navController, focusManager)
}
account?.also { acct ->
commentReplyViewModel.createComment(
content = reply,
account = acct,
ctx = ctx,
navController = navController,
focusManager = focusManager,
personProfileViewModel = personProfileViewModel,
postViewModel = postViewModel,
inboxViewModel = inboxViewModel,
)
}
}
)
},
content = {
if (postViewModel.replyLoading.value) {
if (commentReplyViewModel.loading.value) {
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
} else {

postViewModel.replyToCommentParent?.also { commentView ->
commentReplyViewModel.commentParentView.value?.also { commentView ->
CommentReply(
commentView = commentView,
reply = reply,
Expand All @@ -97,7 +95,7 @@ fun CommentReplyActivity(
isModerator(commentView.creator, postViewModel.moderators)
)
} ?: run {
postViewModel.postView.value?.also { postView ->
commentReplyViewModel.postView.value?.also { postView ->
PostReply(
postView = postView,
reply = reply,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.jerboa.ui.components.comment.reply

import android.content.Context
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.focus.FocusManager
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import com.jerboa.datatypes.CommentView
import com.jerboa.datatypes.PostView
import com.jerboa.db.Account
import com.jerboa.ui.components.comment.createCommentRoutine
import com.jerboa.ui.components.inbox.InboxViewModel
import com.jerboa.ui.components.person.PersonProfileViewModel
import com.jerboa.ui.components.post.PostViewModel

class CommentReplyViewModel : ViewModel() {

var commentParentView = mutableStateOf<CommentView?>(null)
private set
var postId = mutableStateOf<Int?>(null)
private set
var postView = mutableStateOf<PostView?>(null)
private set
var loading = mutableStateOf(false)
private set

fun setCommentParentView(
newCommentParentView: CommentView?
) {
commentParentView.value = newCommentParentView
}

fun setPostView(
newPostView: PostView?
) {
postView.value = newPostView
}

fun setPostId(
newPostId: Int
) {
postId.value = newPostId
}

fun createComment(
content: String,
account: Account,
ctx: Context,
navController: NavController,
focusManager: FocusManager,
personProfileViewModel: PersonProfileViewModel,
postViewModel: PostViewModel,
inboxViewModel: InboxViewModel,
) {
postId.value?.also { postId ->
createCommentRoutine(
content = content,
parentCommentView = commentParentView.value,
postId = postId,
account = account,
loading = loading,
ctx = ctx,
scope = viewModelScope,
navController = navController,
focusManager = focusManager,
personProfileViewModel = personProfileViewModel,
postViewModel = postViewModel,
inboxViewModel = inboxViewModel,
)
}
}
}
18 changes: 10 additions & 8 deletions app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.jerboa.db.AccountViewModel
import com.jerboa.ui.components.comment.CommentNode
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
import com.jerboa.ui.components.comment.edit.commentEditClickWrapper
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
import com.jerboa.ui.components.comment.reply.commentReplyClickWrapper
import com.jerboa.ui.components.common.BottomAppBarAll
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.community.CommunityViewModel
Expand All @@ -48,6 +50,7 @@ fun InboxActivity(
communityViewModel: CommunityViewModel,
accountViewModel: AccountViewModel,
commentEditViewModel: CommentEditViewModel,
commentReplyViewModel: CommentReplyViewModel,
) {

Log.d("jerboa", "got to inbox activity")
Expand Down Expand Up @@ -106,6 +109,7 @@ fun InboxActivity(
navController = navController,
personProfileViewModel = personProfileViewModel,
commentEditViewModel = commentEditViewModel,
commentReplyViewModel = commentReplyViewModel,
inboxViewModel = inboxViewModel,
postViewModel = postViewModel,
communityViewModel = communityViewModel,
Expand Down Expand Up @@ -159,6 +163,7 @@ fun InboxTabs(
scope: CoroutineScope,
postViewModel: PostViewModel,
commentEditViewModel: CommentEditViewModel,
commentReplyViewModel: CommentReplyViewModel,
padding: PaddingValues,
) {
val tabTitles = InboxTab.values().map { it.toString() }
Expand Down Expand Up @@ -272,15 +277,12 @@ fun InboxTabs(
}
},
onReplyClick = { commentView ->
// TODO To do replies from elsewhere than postView,
// you need to refetch that post view
postViewModel.replyToCommentParent = commentView
postViewModel.fetchPost(
id = commentView.post.id,
account = account,
ctx = ctx,
commentReplyClickWrapper(
commentReplyViewModel = commentReplyViewModel,
parentCommentView = commentView,
postId = commentView.post.id,
navController = navController,
)
navController.navigate("commentReply")
},
onEditCommentClick = { commentView ->
commentEditClickWrapper(
Expand Down
Loading

0 comments on commit 26da8fd

Please sign in to comment.