refactor: remove constructors from IntentChooserSheet

This commit is contained in:
Bnyro 2023-09-11 11:02:17 +02:00
parent a32e5c2841
commit 4b8482722f
3 changed files with 29 additions and 12 deletions

View File

@ -29,4 +29,5 @@ object IntentData {
const val logoutTask = "logoutTask"
const val color = "color"
const val streamItem = "streamItem"
const val url = "url"
}

View File

@ -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<ResolveInfo> {
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)
}
}

View File

@ -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<ResolveInfo>,
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)
}