Skip to content

Commit 26da8fd

Browse files
committed
CommentReplyView refactored on its own viewmodel. Added mark replied message as read. Fixes #45
1 parent 3dc8bca commit 26da8fd

File tree

10 files changed

+194
-74
lines changed

10 files changed

+194
-74
lines changed

Diff for: app/src/main/java/com/jerboa/MainActivity.kt

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.jerboa.db.AppDB
1717
import com.jerboa.ui.components.comment.edit.CommentEditActivity
1818
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
1919
import com.jerboa.ui.components.comment.reply.CommentReplyActivity
20+
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
2021
import com.jerboa.ui.components.common.getCurrentAccount
2122
import com.jerboa.ui.components.community.CommunityActivity
2223
import com.jerboa.ui.components.community.CommunityViewModel
@@ -54,6 +55,7 @@ class MainActivity : ComponentActivity() {
5455
private val inboxViewModel by viewModels<InboxViewModel>()
5556
private val communityListViewModel by viewModels<CommunityListViewModel>()
5657
private val createPostViewModel by viewModels<CreatePostViewModel>()
58+
private val commentReplyViewModel by viewModels<CommentReplyViewModel>()
5759
private val commentEditViewModel by viewModels<CommentEditViewModel>()
5860
private val postEditViewModel by viewModels<PostEditViewModel>()
5961

@@ -128,6 +130,7 @@ class MainActivity : ComponentActivity() {
128130
homeViewModel = homeViewModel,
129131
inboxViewModel = inboxViewModel,
130132
commentEditViewModel = commentEditViewModel,
133+
commentReplyViewModel = commentReplyViewModel,
131134
postEditViewModel = postEditViewModel,
132135
)
133136
}
@@ -170,6 +173,7 @@ class MainActivity : ComponentActivity() {
170173
accountViewModel = accountViewModel,
171174
homeViewModel = homeViewModel,
172175
commentEditViewModel = commentEditViewModel,
176+
commentReplyViewModel = commentReplyViewModel,
173177
)
174178
}
175179
composable(
@@ -181,6 +185,7 @@ class MainActivity : ComponentActivity() {
181185
communityViewModel = communityViewModel,
182186
personProfileViewModel = personProfileViewModel,
183187
commentEditViewModel = commentEditViewModel,
188+
commentReplyViewModel = commentReplyViewModel,
184189
postEditViewModel = postEditViewModel,
185190
navController = navController,
186191
)
@@ -189,9 +194,11 @@ class MainActivity : ComponentActivity() {
189194
route = "commentReply",
190195
) {
191196
CommentReplyActivity(
197+
commentReplyViewModel = commentReplyViewModel,
192198
postViewModel = postViewModel,
193199
accountViewModel = accountViewModel,
194200
personProfileViewModel = personProfileViewModel,
201+
inboxViewModel = inboxViewModel,
195202
navController = navController,
196203
)
197204
}

Diff for: app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,14 @@ fun CommentFooterLine(
312312
},
313313
account = account
314314
)
315-
ActionBarButton(
316-
icon = Icons.Filled.Reply,
317-
onClick = { onReplyClick(commentView) },
318-
account = account,
319-
)
315+
// Don't let you respond to your own comment.
316+
if (commentView.creator.id != account?.id) {
317+
ActionBarButton(
318+
icon = Icons.Filled.Reply,
319+
onClick = { onReplyClick(commentView) },
320+
account = account,
321+
)
322+
}
320323
ActionBarButton(
321324
icon = Icons.Filled.MoreVert,
322325
account = account,

Diff for: app/src/main/java/com/jerboa/ui/components/comment/CommentRoutines.kt

+38-3
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,48 @@ fun markPrivateMessageAsReadRoutine(
132132
}
133133

134134
fun createCommentRoutine(
135-
comments: MutableList<CommentView>? = null,
136135
loading: MutableState<Boolean>,
137-
form: CreateComment,
136+
content: String,
137+
parentCommentView: CommentView?,
138+
postId: Int,
138139
ctx: Context,
139140
scope: CoroutineScope,
140141
navController: NavController,
141142
focusManager: FocusManager,
143+
account: Account,
144+
postViewModel: PostViewModel,
145+
personProfileViewModel: PersonProfileViewModel,
146+
inboxViewModel: InboxViewModel,
142147
) {
143148
scope.launch {
144149
loading.value = true
150+
val form = CreateComment(
151+
content = content,
152+
parent_id = parentCommentView?.comment?.id,
153+
post_id = postId,
154+
auth = account.jwt
155+
)
145156
val commentView = createCommentWrapper(form, ctx).comment_view
146-
comments?.add(0, commentView)
157+
147158
loading.value = false
148159
focusManager.clearFocus()
160+
161+
// Add to all the views which might have your comment
162+
addCommentToMutableList(postViewModel.comments, commentView)
163+
164+
// Maybe a back button would view this page.
165+
if (account.id == personProfileViewModel.personId.value) {
166+
addCommentToMutableList(personProfileViewModel.comments, commentView)
167+
}
168+
169+
// Mark as read if you replied to it, and the grandparent is you
170+
parentCommentView?.also { pcv ->
171+
if (listOf(pcv.comment.parent_id, pcv.post.creator_id).contains(account.id)) {
172+
val readCommentView = pcv.copy(comment = pcv.comment.copy(read = true))
173+
findAndUpdateComment(inboxViewModel.replies, readCommentView)
174+
}
175+
}
176+
149177
navController.navigateUp()
150178
}
151179
}
@@ -218,6 +246,13 @@ fun findAndUpdateComment(
218246
}
219247
}
220248

249+
fun addCommentToMutableList(
250+
comments: MutableList<CommentView>,
251+
newCommentView: CommentView
252+
) {
253+
comments.add(0, newCommentView)
254+
}
255+
221256
fun findAndUpdateMention(
222257
mentions: MutableList<PersonMentionView>,
223258
updatedPersonMentionView: PersonMentionView?

Diff for: app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReply.kt

+14
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,17 @@ fun PostReply(
166166
}
167167
}
168168
}
169+
170+
fun commentReplyClickWrapper(
171+
commentReplyViewModel: CommentReplyViewModel,
172+
postId: Int,
173+
parentCommentView: CommentView? = null,
174+
postView: PostView? = null,
175+
navController: NavController,
176+
) {
177+
// Post id is mandatory, but the other two only one must be set
178+
commentReplyViewModel.setPostId(postId)
179+
commentReplyViewModel.setCommentParentView(parentCommentView)
180+
commentReplyViewModel.setPostView(postView)
181+
navController.navigate("commentReply")
182+
}

Diff for: app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt

+18-20
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ import androidx.compose.ui.platform.LocalFocusManager
1414
import androidx.navigation.NavController
1515
import com.jerboa.api.uploadPictrsImage
1616
import com.jerboa.appendMarkdownImage
17-
import com.jerboa.datatypes.api.CreateComment
1817
import com.jerboa.db.AccountViewModel
1918
import com.jerboa.imageInputStreamFromUri
2019
import com.jerboa.isModerator
2120
import com.jerboa.ui.components.common.getCurrentAccount
21+
import com.jerboa.ui.components.inbox.InboxViewModel
2222
import com.jerboa.ui.components.person.PersonProfileViewModel
2323
import com.jerboa.ui.components.person.personClickWrapper
2424
import com.jerboa.ui.components.post.PostViewModel
2525
import kotlinx.coroutines.launch
2626

27-
// TODO this should probably be refactored to not rely on postViewModel, since you should be able
28-
// to create comments from many other screens.
29-
// It should have its own viewmodel
3027
@Composable
3128
fun CommentReplyActivity(
32-
postViewModel: PostViewModel,
29+
commentReplyViewModel: CommentReplyViewModel,
3330
accountViewModel: AccountViewModel,
3431
personProfileViewModel: PersonProfileViewModel,
32+
postViewModel: PostViewModel,
33+
inboxViewModel: InboxViewModel,
3534
navController: NavController,
3635
) {
3736

@@ -50,28 +49,27 @@ fun CommentReplyActivity(
5049
CommentReplyHeader(
5150
navController = navController,
5251
onSendClick = {
53-
postViewModel.postView.value?.also { postView ->
54-
account?.also { account ->
55-
val parentId = postViewModel.replyToCommentParent?.comment?.id
56-
val form =
57-
CreateComment(
58-
content = reply,
59-
parent_id = parentId,
60-
post_id = postView.post.id,
61-
auth = account.jwt
62-
)
63-
postViewModel.createComment(form, ctx, navController, focusManager)
64-
}
52+
account?.also { acct ->
53+
commentReplyViewModel.createComment(
54+
content = reply,
55+
account = acct,
56+
ctx = ctx,
57+
navController = navController,
58+
focusManager = focusManager,
59+
personProfileViewModel = personProfileViewModel,
60+
postViewModel = postViewModel,
61+
inboxViewModel = inboxViewModel,
62+
)
6563
}
6664
}
6765
)
6866
},
6967
content = {
70-
if (postViewModel.replyLoading.value) {
68+
if (commentReplyViewModel.loading.value) {
7169
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
7270
} else {
7371

74-
postViewModel.replyToCommentParent?.also { commentView ->
72+
commentReplyViewModel.commentParentView.value?.also { commentView ->
7573
CommentReply(
7674
commentView = commentView,
7775
reply = reply,
@@ -97,7 +95,7 @@ fun CommentReplyActivity(
9795
isModerator(commentView.creator, postViewModel.moderators)
9896
)
9997
} ?: run {
100-
postViewModel.postView.value?.also { postView ->
98+
commentReplyViewModel.postView.value?.also { postView ->
10199
PostReply(
102100
postView = postView,
103101
reply = reply,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.jerboa.ui.components.comment.reply
2+
3+
import android.content.Context
4+
import androidx.compose.runtime.mutableStateOf
5+
import androidx.compose.ui.focus.FocusManager
6+
import androidx.lifecycle.ViewModel
7+
import androidx.lifecycle.viewModelScope
8+
import androidx.navigation.NavController
9+
import com.jerboa.datatypes.CommentView
10+
import com.jerboa.datatypes.PostView
11+
import com.jerboa.db.Account
12+
import com.jerboa.ui.components.comment.createCommentRoutine
13+
import com.jerboa.ui.components.inbox.InboxViewModel
14+
import com.jerboa.ui.components.person.PersonProfileViewModel
15+
import com.jerboa.ui.components.post.PostViewModel
16+
17+
class CommentReplyViewModel : ViewModel() {
18+
19+
var commentParentView = mutableStateOf<CommentView?>(null)
20+
private set
21+
var postId = mutableStateOf<Int?>(null)
22+
private set
23+
var postView = mutableStateOf<PostView?>(null)
24+
private set
25+
var loading = mutableStateOf(false)
26+
private set
27+
28+
fun setCommentParentView(
29+
newCommentParentView: CommentView?
30+
) {
31+
commentParentView.value = newCommentParentView
32+
}
33+
34+
fun setPostView(
35+
newPostView: PostView?
36+
) {
37+
postView.value = newPostView
38+
}
39+
40+
fun setPostId(
41+
newPostId: Int
42+
) {
43+
postId.value = newPostId
44+
}
45+
46+
fun createComment(
47+
content: String,
48+
account: Account,
49+
ctx: Context,
50+
navController: NavController,
51+
focusManager: FocusManager,
52+
personProfileViewModel: PersonProfileViewModel,
53+
postViewModel: PostViewModel,
54+
inboxViewModel: InboxViewModel,
55+
) {
56+
postId.value?.also { postId ->
57+
createCommentRoutine(
58+
content = content,
59+
parentCommentView = commentParentView.value,
60+
postId = postId,
61+
account = account,
62+
loading = loading,
63+
ctx = ctx,
64+
scope = viewModelScope,
65+
navController = navController,
66+
focusManager = focusManager,
67+
personProfileViewModel = personProfileViewModel,
68+
postViewModel = postViewModel,
69+
inboxViewModel = inboxViewModel,
70+
)
71+
}
72+
}
73+
}

Diff for: app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import com.jerboa.db.AccountViewModel
2525
import com.jerboa.ui.components.comment.CommentNode
2626
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
2727
import com.jerboa.ui.components.comment.edit.commentEditClickWrapper
28+
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
29+
import com.jerboa.ui.components.comment.reply.commentReplyClickWrapper
2830
import com.jerboa.ui.components.common.BottomAppBarAll
2931
import com.jerboa.ui.components.common.getCurrentAccount
3032
import com.jerboa.ui.components.community.CommunityViewModel
@@ -48,6 +50,7 @@ fun InboxActivity(
4850
communityViewModel: CommunityViewModel,
4951
accountViewModel: AccountViewModel,
5052
commentEditViewModel: CommentEditViewModel,
53+
commentReplyViewModel: CommentReplyViewModel,
5154
) {
5255

5356
Log.d("jerboa", "got to inbox activity")
@@ -106,6 +109,7 @@ fun InboxActivity(
106109
navController = navController,
107110
personProfileViewModel = personProfileViewModel,
108111
commentEditViewModel = commentEditViewModel,
112+
commentReplyViewModel = commentReplyViewModel,
109113
inboxViewModel = inboxViewModel,
110114
postViewModel = postViewModel,
111115
communityViewModel = communityViewModel,
@@ -159,6 +163,7 @@ fun InboxTabs(
159163
scope: CoroutineScope,
160164
postViewModel: PostViewModel,
161165
commentEditViewModel: CommentEditViewModel,
166+
commentReplyViewModel: CommentReplyViewModel,
162167
padding: PaddingValues,
163168
) {
164169
val tabTitles = InboxTab.values().map { it.toString() }
@@ -272,15 +277,12 @@ fun InboxTabs(
272277
}
273278
},
274279
onReplyClick = { commentView ->
275-
// TODO To do replies from elsewhere than postView,
276-
// you need to refetch that post view
277-
postViewModel.replyToCommentParent = commentView
278-
postViewModel.fetchPost(
279-
id = commentView.post.id,
280-
account = account,
281-
ctx = ctx,
280+
commentReplyClickWrapper(
281+
commentReplyViewModel = commentReplyViewModel,
282+
parentCommentView = commentView,
283+
postId = commentView.post.id,
284+
navController = navController,
282285
)
283-
navController.navigate("commentReply")
284286
},
285287
onEditCommentClick = { commentView ->
286288
commentEditClickWrapper(

0 commit comments

Comments
 (0)