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

Adding admin / mod view votes. #1331

Merged
merged 10 commits into from
Feb 5, 2024
8 changes: 8 additions & 0 deletions app/src/main/java/com/jerboa/JerboaAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ class JerboaAppState(
navController.navigate(Route.ProfileFromIdArgs.makeRoute(id = "$id", saved = "$saved"))
}

fun toPostLikes(postId: PostId) {
navController.navigate(Route.PostLikesArgs.makeRoute(id = "$postId"))
}

fun toCommentLikes(commentId: CommentId) {
navController.navigate(Route.CommentLikesArgs.makeRoute(id = "$commentId"))
}

fun toCommunityList(select: Boolean = Route.CommunityListArgs.SELECT_DEFAULT) {
navController.navigate(Route.CommunityListArgs.makeRoute(select = "$select"))
}
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import com.jerboa.ui.components.settings.about.AboutActivity
import com.jerboa.ui.components.settings.account.AccountSettingsActivity
import com.jerboa.ui.components.settings.crashlogs.CrashLogsActivity
import com.jerboa.ui.components.settings.lookandfeel.LookAndFeelActivity
import com.jerboa.ui.components.viewvotes.comment.CommentLikesActivity
import com.jerboa.ui.components.viewvotes.post.PostLikesActivity
import com.jerboa.ui.theme.JerboaTheme
import com.jerboa.util.markwon.BetterLinkMovementMethod

Expand Down Expand Up @@ -605,6 +607,40 @@ class MainActivity : AppCompatActivity() {
)
}

composable(
route = Route.POST_LIKES,
arguments =
listOf(
navArgument(Route.PostLikesArgs.ID) {
type = Route.PostLikesArgs.ID_TYPE
},
),
) {
val args = Route.PostLikesArgs(it)
PostLikesActivity(
appState = appState,
postId = args.id,
onBack = appState::navigateUp,
)
}

composable(
route = Route.COMMENT_LIKES,
arguments =
listOf(
navArgument(Route.CommentLikesArgs.ID) {
type = Route.CommentLikesArgs.ID_TYPE
},
),
) {
val args = Route.CommentLikesArgs(it)
CommentLikesActivity(
appState = appState,
commentId = args.id,
onBack = appState::navigateUp,
)
}

composable(
route = Route.COMMENT_REPORT,
arguments =
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/jerboa/datatypes/SampleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ val sampleCommunityAggregates =
published = "2022-01-02T04:02:44.592929",
community_id = 834,
subscribers = 52,
subscribers_local = 21,
posts = 82,
comments = 987,
users_active_day = 28,
Expand Down
86 changes: 86 additions & 0 deletions app/src/main/java/com/jerboa/model/CommentLikesViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.jerboa.model

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.toApiState
import com.jerboa.appendData
import it.vercruysse.lemmyapi.v0x19.datatypes.CommentId
import it.vercruysse.lemmyapi.v0x19.datatypes.ListCommentLikes
import it.vercruysse.lemmyapi.v0x19.datatypes.ListCommentLikesResponse
import kotlinx.coroutines.launch

class CommentLikesViewModel(val id: CommentId) : ViewModel() {
var likesRes: ApiState<ListCommentLikesResponse> by mutableStateOf(ApiState.Empty)
private set
private var page by mutableLongStateOf(1)

init {
getLikes()
}

fun resetPage() {
page = 1
}

fun getLikes(state: ApiState<ListCommentLikesResponse> = ApiState.Loading) {
viewModelScope.launch {
likesRes = state
likesRes = API.getInstance().listCommentLikes(getForm()).toApiState()
}
}

private fun getForm(): ListCommentLikes {
return ListCommentLikes(
comment_id = id,
page = page,
)
}

fun appendLikes() {
viewModelScope.launch {
val oldRes = likesRes
when (oldRes) {
is ApiState.Success -> likesRes = ApiState.Appending(oldRes.data)
else -> return@launch
}

page += 1
val newRes = API.getInstance().listCommentLikes(getForm()).toApiState()

likesRes =
when (newRes) {
is ApiState.Success -> {
val appended = appendData(oldRes.data.comment_likes, newRes.data.comment_likes)

ApiState.Success(oldRes.data.copy(comment_likes = appended))
}

else -> {
oldRes
}
}
}
}

companion object {
class Factory(
private val id: CommentId,
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras,
): T {
return CommentLikesViewModel(id) as T
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CommunityListViewModel(communities: List<CommunityFollowerView>) : ViewMod
CommunityAggregates(
community_id = cfv.community.id,
subscribers = 0,
subscribers_local = 0,
posts = 0,
comments = 0,
published = "",
Expand Down
86 changes: 86 additions & 0 deletions app/src/main/java/com/jerboa/model/PostLikesViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.jerboa.model

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.toApiState
import com.jerboa.appendData
import it.vercruysse.lemmyapi.v0x19.datatypes.ListPostLikes
import it.vercruysse.lemmyapi.v0x19.datatypes.ListPostLikesResponse
import it.vercruysse.lemmyapi.v0x19.datatypes.PostId
import kotlinx.coroutines.launch

class PostLikesViewModel(val id: PostId) : ViewModel() {
var likesRes: ApiState<ListPostLikesResponse> by mutableStateOf(ApiState.Empty)
private set
private var page by mutableLongStateOf(1)

init {
getLikes()
}

fun resetPage() {
page = 1
}

fun getLikes(state: ApiState<ListPostLikesResponse> = ApiState.Loading) {
viewModelScope.launch {
likesRes = state
likesRes = API.getInstance().listPostLikes(getForm()).toApiState()
}
}

private fun getForm(): ListPostLikes {
return ListPostLikes(
post_id = id,
page = page,
)
}

fun appendLikes() {
viewModelScope.launch {
val oldRes = likesRes
when (oldRes) {
is ApiState.Success -> likesRes = ApiState.Appending(oldRes.data)
else -> return@launch
}

page += 1
val newRes = API.getInstance().listPostLikes(getForm()).toApiState()

likesRes =
when (newRes) {
is ApiState.Success -> {
val appended = appendData(oldRes.data.post_likes, newRes.data.post_likes)

ApiState.Success(oldRes.data.copy(post_likes = appended))
}

else -> {
oldRes
}
}
}
}

companion object {
class Factory(
private val id: PostId,
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras,
): T {
return PostLikesViewModel(id) as T
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import com.jerboa.db.entity.Account
import com.jerboa.db.entity.AnonAccount
import com.jerboa.feat.InstantScores
import com.jerboa.feat.VoteType
import com.jerboa.feat.amAdmin
import com.jerboa.feat.amMod
import com.jerboa.feat.canMod
import com.jerboa.isPostCreator
import com.jerboa.ui.components.common.ActionBarButton
Expand Down Expand Up @@ -188,6 +190,7 @@ fun LazyListScope.commentNodeItem(
onEditCommentClick: (commentView: CommentView) -> Unit,
onDeleteCommentClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: PersonId) -> Unit,
onViewVotesClick: (CommentId) -> Unit,
onHeaderClick: (commentView: CommentView) -> Unit,
onHeaderLongClick: (commentView: CommentView) -> Unit,
onCommunityClick: (community: Community) -> Unit,
Expand Down Expand Up @@ -350,6 +353,7 @@ fun LazyListScope.commentNodeItem(
onBanFromCommunityClick = onBanFromCommunityClick,
onCommentLinkClick = onCommentLinkClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onBlockCreatorClick = onBlockCreatorClick,
onClick = {
toggleExpanded(commentId)
Expand Down Expand Up @@ -405,6 +409,7 @@ fun LazyListScope.commentNodeItem(
onCommentLinkClick = onCommentLinkClick,
onFetchChildrenClick = onFetchChildrenClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onHeaderClick = onHeaderClick,
onHeaderLongClick = onHeaderLongClick,
onCommunityClick = onCommunityClick,
Expand Down Expand Up @@ -443,6 +448,7 @@ fun LazyListScope.missingCommentNodeItem(
onEditCommentClick: (commentView: CommentView) -> Unit,
onDeleteCommentClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: PersonId) -> Unit,
onViewVotesClick: (CommentId) -> Unit,
onHeaderClick: (commentView: CommentView) -> Unit,
onHeaderLongClick: (commentView: CommentView) -> Unit,
onCommunityClick: (community: Community) -> Unit,
Expand Down Expand Up @@ -553,6 +559,7 @@ fun LazyListScope.missingCommentNodeItem(
onCommentLinkClick = onCommentLinkClick,
onFetchChildrenClick = onFetchChildrenClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onHeaderClick = onHeaderClick,
onHeaderLongClick = onHeaderLongClick,
onCommunityClick = onCommunityClick,
Expand Down Expand Up @@ -684,6 +691,7 @@ fun CommentFooterLine(
onCommentLinkClick: (commentView: CommentView) -> Unit,
onBlockCreatorClick: (creator: Person) -> Unit,
onPersonClick: (personId: PersonId) -> Unit,
onViewVotesClick: (CommentId) -> Unit,
onClick: () -> Unit,
onLongClick: () -> Unit,
account: Account,
Expand All @@ -692,6 +700,22 @@ fun CommentFooterLine(
) {
var showMoreOptions by remember { mutableStateOf(false) }

val amAdmin =
remember(admins) {
amAdmin(
admins = admins,
myId = account.id,
)
}

val amMod =
remember {
amMod(
moderators = moderators,
myId = account.id,
)
}

val canMod =
remember(admins) {
canMod(
Expand All @@ -716,8 +740,11 @@ fun CommentFooterLine(
onBlockCreatorClick = onBlockCreatorClick,
onCommentLinkClick = onCommentLinkClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
isCreator = account.id == commentView.creator.id,
canMod = canMod,
amMod = amMod,
amAdmin = amAdmin,
viewSource = viewSource,
)
}
Expand Down Expand Up @@ -831,6 +858,7 @@ fun CommentNodesPreview() {
onBanFromCommunityClick = {},
onCommentLinkClick = {},
onPersonClick = {},
onViewVotesClick = {},
onHeaderClick = {},
onHeaderLongClick = {},
onCommunityClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fun CommentNodes(
onCommentLinkClick: (commentView: CommentView) -> Unit,
onFetchChildrenClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: PersonId) -> Unit,
onViewVotesClick: (CommentId) -> Unit,
onHeaderClick: (commentView: CommentView) -> Unit,
onHeaderLongClick: (commentView: CommentView) -> Unit,
onCommunityClick: (community: Community) -> Unit,
Expand Down Expand Up @@ -90,6 +91,7 @@ fun CommentNodes(
onCommentLinkClick = onCommentLinkClick,
onFetchChildrenClick = onFetchChildrenClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onHeaderClick = onHeaderClick,
onHeaderLongClick = onHeaderLongClick,
onCommunityClick = onCommunityClick,
Expand Down Expand Up @@ -136,6 +138,7 @@ fun LazyListScope.commentNodeItems(
onCommentLinkClick: (commentView: CommentView) -> Unit,
onFetchChildrenClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: PersonId) -> Unit,
onViewVotesClick: (CommentId) -> Unit,
onHeaderClick: (commentView: CommentView) -> Unit,
onHeaderLongClick: (commentView: CommentView) -> Unit,
onCommunityClick: (community: Community) -> Unit,
Expand Down Expand Up @@ -172,6 +175,7 @@ fun LazyListScope.commentNodeItems(
onMarkAsReadClick = onMarkAsReadClick,
onCommentClick = onCommentClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onHeaderClick = onHeaderClick,
onHeaderLongClick = onHeaderLongClick,
onCommunityClick = onCommunityClick,
Expand Down Expand Up @@ -214,6 +218,7 @@ fun LazyListScope.commentNodeItems(
onMarkAsReadClick = onMarkAsReadClick,
onCommentClick = onCommentClick,
onPersonClick = onPersonClick,
onViewVotesClick = onViewVotesClick,
onHeaderClick = onHeaderClick,
onHeaderLongClick = onHeaderLongClick,
onCommunityClick = onCommunityClick,
Expand Down
Loading