Display error messages from Funkwhale's API on login error.
This commit is contained in:
parent
9ea5446f58
commit
40117122c7
|
@ -8,15 +8,17 @@ import com.github.apognu.otter.R
|
||||||
import com.github.apognu.otter.fragments.LoginDialog
|
import com.github.apognu.otter.fragments.LoginDialog
|
||||||
import com.github.apognu.otter.utils.AppContext
|
import com.github.apognu.otter.utils.AppContext
|
||||||
import com.github.kittinunf.fuel.Fuel
|
import com.github.kittinunf.fuel.Fuel
|
||||||
import com.github.kittinunf.fuel.coroutines.awaitObjectResult
|
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
|
||||||
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
||||||
|
import com.github.kittinunf.result.Result
|
||||||
|
import com.google.gson.Gson
|
||||||
import com.preference.PowerPreference
|
import com.preference.PowerPreference
|
||||||
import kotlinx.android.synthetic.main.activity_login.*
|
import kotlinx.android.synthetic.main.activity_login.*
|
||||||
import kotlinx.coroutines.Dispatchers.Main
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
data class FwCredentials(val token: String)
|
data class FwCredentials(val token: String, val non_field_errors: List<String>)
|
||||||
|
|
||||||
class LoginActivity : AppCompatActivity() {
|
class LoginActivity : AppCompatActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
@ -64,28 +66,38 @@ class LoginActivity : AppCompatActivity() {
|
||||||
|
|
||||||
GlobalScope.launch(Main) {
|
GlobalScope.launch(Main) {
|
||||||
try {
|
try {
|
||||||
val result = Fuel.post("$hostname/api/v1/token/", body)
|
val (_, response, result) = Fuel.post("$hostname/api/v1/token/", body)
|
||||||
.awaitObjectResult(gsonDeserializerOf(FwCredentials::class.java))
|
.awaitObjectResponseResult(gsonDeserializerOf(FwCredentials::class.java))
|
||||||
|
|
||||||
result.fold(
|
when (result) {
|
||||||
{ data ->
|
is Result.Success -> {
|
||||||
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).apply {
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).apply {
|
||||||
setString("hostname", hostname)
|
setString("hostname", hostname)
|
||||||
setString("username", username)
|
setString("username", username)
|
||||||
setString("password", password)
|
setString("password", password)
|
||||||
setString("access_token", data.token)
|
setString("access_token", result.get().token)
|
||||||
}
|
}
|
||||||
|
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
||||||
finish()
|
finish()
|
||||||
},
|
}
|
||||||
{ error ->
|
|
||||||
|
is Result.Failure -> {
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
|
|
||||||
hostname_field.error = error.localizedMessage
|
val error = Gson().fromJson(String(response.data), FwCredentials::class.java)
|
||||||
|
|
||||||
|
hostname_field.error = null
|
||||||
|
username_field.error = null
|
||||||
|
|
||||||
|
if (error != null && error.non_field_errors.isNotEmpty()) {
|
||||||
|
username_field.error = error.non_field_errors[0]
|
||||||
|
} else {
|
||||||
|
hostname_field.error = result.error.localizedMessage
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue