mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Fix: DownloadState and mainAction updating
This commit is contained in:
parent
482514e397
commit
e1760f5aa0
@ -2,12 +2,12 @@ package com.looker.droidify.ui.compose.utils
|
|||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import com.looker.droidify.database.entity.Release
|
import com.looker.droidify.database.entity.Release
|
||||||
import com.looker.droidify.entity.ComponentState
|
import com.looker.droidify.entity.ActionState
|
||||||
import com.looker.droidify.entity.ProductPreference
|
import com.looker.droidify.entity.ProductPreference
|
||||||
import com.looker.droidify.entity.Screenshot
|
import com.looker.droidify.entity.Screenshot
|
||||||
|
|
||||||
interface Callbacks {
|
interface Callbacks {
|
||||||
fun onActionClick(action: ComponentState?)
|
fun onActionClick(action: ActionState?)
|
||||||
fun onPreferenceChanged(preference: ProductPreference)
|
fun onPreferenceChanged(preference: ProductPreference)
|
||||||
fun onPermissionsClick(group: String?, permissions: List<String>)
|
fun onPermissionsClick(group: String?, permissions: List<String>)
|
||||||
fun onScreenshotClick(screenshot: Screenshot)
|
fun onScreenshotClick(screenshot: Screenshot)
|
||||||
|
@ -43,21 +43,12 @@ import com.looker.droidify.RELEASE_STATE_SUGGESTED
|
|||||||
import com.looker.droidify.content.Preferences
|
import com.looker.droidify.content.Preferences
|
||||||
import com.looker.droidify.content.ProductPreferences
|
import com.looker.droidify.content.ProductPreferences
|
||||||
import com.looker.droidify.database.entity.Release
|
import com.looker.droidify.database.entity.Release
|
||||||
|
import com.looker.droidify.entity.ActionState
|
||||||
import com.looker.droidify.entity.AntiFeature
|
import com.looker.droidify.entity.AntiFeature
|
||||||
import com.looker.droidify.entity.Cancelable
|
|
||||||
import com.looker.droidify.entity.Connecting
|
|
||||||
import com.looker.droidify.entity.Details
|
|
||||||
import com.looker.droidify.entity.DonateType
|
import com.looker.droidify.entity.DonateType
|
||||||
import com.looker.droidify.entity.Downloading
|
import com.looker.droidify.entity.DownloadState
|
||||||
import com.looker.droidify.entity.Install
|
|
||||||
import com.looker.droidify.entity.Launch
|
|
||||||
import com.looker.droidify.entity.PackageState
|
|
||||||
import com.looker.droidify.entity.Pending
|
|
||||||
import com.looker.droidify.entity.ProductPreference
|
import com.looker.droidify.entity.ProductPreference
|
||||||
import com.looker.droidify.entity.Screenshot
|
import com.looker.droidify.entity.Screenshot
|
||||||
import com.looker.droidify.entity.Share
|
|
||||||
import com.looker.droidify.entity.Uninstall
|
|
||||||
import com.looker.droidify.entity.Update
|
|
||||||
import com.looker.droidify.installer.AppInstaller
|
import com.looker.droidify.installer.AppInstaller
|
||||||
import com.looker.droidify.network.CoilDownloader
|
import com.looker.droidify.network.CoilDownloader
|
||||||
import com.looker.droidify.screen.MessageDialog
|
import com.looker.droidify.screen.MessageDialog
|
||||||
@ -116,7 +107,6 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
val packageName: String
|
val packageName: String
|
||||||
get() = requireArguments().getString(EXTRA_PACKAGE_NAME)!!
|
get() = requireArguments().getString(EXTRA_PACKAGE_NAME)!!
|
||||||
|
|
||||||
private var downloading = false
|
|
||||||
private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder ->
|
private val downloadConnection = Connection(DownloadService::class.java, onBind = { _, binder ->
|
||||||
binder.stateSubject
|
binder.stateSubject
|
||||||
.filter { it.packageName == packageName }
|
.filter { it.packageName == packageName }
|
||||||
@ -190,75 +180,76 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
product != null && installed != null && installed.launcherActivities.isNotEmpty()
|
product != null && installed != null && installed.launcherActivities.isNotEmpty()
|
||||||
val canShare = product != null && productRepos[0].second.name == "F-Droid"
|
val canShare = product != null && productRepos[0].second.name == "F-Droid"
|
||||||
|
|
||||||
val actions = mutableSetOf<PackageState>()
|
val actions = mutableSetOf<ActionState>()
|
||||||
launch {
|
launch {
|
||||||
if (canInstall) {
|
if (canInstall) {
|
||||||
actions += Install
|
actions += ActionState.Install
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
if (canUpdate) {
|
if (canUpdate) {
|
||||||
actions += Update
|
actions += ActionState.Update
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
if (canLaunch) {
|
if (canLaunch) {
|
||||||
actions += Launch
|
actions += ActionState.Launch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
if (installed != null) {
|
if (installed != null) {
|
||||||
actions += Details
|
actions += ActionState.Details
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
if (canUninstall) {
|
if (canUninstall) {
|
||||||
actions += Uninstall
|
actions += ActionState.Uninstall
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
if (canShare) {
|
if (canShare) {
|
||||||
actions += Share
|
actions += ActionState.Share
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO prioritize actions set and choose the first for main others for extra actions
|
||||||
val primaryAction = when {
|
val primaryAction = when {
|
||||||
canUpdate -> Update
|
canUpdate -> ActionState.Update
|
||||||
canLaunch -> Launch
|
canLaunch -> ActionState.Launch
|
||||||
canInstall -> Install
|
canInstall -> ActionState.Install
|
||||||
canShare -> Share
|
canShare -> ActionState.Share
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
val secondaryAction = when {
|
val secondaryAction = when {
|
||||||
primaryAction != Share && canShare -> Share
|
primaryAction != ActionState.Share && canShare -> ActionState.Share
|
||||||
primaryAction != Launch && canLaunch -> Launch
|
primaryAction != ActionState.Launch && canLaunch -> ActionState.Launch
|
||||||
installed != null && canUninstall -> Uninstall
|
installed != null && canUninstall -> ActionState.Uninstall
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
viewModel.actions.value = actions
|
viewModel.actions.value = actions
|
||||||
if (!downloading) {
|
|
||||||
viewModel.state.value = primaryAction
|
if (viewModel.downloadState.value != null && viewModel.mainAction.value?.textId != viewModel.downloadState.value?.textId)
|
||||||
viewModel.secondaryAction.value = secondaryAction
|
viewModel.downloadState.value?.let {
|
||||||
} else {
|
viewModel.mainAction.value = ActionState.Cancel(it.textId)
|
||||||
viewModel.secondaryAction.value = null
|
}
|
||||||
}
|
else if (viewModel.downloadState.value == null) // && viewModel.mainAction.value != primaryAction)
|
||||||
|
viewModel.mainAction.value = primaryAction
|
||||||
|
viewModel.secondaryAction.value = secondaryAction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateDownloadState(downloadState: DownloadService.State?) {
|
private suspend fun updateDownloadState(downloadState: DownloadService.State?) {
|
||||||
val packageState = when (downloadState) {
|
val state = when (downloadState) {
|
||||||
is DownloadService.State.Pending -> Pending
|
is DownloadService.State.Pending -> DownloadState.Pending
|
||||||
is DownloadService.State.Connecting -> Connecting
|
is DownloadService.State.Connecting -> DownloadState.Connecting
|
||||||
is DownloadService.State.Downloading -> Downloading(
|
is DownloadService.State.Downloading -> DownloadState.Downloading(
|
||||||
downloadState.read,
|
downloadState.read,
|
||||||
downloadState.total
|
downloadState.total
|
||||||
)
|
)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
val downloading = packageState is Cancelable
|
viewModel.downloadState.value = state
|
||||||
this.downloading = downloading
|
|
||||||
viewModel.state.value = packageState
|
|
||||||
updateButtons()
|
updateButtons()
|
||||||
if (downloadState is DownloadService.State.Success && isResumed && !rootInstallerEnabled) {
|
if (downloadState is DownloadService.State.Success && isResumed && !rootInstallerEnabled) {
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
@ -267,11 +258,11 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActionClick(action: PackageState?) {
|
override fun onActionClick(action: ActionState?) {
|
||||||
val productRepos = viewModel.productRepos
|
val productRepos = viewModel.productRepos
|
||||||
when (action) {
|
when (action) {
|
||||||
Install,
|
ActionState.Install,
|
||||||
Update,
|
ActionState.Update,
|
||||||
-> {
|
-> {
|
||||||
val installedItem = viewModel.installedItem.value
|
val installedItem = viewModel.installedItem.value
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
@ -284,7 +275,7 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
}
|
}
|
||||||
Unit
|
Unit
|
||||||
}
|
}
|
||||||
Launch -> {
|
ActionState.Launch -> {
|
||||||
viewModel.installedItem.value?.let {
|
viewModel.installedItem.value?.let {
|
||||||
requireContext().onLaunchClick(
|
requireContext().onLaunchClick(
|
||||||
it,
|
it,
|
||||||
@ -293,25 +284,26 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
}
|
}
|
||||||
Unit
|
Unit
|
||||||
}
|
}
|
||||||
Details -> {
|
ActionState.Details -> {
|
||||||
startActivity(
|
startActivity(
|
||||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||||
.setData(Uri.parse("package:$packageName"))
|
.setData(Uri.parse("package:$packageName"))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Uninstall -> {
|
ActionState.Uninstall -> {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
|
AppInstaller.getInstance(context)?.defaultInstaller?.uninstall(packageName)
|
||||||
}
|
}
|
||||||
Unit
|
Unit
|
||||||
}
|
}
|
||||||
is Cancelable -> {
|
is ActionState.Cancel -> {
|
||||||
|
// TODO fix cancel, send a cancel intent maybe?
|
||||||
val binder = downloadConnection.binder
|
val binder = downloadConnection.binder
|
||||||
if (downloading && binder != null) {
|
if (viewModel.downloadState.value != null && binder != null) {
|
||||||
binder.cancel(packageName)
|
binder.cancel(packageName)
|
||||||
} else Unit
|
} else Unit
|
||||||
}
|
}
|
||||||
Share -> {
|
ActionState.Share -> {
|
||||||
shareIntent(packageName, productRepos[0].first.label)
|
shareIntent(packageName, productRepos[0].first.label)
|
||||||
}
|
}
|
||||||
else -> Unit
|
else -> Unit
|
||||||
@ -426,9 +418,10 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
val installed by viewModel.installedItem.observeAsState()
|
val installed by viewModel.installedItem.observeAsState()
|
||||||
val products by viewModel.products.observeAsState()
|
val products by viewModel.products.observeAsState()
|
||||||
val repos by viewModel.repositories.observeAsState()
|
val repos by viewModel.repositories.observeAsState()
|
||||||
val packageState by viewModel.state.observeAsState(if (installed == null) Install else Launch)
|
val downloadState by viewModel.downloadState.observeAsState(null)
|
||||||
|
val mainAction by viewModel.mainAction.observeAsState(if (installed == null) ActionState.Install else ActionState.Launch)
|
||||||
val actions by viewModel.actions.observeAsState() // TODO add rest actions to UI
|
val actions by viewModel.actions.observeAsState() // TODO add rest actions to UI
|
||||||
val secondaryAction by viewModel.secondaryAction.observeAsState() // TODO add secondaryAction
|
val secondaryAction by viewModel.secondaryAction.observeAsState()
|
||||||
val productRepos = products?.mapNotNull { product ->
|
val productRepos = products?.mapNotNull { product ->
|
||||||
repos?.firstOrNull { it.id == product.repositoryId }
|
repos?.firstOrNull { it.id == product.repositoryId }
|
||||||
?.let { Pair(product, it) }
|
?.let { Pair(product, it) }
|
||||||
@ -478,7 +471,7 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
appName = product.label,
|
appName = product.label,
|
||||||
packageName = product.packageName,
|
packageName = product.packageName,
|
||||||
icon = imageData,
|
icon = imageData,
|
||||||
state = packageState
|
state = downloadState
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
) { paddingValues ->
|
) { paddingValues ->
|
||||||
@ -493,7 +486,7 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
versionCode = product.versionCode.toString(),
|
versionCode = product.versionCode.toString(),
|
||||||
appSize = product.displayRelease?.size?.formatSize().orEmpty(),
|
appSize = product.displayRelease?.size?.formatSize().orEmpty(),
|
||||||
appDev = product.author.name.replaceFirstChar { it.titlecase() },
|
appDev = product.author.name.replaceFirstChar { it.titlecase() },
|
||||||
state = packageState,
|
mainAction = mainAction,
|
||||||
secondaryAction = secondaryAction,
|
secondaryAction = secondaryAction,
|
||||||
onSource = {
|
onSource = {
|
||||||
product.source.let { link ->
|
product.source.let { link ->
|
||||||
@ -514,7 +507,7 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), Callbacks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onAction = { onActionClick(packageState) },
|
onAction = { onActionClick(mainAction) },
|
||||||
onSecondaryAction = { onActionClick(secondaryAction) }
|
onSecondaryAction = { onActionClick(secondaryAction) }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user