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

View File

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