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

Allow staring and unstarring repos within the repo details page #33

Merged
merged 9 commits into from
Nov 14, 2023
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment RepoDetails on Repository {
id
description
readme {
contentHTML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.materiiapps.gloom.ui.components
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -26,15 +27,19 @@ import androidx.compose.ui.unit.dp
fun RowScope.LargeSegmentedButton(
icon: Any,
iconDescription: String? = null,
text: String
text: String,
onClick: () -> Unit = {},
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
enabled: Boolean = true
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically),
modifier = Modifier
.clickable(enabled) { onClick() }
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp))
.weight(1f)
.padding(16.dp)

oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
) {
when (icon) {
is ImageVector -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ class DetailsTab(
res = Res.plurals.stars,
count = repoDetails.stargazerCount,
repoDetails.stargazerCount
)
),
onClick = {
if (!repoDetails.viewerHasStarred)
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
viewModel.starRepo()
else
viewModel.unstarRepo()
},
enabled = !viewModel.isStarLoading
)
repoDetails.licenseInfo?.let {
LargeSegmentedButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class RepoDetailsViewModel(
var detailsLoading by mutableStateOf(false)
var hasError by mutableStateOf(false)

var isStarLoading by mutableStateOf(false)

init {
loadDetails()
}
Expand All @@ -42,4 +44,50 @@ class RepoDetailsViewModel(
}
}

private fun updateStarDetails(starred: Boolean) {
details = details!!.copy(
viewerHasStarred = starred,
stargazerCount = details!!.stargazerCount + if (starred) 1 else -1
)
}

fun starRepo() {
updateStarDetails(true)
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved

coroutineScope.launch {
isStarLoading = true

gql.starRepo(details!!.id).fold(
onSuccess = {
isStarLoading = false
},
onError = {
updateStarDetails(false)
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved

isStarLoading = false
hasError = true
}
)
}
}

fun unstarRepo() {
updateStarDetails(false)
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved

coroutineScope.launch {
isStarLoading = true

gql.unstarRepo(details!!.id).fold(
onSuccess = {
isStarLoading = false
},
onError = {
updateStarDetails(true)
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved

isStarLoading = false
hasError = true
oSumAtrIX marked this conversation as resolved.
Show resolved Hide resolved
}
)
}
}
}
Loading