refactor: Provide properties for more shared preferences (#992)

Continues the work of providing properties with accessors for specific
shared preferences.
This commit is contained in:
Nik Clayton 2024-10-10 13:12:56 +02:00 committed by GitHub
parent 367747838d
commit e307796251
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 54 additions and 43 deletions

View File

@ -109,7 +109,7 @@ import app.pachli.core.navigation.pachliAccountId
import app.pachli.core.network.model.Account
import app.pachli.core.network.model.Notification
import app.pachli.core.preferences.MainNavigationPosition
import app.pachli.core.preferences.PrefKeys
import app.pachli.core.preferences.PrefKeys.FONT_FAMILY
import app.pachli.core.ui.extensions.reduceSwipeSensitivity
import app.pachli.core.ui.makeIcon
import app.pachli.databinding.ActivityMainBinding
@ -320,7 +320,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
// Determine which of the three toolbars should be the supportActionBar (which hosts
// the options menu).
val hideTopToolbar = sharedPreferencesRepository.getBoolean(PrefKeys.HIDE_TOP_TOOLBAR, false)
val hideTopToolbar = sharedPreferencesRepository.hideTopToolbar
if (hideTopToolbar) {
when (sharedPreferencesRepository.mainNavigationPosition) {
MainNavigationPosition.TOP -> setSupportActionBar(binding.topNav)
@ -591,9 +591,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
header.currentProfileName.setTextColor(headerTextColor)
header.currentProfileEmail.setTextColor(headerTextColor)
val animateAvatars = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
DrawerImageLoader.init(MainDrawerImageLoader(glide, animateAvatars))
DrawerImageLoader.init(MainDrawerImageLoader(glide, sharedPreferencesRepository.animateAvatars))
binding.mainDrawer.apply {
refreshMainDrawerItems(activeAccountId, addSearchButton)
@ -860,7 +858,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
updateMainDrawerTypeface(
EmbeddedFontFamily.from(sharedPreferencesRepository.getString(PrefKeys.FONT_FAMILY, "default")),
EmbeddedFontFamily.from(sharedPreferencesRepository.getString(FONT_FAMILY, "default")),
)
}
@ -978,8 +976,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
val pageMargin = resources.getDimensionPixelSize(DR.dimen.tab_page_margin)
binding.viewPager.setPageTransformer(MarginPageTransformer(pageMargin))
val enableSwipeForTabs = sharedPreferencesRepository.getBoolean(PrefKeys.ENABLE_SWIPE_FOR_TABS, true)
binding.viewPager.isUserInputEnabled = enableSwipeForTabs
binding.viewPager.isUserInputEnabled = sharedPreferencesRepository.enableTabSwipe
onTabSelectedListener?.let {
activeTabLayout.removeOnTabSelectedListener(it)
@ -1122,8 +1119,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
@SuppressLint("CheckResult")
private fun loadDrawerAvatar(avatarUrl: String, showPlaceholder: Boolean) {
val hideTopToolbar = sharedPreferencesRepository.getBoolean(PrefKeys.HIDE_TOP_TOOLBAR, false)
val animateAvatars = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val hideTopToolbar = sharedPreferencesRepository.hideTopToolbar
val animateAvatars = sharedPreferencesRepository.animateAvatars
val activeToolbar = if (hideTopToolbar) {
when (sharedPreferencesRepository.mainNavigationPosition) {
@ -1227,7 +1224,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
private fun updateProfiles() {
val animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val animateEmojis = sharedPreferencesRepository.animateEmojis
val profiles: MutableList<IProfile> =
accountManager.getAllAccountsOrderedByActive().map { acc ->
ProfileDrawerItem().apply {

View File

@ -151,8 +151,8 @@ class AccountActivity :
private var subscribing: Boolean = false
private var loadedAccount: Account? = null
private var animateAvatar: Boolean = false
private var animateEmojis: Boolean = false
private var animateAvatar: Boolean = sharedPreferencesRepository.animateAvatars
private var animateEmojis: Boolean = sharedPreferencesRepository.animateEmojis
// fields for scroll animation
private var hideFab: Boolean = false
@ -199,8 +199,6 @@ class AccountActivity :
// Obtain information to fill out the profile.
viewModel.setAccountInfo(AccountActivityIntent.getAccountId(intent))
animateAvatar = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
hideFab = sharedPreferencesRepository.getBoolean(PrefKeys.FAB_HIDE, false)
handleWindowInsets()
@ -297,8 +295,7 @@ class AccountActivity :
val pageMargin = resources.getDimensionPixelSize(DR.dimen.tab_page_margin)
binding.accountFragmentViewPager.setPageTransformer(MarginPageTransformer(pageMargin))
val enableSwipeForTabs = sharedPreferencesRepository.getBoolean(PrefKeys.ENABLE_SWIPE_FOR_TABS, true)
binding.accountFragmentViewPager.isUserInputEnabled = enableSwipeForTabs
binding.accountFragmentViewPager.isUserInputEnabled = sharedPreferencesRepository.enableTabSwipe
binding.accountTabLayout.addOnTabSelectedListener(
object : TabLayout.OnTabSelectedListener {

View File

@ -118,8 +118,8 @@ class AccountListFragment :
binding.swipeRefreshLayout.setOnRefreshListener { fetchAccounts() }
binding.swipeRefreshLayout.setColorSchemeColors(MaterialColors.getColor(binding.root, androidx.appcompat.R.attr.colorPrimary))
val animateAvatar = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val animateAvatar = sharedPreferencesRepository.animateAvatars
val animateEmojis = sharedPreferencesRepository.animateEmojis
val showBotOverlay = sharedPreferencesRepository.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, true)
val activeAccount = accountManager.activeAccount!!

View File

@ -99,7 +99,7 @@ class AnnouncementsActivity :
)
val wellbeingEnabled = sharedPreferencesRepository.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_POSTS, false)
val animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val animateEmojis = sharedPreferencesRepository.animateEmojis
val useAbsoluteTime = sharedPreferencesRepository.getBoolean(PrefKeys.ABSOLUTE_TIME_VIEW, false)
adapter = AnnouncementAdapter(emptyList(), this, wellbeingEnabled, animateEmojis, useAbsoluteTime)

View File

@ -415,8 +415,8 @@ class ComposeActivity :
binding.composeEditField.setAdapter(
ComposeAutoCompleteAdapter(
this,
preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false),
preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false),
sharedPreferencesRepository.animateAvatars,
sharedPreferencesRepository.animateEmojis,
preferences.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, true),
),
)
@ -629,12 +629,11 @@ class ComposeActivity :
a.getDimensionPixelSize(0, 1)
}
val animateAvatars = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
loadAvatar(
activeAccount.profilePictureUrl,
binding.composeAvatar,
avatarSize / 8,
animateAvatars,
sharedPreferencesRepository.animateAvatars,
)
binding.composeAvatar.contentDescription = getString(
R.string.compose_active_account_description,
@ -1368,7 +1367,7 @@ class ComposeActivity :
private fun setEmojiList(emojiList: List<Emoji>?) {
if (emojiList != null) {
val animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val animateEmojis = sharedPreferencesRepository.animateEmojis
binding.emojiView.adapter = EmojiAdapter(emojiList, this@ComposeActivity, animateEmojis)
enableButton(binding.composeEmojiButton, true, emojiList.isNotEmpty())
}

View File

@ -90,7 +90,6 @@ import app.pachli.core.model.ServerOperation.ORG_JOINMASTODON_SEARCH_QUERY_IS_SE
import app.pachli.core.model.ServerOperation.ORG_JOINMASTODON_SEARCH_QUERY_LANGUAGE
import app.pachli.core.navigation.pachliAccountId
import app.pachli.core.network.Server
import app.pachli.core.preferences.PrefKeys
import app.pachli.core.ui.extensions.await
import app.pachli.core.ui.extensions.awaitSingleChoiceItem
import app.pachli.core.ui.extensions.reduceSwipeSensitivity
@ -154,8 +153,7 @@ class SearchActivity :
binding.pages.reduceSwipeSensitivity()
binding.pages.adapter = SearchPagerAdapter(this, intent.pachliAccountId)
val enableSwipeForTabs = sharedPreferencesRepository.getBoolean(PrefKeys.ENABLE_SWIPE_FOR_TABS, true)
binding.pages.isUserInputEnabled = enableSwipeForTabs
binding.pages.isUserInputEnabled = sharedPreferencesRepository.enableTabSwipe
TabLayoutMediator(binding.tabs, binding.pages) { tab, position ->
tab.text = getPageTitle(position)

View File

@ -44,8 +44,8 @@ class SearchAccountsFragment : SearchFragment<TimelineAccount>() {
override fun createAdapter(): PagingDataAdapter<TimelineAccount, *> {
return SearchAccountsAdapter(
this,
sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false),
sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false),
sharedPreferencesRepository.animateAvatars,
sharedPreferencesRepository.animateEmojis,
sharedPreferencesRepository.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, true),
)
}

View File

@ -97,8 +97,8 @@ class ViewEditsFragment :
statusId = requireArguments().getString(ARG_STATUS_ID)!!
val animateAvatars = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val animateAvatars = sharedPreferencesRepository.animateAvatars
val animateEmojis = sharedPreferencesRepository.animateEmojis
val useBlurhash = sharedPreferencesRepository.getBoolean(PrefKeys.USE_BLURHASH, true)
val avatarRadius: Int = requireContext().resources.getDimensionPixelSize(DR.dimen.avatar_radius_48dp)

View File

@ -26,9 +26,12 @@ import androidx.preference.PreferenceManager
import app.pachli.core.activity.databinding.ItemAutocompleteAccountBinding
import app.pachli.core.database.model.AccountEntity
import app.pachli.core.designsystem.R as DR
import app.pachli.core.preferences.PrefKeys
class AccountSelectionAdapter(context: Context) : ArrayAdapter<AccountEntity>(context, R.layout.item_autocomplete_account) {
class AccountSelectionAdapter(
context: Context,
private val animateAvatars: Boolean,
private val animateEmojis: Boolean,
) : ArrayAdapter<AccountEntity>(context, R.layout.item_autocomplete_account) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = if (convertView == null) {
@ -40,16 +43,14 @@ class AccountSelectionAdapter(context: Context) : ArrayAdapter<AccountEntity>(co
val account = getItem(position)
if (account != null) {
val pm = PreferenceManager.getDefaultSharedPreferences(binding.avatar.context)
val animateEmojis = pm.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
binding.username.text = account.fullName
binding.displayName.text = account.displayName.emojify(account.emojis, binding.displayName, animateEmojis)
binding.avatarBadge.visibility = View.GONE // We never want to display the bot badge here
val avatarRadius = context.resources.getDimensionPixelSize(DR.dimen.avatar_radius_42dp)
val animateAvatar = pm.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
loadAvatar(account.profilePictureUrl, binding.avatar, avatarRadius, animateAvatar)
loadAvatar(account.profilePictureUrl, binding.avatar, avatarRadius, animateAvatars)
}
return binding.root

View File

@ -241,7 +241,11 @@ abstract class BaseActivity : AppCompatActivity(), MenuProvider {
if (!showActiveAccount) {
accounts.remove(activeAccount)
}
val adapter = AccountSelectionAdapter(this)
val adapter = AccountSelectionAdapter(
this,
sharedPreferencesRepository.animateAvatars,
sharedPreferencesRepository.animateEmojis,
)
adapter.addAll(accounts)
AlertDialog.Builder(this)
.setTitle(dialogTitle)

View File

@ -175,8 +175,8 @@ class StatusDisplayOptionsRepository @Inject constructor(
@VisibleForTesting(otherwise = PRIVATE)
fun initialStatusDisplayOptions(account: AccountEntity? = null): StatusDisplayOptions {
return StatusDisplayOptions(
animateAvatars = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, default.animateAvatars),
animateEmojis = sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, default.animateEmojis),
animateAvatars = sharedPreferencesRepository.animateAvatars,
animateEmojis = sharedPreferencesRepository.animateEmojis,
mediaPreviewEnabled = account?.mediaPreviewEnabled ?: default.mediaPreviewEnabled,
useAbsoluteTime = sharedPreferencesRepository.getBoolean(PrefKeys.ABSOLUTE_TIME_VIEW, default.useAbsoluteTime),
showBotOverlay = sharedPreferencesRepository.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, default.showBotOverlay),

View File

@ -49,10 +49,26 @@ class SharedPreferencesRepository @Inject constructor(
val appTheme: AppTheme
get() = getEnum(PrefKeys.APP_THEME, AppTheme.AUTO_SYSTEM)
/** True if avatars should be animated. */
val animateAvatars: Boolean
get() = getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
/** True if emojis should be animated. */
val animateEmojis: Boolean
get() = getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
/** Location of downloaded files. */
val downloadLocation: DownloadLocation
get() = getEnum(PrefKeys.DOWNLOAD_LOCATION, DownloadLocation.DOWNLOADS)
/** True if swipe-gesture between tabs should be enabled. */
val enableTabSwipe: Boolean
get() = getBoolean(PrefKeys.ENABLE_SWIPE_FOR_TABS, true)
/** Whether to hide the top toolbar. */
val hideTopToolbar: Boolean
get() = getBoolean(PrefKeys.HIDE_TOP_TOOLBAR, false)
/** Screen location of primary navigation UI (tabs, etc). */
val mainNavigationPosition: MainNavigationPosition
get() = getEnum(PrefKeys.MAIN_NAV_POSITION, MainNavigationPosition.TOP)

View File

@ -44,7 +44,6 @@ import app.pachli.core.data.repository.ListsError
import app.pachli.core.designsystem.R as DR
import app.pachli.core.network.model.TimelineAccount
import app.pachli.core.network.retrofit.apiresult.ApiError
import app.pachli.core.preferences.PrefKeys
import app.pachli.core.preferences.SharedPreferencesRepository
import app.pachli.core.ui.BindingHolder
import app.pachli.feature.lists.databinding.FragmentAccountsInListBinding
@ -89,8 +88,8 @@ class AccountsInListFragment : DialogFragment() {
@Inject
lateinit var sharedPreferencesRepository: SharedPreferencesRepository
private val animateAvatar by unsafeLazy { sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false) }
private val animateEmojis by unsafeLazy { sharedPreferencesRepository.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false) }
private val animateAvatar by unsafeLazy { sharedPreferencesRepository.animateAvatars }
private val animateEmojis by unsafeLazy { sharedPreferencesRepository.animateEmojis }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)