From 3c3549b56fd1901c8e0d2c796bfda49cf0c4f302 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 2 Jun 2022 21:40:36 +0200 Subject: [PATCH] seperated sharedialog --- .../github/libretube/dialogs/ShareDialog.kt | 41 +++++++++++++ .../libretube/dialogs/VideoOptionsDialog.kt | 43 +------------ .../libretube/fragments/PlayerFragment.kt | 60 +++++-------------- 3 files changed, 57 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt diff --git a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt new file mode 100644 index 000000000..327eff80c --- /dev/null +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -0,0 +1,41 @@ +package com.github.libretube.dialogs + +import android.content.Context +import android.content.Intent +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() +} 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 95096c3d4..aa557fbf3 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -2,7 +2,6 @@ package com.github.libretube.dialogs import android.app.Dialog import android.content.Context -import android.content.Intent import android.os.Bundle import android.widget.ArrayAdapter import android.widget.Toast @@ -67,47 +66,7 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog } } 2 -> { - /* crashes - 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( - getString(R.string.piped), - getString(R.string.instance), - getString(R.string.youtube) - ) - MaterialAlertDialogBuilder(requireContext()) - .setTitle(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" - } - dismiss() - val intent = Intent() - intent.action = Intent.ACTION_SEND - intent.putExtra(Intent.EXTRA_TEXT, url) - intent.type = "text/plain" - startActivity(Intent.createChooser(intent, "Share Url To:")) - } - .show() - */ - val intent = Intent() - intent.action = Intent.ACTION_SEND - intent.putExtra( - Intent.EXTRA_TEXT, - "https://piped.kavin.rocks/watch?v=$videoId" - ) - intent.type = "text/plain" - startActivity(Intent.createChooser(intent, "Share Url To:")) + showShareDialog(requireContext(), videoId) } 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 6dde763d9..7d0c3ea00 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -52,6 +52,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.formatShort import com.github.libretube.hideKeyboard import com.github.libretube.obj.PipedStream @@ -83,12 +84,11 @@ import com.google.android.exoplayer2.util.RepeatModeUtil import com.google.android.material.button.MaterialButton import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.squareup.picasso.Picasso -import java.io.IOException -import java.net.URLEncoder -import java.util.concurrent.Executors -import kotlin.math.abs import org.chromium.net.CronetEngine import retrofit2.HttpException +import java.io.IOException +import java.util.concurrent.Executors +import kotlin.math.abs var isFullScreen = false @@ -419,6 +419,16 @@ class PlayerFragment : Fragment() { private fun initializePlayerView(view: View, response: Streams) { isLoading = false runOnUiThread { + createExoPlayer(view) + + exoPlayerView.setShowSubtitleButton(true) + exoPlayerView.setShowNextButton(false) + exoPlayerView.setShowPreviousButton(false) + exoPlayerView.setRepeatToggleModes(RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) + // exoPlayerView.controllerShowTimeoutMs = 1500 + exoPlayerView.controllerHideOnTouch = true + exoPlayerView.player = exoPlayer + var videosNameArray: Array = arrayOf() videosNameArray += "HLS" for (vid in response.videoStreams!!) { @@ -434,16 +444,6 @@ class PlayerFragment : Fragment() { .build() ) } - - createExoPlayer(view) - - exoPlayerView.setShowSubtitleButton(true) - exoPlayerView.setShowNextButton(false) - exoPlayerView.setShowPreviousButton(false) - exoPlayerView.setRepeatToggleModes(RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) - // exoPlayerView.controllerShowTimeoutMs = 1500 - exoPlayerView.controllerHideOnTouch = true - exoPlayerView.player = exoPlayer val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val defres = sharedPreferences.getString("default_res", "")!! @@ -709,37 +709,7 @@ class PlayerFragment : Fragment() { } // share button view.findViewById(R.id.relPlayer_share).setOnClickListener { - 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( - getString(R.string.piped), - getString(R.string.instance), - getString(R.string.youtube) - ) - MaterialAlertDialogBuilder(requireContext()) - .setTitle(getString(R.string.share)) - .setItems( - shareOptions, - DialogInterface.OnClickListener { _, 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" - startActivity(Intent.createChooser(intent, "Share Url To:")) - } - ) - .show() + showShareDialog(requireContext(), videoId!!) } // check if livestream if (response.duration!! > 0) {