diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt index b9f77a1db..bcd10db1e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt @@ -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 { - override fun onResponse(call: Call, response: Response) { - 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, t: Throwable) { + }, { t -> Log.e("AccountPreferences", "failed updating settings on server", t) showErrorSnackbar(visibility, sensitive) - } - }) + }) + } } private fun showErrorSnackbar(visibility: String?, sensitive: Boolean?) { diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt index 5498f5382..291f548dd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt @@ -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 + ): NetworkResult @Multipart @PATCH("api/v1/accounts/update_credentials")