Implement syncing of nsfw show preference

This commit is contained in:
Matthieu 2024-08-31 00:59:35 +02:00
parent a103954beb
commit d8b5af2a61
5 changed files with 88 additions and 3 deletions

View File

@ -32,6 +32,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.ExperimentalPagingApi
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
@ -554,6 +555,12 @@ class MainActivity : BaseActivity() {
val account = api.verifyCredentials()
updateUserInfoDb(db, account)
val show = api.getSettings().common.media.always_show_cw
launch {
PreferenceManager.getDefaultSharedPreferences(applicationContext)
.edit().putBoolean("always_show_nsfw", show).commit()
}
//No need to update drawer account info here, the ViewModel listens to db updates
} catch (exception: Exception) {
Log.e("ACCOUNT UPDATE:", exception.toString())

View File

@ -4,11 +4,13 @@ import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.CheckBoxPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
@ -22,12 +24,14 @@ import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.SettingsBinding
import org.pixeldroid.app.main.MainActivity
import org.pixeldroid.app.settings.TutorialSettingsDialog.Companion.START_TUTORIAL
import org.pixeldroid.app.utils.BaseActivity
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.api.objects.CommonWrapper
import org.pixeldroid.app.utils.setThemeFromPreferences
import org.pixeldroid.common.ThemedActivity
@AndroidEntryPoint
class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
class SettingsActivity : BaseActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
private var restartMainOnExit = false
private lateinit var binding: SettingsBinding
@ -105,6 +109,27 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
"themeColor" -> {
recreateWithRestartStatus()
}
"always_show_nsfw" -> {
lifecycleScope.launch {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setToCurrentUser()
try {
// Get old settings and modify just the nsfw one
val settings = api.getSettings().let { settings ->
settings.common.copy(
media = settings.common.media.copy(
always_show_cw = sharedPreferences.getBoolean(key, settings.common.media.always_show_cw)
)
)
}
api.setSettings(CommonWrapper(settings))
} catch (e: Exception) {
Log.e("Pixelfed API settings", e.toString())
}
}
}
else -> {}
}
}
}
@ -191,6 +216,16 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
locale?.getDisplayName(locale) ?: getString(R.string.default_system)
}
}
findPreference<CheckBoxPreference>("always_show_nsfw")?.let {
lifecycleScope.launch {
val api: PixelfedAPI = (requireActivity() as SettingsActivity).apiHolder.api ?: (requireActivity() as SettingsActivity).apiHolder.setToCurrentUser()
try {
val show = api.getSettings().common.media.always_show_cw
it.isChecked = show
} catch (_: Exception){}
}
}
//Hide Notification setting for Android versions where it doesn't work
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {

View File

@ -469,4 +469,11 @@ interface PixelfedAPI {
@Field("forward") forward: Boolean = true
) : Report
@POST("/api/pixelfed/v1/app/settings")
suspend fun setSettings(
@Body account_id: CommonWrapper,
) : PixelfedAppSettings
@GET("/api/pixelfed/v1/app/settings")
suspend fun getSettings() : PixelfedAppSettings
}

View File

@ -0,0 +1,34 @@
package org.pixeldroid.app.utils.api.objects
data class PixelfedAppSettings(
val id: String,
val username: String,
val updated_at: String,
val common: Common
)
data class CommonWrapper(
val common: Common
)
data class Common(
val timelines: Timelines,
val media: Media,
val appearance: Appearance
)
data class Timelines(
val show_public: Boolean,
val show_network: Boolean,
val hide_likes_shares: Boolean
)
data class Media(
val hide_public_behind_cw: Boolean,
val always_show_cw: Boolean,
val show_alt_text: Boolean
)
data class Appearance(
val links_use_in_app_browser: Boolean,
val theme: String
)

View File

@ -33,7 +33,9 @@
android:summary="@string/arrange_tabs_description"
android:icon="@drawable/outline_bottom_navigation" />
<CheckBoxPreference app:key="always_show_nsfw" app:title="@string/always_show_nsfw"
<CheckBoxPreference
app:key="always_show_nsfw"
app:title="@string/always_show_nsfw"
app:icon="@drawable/eye_black_24dp" android:defaultValue="false"
android:summary="@string/summary_always_show_nsfw"/>