funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/activities/LoginActivity.kt

212 lines
6.3 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.activities
2019-08-19 16:50:33 +02:00
import android.content.Intent
import android.content.res.Configuration
2019-08-19 16:50:33 +02:00
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
2019-08-19 16:50:33 +02:00
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.doOnLayout
import androidx.lifecycle.lifecycleScope
import audio.funkwhale.ffa.R
2021-07-16 10:03:52 +02:00
import audio.funkwhale.ffa.databinding.ActivityLoginBinding
import audio.funkwhale.ffa.fragments.LoginDialog
import audio.funkwhale.ffa.utils.AppContext
import audio.funkwhale.ffa.utils.Userinfo
2019-08-19 16:50:33 +02:00
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
2019-08-19 16:50:33 +02:00
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.github.kittinunf.result.Result
import com.google.gson.Gson
2019-08-19 16:50:33 +02:00
import com.preference.PowerPreference
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
data class FwCredentials(val token: String, val non_field_errors: List<String>?)
2019-08-19 16:50:33 +02:00
class LoginActivity : AppCompatActivity() {
2021-07-16 10:03:52 +02:00
private lateinit var binding: ActivityLoginBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2019-08-19 16:50:33 +02:00
2021-07-16 10:03:52 +02:00
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
limitContainerWidth()
}
2019-08-19 16:50:33 +02:00
override fun onResume() {
super.onResume()
2019-08-19 16:50:33 +02:00
2021-07-16 10:03:52 +02:00
binding.anonymous.setOnCheckedChangeListener { _, isChecked ->
val state = when (isChecked) {
true -> View.GONE
false -> View.VISIBLE
}
2021-07-16 10:03:52 +02:00
binding.usernameField.visibility = state
binding.passwordField.visibility = state
}
2021-07-16 10:03:52 +02:00
binding.login?.setOnClickListener {
var hostname = binding.hostname.text.toString().trim()
val username = binding.username.text.toString()
val password = binding.password.text.toString()
2019-08-19 16:50:33 +02:00
try {
if (hostname.isEmpty()) throw Exception(getString(R.string.login_error_hostname))
Uri.parse(hostname).apply {
2021-07-16 10:03:52 +02:00
if (!binding.cleartext.isChecked && scheme == "http") {
throw Exception(getString(R.string.login_error_hostname_https))
}
2019-08-19 16:50:33 +02:00
if (scheme == null) {
2021-07-16 10:03:52 +02:00
hostname = when (binding.cleartext.isChecked) {
true -> "http://$hostname"
false -> "https://$hostname"
}
}
}
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = ""
2021-07-16 10:03:52 +02:00
when (binding.anonymous.isChecked) {
false -> authedLogin(hostname, username, password)
true -> anonymousLogin(hostname)
2019-08-19 16:50:33 +02:00
}
} catch (e: Exception) {
val message =
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname)
else e.message
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = message
2019-08-19 16:50:33 +02:00
}
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
limitContainerWidth()
}
private fun authedLogin(hostname: String, username: String, password: String) {
val body = mapOf(
"username" to username,
"password" to password
).toList()
val dialog = LoginDialog().apply {
show(supportFragmentManager, "LoginDialog")
}
2019-08-19 16:50:33 +02:00
lifecycleScope.launch(Main) {
try {
val (_, response, result) = Fuel.post("$hostname/api/v1/token/", body)
.awaitObjectResponseResult(gsonDeserializerOf(FwCredentials::class.java))
when (result) {
is Result.Success -> {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).apply {
setString("hostname", hostname)
setBoolean("anonymous", false)
setString("username", username)
setString("password", password)
setString("access_token", result.get().token)
}
Userinfo.get()?.let {
dialog.dismiss()
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
return@launch finish()
}
throw Exception(getString(R.string.login_error_userinfo))
}
is Result.Failure -> {
dialog.dismiss()
val error = Gson().fromJson(String(response.data), FwCredentials::class.java)
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = null
binding.usernameField.error = null
if (error != null && error.non_field_errors?.isNotEmpty() == true) {
2021-07-16 10:03:52 +02:00
binding.usernameField.error = error.non_field_errors[0]
} else {
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = result.error.localizedMessage
2019-08-19 16:50:33 +02:00
}
}
}
} catch (e: Exception) {
dialog.dismiss()
2019-08-19 16:50:33 +02:00
val message =
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname)
else e.message
2019-08-19 16:50:33 +02:00
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = message
}
}
}
private fun anonymousLogin(hostname: String) {
val dialog = LoginDialog().apply {
show(supportFragmentManager, "LoginDialog")
}
lifecycleScope.launch(Main) {
try {
val (_, _, result) = Fuel.get("$hostname/api/v1/tracks/")
.awaitObjectResponseResult(gsonDeserializerOf(FwCredentials::class.java))
when (result) {
is Result.Success -> {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).apply {
setString("hostname", hostname)
setBoolean("anonymous", true)
}
dialog.dismiss()
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
finish()
}
is Result.Failure -> {
dialog.dismiss()
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = result.error.localizedMessage
}
}
} catch (e: Exception) {
dialog.dismiss()
val message =
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname)
else e.message
2021-07-16 10:03:52 +02:00
binding.hostnameField.error = message
2019-08-19 16:50:33 +02:00
}
}
}
private fun limitContainerWidth() {
2021-07-16 10:03:52 +02:00
binding.container.doOnLayout {
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && binding.container.width >= 1440) {
binding.container.layoutParams.width = 1440
} else {
2021-07-16 10:03:52 +02:00
binding.container.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
}
2021-07-16 10:03:52 +02:00
binding.container.requestLayout()
}
}
2021-07-02 13:55:49 +02:00
}