refactor accountUpdateSource from Call to coroutine (#4386)

It is the last place where we used a Call 🥳
This commit is contained in:
Konrad Pozniak 2024-05-03 13:22:04 +02:00 committed by GitHub
parent f8a25f896b
commit d5a01f671c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 26 deletions

View File

@ -21,7 +21,9 @@ import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.DrawableRes
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceFragmentCompat
import at.connyduck.calladapter.networkresult.fold
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.BaseActivity
@ -58,9 +60,7 @@ import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeRes
import javax.inject.Inject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlinx.coroutines.launch
class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
@Inject
@ -293,29 +293,21 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
) {
// TODO these could also be "datastore backed" preferences (a ServerPreferenceDataStore); follow-up of issue #3204
mastodonApi.accountUpdateSource(visibility, sensitive, language)
.enqueue(object : Callback<Account> {
override fun onResponse(call: Call<Account>, response: Response<Account>) {
val account = response.body()
if (response.isSuccessful && account != null) {
accountManager.activeAccount?.let {
it.defaultPostPrivacy = account.source?.privacy
?: Status.Visibility.PUBLIC
it.defaultMediaSensitivity = account.source?.sensitive ?: false
it.defaultPostLanguage = language.orEmpty()
accountManager.saveAccount(it)
}
} else {
Log.e("AccountPreferences", "failed updating settings on server")
showErrorSnackbar(visibility, sensitive)
viewLifecycleOwner.lifecycleScope.launch {
mastodonApi.accountUpdateSource(visibility, sensitive, language)
.fold({ account: Account ->
accountManager.activeAccount?.let {
it.defaultPostPrivacy = account.source?.privacy
?: Status.Visibility.PUBLIC
it.defaultMediaSensitivity = account.source?.sensitive ?: false
it.defaultPostLanguage = language.orEmpty()
accountManager.saveAccount(it)
}
}
override fun onFailure(call: Call<Account>, t: Throwable) {
}, { t ->
Log.e("AccountPreferences", "failed updating settings on server", t)
showErrorSnackbar(visibility, sensitive)
}
})
})
}
}
private fun showErrorSnackbar(visibility: String?, sensitive: Boolean?) {

View File

@ -49,7 +49,6 @@ import com.keylesspalace.tusky.entity.Translation
import com.keylesspalace.tusky.entity.TrendingTag
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
@ -292,11 +291,11 @@ interface MastodonApi {
@FormUrlEncoded
@PATCH("api/v1/accounts/update_credentials")
fun accountUpdateSource(
suspend fun accountUpdateSource(
@Field("source[privacy]") privacy: String?,
@Field("source[sensitive]") sensitive: Boolean?,
@Field("source[language]") language: String?
): Call<Account>
): NetworkResult<Account>
@Multipart
@PATCH("api/v1/accounts/update_credentials")