custom instance frontend url

This commit is contained in:
Bnyro 2022-06-18 11:14:46 +02:00
parent 2673ff2e03
commit 4b0fdb9871
8 changed files with 113 additions and 37 deletions

View File

@ -2,7 +2,6 @@ package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
@ -27,6 +26,8 @@ class CustomInstanceDialog : DialogFragment() {
val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName) val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName)
val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl) val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl)
val instanceFrontendUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceFrontendUrl)
val addInstanceButton = view.findViewById<Button>(R.id.addInstance) val addInstanceButton = view.findViewById<Button>(R.id.addInstance)
val cancelButton = view.findViewById<Button>(R.id.cancel) val cancelButton = view.findViewById<Button>(R.id.cancel)
cancelButton.setOnClickListener { cancelButton.setOnClickListener {
@ -36,12 +37,15 @@ class CustomInstanceDialog : DialogFragment() {
addInstanceButton.setOnClickListener { addInstanceButton.setOnClickListener {
val instanceName = instanceNameEditText.text.toString() val instanceName = instanceNameEditText.text.toString()
val instanceApiUrl = instanceApiUrlEditText.text.toString() val instanceApiUrl = instanceApiUrlEditText.text.toString()
val instanceFrontendUrl = instanceFrontendUrlEditText.text.toString()
if (instanceName != "" && instanceApiUrl != "") { if (instanceName != "" && instanceApiUrl != "" && instanceFrontendUrl != "") {
try { try {
// check whether the URL is valid, otherwise catch // check whether the URL is valid, otherwise catch
val u = URL(instanceApiUrl).toURI() URL(instanceApiUrl).toURI()
saveCustomInstance(instanceName, instanceApiUrl) URL(instanceFrontendUrl).toURI()
saveCustomInstance(instanceName, instanceApiUrl, instanceFrontendUrl)
activity?.recreate() activity?.recreate()
dismiss() dismiss()
} catch (e: Exception) { } catch (e: Exception) {
@ -72,7 +76,11 @@ class CustomInstanceDialog : DialogFragment() {
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
private fun saveCustomInstance(instanceName: String, instanceApiUrl: String) { private fun saveCustomInstance(
instanceName: String,
instanceApiUrl: String,
instanceFrontendApiUrl: String
) {
val sharedPreferences = PreferenceManager val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext()) .getDefaultSharedPreferences(requireContext())
@ -84,7 +92,7 @@ class CustomInstanceDialog : DialogFragment() {
emptyList() emptyList()
} }
// get the urls of the other custom instances // get the api urls of the other custom instances
var customInstancesUrls = try { var customInstancesUrls = try {
sharedPreferences sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList() .getStringSet("custom_instances_url", HashSet())!!.toList()
@ -92,15 +100,24 @@ class CustomInstanceDialog : DialogFragment() {
emptyList() emptyList()
} }
// get the frontend urls of the other custom instances
var customInstancesFrontendUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// append new instance to the list // append new instance to the list
customInstancesNames += instanceName customInstancesNames += instanceName
customInstancesUrls += instanceApiUrl customInstancesUrls += instanceApiUrl
Log.e(TAG, customInstancesNames.toString()) customInstancesFrontendUrls += instanceFrontendApiUrl
// save them to the shared preferences // save them to the shared preferences
sharedPreferences.edit() sharedPreferences.edit()
.putStringSet("custom_instances_name", HashSet(customInstancesNames)) .putStringSet("custom_instances_name", HashSet(customInstancesNames))
.putStringSet("custom_instances_url", HashSet(customInstancesUrls)) .putStringSet("custom_instances_url", HashSet(customInstancesUrls))
.putStringSet("custom_instances_frontend_url", HashSet(customInstancesFrontendUrls))
.apply() .apply()
} }
} }

View File

@ -7,24 +7,20 @@ import androidx.fragment.app.DialogFragment
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.github.libretube.R import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.net.URLEncoder
class ShareDialog(private val videoId: String) : DialogFragment() { class ShareDialog(private val videoId: String) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val sharedPreferences = var shareOptions = arrayOf(
PreferenceManager.getDefaultSharedPreferences(requireContext()) getString(R.string.piped),
val instancePref = sharedPreferences.getString( getString(R.string.youtube)
"selectInstance",
"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)
) )
val instanceUrl = getCustomInstanceFrontendUrl()
// add instanceUrl option if custom instance frontend url available
if (instanceUrl != "") shareOptions += getString(R.string.instance)
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.share)) .setTitle(context?.getString(R.string.share))
.setItems( .setItems(
@ -32,8 +28,8 @@ class ShareDialog(private val videoId: String) : DialogFragment() {
) { _, id -> ) { _, id ->
val url = when (id) { val url = when (id) {
0 -> "https://piped.kavin.rocks/watch?v=$videoId" 0 -> "https://piped.kavin.rocks/watch?v=$videoId"
1 -> "https://piped.kavin.rocks/watch?v=$videoId$instance" 1 -> "https://youtu.be/$videoId"
2 -> "https://youtu.be/$videoId" 2 -> "$instanceUrl/watch?v=$videoId" // only available for custom instances
else -> "https://piped.kavin.rocks/watch?v=$videoId" else -> "https://piped.kavin.rocks/watch?v=$videoId"
} }
val intent = Intent() val intent = Intent()
@ -47,4 +43,38 @@ class ShareDialog(private val videoId: String) : DialogFragment() {
.show() .show()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
// get the frontend url if it's a custom instance
private fun getCustomInstanceFrontendUrl(): String {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
val instancePref = sharedPreferences.getString(
"selectInstance",
"https://pipedapi.kavin.rocks"
)
// get the api urls of the other custom instances
var customInstancesUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// get the frontend urls of the other custom instances
var customInstancesFrontendUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// return the custom instance frontend url if available
return if (customInstancesUrls.contains(instancePref)) {
val index = customInstancesUrls.indexOf(instancePref)
return customInstancesFrontendUrls[index]
} else {
""
}
}
} }

View File

@ -18,6 +18,7 @@ import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.dialogs.CreatePlaylistDialog import com.github.libretube.dialogs.CreatePlaylistDialog
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.io.IOException import java.io.IOException
import retrofit2.HttpException import retrofit2.HttpException
@ -68,16 +69,7 @@ class Library : Fragment() {
} }
} else { } else {
refreshLayout.isEnabled = false refreshLayout.isEnabled = false
view.findViewById<com.google.android.material.floatingactionbutton view.findViewById<FloatingActionButton>(R.id.create_playlist).visibility = View.GONE
.FloatingActionButton>(R.id.create_playlist).visibility = View.GONE
with(view.findViewById<ImageView>(R.id.boogh2)) {
visibility = View.VISIBLE
setImageResource(R.drawable.ic_login)
}
with(view.findViewById<TextView>(R.id.textLike2)) {
visibility = View.VISIBLE
text = getString(R.string.please_login)
}
} }
} }

View File

@ -5,6 +5,10 @@
android:viewportHeight="24" android:viewportHeight="24"
android:tint="?android:attr/colorControlNormal"> android:tint="?android:attr/colorControlNormal">
<path <path
android:fillColor="#FF000000" android:pathData="M9,3h8a2,2 0,0 1,2 2v14a2,2 0,0 1,-2 2H9m6,-9 l-4,-4m4,4 l-4,4m4,-4H5"
android:pathData="M10,11H4V3a1,1 0,0 1,1 -1h14a1,1 0,0 1,1 1v18a1,1 0,0 1,-1 1H5a1,1 0,0 1,-1 -1v-8h6v3l5,-4 -5,-4v3z"/> android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/black"
android:strokeLineCap="round"/>
</vector> </vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?android:attr/colorControlNormal">
<path
android:fillColor="#FF000000"
android:pathData="M10,11H4V3a1,1 0,0 1,1 -1h14a1,1 0,0 1,1 1v18a1,1 0,0 1,-1 1H5a1,1 0,0 1,-1 -1v-8h6v3l5,-4 -5,-4v3z"/>
</vector>

View File

@ -39,6 +39,30 @@
android:padding="12dp" /> android:padding="12dp" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintEnabled="false"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="4dp"
app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/instanceFrontendUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/instance_frontend_url"
android:inputType="text"
android:padding="12dp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -52,9 +76,7 @@
app:boxCornerRadiusBottomStart="15dp" app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusBottomEnd="15dp" app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusTopEnd="15dp" app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp" app:boxCornerRadiusTopStart="15dp">
>
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/instanceApiUrl" android:id="@+id/instanceApiUrl"

View File

@ -191,4 +191,5 @@
<string name="downloading">Downloading</string> <string name="downloading">Downloading</string>
<string name="player_autoplay">Autoplay</string> <string name="player_autoplay">Autoplay</string>
<string name="hideTrendingPage">Hide trending page</string> <string name="hideTrendingPage">Hide trending page</string>
<string name="instance_frontend_url">URL to instance frontend</string>
</resources> </resources>

View File

@ -24,7 +24,7 @@
android:icon="@drawable/ic_trash" /> android:icon="@drawable/ic_trash" />
<Preference <Preference
android:icon="@drawable/ic_login" android:icon="@drawable/ic_login_filled"
android:summary="@string/notgmail" android:summary="@string/notgmail"
app:key="login_register" app:key="login_register"
app:title="@string/login_register" /> app:title="@string/login_register" />