Simplify failedregistration calls

This commit is contained in:
Matthieu 2020-03-07 18:31:58 +01:00
parent b79ebca03a
commit 34f19810bc
1 changed files with 9 additions and 15 deletions

View File

@ -59,8 +59,7 @@ class LoginActivity : AppCompatActivity() {
try{ try{
HttpUrl.Builder().host(normalizedDomain).scheme("https").build() HttpUrl.Builder().host(normalizedDomain).scheme("https").build()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
failedRegistration(getString(R.string.invalid_domain)) return failedRegistration(getString(R.string.invalid_domain))
return
} }
preferences.edit() preferences.edit()
@ -80,8 +79,7 @@ class LoginActivity : AppCompatActivity() {
val callback = object : Callback<Application> { val callback = object : Callback<Application> {
override fun onResponse(call: Call<Application>, response: Response<Application>) { override fun onResponse(call: Call<Application>, response: Response<Application>) {
if (!response.isSuccessful) { if (!response.isSuccessful) {
failedRegistration() return failedRegistration()
return
} }
val credentials = response.body() val credentials = response.body()
@ -97,8 +95,7 @@ class LoginActivity : AppCompatActivity() {
} }
override fun onFailure(call: Call<Application>, t: Throwable) { override fun onFailure(call: Call<Application>, t: Throwable) {
failedRegistration() return failedRegistration()
return
} }
} }
PixelfedAPI.create("https://$normalizedDomain").registerApplication( PixelfedAPI.create("https://$normalizedDomain").registerApplication(
@ -123,8 +120,7 @@ class LoginActivity : AppCompatActivity() {
if (browserIntent.resolveActivity(packageManager) != null) { if (browserIntent.resolveActivity(packageManager) != null) {
startActivity(browserIntent) startActivity(browserIntent)
} else { } else {
failedRegistration(getString(R.string.browser_launch_failed)) return failedRegistration(getString(R.string.browser_launch_failed))
return
} }
} }
connect_instance_button.isEnabled = true connect_instance_button.isEnabled = true
@ -138,27 +134,25 @@ class LoginActivity : AppCompatActivity() {
val clientSecret = preferences.getString("clientSecret", "") val clientSecret = preferences.getString("clientSecret", "")
if (code == null || domain.isNullOrEmpty() || clientId.isNullOrEmpty() || clientSecret.isNullOrEmpty()) { if (code == null || domain.isNullOrEmpty() || clientId.isNullOrEmpty() || clientSecret.isNullOrEmpty()) {
failedRegistration(getString(R.string.auth_failed)) return failedRegistration(getString(R.string.auth_failed))
return
} }
//Successful authorization //Successful authorization
val callback = object : Callback<Token> { val callback = object : Callback<Token> {
override fun onResponse(call: Call<Token>, response: Response<Token>) { override fun onResponse(call: Call<Token>, response: Response<Token>) {
if (!response.isSuccessful || response.body() == null) { if (!response.isSuccessful || response.body() == null) {
failedRegistration(getString(R.string.token_error)) return failedRegistration(getString(R.string.token_error))
return
} }
authenticationSuccessful(domain, response.body()!!.access_token) authenticationSuccessful(domain, response.body()!!.access_token)
} }
override fun onFailure(call: Call<Token>, t: Throwable) { override fun onFailure(call: Call<Token>, t: Throwable) {
failedRegistration(getString(R.string.token_error)) return failedRegistration(getString(R.string.token_error))
} }
} }
val pixelfedAPI = PixelfedAPI.create("https://$domain") PixelfedAPI.create("https://$domain")
pixelfedAPI.obtainToken( .obtainToken(
clientId, clientSecret, "$OAUTH_SCHEME://$PACKAGE_ID", SCOPE, code, clientId, clientSecret, "$OAUTH_SCHEME://$PACKAGE_ID", SCOPE, code,
"authorization_code" "authorization_code"
).enqueue(callback) ).enqueue(callback)