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

Feature post #1330

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/src/main/java/com/jerboa/datatypes/Others.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import com.jerboa.ui.components.inbox.InboxTab
import com.jerboa.ui.components.person.UserTab
import it.vercruysse.lemmyapi.dto.CommentSortType
import it.vercruysse.lemmyapi.dto.ListingType
import it.vercruysse.lemmyapi.dto.PostFeatureType
import it.vercruysse.lemmyapi.dto.SortType
import it.vercruysse.lemmyapi.v0x19.datatypes.Post


data class CommentSortData(
Expand Down Expand Up @@ -187,3 +189,9 @@ fun getLocalizedStringForInboxTab(
}
return returnString
}

data class PostFeatureData(
val post: Post,
val type: PostFeatureType,
val featured: Boolean,
)
15 changes: 15 additions & 0 deletions app/src/main/java/com/jerboa/model/PersonProfileViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PersonProfileViewModel(personArg: Either<PersonId, String>, savedMode: Boo
private var savePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var lockPostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var featurePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockCommunityRes: ApiState<BlockCommunityResponse> by mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)

Expand Down Expand Up @@ -195,6 +196,20 @@ class PersonProfileViewModel(personArg: Either<PersonId, String>, savedMode: Boo
}
}

fun featurePost(form: FeaturePost) {
viewModelScope.launch {
featurePostRes = ApiState.Loading
featurePostRes = API.getInstance().featurePost(form).toApiState()
when (val featurePost = featurePostRes) {
is ApiState.Success -> {
updatePost(featurePost.data.post_view)
}

else -> {}
}
}
}

fun blockCommunity(
form: BlockCommunity,
ctx: Context,
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/jerboa/model/PostViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PostViewModel(val id: Either<PostId, CommentId>) : ViewModel() {
private var savePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var deletePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var lockPostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var featurePostRes: ApiState<PostResponse> by mutableStateOf(ApiState.Empty)
private var blockPersonRes: ApiState<BlockPersonResponse> by mutableStateOf(ApiState.Empty)

val unExpandedComments = mutableStateListOf<Int>()
Expand Down Expand Up @@ -229,6 +230,20 @@ class PostViewModel(val id: Either<PostId, CommentId>) : ViewModel() {
}
}

fun featurePost(form: FeaturePost) {
viewModelScope.launch {
featurePostRes = ApiState.Loading
featurePostRes = API.getInstance().featurePost(form).toApiState()
when (val featurePost = featurePostRes) {
is ApiState.Success -> {
updatePost(featurePost.data.post_view)
}

else -> {}
}
}
}

fun blockPerson(
form: BlockPerson,
ctx: Context,
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/jerboa/model/PostsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import it.vercruysse.lemmyapi.dto.ListingType
import it.vercruysse.lemmyapi.dto.SortType
import it.vercruysse.lemmyapi.v0x19.datatypes.CreatePostLike
import it.vercruysse.lemmyapi.v0x19.datatypes.DeletePost
import it.vercruysse.lemmyapi.v0x19.datatypes.FeaturePost
import it.vercruysse.lemmyapi.v0x19.datatypes.GetPosts
import it.vercruysse.lemmyapi.v0x19.datatypes.GetPostsResponse
import it.vercruysse.lemmyapi.v0x19.datatypes.LockPost
Expand Down Expand Up @@ -206,4 +207,12 @@ open class PostsViewModel(protected val accountRepository: AccountRepository) :
}
}
}

fun featurePost(form: FeaturePost) {
viewModelScope.launch {
API.getInstance().featurePost(form).onSuccess {
updatePost(it.post_view)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import it.vercruysse.lemmyapi.v0x19.datatypes.BlockCommunity
import it.vercruysse.lemmyapi.v0x19.datatypes.CommunityId
import it.vercruysse.lemmyapi.v0x19.datatypes.CreatePostLike
import it.vercruysse.lemmyapi.v0x19.datatypes.DeletePost
import it.vercruysse.lemmyapi.v0x19.datatypes.FeaturePost
import it.vercruysse.lemmyapi.v0x19.datatypes.FollowCommunity
import it.vercruysse.lemmyapi.v0x19.datatypes.LockPost
import it.vercruysse.lemmyapi.v0x19.datatypes.MarkPostAsRead
Expand Down Expand Up @@ -351,6 +352,24 @@ fun CommunityActivity(
)
}
},
onFeaturePostClick = { data ->
account.doIfReadyElseDisplayInfo(
appState,
ctx,
snackbarHostState,
scope,
siteViewModel,
accountViewModel,
) {
communityViewModel.featurePost(
FeaturePost(
post_id = data.post.id,
featured = !data.featured,
feature_type = data.type,
),
)
}
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
18 changes: 18 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 @@ -65,6 +65,7 @@ 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.FeaturePost
import it.vercruysse.lemmyapi.v0x19.datatypes.LockPost
import it.vercruysse.lemmyapi.v0x19.datatypes.MarkPostAsRead
import it.vercruysse.lemmyapi.v0x19.datatypes.PostView
Expand Down Expand Up @@ -360,6 +361,23 @@ fun MainPostListingsContent(
)
}
},
onFeaturePostClick = { data ->
account.doIfReadyElseDisplayInfo(
appState,
ctx,
snackbarHostState,
scope,
siteViewModel,
) {
homeViewModel.featurePost(
FeaturePost(
post_id = data.post.id,
featured = !data.featured,
feature_type = data.type,
),
)
}
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import it.vercruysse.lemmyapi.v0x19.datatypes.CreateCommentLike
import it.vercruysse.lemmyapi.v0x19.datatypes.CreatePostLike
import it.vercruysse.lemmyapi.v0x19.datatypes.DeleteComment
import it.vercruysse.lemmyapi.v0x19.datatypes.DeletePost
import it.vercruysse.lemmyapi.v0x19.datatypes.FeaturePost
import it.vercruysse.lemmyapi.v0x19.datatypes.GetPersonDetails
import it.vercruysse.lemmyapi.v0x19.datatypes.LockPost
import it.vercruysse.lemmyapi.v0x19.datatypes.MarkPostAsRead
Expand Down Expand Up @@ -561,6 +562,23 @@ fun UserTabs(
)
}
},
onFeaturePostClick = { data ->
account.doIfReadyElseDisplayInfo(
appState,
ctx,
snackbarHostState,
scope,
loginAsToast = true,
) {
personProfileViewModel.featurePost(
FeaturePost(
post_id = data.post.id,
featured = !data.featured,
feature_type = data.type,
),
)
}
},
onCommunityClick = { community ->
appState.toCommunity(id = community.id)
},
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ fun PostActivity(
)
}
},
onFeaturePostClick = { data ->
account.doIfReadyElseDisplayInfo(
appState,
ctx,
snackbarHostState,
scope,
siteViewModel,
accountViewModel,
) {
postViewModel.featurePost(
FeaturePost(
post_id = data.post.id,
featured = !data.featured,
feature_type = data.type,
),
)
}
},
onPersonClick = appState::toProfile,
showReply = true, // Do nothing
fullBody = true,
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/post/PostListing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.jerboa.amAdmin
import com.jerboa.amMod
import com.jerboa.calculateNewInstantScores
import com.jerboa.canMod
import com.jerboa.datatypes.PostFeatureData
import com.jerboa.datatypes.sampleImagePostView
import com.jerboa.datatypes.sampleLinkNoThumbnailPostView
import com.jerboa.datatypes.sampleLinkPostView
Expand Down Expand Up @@ -557,6 +558,7 @@ fun PostFooterLine(
onReportClick: (postView: PostView) -> Unit,
onRemoveClick: (postView: PostView) -> Unit,
onLockPostClick: (postView: PostView) -> Unit,
onFeaturePostClick: (data: PostFeatureData) -> Unit,
onCommunityClick: (community: Community) -> Unit,
onPersonClick: (personId: Int) -> Unit,
onViewSourceClick: () -> Unit,
Expand Down Expand Up @@ -609,6 +611,7 @@ fun PostFooterLine(
onReportClick = onReportClick,
onRemoveClick = onRemoveClick,
onLockPostClick = onLockPostClick,
onFeaturePostClick = onFeaturePostClick,
onViewSourceClick = onViewSourceClick,
isCreator = account.id == postView.creator.id,
canMod = canMod,
Expand Down Expand Up @@ -805,6 +808,7 @@ fun PostFooterLinePreview() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onCommunityClick = {},
onPersonClick = {},
onViewSourceClick = {},
Expand Down Expand Up @@ -838,6 +842,7 @@ fun PreviewPostListingCard() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onPersonClick = {},
fullBody = false,
account = AnonAccount,
Expand Down Expand Up @@ -874,6 +879,7 @@ fun PreviewLinkPostListing() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onPersonClick = {},
fullBody = false,
account = AnonAccount,
Expand Down Expand Up @@ -910,6 +916,7 @@ fun PreviewImagePostListingCard() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onPersonClick = {},
fullBody = false,
account = AnonAccount,
Expand Down Expand Up @@ -946,6 +953,7 @@ fun PreviewImagePostListingSmallCard() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onPersonClick = {},
fullBody = false,
account = AnonAccount,
Expand Down Expand Up @@ -982,6 +990,7 @@ fun PreviewLinkNoThumbnailPostListing() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onPersonClick = {},
fullBody = false,
account = AnonAccount,
Expand Down Expand Up @@ -1016,6 +1025,7 @@ fun PostListing(
onReportClick: (postView: PostView) -> Unit,
onRemoveClick: (postView: PostView) -> Unit,
onLockPostClick: (postView: PostView) -> Unit,
onFeaturePostClick: (data: PostFeatureData) -> Unit,
onPersonClick: (personId: Int) -> Unit,
showReply: Boolean = false,
showCommunityName: Boolean = true,
Expand Down Expand Up @@ -1079,6 +1089,7 @@ fun PostListing(
onReportClick = onReportClick,
onRemoveClick = onRemoveClick,
onLockPostClick = onLockPostClick,
onFeaturePostClick = onFeaturePostClick,
onPersonClick = onPersonClick,
onViewSourceClick = {
viewSource = !viewSource
Expand Down Expand Up @@ -1132,6 +1143,7 @@ fun PostListing(
onReportClick = onReportClick,
onRemoveClick = onRemoveClick,
onLockPostClick = onLockPostClick,
onFeaturePostClick = onFeaturePostClick,
onPersonClick = onPersonClick,
onViewSourceClick = {
viewSource = !viewSource
Expand Down Expand Up @@ -1528,6 +1540,7 @@ fun PostListingCard(
onReportClick: (postView: PostView) -> Unit,
onRemoveClick: (postView: PostView) -> Unit,
onLockPostClick: (postView: PostView) -> Unit,
onFeaturePostClick: (data: PostFeatureData) -> Unit,
onPersonClick: (personId: Int) -> Unit,
onViewSourceClick: () -> Unit,
viewSource: Boolean,
Expand Down Expand Up @@ -1605,6 +1618,7 @@ fun PostListingCard(
onReportClick = onReportClick,
onRemoveClick = onRemoveClick,
onLockPostClick = onLockPostClick,
onFeaturePostClick = onFeaturePostClick,
onCommunityClick = onCommunityClick,
onPersonClick = onPersonClick,
onViewSourceClick = onViewSourceClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jerboa.JerboaAppState
import com.jerboa.PostViewMode
import com.jerboa.datatypes.PostFeatureData
import com.jerboa.datatypes.sampleLinkPostView
import com.jerboa.datatypes.samplePostView
import com.jerboa.db.entity.Account
Expand Down Expand Up @@ -51,6 +52,7 @@ fun PostListings(
onReportClick: (postView: PostView) -> Unit,
onRemoveClick: (postView: PostView) -> Unit,
onLockPostClick: (postView: PostView) -> Unit,
onFeaturePostClick: (data: PostFeatureData) -> Unit,
onCommunityClick: (community: Community) -> Unit,
onPersonClick: (personId: Int) -> Unit,
loadMorePosts: () -> Unit,
Expand Down Expand Up @@ -107,6 +109,7 @@ fun PostListings(
onReportClick = onReportClick,
onRemoveClick = onRemoveClick,
onLockPostClick = onLockPostClick,
onFeaturePostClick = onFeaturePostClick,
onPersonClick = onPersonClick,
showCommunityName = showCommunityName,
fullBody = false,
Expand Down Expand Up @@ -173,6 +176,7 @@ fun PreviewPostListings() {
onReportClick = {},
onRemoveClick = {},
onLockPostClick = {},
onFeaturePostClick = {},
onCommunityClick = {},
onPersonClick = {},
loadMorePosts = {},
Expand Down
Loading