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

176 lines
5.2 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.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
2021-07-23 14:10:13 +02:00
import audio.funkwhale.ffa.utils.OAuth
2021-07-30 10:57:49 +02:00
import audio.funkwhale.ffa.utils.OAuthFactory
import audio.funkwhale.ffa.utils.Userinfo
2021-07-23 14:10:13 +02:00
import audio.funkwhale.ffa.utils.log
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
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
2021-07-30 10:57:49 +02:00
private lateinit var oAuth: OAuth
2021-07-23 14:10:13 +02:00
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)
2021-07-30 10:57:49 +02:00
oAuth = OAuthFactory.instance()
2021-07-16 10:03:52 +02:00
setContentView(binding.root)
limitContainerWidth()
}
2019-08-19 16:50:33 +02:00
2021-07-23 14:10:13 +02:00
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
data?.let {
when (requestCode) {
0 -> {
2021-07-30 10:57:49 +02:00
oAuth.exchange(this, data,
2021-07-23 14:10:13 +02:00
{
PowerPreference
.getFileByName(AppContext.PREFS_CREDENTIALS)
.setBoolean("anonymous", false)
lifecycleScope.launch(Main) {
Userinfo.get(this@LoginActivity)?.let {
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
return@launch finish()
}
throw Exception(getString(R.string.login_error_userinfo))
}
},
{ "error".log() }
)
}
}
}
2021-07-23 14:10:13 +02:00
}
2021-07-23 14:10:13 +02:00
override fun onResume() {
super.onResume()
with(binding) {
login.setOnClickListener {
var hostname = hostname.text.toString().trim()
2019-08-19 16:50:33 +02:00
2021-07-23 14:10:13 +02:00
try {
if (hostname.isEmpty()) throw Exception(getString(R.string.login_error_hostname))
2019-08-19 16:50:33 +02:00
2021-07-23 14:10:13 +02:00
Uri.parse(hostname).apply {
if (!cleartext.isChecked && scheme == "http") {
throw Exception(getString(R.string.login_error_hostname_https))
}
2019-08-19 16:50:33 +02:00
2021-07-23 14:10:13 +02:00
if (scheme == null) {
hostname = when (cleartext.isChecked) {
true -> "http://$hostname"
false -> "https://$hostname"
}
}
}
2021-07-23 14:10:13 +02:00
hostnameField.error = ""
2021-07-23 14:10:13 +02:00
when (anonymous.isChecked) {
false -> authedLogin(hostname)
true -> anonymousLogin(hostname)
}
} catch (e: Exception) {
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-23 14:10:13 +02:00
hostnameField.error = message
}
2019-08-19 16:50:33 +02:00
}
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
limitContainerWidth()
}
2021-07-23 14:10:13 +02:00
private fun authedLogin(hostname: String) {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname)
2021-07-30 10:57:49 +02:00
oAuth.init(hostname)
oAuth.register {
oAuth.authorize(this)
}
}
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
}