mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Merge pull request #670 from Bnyro/master
Instance selection for login and fetching
This commit is contained in:
commit
05e44a3419
@ -61,8 +61,17 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
RetrofitInstance.url =
|
RetrofitInstance.url =
|
||||||
PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!!
|
PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!!
|
||||||
|
// set auth instance
|
||||||
RetrofitInstance.authUrl =
|
RetrofitInstance.authUrl =
|
||||||
PreferenceHelper.getString(this, "selectAuthInstance", "https://pipedapi.kavin.rocks/")!!
|
if (PreferenceHelper.getBoolean(this, "auth_instance_toggle", false)) {
|
||||||
|
PreferenceHelper.getString(
|
||||||
|
this,
|
||||||
|
"selectAuthInstance",
|
||||||
|
"https://pipedapi.kavin.rocks/"
|
||||||
|
)!!
|
||||||
|
} else {
|
||||||
|
RetrofitInstance.url
|
||||||
|
}
|
||||||
|
|
||||||
ThemeHelper.updateTheme(this)
|
ThemeHelper.updateTheme(this)
|
||||||
LocaleHelper.updateLanguage(this)
|
LocaleHelper.updateLanguage(this)
|
||||||
|
@ -18,6 +18,7 @@ import androidx.lifecycle.lifecycleScope
|
|||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
|
import androidx.preference.SwitchPreference
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.activities.SettingsActivity
|
import com.github.libretube.activities.SettingsActivity
|
||||||
import com.github.libretube.activities.requireMainActivityRestart
|
import com.github.libretube.activities.requireMainActivityRestart
|
||||||
@ -116,16 +117,44 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
|
|
||||||
val instance = findPreference<ListPreference>("selectInstance")
|
val instance = findPreference<ListPreference>("selectInstance")
|
||||||
// fetchInstance()
|
// fetchInstance()
|
||||||
initCustomInstances()
|
initCustomInstances(instance!!)
|
||||||
instance?.setOnPreferenceChangeListener { _, newValue ->
|
instance.setOnPreferenceChangeListener { _, newValue ->
|
||||||
requireMainActivityRestart = true
|
requireMainActivityRestart = true
|
||||||
RetrofitInstance.url = newValue.toString()
|
RetrofitInstance.url = newValue.toString()
|
||||||
|
if (!PreferenceHelper.getBoolean(requireContext(), "auth_instance_toggle", false)) {
|
||||||
|
RetrofitInstance.authUrl = newValue.toString()
|
||||||
|
logout()
|
||||||
|
}
|
||||||
|
RetrofitInstance.lazyMgr.reset()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
val authInstance = findPreference<ListPreference>("selectAuthInstance")
|
||||||
|
initCustomInstances(authInstance!!)
|
||||||
|
// hide auth instance if option deselected
|
||||||
|
if (!PreferenceHelper.getBoolean(requireContext(), "auth_instance_toggle", false)) {
|
||||||
|
authInstance.isVisible = false
|
||||||
|
}
|
||||||
|
authInstance.setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
requireMainActivityRestart = true
|
||||||
|
// save new auth url
|
||||||
RetrofitInstance.authUrl = newValue.toString()
|
RetrofitInstance.authUrl = newValue.toString()
|
||||||
RetrofitInstance.lazyMgr.reset()
|
RetrofitInstance.lazyMgr.reset()
|
||||||
logout()
|
logout()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val authInstanceToggle = findPreference<SwitchPreference>("auth_instance_toggle")
|
||||||
|
authInstanceToggle?.setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
requireMainActivityRestart = true
|
||||||
|
authInstance.isVisible = newValue == true
|
||||||
|
logout()
|
||||||
|
// either use new auth url or the normal api url if auth instance disabled
|
||||||
|
RetrofitInstance.authUrl = if (newValue == false) RetrofitInstance.url
|
||||||
|
else authInstance.value
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
val customInstance = findPreference<Preference>("customInstance")
|
val customInstance = findPreference<Preference>("customInstance")
|
||||||
customInstance?.setOnPreferenceClickListener {
|
customInstance?.setOnPreferenceClickListener {
|
||||||
val newFragment = CustomInstanceDialog()
|
val newFragment = CustomInstanceDialog()
|
||||||
@ -169,58 +198,12 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
|
|
||||||
val importFromYt = findPreference<Preference>("import_from_yt")
|
val importFromYt = findPreference<Preference>("import_from_yt")
|
||||||
importFromYt?.setOnPreferenceClickListener {
|
importFromYt?.setOnPreferenceClickListener {
|
||||||
val token = PreferenceHelper.getToken(requireContext())
|
importSubscriptions()
|
||||||
// check StorageAccess
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
||||||
Log.d("myz", "" + Build.VERSION.SDK_INT)
|
|
||||||
if (ContextCompat.checkSelfPermission(
|
|
||||||
this.requireContext(),
|
|
||||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
|
||||||
)
|
|
||||||
!= PackageManager.PERMISSION_GRANTED
|
|
||||||
) {
|
|
||||||
ActivityCompat.requestPermissions(
|
|
||||||
this.requireActivity(),
|
|
||||||
arrayOf(
|
|
||||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
||||||
Manifest.permission.MANAGE_EXTERNAL_STORAGE
|
|
||||||
),
|
|
||||||
1
|
|
||||||
) // permission request code is just an int
|
|
||||||
} else if (token != "") {
|
|
||||||
MainSettings.getContent.launch("*/*")
|
|
||||||
} else {
|
|
||||||
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (ActivityCompat.checkSelfPermission(
|
|
||||||
requireContext(),
|
|
||||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
|
||||||
) != PackageManager.PERMISSION_GRANTED ||
|
|
||||||
ActivityCompat.checkSelfPermission(
|
|
||||||
requireContext(),
|
|
||||||
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
||||||
) != PackageManager.PERMISSION_GRANTED
|
|
||||||
) {
|
|
||||||
ActivityCompat.requestPermissions(
|
|
||||||
this.requireActivity(),
|
|
||||||
arrayOf(
|
|
||||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
||||||
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
||||||
),
|
|
||||||
1
|
|
||||||
)
|
|
||||||
} else if (token != "") {
|
|
||||||
MainSettings.getContent.launch("*/*")
|
|
||||||
} else {
|
|
||||||
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initCustomInstances() {
|
private fun initCustomInstances(instancePref: ListPreference) {
|
||||||
val customInstances = PreferenceHelper.getCustomInstances(requireContext())
|
val customInstances = PreferenceHelper.getCustomInstances(requireContext())
|
||||||
|
|
||||||
var instanceNames = resources.getStringArray(R.array.instances)
|
var instanceNames = resources.getStringArray(R.array.instances)
|
||||||
@ -231,10 +214,9 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add custom instances to the list preference
|
// add custom instances to the list preference
|
||||||
val instance = findPreference<ListPreference>("selectInstance")
|
instancePref.entries = instanceNames
|
||||||
instance?.entries = instanceNames
|
instancePref.entryValues = instanceValues
|
||||||
instance?.entryValues = instanceValues
|
instancePref.summaryProvider =
|
||||||
instance?.summaryProvider =
|
|
||||||
Preference.SummaryProvider<ListPreference> { preference ->
|
Preference.SummaryProvider<ListPreference> { preference ->
|
||||||
val text = preference.entry
|
val text = preference.entry
|
||||||
if (TextUtils.isEmpty(text)) {
|
if (TextUtils.isEmpty(text)) {
|
||||||
@ -247,6 +229,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
|
|
||||||
private fun logout() {
|
private fun logout() {
|
||||||
PreferenceHelper.setToken(requireContext(), "")
|
PreferenceHelper.setToken(requireContext(), "")
|
||||||
|
Toast.makeText(context, getString(R.string.loggedout), Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchInstance() {
|
private fun fetchInstance() {
|
||||||
@ -298,6 +281,56 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
|||||||
activity?.runOnUiThread(action)
|
activity?.runOnUiThread(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun importSubscriptions() {
|
||||||
|
val token = PreferenceHelper.getToken(requireContext())
|
||||||
|
// check StorageAccess
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
Log.d("myz", "" + Build.VERSION.SDK_INT)
|
||||||
|
if (ContextCompat.checkSelfPermission(
|
||||||
|
this.requireContext(),
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||||
|
)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this.requireActivity(),
|
||||||
|
arrayOf(
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
|
Manifest.permission.MANAGE_EXTERNAL_STORAGE
|
||||||
|
),
|
||||||
|
1
|
||||||
|
) // permission request code is just an int
|
||||||
|
} else if (token != "") {
|
||||||
|
MainSettings.getContent.launch("*/*")
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ActivityCompat.checkSelfPermission(
|
||||||
|
requireContext(),
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||||
|
) != PackageManager.PERMISSION_GRANTED ||
|
||||||
|
ActivityCompat.checkSelfPermission(
|
||||||
|
requireContext(),
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this.requireActivity(),
|
||||||
|
arrayOf(
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
|
),
|
||||||
|
1
|
||||||
|
)
|
||||||
|
} else if (token != "") {
|
||||||
|
MainSettings.getContent.launch("*/*")
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun subscribe(channels: List<String>) {
|
private fun subscribe(channels: List<String>) {
|
||||||
fun run() {
|
fun run() {
|
||||||
lifecycleScope.launchWhenCreated {
|
lifecycleScope.launchWhenCreated {
|
||||||
|
14
app/src/main/res/drawable/ic_auth.xml
Normal file
14
app/src/main/res/drawable/ic_auth.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:tint="?android:attr/colorControlNormal"
|
||||||
|
android:viewportWidth="36"
|
||||||
|
android:viewportHeight="36">
|
||||||
|
<path
|
||||||
|
android:fillColor="#FF000000"
|
||||||
|
android:pathData="M23.38,16.77l0.6,-0.6A5,5 0,0 0,24 9.1L18.71,3.84a5,5 0,0 0,-7.07 0L3.09,12.39a5,5 0,0 0,0 7.07l5.26,5.26a5,5 0,0 0,7.07 0l0.45,-0.45 2.1,2.2h3.44v3h3.69v1.63L28,34h6L34,27.45ZM14.82,10.18L9.37,15.64a1,1 0,0 1,-1.41 0l-0.4,-0.4a1,1 0,0 1,0 -1.41L13,8.36a1,1 0,0 1,1.41 0l0.4,0.4A1,1 0,0 1,14.82 10.18ZM32,32L28.86,32l-1.77,-1.76v-2.8L23.41,27.44v-3L18.8,24.44l-1.52,-1.61L22,18.18 32,28.28Z" />
|
||||||
|
<path
|
||||||
|
android:fillAlpha="0"
|
||||||
|
android:fillColor="#FF000000"
|
||||||
|
android:pathData="M0,0h36v36h-36z" />
|
||||||
|
</vector>
|
@ -210,4 +210,7 @@
|
|||||||
<string name="watch_history">Watch History</string>
|
<string name="watch_history">Watch History</string>
|
||||||
<string name="watch_positions">Remember position</string>
|
<string name="watch_positions">Remember position</string>
|
||||||
<string name="watch_positions_summary">Remember the watch position and automatically seek to it.</string>
|
<string name="watch_positions_summary">Remember the watch position and automatically seek to it.</string>
|
||||||
|
<string name="auth_instance">Authentication instance</string>
|
||||||
|
<string name="auth_instance_summary">Use a different instance for authenticated calls.</string>
|
||||||
|
<string name="auth_instances">Choose an auth instance</string>
|
||||||
</resources>
|
</resources>
|
@ -23,6 +23,21 @@
|
|||||||
app:key="clearCustomInstances"
|
app:key="clearCustomInstances"
|
||||||
app:title="@string/clear_customInstances" />
|
app:title="@string/clear_customInstances" />
|
||||||
|
|
||||||
|
<SwitchPreference
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:icon="@drawable/ic_auth"
|
||||||
|
app:key="auth_instance_toggle"
|
||||||
|
app:summary="@string/auth_instance_summary"
|
||||||
|
app:title="@string/auth_instance" />
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
android:icon="@drawable/ic_server"
|
||||||
|
app:defaultValue="https://pipedapi.kavin.rocks/"
|
||||||
|
app:entries="@array/instances"
|
||||||
|
app:entryValues="@array/instancesValue"
|
||||||
|
app:key="selectAuthInstance"
|
||||||
|
app:title="@string/auth_instances" />
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory app:title="@string/account">
|
<PreferenceCategory app:title="@string/account">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user