From 4a323ded0b8bb79de6607a6acee7fed4c6f05f4a Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Thu, 2 Jun 2022 03:58:44 +0200 Subject: [PATCH] Update: Revamp PackageState to taxonomical ComponentStates --- .../com/looker/droidify/entity/SubEntities.kt | 35 ++++++++++--------- .../ui/compose/components/ActionButton.kt | 13 ++++--- .../ui/compose/components/ProductsListItem.kt | 7 ++-- .../pages/app_detail/components/Header.kt | 28 ++++++++------- .../droidify/ui/compose/utils/Callbacks.kt | 4 +-- .../droidify/ui/viewmodels/AppViewModelX.kt | 10 +++--- 6 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt index 35e837b5..fcfbf8c6 100644 --- a/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt +++ b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt @@ -89,34 +89,37 @@ enum class AntiFeature(val key: String, @StringRes val titleResId: Int) { NSFW("NSFW", R.string.not_safe_for_work) } -sealed interface PackageState { +sealed interface ComponentState { val icon: ImageVector val textId: Int } -sealed class Cancelable( +sealed class DownloadState( @StringRes override val textId: Int, override val icon: ImageVector = Icons.Rounded.Close -) : PackageState +) : ComponentState { -object Pending : Cancelable(R.string.pending) -object Connecting : Cancelable(R.string.connecting) -class Downloading(val downloaded: Long, val total: Long?) : - Cancelable(R.string.downloading) + object Pending : DownloadState(R.string.pending) + object Connecting : DownloadState(R.string.connecting) + class Downloading(val downloaded: Long, val total: Long?) : + DownloadState(R.string.downloading) -object Installing : Cancelable(R.string.installing) + object Installing : DownloadState(R.string.installing) +} -sealed class ButtonWork( +sealed class ActionState( @StringRes override val textId: Int, override val icon: ImageVector = Icons.Rounded.Download -) : PackageState +) : ComponentState { -object Install : ButtonWork(R.string.install, Icons.Rounded.Download) -object Update : ButtonWork(R.string.update, Icons.Rounded.Download) -object Uninstall : ButtonWork(R.string.uninstall, Icons.Rounded.Delete) -object Launch : ButtonWork(R.string.launch, Icons.Rounded.Launch) -object Details : ButtonWork(R.string.details, Icons.Rounded.Tune) -object Share : ButtonWork(R.string.share, Icons.Rounded.Share) + object Install : ActionState(R.string.install, Icons.Rounded.Download) + object Update : ActionState(R.string.update, Icons.Rounded.Download) + object Uninstall : ActionState(R.string.uninstall, Icons.Rounded.Delete) + object Launch : ActionState(R.string.launch, Icons.Rounded.Launch) + object Details : ActionState(R.string.details, Icons.Rounded.Tune) + object Share : ActionState(R.string.share, Icons.Rounded.Share) + class Cancel(@StringRes stateId: Int) : ActionState(stateId, Icons.Rounded.Close) +} open class LinkType( @DrawableRes val iconResId: Int, diff --git a/src/main/kotlin/com/looker/droidify/ui/compose/components/ActionButton.kt b/src/main/kotlin/com/looker/droidify/ui/compose/components/ActionButton.kt index 45507978..2da32c21 100644 --- a/src/main/kotlin/com/looker/droidify/ui/compose/components/ActionButton.kt +++ b/src/main/kotlin/com/looker/droidify/ui/compose/components/ActionButton.kt @@ -17,18 +17,21 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import com.looker.droidify.entity.PackageState +import com.looker.droidify.entity.ActionState +import com.looker.droidify.entity.ComponentState +import com.looker.droidify.entity.DownloadState @OptIn(ExperimentalAnimationApi::class) @Composable fun MainActionButton( modifier: Modifier = Modifier, - packageState: PackageState, - onClick: (PackageState) -> Unit + actionState: ActionState, + downloadState: DownloadState?, + onClick: (ComponentState) -> Unit ) { ElevatedButton( modifier = modifier, - onClick = { onClick(packageState) } + onClick = { onClick(actionState) } ) { AnimatedContent( targetState = packageState, @@ -66,7 +69,7 @@ fun MainActionButton( @Composable fun SecondaryActionButton( modifier: Modifier = Modifier, - packageState: PackageState?, + packageState: ComponentState?, onClick: () -> Unit ) { packageState?.let { diff --git a/src/main/kotlin/com/looker/droidify/ui/compose/components/ProductsListItem.kt b/src/main/kotlin/com/looker/droidify/ui/compose/components/ProductsListItem.kt index 78587b6f..b6314efb 100644 --- a/src/main/kotlin/com/looker/droidify/ui/compose/components/ProductsListItem.kt +++ b/src/main/kotlin/com/looker/droidify/ui/compose/components/ProductsListItem.kt @@ -32,8 +32,7 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import com.looker.droidify.database.entity.Installed import com.looker.droidify.database.entity.Repository -import com.looker.droidify.entity.Install -import com.looker.droidify.entity.Launch +import com.looker.droidify.entity.ActionState import com.looker.droidify.entity.ProductItem import com.looker.droidify.network.CoilDownloader import com.looker.droidify.ui.compose.utils.ExpandableCard @@ -155,8 +154,8 @@ fun ExpandedItemContent( onClick = { onActionClicked(item) } ) { val action = when { - installed != null -> Launch - else -> Install + installed != null -> ActionState.Launch + else -> ActionState.Install } Icon( imageVector = action.icon, diff --git a/src/main/kotlin/com/looker/droidify/ui/compose/pages/app_detail/components/Header.kt b/src/main/kotlin/com/looker/droidify/ui/compose/pages/app_detail/components/Header.kt index 22b29c03..33d151f1 100644 --- a/src/main/kotlin/com/looker/droidify/ui/compose/pages/app_detail/components/Header.kt +++ b/src/main/kotlin/com/looker/droidify/ui/compose/pages/app_detail/components/Header.kt @@ -26,10 +26,9 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.looker.droidify.R -import com.looker.droidify.entity.Cancelable -import com.looker.droidify.entity.Downloading -import com.looker.droidify.entity.Install -import com.looker.droidify.entity.PackageState +import com.looker.droidify.entity.ActionState +import com.looker.droidify.entity.ComponentState +import com.looker.droidify.entity.DownloadState import com.looker.droidify.ui.compose.components.MainActionButton import com.looker.droidify.ui.compose.components.SecondaryActionButton import com.looker.droidify.ui.compose.utils.NetworkImage @@ -41,8 +40,9 @@ fun AppInfoHeader( versionCode: String, appSize: String, appDev: String, - state: PackageState? = Install, - secondaryAction: PackageState? = null, + mainAction: ActionState? = ActionState.Install, + downloadState: DownloadState?, + secondaryAction: ComponentState? = null, onSource: () -> Unit = { }, onSourceLong: () -> Unit = { }, onAction: () -> Unit = { }, @@ -73,10 +73,12 @@ fun AppInfoHeader( }) MainActionButton( modifier = Modifier.weight(1f), - packageState = state ?: Install, + actionState = mainAction ?: ActionState.Install, + downloadState = downloadState, onClick = { onAction() - }) + } + ) } } } @@ -88,7 +90,7 @@ fun TopBarHeader( icon: String? = null, appName: String, packageName: String, - state: PackageState? = Install, + state: DownloadState? = null, actions: @Composable () -> Unit = {} ) { Surface( @@ -118,12 +120,12 @@ fun TopBarHeader( Box { actions() } } - AnimatedVisibility(visible = state is Cancelable) { + AnimatedVisibility(visible = state is DownloadState) { DownloadProgress( modifier = Modifier.padding(horizontal = 12.dp), - totalSize = if (state is Downloading) state.total ?: 1L else 1L, - isIndeterminate = state !is Downloading, - downloaded = if (state is Downloading) state.downloaded else 0L, + totalSize = if (state is DownloadState.Downloading) state.total ?: 1L else 1L, + isIndeterminate = state !is DownloadState.Downloading, + downloaded = if (state is DownloadState.Downloading) state.downloaded else 0L, ) } } diff --git a/src/main/kotlin/com/looker/droidify/ui/compose/utils/Callbacks.kt b/src/main/kotlin/com/looker/droidify/ui/compose/utils/Callbacks.kt index 4c0680b8..87a553af 100644 --- a/src/main/kotlin/com/looker/droidify/ui/compose/utils/Callbacks.kt +++ b/src/main/kotlin/com/looker/droidify/ui/compose/utils/Callbacks.kt @@ -2,12 +2,12 @@ package com.looker.droidify.ui.compose.utils import android.net.Uri import com.looker.droidify.database.entity.Release -import com.looker.droidify.entity.PackageState +import com.looker.droidify.entity.ComponentState import com.looker.droidify.entity.ProductPreference import com.looker.droidify.entity.Screenshot interface Callbacks { - fun onActionClick(action: PackageState?) + fun onActionClick(action: ComponentState?) fun onPreferenceChanged(preference: ProductPreference) fun onPermissionsClick(group: String?, permissions: List) fun onScreenshotClick(screenshot: Screenshot) diff --git a/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppViewModelX.kt b/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppViewModelX.kt index 08791cc6..20849b7c 100644 --- a/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppViewModelX.kt +++ b/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppViewModelX.kt @@ -8,7 +8,8 @@ import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Installed import com.looker.droidify.database.entity.Product import com.looker.droidify.database.entity.Repository -import com.looker.droidify.entity.PackageState +import com.looker.droidify.entity.ActionState +import com.looker.droidify.entity.DownloadState class AppViewModelX(val db: DatabaseX, val packageName: String) : ViewModel() { @@ -21,9 +22,10 @@ class AppViewModelX(val db: DatabaseX, val packageName: String) : ViewModel() { set(value) { _productRepos.value = value } - val state = MutableLiveData() - val actions = MutableLiveData>() - val secondaryAction = MutableLiveData() + val downloadState = MutableLiveData() + val mainAction = MutableLiveData() + val actions = MutableLiveData>() + val secondaryAction = MutableLiveData() init { products.addSource(db.productDao.getLive(packageName)) { products.setValue(it.filterNotNull()) }