Skip to content

Commit

Permalink
Allow staring and unstarring repos within the repo details page
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Nov 6, 2023
1 parent d452611 commit 84cf0a6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
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 = {},
enabled: Boolean = true
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically),
modifier = Modifier
.clickable(enabled) { onClick() }
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp))
.weight(1f)
.padding(16.dp)

) {
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)
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)

coroutineScope.launch {
isStarLoading = true

gql.starRepo(details!!.id).fold(
onSuccess = {
isStarLoading = false
},
onError = {
updateStarDetails(false)

isStarLoading = false
hasError = true
}
)
}
}

fun unstarRepo() {
updateStarDetails(false)

coroutineScope.launch {
isStarLoading = true

gql.unstarRepo(details!!.id).fold(
onSuccess = {
isStarLoading = false
},
onError = {
updateStarDetails(true)

isStarLoading = false
hasError = true
}
)
}
}
}

0 comments on commit 84cf0a6

Please sign in to comment.