diff --git a/api/src/commonMain/kotlin/com/materiiapps/gloom/api/utils/GraphQLResponse.kt b/api/src/commonMain/kotlin/com/materiiapps/gloom/api/utils/GraphQLResponse.kt index 1aff330..d0b9374 100644 --- a/api/src/commonMain/kotlin/com/materiiapps/gloom/api/utils/GraphQLResponse.kt +++ b/api/src/commonMain/kotlin/com/materiiapps/gloom/api/utils/GraphQLResponse.kt @@ -33,6 +33,13 @@ inline fun GraphQLResponse.ifSuccessful(block: (T) -> Unit) { } } +inline fun GraphQLResponse.ifUnsuccessful(block: (String) -> Unit) { + fold( + onSuccess = {}, + onError = block + ) +} + fun GraphQLResponse.getOrNull(): T? = when (this) { is GraphQLResponse.Success -> data is GraphQLResponse.Error, diff --git a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/components/LargeSegmentedButton.kt b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/components/LargeSegmentedButton.kt index 9ad7a11..d14454b 100644 --- a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/components/LargeSegmentedButton.kt +++ b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/components/LargeSegmentedButton.kt @@ -11,10 +11,12 @@ import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Icon +import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.surfaceColorAtElevation import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -41,31 +43,32 @@ fun RowScope.LargeSegmentedButton( .weight(1f) .padding(16.dp) ) { - when (icon) { - is ImageVector -> { - Icon( - imageVector = icon, - contentDescription = iconDescription, - tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f) - ) - } + CompositionLocalProvider( + LocalContentColor provides MaterialTheme.colorScheme.primary.copy(if (enabled) 1f else 0.5f) + ) { + when (icon) { + is ImageVector -> { + Icon( + imageVector = icon, + contentDescription = iconDescription + ) + } - is Painter -> { - Icon( - painter = icon, - contentDescription = iconDescription, - tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f) - ) + is Painter -> { + Icon( + painter = icon, + contentDescription = iconDescription + ) + } } - } - Text( - text = text, - style = MaterialTheme.typography.labelLarge, - color = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f), - maxLines = 1, - modifier = Modifier.basicMarquee(Int.MAX_VALUE) - ) + Text( + text = text, + style = MaterialTheme.typography.labelLarge, + maxLines = 1, + modifier = Modifier.basicMarquee(Int.MAX_VALUE) + ) + } } } diff --git a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/screens/repo/tab/DetailsTab.kt b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/screens/repo/tab/DetailsTab.kt index ac0fa30..c17d496 100644 --- a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/screens/repo/tab/DetailsTab.kt +++ b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/screens/repo/tab/DetailsTab.kt @@ -170,7 +170,7 @@ class DetailsTab( LargeSegmentedButton( icon = Icons.Custom.Balance, text = it.nickname ?: it.key.uppercase(), - onClick = { } + onClick = { /* TODO */ } ) } LargeSegmentedButton( @@ -180,7 +180,7 @@ class DetailsTab( count = repoDetails.forkCount, repoDetails.forkCount ), - onClick = { } + onClick = { /* TODO */ } ) } } diff --git a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/viewmodels/repo/tab/RepoDetailsViewModel.kt b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/viewmodels/repo/tab/RepoDetailsViewModel.kt index 7054e2f..875c2bb 100644 --- a/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/viewmodels/repo/tab/RepoDetailsViewModel.kt +++ b/ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/viewmodels/repo/tab/RepoDetailsViewModel.kt @@ -7,6 +7,7 @@ import cafe.adriel.voyager.core.model.ScreenModel import cafe.adriel.voyager.core.model.coroutineScope import com.materiiapps.gloom.api.repository.GraphQLRepository import com.materiiapps.gloom.api.utils.fold +import com.materiiapps.gloom.api.utils.ifUnsuccessful import com.materiiapps.gloom.gql.fragment.RepoDetails import kotlinx.coroutines.launch @@ -44,14 +45,14 @@ class RepoDetailsViewModel( } } - fun toggleStar() { - fun updateStarDetails(starred: Boolean) { - details = details!!.copy( - viewerHasStarred = starred, - stargazerCount = details!!.stargazerCount + if (starred) 1 else -1 - ) - } + private fun updateStarDetails(starred: Boolean) { + details = details?.copy( + viewerHasStarred = starred, + stargazerCount = details!!.stargazerCount + if (starred) 1 else -1 + ) + } + fun toggleStar() { val details = details!! val hasStarred = details.viewerHasStarred @@ -65,17 +66,12 @@ class RepoDetailsViewModel( gql.unstarRepo(details.id) } else { gql.starRepo(details.id) - }.fold( - onSuccess = { - isStarLoading = false - }, - onError = { - // Revert optimistic update - updateStarDetails(starred = hasStarred) - - isStarLoading = false - } - ) + }.ifUnsuccessful { + // Revert optimistic update + updateStarDetails(starred = hasStarred) + } + isStarLoading = false } } + } \ No newline at end of file