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

@@ -0,0 +1,6 @@
package org.koitharu.kotatsu.core.exceptions
class SyncApiException(
message: String,
val code: Int,
) : RuntimeException(message)

View File

@@ -1,12 +1,9 @@
package org.koitharu.kotatsu.settings
import android.accounts.Account
import android.accounts.AccountManager
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.preference.PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS
import android.view.View
import androidx.preference.ListPreference
import androidx.preference.Preference
@@ -23,6 +20,7 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.local.data.LocalStorageManager
import org.koitharu.kotatsu.parsers.util.names
import org.koitharu.kotatsu.settings.utils.SliderPreference
import org.koitharu.kotatsu.sync.ui.SyncSettingsIntent
import org.koitharu.kotatsu.utils.ext.getStorageName
import org.koitharu.kotatsu.utils.ext.setDefaultValueCompat
import org.koitharu.kotatsu.utils.ext.viewLifecycleScope
@@ -111,7 +109,7 @@ class ContentSettingsFragment :
am.addAccount(accountType, accountType, null, null, requireActivity(), null, null)
} else {
try {
startActivity(getSyncSettingsIntent(account))
startActivity(SyncSettingsIntent(account))
} catch (_: ActivityNotFoundException) {
Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show()
}
@@ -151,16 +149,4 @@ class ContentSettingsFragment :
}
}
}
/**
* Some magic
*/
private fun getSyncSettingsIntent(account: Account): Intent {
val args = Bundle(1)
args.putParcelable("account", account)
val intent = Intent("android.settings.ACCOUNT_SYNC_SETTINGS")
@Suppress("DEPRECATION")
intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args)
return intent
}
}
}

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
}

View File

@@ -19,6 +19,7 @@ fun Throwable.getDisplayMessage(resources: Resources): String = when (this) {
is UnsupportedFileException -> resources.getString(R.string.text_file_not_supported)
is FileNotFoundException -> resources.getString(R.string.file_not_found)
is EmptyHistoryException -> resources.getString(R.string.history_is_empty)
is SyncApiException,
is ContentUnavailableException -> message
is ParseException -> shortMessage
is SocketTimeoutException -> resources.getString(R.string.network_error)
@@ -36,4 +37,4 @@ fun Throwable.isReportable(): Boolean {
fun Throwable.report(message: String?) {
CaughtException(this, message).sendWithAcra()
}
}