diff --git a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt index 327eff80c..afe2b6a06 100644 --- a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -1,41 +1,48 @@ package com.github.libretube.dialogs -import android.content.Context +import android.app.Dialog import android.content.Intent +import android.os.Bundle +import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager import com.github.libretube.R import com.google.android.material.dialog.MaterialAlertDialogBuilder import java.net.URLEncoder -fun showShareDialog(context: Context, videoId: String) { - val sharedPreferences = - PreferenceManager.getDefaultSharedPreferences(context) - val instancePref = sharedPreferences.getString( - "instance", - "https://pipedapi.kavin.rocks" - )!! - val instance = "&instance=${URLEncoder.encode(instancePref, "UTF-8")}" - val shareOptions = arrayOf( - context.getString(R.string.piped), - context.getString(R.string.instance), - context.getString(R.string.youtube) - ) - MaterialAlertDialogBuilder(context) - .setTitle(context.getString(R.string.share)) - .setItems( - shareOptions - ) { _, id -> - val url = when (id) { - 0 -> "https://piped.kavin.rocks/watch?v=$videoId" - 1 -> "https://piped.kavin.rocks/watch?v=$videoId$instance" - 2 -> "https://youtu.be/$videoId" - else -> "https://piped.kavin.rocks/watch?v=$videoId" - } - val intent = Intent() - intent.action = Intent.ACTION_SEND - intent.putExtra(Intent.EXTRA_TEXT, url) - intent.type = "text/plain" - context.startActivity(Intent.createChooser(intent, "Share Url To:")) - } - .show() +class ShareDialog(private val videoId: String) : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + return activity?.let { + val sharedPreferences = + PreferenceManager.getDefaultSharedPreferences(requireContext()) + val instancePref = sharedPreferences.getString( + "instance", + "https://pipedapi.kavin.rocks" + )!! + val instance = "&instance=${URLEncoder.encode(instancePref, "UTF-8")}" + val shareOptions = arrayOf( + context?.getString(R.string.piped), + context?.getString(R.string.instance), + context?.getString(R.string.youtube) + ) + MaterialAlertDialogBuilder(requireContext()) + .setTitle(context?.getString(R.string.share)) + .setItems( + shareOptions + ) { _, id -> + val url = when (id) { + 0 -> "https://piped.kavin.rocks/watch?v=$videoId" + 1 -> "https://piped.kavin.rocks/watch?v=$videoId$instance" + 2 -> "https://youtu.be/$videoId" + else -> "https://piped.kavin.rocks/watch?v=$videoId" + } + val intent = Intent() + intent.action = Intent.ACTION_SEND + intent.putExtra(Intent.EXTRA_TEXT, url) + intent.type = "text/plain" + context?.startActivity(Intent.createChooser(intent, "Share Url To:")) + } + .show() + } ?: throw IllegalStateException("Activity cannot be null") + } } diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index aa557fbf3..19a521ee8 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -66,7 +66,8 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog } } 2 -> { - showShareDialog(requireContext(), videoId) + val shareDialog = ShareDialog(videoId) + shareDialog.show(childFragmentManager, "ShareDialog") } else -> { dialog.dismiss() diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index e2b0c48db..f8a86cad6 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -48,7 +48,7 @@ import com.github.libretube.adapters.CommentsAdapter import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.dialogs.AddtoPlaylistDialog import com.github.libretube.dialogs.DownloadDialog -import com.github.libretube.dialogs.showShareDialog +import com.github.libretube.dialogs.ShareDialog import com.github.libretube.formatShort import com.github.libretube.hideKeyboard import com.github.libretube.obj.PipedStream @@ -493,7 +493,8 @@ class PlayerFragment : Fragment() { // share button view.findViewById(R.id.relPlayer_share).setOnClickListener { - showShareDialog(requireContext(), videoId!!) + val shareDialog = ShareDialog(videoId!!) + shareDialog.show(childFragmentManager, "ShareDialog") } // check if livestream if (response.duration!! > 0) {