Sync fixes

This commit is contained in:
Koitharu
2022-07-19 14:31:25 +03:00
parent 57c1d070d1
commit 59243be030
5 changed files with 40 additions and 21 deletions

View File

@@ -5,8 +5,10 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.exceptions.SyncApiException
import org.koitharu.kotatsu.parsers.util.await
import org.koitharu.kotatsu.parsers.util.parseJson
import org.koitharu.kotatsu.parsers.util.removeSurrounding
import org.koitharu.kotatsu.utils.ext.toRequestBody
class SyncAuthApi(
@@ -24,7 +26,13 @@ class SyncAuthApi(
.url("$baseUrl/auth")
.post(body)
.build()
val response = okHttpClient.newCall(request).await().parseJson()
return response.getString("token")
val response = okHttpClient.newCall(request).await()
if (response.isSuccessful) {
return response.parseJson().getString("token")
} else {
val code = response.code
val message = response.use { checkNotNull(it.body).string() }.removeSurrounding('"')
throw SyncApiException(message, code)
}
}
}
}

View File

@@ -0,0 +1,18 @@
package org.koitharu.kotatsu.sync.ui
import android.accounts.Account
import android.content.Intent
import android.os.Bundle
private const val ACCOUNT_KEY = "account"
private const val ACTION_ACCOUNT_SYNC_SETTINGS = "android.settings.ACCOUNT_SYNC_SETTINGS"
private const val EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args"
@Suppress("FunctionName")
fun SyncSettingsIntent(account: Account): Intent {
val args = Bundle(1)
args.putParcelable(ACCOUNT_KEY, account)
val intent = Intent(ACTION_ACCOUNT_SYNC_SETTINGS)
intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args)
return intent
}