From 6efdb0f1f4d3be994c1ab0ed81b5e63dddfe7d03 Mon Sep 17 00:00:00 2001 From: darken Date: Mon, 2 Sep 2024 13:52:57 +0200 Subject: [PATCH] Clicking entries in the app list opens their Google Play Store page --- .../modules/apps/ui/appslist/AppListAction.kt | 10 +++++++ .../apps/ui/appslist/AppsListFragment.kt | 18 ++++++++++++ .../modules/apps/ui/appslist/AppsListVM.kt | 28 ++++++++++++++++--- .../modules/apps/ui/appslist/DefaultPkgVH.kt | 3 ++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppListAction.kt diff --git a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppListAction.kt b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppListAction.kt new file mode 100644 index 00000000..152910d4 --- /dev/null +++ b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppListAction.kt @@ -0,0 +1,10 @@ +package eu.darken.octi.modules.apps.ui.appslist + +import android.content.Intent + +sealed interface AppListAction { + data class OpenAppOrStore( + val intent: Intent, + val fallback: Intent, + ) : AppListAction +} \ No newline at end of file diff --git a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListFragment.kt b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListFragment.kt index cad8f6fd..f760d102 100644 --- a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListFragment.kt +++ b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListFragment.kt @@ -7,6 +7,7 @@ import androidx.navigation.fragment.findNavController import androidx.navigation.ui.setupWithNavController import dagger.hilt.android.AndroidEntryPoint import eu.darken.octi.R +import eu.darken.octi.common.error.asErrorDialogBuilder import eu.darken.octi.common.lists.differ.update import eu.darken.octi.common.lists.setupDefaults import eu.darken.octi.common.observe2 @@ -41,6 +42,23 @@ class AppsListFragment : Fragment3(R.layout.module_apps_list_fragment) { appsAdapter.update(it.items) } + vm.events.observe2 { event -> + when (event) { + is AppListAction.OpenAppOrStore -> { + try { + startActivity(event.intent) + } catch (e: Exception) { + try { + startActivity(event.fallback) + } catch (e: Exception) { + e.asErrorDialogBuilder(requireActivity()).show() + } + } + + } + } + } + super.onViewCreated(view, savedInstanceState) } } diff --git a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListVM.kt b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListVM.kt index 718427e3..4394602a 100644 --- a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListVM.kt +++ b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/AppsListVM.kt @@ -1,6 +1,8 @@ package eu.darken.octi.modules.apps.ui.appslist import android.annotation.SuppressLint +import android.content.Intent +import android.net.Uri import androidx.lifecycle.LiveData import androidx.lifecycle.SavedStateHandle import dagger.hilt.android.lifecycle.HiltViewModel @@ -8,6 +10,7 @@ import eu.darken.octi.common.coroutine.DispatcherProvider import eu.darken.octi.common.debug.logging.Logging.Priority.ERROR import eu.darken.octi.common.debug.logging.log import eu.darken.octi.common.debug.logging.logTag +import eu.darken.octi.common.livedata.SingleLiveEvent import eu.darken.octi.common.navigation.navArgs import eu.darken.octi.common.uix.ViewModel3 import eu.darken.octi.modules.apps.core.AppsRepo @@ -18,14 +21,15 @@ import javax.inject.Inject @HiltViewModel @SuppressLint("StaticFieldLeak") class AppsListVM @Inject constructor( - @Suppress("UNUSED_PARAMETER") handle: SavedStateHandle, + handle: SavedStateHandle, dispatcherProvider: DispatcherProvider, - private val metaRepo: MetaRepo, - private val appsRepo: AppsRepo, + metaRepo: MetaRepo, + appsRepo: AppsRepo, ) : ViewModel3(dispatcherProvider = dispatcherProvider) { private val navArgs: AppsListFragmentArgs by handle.navArgs() + val events = SingleLiveEvent() data class State( val deviceLabel: String = "", @@ -46,7 +50,23 @@ class AppsListVM @Inject constructor( } val items = moduleData.data.installedPackages - .map { pkg -> DefaultPkgVH.Item(pkg = pkg) } + .map { pkg -> + DefaultPkgVH.Item( + pkg = pkg, + onClick = { + val intent = Intent().apply { + action = Intent.ACTION_VIEW + data = Uri.parse("market://details?id=${pkg.packageName}") + setPackage("com.android.vending") + } + val fallback = Intent().apply { + action = Intent.ACTION_VIEW + data = Uri.parse("https://play.google.com/store/apps/details?id=${pkg.packageName}") + } + events.postValue(AppListAction.OpenAppOrStore(intent, fallback)) + } + ) + } .sortedByDescending { it.pkg.installedAt } State( diff --git a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/DefaultPkgVH.kt b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/DefaultPkgVH.kt index fb476ea9..d4ddfb87 100644 --- a/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/DefaultPkgVH.kt +++ b/app/src/main/java/eu/darken/octi/modules/apps/ui/appslist/DefaultPkgVH.kt @@ -26,10 +26,13 @@ class DefaultPkgVH(parent: ViewGroup) : primary.text = pkg.label ?: pkg.packageName secondary.text = "${pkg.versionName} (${pkg.versionCode}) - ${pkg.packageName}" + + root.setOnClickListener { item.onClick() } } data class Item( val pkg: AppsInfo.Pkg, + val onClick: () -> Unit, ) : AppsListAdapter.Item { override val stableId: Long = this.javaClass.hashCode().toLong() }