Skip to content

Commit

Permalink
Fix disabled state and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Nov 14, 2023
1 parent b41ef5f commit 7b1be7d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ inline fun <T> GraphQLResponse<T>.ifSuccessful(block: (T) -> Unit) {
}
}

inline fun <T> GraphQLResponse<T>.ifUnsuccessful(block: (String) -> Unit) {
fold(
onSuccess = {},
onError = block
)
}

fun <T> GraphQLResponse<T>.getOrNull(): T? = when (this) {
is GraphQLResponse.Success -> data
is GraphQLResponse.Error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class DetailsTab(
LargeSegmentedButton(
icon = Icons.Custom.Balance,
text = it.nickname ?: it.key.uppercase(),
onClick = { }
onClick = { /* TODO */ }
)
}
LargeSegmentedButton(
Expand All @@ -180,7 +180,7 @@ class DetailsTab(
count = repoDetails.forkCount,
repoDetails.forkCount
),
onClick = { }
onClick = { /* TODO */ }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
}
}

}

0 comments on commit 7b1be7d

Please sign in to comment.