mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
auth instance
This commit is contained in:
parent
d5b163c879
commit
bcc475102a
@ -62,7 +62,15 @@ class MainActivity : AppCompatActivity() {
|
||||
RetrofitInstance.url =
|
||||
PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!!
|
||||
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)
|
||||
LocaleHelper.updateLanguage(this)
|
||||
|
@ -69,6 +69,7 @@ class LoginDialog : DialogFragment() {
|
||||
private fun login(login: Login) {
|
||||
fun run() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
Log.e(TAG, RetrofitInstance.authUrl)
|
||||
val response = try {
|
||||
RetrofitInstance.authApi.login(login)
|
||||
} catch (e: IOException) {
|
||||
|
@ -18,6 +18,7 @@ import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.SwitchPreference
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.activities.SettingsActivity
|
||||
import com.github.libretube.activities.requireMainActivityRestart
|
||||
@ -33,6 +34,7 @@ import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
import kotlin.math.log
|
||||
|
||||
class InstanceSettings : PreferenceFragmentCompat() {
|
||||
val TAG = "InstanceSettings"
|
||||
@ -60,8 +62,8 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
val jsonObject = JSONTokener(json).nextValue() as JSONObject
|
||||
Log.e(TAG, jsonObject.getJSONArray("subscriptions").toString())
|
||||
for (
|
||||
i in 0 until jsonObject.getJSONArray("subscriptions")
|
||||
.length()
|
||||
i in 0 until jsonObject.getJSONArray("subscriptions")
|
||||
.length()
|
||||
) {
|
||||
var url =
|
||||
jsonObject.getJSONArray("subscriptions").getJSONObject(i)
|
||||
@ -116,16 +118,42 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val instance = findPreference<ListPreference>("selectInstance")
|
||||
// fetchInstance()
|
||||
initCustomInstances()
|
||||
instance?.setOnPreferenceChangeListener { _, newValue ->
|
||||
initCustomInstances(instance!!)
|
||||
instance.setOnPreferenceChangeListener { _, newValue ->
|
||||
requireMainActivityRestart = true
|
||||
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
|
||||
RetrofitInstance.authUrl = newValue.toString()
|
||||
RetrofitInstance.lazyMgr.reset()
|
||||
logout()
|
||||
true
|
||||
}
|
||||
|
||||
val authInstanceToggle = findPreference<SwitchPreference>("auth_instance_toggle")
|
||||
authInstanceToggle?.setOnPreferenceChangeListener { _, newValue ->
|
||||
requireMainActivityRestart = true
|
||||
authInstance.isVisible = newValue == true
|
||||
logout()
|
||||
RetrofitInstance.authUrl = if (newValue == false) RetrofitInstance.url
|
||||
else authInstance.value
|
||||
true
|
||||
}
|
||||
|
||||
val customInstance = findPreference<Preference>("customInstance")
|
||||
customInstance?.setOnPreferenceClickListener {
|
||||
val newFragment = CustomInstanceDialog()
|
||||
@ -169,58 +197,12 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val importFromYt = findPreference<Preference>("import_from_yt")
|
||||
importFromYt?.setOnPreferenceClickListener {
|
||||
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()
|
||||
}
|
||||
}
|
||||
importSubscriptions()
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun initCustomInstances() {
|
||||
private fun initCustomInstances(instancePref: ListPreference) {
|
||||
val customInstances = PreferenceHelper.getCustomInstances(requireContext())
|
||||
|
||||
var instanceNames = resources.getStringArray(R.array.instances)
|
||||
@ -231,10 +213,9 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
}
|
||||
|
||||
// add custom instances to the list preference
|
||||
val instance = findPreference<ListPreference>("selectInstance")
|
||||
instance?.entries = instanceNames
|
||||
instance?.entryValues = instanceValues
|
||||
instance?.summaryProvider =
|
||||
instancePref.entries = instanceNames
|
||||
instancePref.entryValues = instanceValues
|
||||
instancePref.summaryProvider =
|
||||
Preference.SummaryProvider<ListPreference> { preference ->
|
||||
val text = preference.entry
|
||||
if (TextUtils.isEmpty(text)) {
|
||||
@ -247,6 +228,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
|
||||
private fun logout() {
|
||||
PreferenceHelper.setToken(requireContext(), "")
|
||||
Toast.makeText(context, getString(R.string.loggedout), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
private fun fetchInstance() {
|
||||
@ -298,6 +280,56 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
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>) {
|
||||
fun run() {
|
||||
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_positions">Remember position</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>
|
@ -23,6 +23,21 @@
|
||||
app:key="clearCustomInstances"
|
||||
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 app:title="@string/account">
|
||||
|
Loading…
x
Reference in New Issue
Block a user