Skip to content

Commit

Permalink
Clicking entries in the app list opens their Google Play Store page
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Sep 2, 2024
1 parent 6fa399b commit 6efdb0f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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
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
Expand All @@ -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<AppListAction>()

data class State(
val deviceLabel: String = "",
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 6efdb0f

Please sign in to comment.