mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-04-19 14:27:01 -04:00
[app] refactor RepoIcon into its own Composable
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.fdroid.fdroid.views.appdetails
|
||||
|
||||
import android.content.res.Configuration
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement.spacedBy
|
||||
@@ -32,8 +31,6 @@ import androidx.compose.ui.Alignment.Companion.CenterVertically
|
||||
import androidx.compose.ui.Alignment.Companion.End
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalInspectionMode
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
@@ -41,17 +38,13 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import androidx.core.util.Consumer
|
||||
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
|
||||
import com.bumptech.glide.integration.compose.GlideImage
|
||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||
import org.fdroid.database.Repository
|
||||
import org.fdroid.fdroid.R
|
||||
import org.fdroid.fdroid.Utils
|
||||
import org.fdroid.fdroid.compose.ComposeUtils.FDroidContent
|
||||
import org.fdroid.fdroid.compose.ComposeUtils.FDroidOutlineButton
|
||||
import org.fdroid.fdroid.views.repos.RepoIcon
|
||||
import org.fdroid.index.IndexFormatVersion.TWO
|
||||
|
||||
/**
|
||||
@@ -109,7 +102,6 @@ fun RepoChooser(
|
||||
}
|
||||
|
||||
@Composable
|
||||
@OptIn(ExperimentalGlideComposeApi::class)
|
||||
private fun RepoDropDown(
|
||||
repos: List<Repository>,
|
||||
currentRepoId: Long,
|
||||
@@ -121,16 +113,13 @@ private fun RepoDropDown(
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
val currentRepo = repos.find { it.repoId == currentRepoId }
|
||||
?: error("Current repoId not in list")
|
||||
val localeList = LocaleListCompat.getDefault()
|
||||
val res = LocalContext.current.resources
|
||||
|
||||
Column(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
) {
|
||||
Box {
|
||||
OutlinedTextField(
|
||||
value = TextFieldValue(buildAnnotatedString {
|
||||
append(currentRepo.getName(localeList) ?: "Unknown Repository")
|
||||
append(currentRepo.getName(LocaleListCompat.getDefault()) ?: "Unknown Repository")
|
||||
if (currentRepo.repoId == preferredRepoId) {
|
||||
append(" ")
|
||||
pushStyle(SpanStyle(fontWeight = FontWeight.Bold))
|
||||
@@ -144,23 +133,7 @@ private fun RepoDropDown(
|
||||
Text(stringResource(R.string.app_details_repositories))
|
||||
},
|
||||
leadingIcon = {
|
||||
if (LocalInspectionMode.current) Image(
|
||||
painter = rememberDrawablePainter(
|
||||
ResourcesCompat.getDrawable(res, R.drawable.ic_launcher, null)
|
||||
),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(24.dp),
|
||||
) else GlideImage(
|
||||
model = Utils.getDownloadRequest(
|
||||
currentRepo,
|
||||
currentRepo.getIcon(localeList)
|
||||
),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(24.dp),
|
||||
) {
|
||||
it.fallback(R.drawable.ic_repo_app_default)
|
||||
.error(R.drawable.ic_repo_app_default)
|
||||
}
|
||||
RepoIcon(repo = currentRepo, modifier = Modifier.size(24.dp))
|
||||
},
|
||||
trailingIcon = {
|
||||
Icon(
|
||||
@@ -206,31 +179,16 @@ private fun RepoDropDown(
|
||||
}
|
||||
|
||||
@Composable
|
||||
@OptIn(ExperimentalGlideComposeApi::class)
|
||||
private fun RepoItem(repo: Repository, isPreferred: Boolean, modifier: Modifier = Modifier) {
|
||||
Row(
|
||||
horizontalArrangement = spacedBy(8.dp),
|
||||
verticalAlignment = CenterVertically,
|
||||
modifier = modifier,
|
||||
) {
|
||||
val localeList = LocaleListCompat.getDefault()
|
||||
val res = LocalContext.current.resources
|
||||
if (LocalInspectionMode.current) Image(
|
||||
painter = rememberDrawablePainter(
|
||||
ResourcesCompat.getDrawable(res, R.drawable.ic_launcher, null)
|
||||
),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(24.dp),
|
||||
) else GlideImage(
|
||||
model = Utils.getDownloadRequest(repo, repo.getIcon(localeList)),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(24.dp),
|
||||
) {
|
||||
it.fallback(R.drawable.ic_repo_app_default).error(R.drawable.ic_repo_app_default)
|
||||
}
|
||||
RepoIcon(repo, Modifier.size(24.dp))
|
||||
Text(
|
||||
text = buildAnnotatedString {
|
||||
append(repo.getName(localeList) ?: "Unknown Repository")
|
||||
append(repo.getName(LocaleListCompat.getDefault()) ?: "Unknown Repository")
|
||||
if (isPreferred) {
|
||||
append(" ")
|
||||
pushStyle(SpanStyle(fontWeight = FontWeight.Bold))
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.fdroid.fdroid.views.repos
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalInspectionMode
|
||||
import androidx.core.content.res.ResourcesCompat.getDrawable
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
|
||||
import com.bumptech.glide.integration.compose.GlideImage
|
||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||
import org.fdroid.database.Repository
|
||||
import org.fdroid.fdroid.R
|
||||
import org.fdroid.fdroid.Utils.getDownloadRequest
|
||||
|
||||
@Composable
|
||||
@OptIn(ExperimentalGlideComposeApi::class)
|
||||
fun RepoIcon(repo: Repository, modifier: Modifier = Modifier) {
|
||||
if (LocalInspectionMode.current) Image(
|
||||
painter = rememberDrawablePainter(
|
||||
getDrawable(LocalContext.current.resources, R.drawable.ic_launcher, null)
|
||||
),
|
||||
contentDescription = null,
|
||||
modifier = modifier,
|
||||
) else GlideImage(
|
||||
model = getDownloadRequest(repo, repo.getIcon(LocaleListCompat.getDefault())),
|
||||
contentDescription = null,
|
||||
modifier = modifier,
|
||||
) { requestBuilder ->
|
||||
requestBuilder
|
||||
.fallback(R.drawable.ic_repo_app_default)
|
||||
.error(R.drawable.ic_repo_app_default)
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,6 @@ fun RepoPreviewScreen(paddingValues: PaddingValues, state: Fetching, onAddRepo:
|
||||
}
|
||||
|
||||
@Composable
|
||||
@OptIn(ExperimentalGlideComposeApi::class)
|
||||
fun RepoPreviewHeader(
|
||||
state: Fetching,
|
||||
onAddRepo: () -> Unit,
|
||||
@@ -101,24 +100,11 @@ fun RepoPreviewHeader(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
val repo = state.repo ?: error("repo was null")
|
||||
val res = LocalContext.current.resources
|
||||
Row(
|
||||
horizontalArrangement = spacedBy(8.dp),
|
||||
verticalAlignment = CenterVertically,
|
||||
) {
|
||||
if (isPreview) Image(
|
||||
painter = rememberDrawablePainter(
|
||||
getDrawable(res, R.drawable.ic_launcher, null)
|
||||
),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(48.dp),
|
||||
) else GlideImage(
|
||||
model = getDownloadRequest(repo, repo.getIcon(localeList)),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(48.dp),
|
||||
) {
|
||||
it.fallback(R.drawable.ic_repo_app_default).error(R.drawable.ic_repo_app_default)
|
||||
}
|
||||
RepoIcon(repo, Modifier.size(48.dp))
|
||||
Column(horizontalAlignment = Alignment.Start) {
|
||||
Text(
|
||||
text = repo.getName(localeList) ?: "Unknown Repository",
|
||||
@@ -132,7 +118,7 @@ fun RepoPreviewHeader(
|
||||
modifier = Modifier.alpha(ContentAlpha.medium),
|
||||
)
|
||||
Text(
|
||||
text = Utils.formatLastUpdated(res, repo.timestamp),
|
||||
text = Utils.formatLastUpdated(LocalContext.current.resources, repo.timestamp),
|
||||
style = MaterialTheme.typography.body2,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user