From 4b8482722ff61c27de5e7d9e4a7e6c5b4c8866a1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 11 Sep 2023 11:02:17 +0200 Subject: [PATCH] refactor: remove constructors from IntentChooserSheet --- .../github/libretube/constants/IntentData.kt | 1 + .../github/libretube/helpers/IntentHelper.kt | 25 +++++++++++++------ .../libretube/ui/sheets/IntentChooserSheet.kt | 15 ++++++++--- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt index 472a1cbe1..461b0fa66 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -29,4 +29,5 @@ object IntentData { const val logoutTask = "logoutTask" const val color = "color" const val streamItem = "streamItem" + const val url = "url" } diff --git a/app/src/main/java/com/github/libretube/helpers/IntentHelper.kt b/app/src/main/java/com/github/libretube/helpers/IntentHelper.kt index 05a57eb68..af7aa8181 100644 --- a/app/src/main/java/com/github/libretube/helpers/IntentHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/IntentHelper.kt @@ -3,21 +3,25 @@ package com.github.libretube.helpers import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.content.pm.ResolveInfo import android.os.Build import androidx.core.net.toUri +import androidx.core.os.bundleOf import androidx.fragment.app.FragmentManager import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.ui.sheets.IntentChooserSheet object IntentHelper { - fun openLinkFromHref(context: Context, fragmentManager: FragmentManager, link: String) { - val intent = Intent(Intent.ACTION_VIEW) - .setData(link.toUri()) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + private fun getResolveIntent(link: String) = Intent(Intent.ACTION_VIEW) + .setData(link.toUri()) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - @Suppress("DEPRECATION") - val resolveInfoList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + fun getResolveInfo(context: Context, link: String): List { + val intent = getResolveIntent(link) + + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { context.packageManager .queryIntentActivities( intent, @@ -27,15 +31,20 @@ object IntentHelper { context.packageManager .queryIntentActivities(intent, PackageManager.MATCH_ALL) } + } + + fun openLinkFromHref(context: Context, fragmentManager: FragmentManager, link: String) { + val resolveInfoList = getResolveInfo(context, link) if (resolveInfoList.isEmpty()) { try { - context.startActivity(intent) + context.startActivity(getResolveIntent(link)) } catch (e: Exception) { context.toastFromMainThread(R.string.error) } } else { - IntentChooserSheet(resolveInfoList, link) + IntentChooserSheet() + .apply { arguments = bundleOf(IntentData.url to link) } .show(fragmentManager) } } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/IntentChooserSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/IntentChooserSheet.kt index 67caeb8cb..a7d9e815f 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/IntentChooserSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/IntentChooserSheet.kt @@ -6,15 +6,21 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.GridLayoutManager +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.BottomSheetBinding +import com.github.libretube.helpers.IntentHelper import com.github.libretube.ui.adapters.IntentChooserAdapter -class IntentChooserSheet( - private val packages: List, - private val url: String -) : BaseBottomSheet() { +class IntentChooserSheet : BaseBottomSheet() { private var _binding: BottomSheetBinding? = null private val binding get() = _binding!! + private lateinit var url: String + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + url = arguments?.getString(IntentData.url)!! + } override fun onCreateView( inflater: LayoutInflater, @@ -27,6 +33,7 @@ class IntentChooserSheet( override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val binding = binding + val packages = IntentHelper.getResolveInfo(requireContext(), url) binding.optionsRecycler.layoutManager = GridLayoutManager(context, 3) binding.optionsRecycler.adapter = IntentChooserAdapter(packages, url) }