LibreTube/app/src/main/java/com/github/libretube/Settings.kt

113 lines
4.3 KiB
Kotlin
Raw Normal View History

2022-02-08 14:58:50 +05:30
package com.github.libretube
2022-02-13 11:12:40 +05:30
import android.content.Context
2022-02-08 14:58:50 +05:30
import android.os.Bundle
2022-02-10 17:09:34 +05:30
import android.text.TextUtils
2022-02-08 19:57:13 +05:30
import android.util.Log
2022-02-10 17:02:22 +05:30
import android.view.View
2022-02-13 11:12:40 +05:30
import android.widget.Toast
2022-02-10 17:02:22 +05:30
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
2022-02-08 19:57:13 +05:30
import androidx.preference.ListPreference
import androidx.preference.Preference
2022-02-08 14:58:50 +05:30
import androidx.preference.PreferenceFragmentCompat
2022-02-10 17:02:22 +05:30
import androidx.preference.PreferenceManager
import com.github.libretube.adapters.TrendingAdapter
import retrofit2.HttpException
2022-02-09 23:40:39 +05:30
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
2022-02-10 17:02:22 +05:30
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.IOException
2022-02-08 14:58:50 +05:30
class Settings : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
2022-02-08 19:57:13 +05:30
val instance = findPreference<ListPreference>("instance")
2022-02-10 17:02:22 +05:30
fetchInstance()
2022-02-08 19:57:13 +05:30
instance?.setOnPreferenceChangeListener { preference, newValue ->
2022-02-10 17:02:22 +05:30
RetrofitInstance.url = newValue.toString()
2022-02-09 23:40:39 +05:30
RetrofitInstance.lazyMgr.reset()
2022-02-13 11:12:40 +05:30
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
if(sharedPref?.getString("token","")!="") {
with(sharedPref!!.edit()) {
putString("token", "")
apply()
}
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
}
2022-02-08 19:57:13 +05:30
true
}
2022-02-10 17:09:34 +05:30
val login = findPreference<Preference>("login_register")
login?.setOnPreferenceClickListener {
val newFragment = LoginDialog()
2022-02-16 20:47:27 +05:30
newFragment.show(childFragmentManager, "Login")
2022-02-10 17:09:34 +05:30
true
}
}
2022-02-10 17:02:22 +05:30
private fun fetchInstance() {
val api: PipedApi by lazy{
Retrofit.Builder()
.baseUrl("https://raw.githubusercontent.com/wiki/TeamPiped/Piped-Frontend/")
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(PipedApi::class.java)
}
lifecycleScope.launchWhenCreated {
val response = try {
api.getInstances()
} catch (e: IOException) {
println(e)
Log.e("settings", "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e("settings", "HttpException, unexpected response ${e.toString()}")
return@launchWhenCreated
} catch (e: Exception){
Log.e("settings",e.toString())
return@launchWhenCreated
}
//println("dafaq $response")
val listEntries: MutableList<String> = ArrayList()
val listEntryValues: MutableList<String> = ArrayList()
var skipped = 0
val lines = response.split("\n")
for(line in lines) {
val split = line.split("|")
if (split.size == 5) {
if (skipped < 2) {
skipped++
}else{
println("dafaq $line")
listEntries.add(split[0])
listEntryValues.add(split[1])
}
}
}
val entries = listEntries.toTypedArray<CharSequence>()
val entryValues = listEntryValues.toTypedArray<CharSequence>()
runOnUiThread {
val instance = findPreference<ListPreference>("instance")
instance?.entries = entries
instance?.entryValues = entryValues
2022-02-10 17:09:34 +05:30
instance?.summaryProvider = Preference.SummaryProvider<ListPreference> { preference ->
val text = preference.entry
if (TextUtils.isEmpty(text)) {
"Not set"
} else {
text
}
}
2022-02-10 17:02:22 +05:30
}
}
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
2022-02-08 14:58:50 +05:30
}