mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Simplify fetch and auth instance logic
This commit is contained in:
parent
eb1382e154
commit
1e13586448
@ -33,7 +33,6 @@ class LibreTubeApp : Application() {
|
|||||||
/**
|
/**
|
||||||
* Set the api and the auth api url
|
* Set the api and the auth api url
|
||||||
*/
|
*/
|
||||||
RetrofitInstance.initialize()
|
|
||||||
ImageHelper.initializeImageLoader(this)
|
ImageHelper.initializeImageLoader(this)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,8 +9,19 @@ import retrofit2.Retrofit
|
|||||||
import retrofit2.create
|
import retrofit2.create
|
||||||
|
|
||||||
object RetrofitInstance {
|
object RetrofitInstance {
|
||||||
lateinit var url: String
|
private val url get() = PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
|
||||||
lateinit var authUrl: String
|
private val authUrl
|
||||||
|
get() = when (PreferenceHelper.getBoolean(
|
||||||
|
PreferenceKeys.AUTH_INSTANCE_TOGGLE,
|
||||||
|
false
|
||||||
|
)) {
|
||||||
|
true -> PreferenceHelper.getString(
|
||||||
|
PreferenceKeys.AUTH_INSTANCE,
|
||||||
|
PIPED_API_URL
|
||||||
|
)
|
||||||
|
false -> url
|
||||||
|
}
|
||||||
|
|
||||||
val lazyMgr = resettableManager()
|
val lazyMgr = resettableManager()
|
||||||
private val kotlinxConverterFactory = JsonHelper.json
|
private val kotlinxConverterFactory = JsonHelper.json
|
||||||
.asConverterFactory("application/json".toMediaType())
|
.asConverterFactory("application/json".toMediaType())
|
||||||
@ -41,27 +52,4 @@ object RetrofitInstance {
|
|||||||
.build()
|
.build()
|
||||||
.create<ExternalApi>()
|
.create<ExternalApi>()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the api urls needed for the [RetrofitInstance]
|
|
||||||
*/
|
|
||||||
fun initialize() {
|
|
||||||
url =
|
|
||||||
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
|
|
||||||
// set auth instance
|
|
||||||
authUrl =
|
|
||||||
if (
|
|
||||||
PreferenceHelper.getBoolean(
|
|
||||||
PreferenceKeys.AUTH_INSTANCE_TOGGLE,
|
|
||||||
false
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
PreferenceHelper.getString(
|
|
||||||
PreferenceKeys.AUTH_INSTANCE,
|
|
||||||
PIPED_API_URL
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,6 @@ class WelcomeActivity : BaseActivity() {
|
|||||||
|
|
||||||
private fun startMainActivity() {
|
private fun startMainActivity() {
|
||||||
// refresh the api urls since they have changed likely
|
// refresh the api urls since they have changed likely
|
||||||
RetrofitInstance.initialize()
|
|
||||||
RetrofitInstance.lazyMgr.reset()
|
RetrofitInstance.lazyMgr.reset()
|
||||||
val mainActivityIntent = Intent(this@WelcomeActivity, MainActivity::class.java)
|
val mainActivityIntent = Intent(this@WelcomeActivity, MainActivity::class.java)
|
||||||
startActivity(mainActivityIntent)
|
startActivity(mainActivityIntent)
|
||||||
|
@ -65,7 +65,6 @@ class LoginDialog(
|
|||||||
|
|
||||||
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
|
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
|
||||||
val login = Login(username, password)
|
val login = Login(username, password)
|
||||||
Log.e("url", RetrofitInstance.authUrl)
|
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val response = try {
|
val response = try {
|
||||||
if (createNewAccount) {
|
if (createNewAccount) {
|
||||||
|
@ -55,10 +55,8 @@ class InstanceSettings : BasePreferenceFragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
instancePref.setOnPreferenceChangeListener { _, newValue ->
|
instancePref.setOnPreferenceChangeListener { _, _ ->
|
||||||
RetrofitInstance.url = newValue.toString()
|
|
||||||
if (!authInstanceToggle.isChecked) {
|
if (!authInstanceToggle.isChecked) {
|
||||||
RetrofitInstance.authUrl = newValue.toString()
|
|
||||||
logoutAndUpdateUI()
|
logoutAndUpdateUI()
|
||||||
}
|
}
|
||||||
RetrofitInstance.lazyMgr.reset()
|
RetrofitInstance.lazyMgr.reset()
|
||||||
@ -66,21 +64,13 @@ class InstanceSettings : BasePreferenceFragment() {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
authInstance.setOnPreferenceChangeListener { _, newValue ->
|
authInstance.setOnPreferenceChangeListener { _, _ ->
|
||||||
// save new auth url
|
|
||||||
RetrofitInstance.authUrl = newValue.toString()
|
|
||||||
RetrofitInstance.lazyMgr.reset()
|
RetrofitInstance.lazyMgr.reset()
|
||||||
logoutAndUpdateUI()
|
logoutAndUpdateUI()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
authInstanceToggle.setOnPreferenceChangeListener { _, newValue ->
|
authInstanceToggle.setOnPreferenceChangeListener { _, _ ->
|
||||||
// either use new auth url or the normal api url if auth instance disabled
|
|
||||||
RetrofitInstance.authUrl = if (newValue == false) {
|
|
||||||
RetrofitInstance.url
|
|
||||||
} else {
|
|
||||||
authInstance.value
|
|
||||||
}
|
|
||||||
RetrofitInstance.lazyMgr.reset()
|
RetrofitInstance.lazyMgr.reset()
|
||||||
logoutAndUpdateUI()
|
logoutAndUpdateUI()
|
||||||
true
|
true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user