From e30779625104b97abb55397264436dce9e9ac47c Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 10 Oct 2024 13:12:56 +0200 Subject: [PATCH] refactor: Provide properties for more shared preferences (#992) Continues the work of providing properties with accessors for specific shared preferences. --- app/src/main/java/app/pachli/MainActivity.kt | 19 ++++++++----------- .../components/account/AccountActivity.kt | 9 +++------ .../accountlist/AccountListFragment.kt | 4 ++-- .../announcements/AnnouncementsActivity.kt | 2 +- .../components/compose/ComposeActivity.kt | 9 ++++----- .../components/search/SearchActivity.kt | 4 +--- .../fragments/SearchAccountsFragment.kt | 4 ++-- .../viewthread/edits/ViewEditsFragment.kt | 4 ++-- .../core/activity/AccountSelectionAdapter.kt | 11 ++++++----- .../app/pachli/core/activity/BaseActivity.kt | 6 +++++- .../StatusDisplayOptionsRepository.kt | 4 ++-- .../SharedPreferencesRepository.kt | 16 ++++++++++++++++ .../feature/lists/AccountsInListFragment.kt | 5 ++--- 13 files changed, 54 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/app/pachli/MainActivity.kt b/app/src/main/java/app/pachli/MainActivity.kt index 2bcff04b5..88321bcd9 100644 --- a/app/src/main/java/app/pachli/MainActivity.kt +++ b/app/src/main/java/app/pachli/MainActivity.kt @@ -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 = accountManager.getAllAccountsOrderedByActive().map { acc -> ProfileDrawerItem().apply { diff --git a/app/src/main/java/app/pachli/components/account/AccountActivity.kt b/app/src/main/java/app/pachli/components/account/AccountActivity.kt index 75e60d0af..61c7abb9d 100644 --- a/app/src/main/java/app/pachli/components/account/AccountActivity.kt +++ b/app/src/main/java/app/pachli/components/account/AccountActivity.kt @@ -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 { diff --git a/app/src/main/java/app/pachli/components/accountlist/AccountListFragment.kt b/app/src/main/java/app/pachli/components/accountlist/AccountListFragment.kt index 86c81bda7..15be7a590 100644 --- a/app/src/main/java/app/pachli/components/accountlist/AccountListFragment.kt +++ b/app/src/main/java/app/pachli/components/accountlist/AccountListFragment.kt @@ -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!! diff --git a/app/src/main/java/app/pachli/components/announcements/AnnouncementsActivity.kt b/app/src/main/java/app/pachli/components/announcements/AnnouncementsActivity.kt index 6230889de..a7b156c5d 100644 --- a/app/src/main/java/app/pachli/components/announcements/AnnouncementsActivity.kt +++ b/app/src/main/java/app/pachli/components/announcements/AnnouncementsActivity.kt @@ -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) diff --git a/app/src/main/java/app/pachli/components/compose/ComposeActivity.kt b/app/src/main/java/app/pachli/components/compose/ComposeActivity.kt index c6dbae093..4e8499a05 100644 --- a/app/src/main/java/app/pachli/components/compose/ComposeActivity.kt +++ b/app/src/main/java/app/pachli/components/compose/ComposeActivity.kt @@ -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?) { 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()) } diff --git a/app/src/main/java/app/pachli/components/search/SearchActivity.kt b/app/src/main/java/app/pachli/components/search/SearchActivity.kt index 9d834a996..af36e6759 100644 --- a/app/src/main/java/app/pachli/components/search/SearchActivity.kt +++ b/app/src/main/java/app/pachli/components/search/SearchActivity.kt @@ -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) diff --git a/app/src/main/java/app/pachli/components/search/fragments/SearchAccountsFragment.kt b/app/src/main/java/app/pachli/components/search/fragments/SearchAccountsFragment.kt index d2f0820ab..f5f6aa889 100644 --- a/app/src/main/java/app/pachli/components/search/fragments/SearchAccountsFragment.kt +++ b/app/src/main/java/app/pachli/components/search/fragments/SearchAccountsFragment.kt @@ -44,8 +44,8 @@ class SearchAccountsFragment : SearchFragment() { override fun createAdapter(): PagingDataAdapter { 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), ) } diff --git a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt index 95a2173fa..67b5644ac 100644 --- a/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt +++ b/app/src/main/java/app/pachli/components/viewthread/edits/ViewEditsFragment.kt @@ -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) diff --git a/core/activity/src/main/kotlin/app/pachli/core/activity/AccountSelectionAdapter.kt b/core/activity/src/main/kotlin/app/pachli/core/activity/AccountSelectionAdapter.kt index c6fba92da..9afaeec69 100644 --- a/core/activity/src/main/kotlin/app/pachli/core/activity/AccountSelectionAdapter.kt +++ b/core/activity/src/main/kotlin/app/pachli/core/activity/AccountSelectionAdapter.kt @@ -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(context, R.layout.item_autocomplete_account) { +class AccountSelectionAdapter( + context: Context, + private val animateAvatars: Boolean, + private val animateEmojis: Boolean, +) : ArrayAdapter(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(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 diff --git a/core/activity/src/main/kotlin/app/pachli/core/activity/BaseActivity.kt b/core/activity/src/main/kotlin/app/pachli/core/activity/BaseActivity.kt index 1c2102602..e4c20410f 100644 --- a/core/activity/src/main/kotlin/app/pachli/core/activity/BaseActivity.kt +++ b/core/activity/src/main/kotlin/app/pachli/core/activity/BaseActivity.kt @@ -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) diff --git a/core/data/src/main/kotlin/app/pachli/core/data/repository/StatusDisplayOptionsRepository.kt b/core/data/src/main/kotlin/app/pachli/core/data/repository/StatusDisplayOptionsRepository.kt index 939845a14..b5ff59a88 100644 --- a/core/data/src/main/kotlin/app/pachli/core/data/repository/StatusDisplayOptionsRepository.kt +++ b/core/data/src/main/kotlin/app/pachli/core/data/repository/StatusDisplayOptionsRepository.kt @@ -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), diff --git a/core/preferences/src/main/kotlin/app/pachli/core/preferences/SharedPreferencesRepository.kt b/core/preferences/src/main/kotlin/app/pachli/core/preferences/SharedPreferencesRepository.kt index 2f504b6c6..85e42f8ec 100644 --- a/core/preferences/src/main/kotlin/app/pachli/core/preferences/SharedPreferencesRepository.kt +++ b/core/preferences/src/main/kotlin/app/pachli/core/preferences/SharedPreferencesRepository.kt @@ -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) diff --git a/feature/lists/src/main/kotlin/app/pachli/feature/lists/AccountsInListFragment.kt b/feature/lists/src/main/kotlin/app/pachli/feature/lists/AccountsInListFragment.kt index 333dabf3d..71de07831 100644 --- a/feature/lists/src/main/kotlin/app/pachli/feature/lists/AccountsInListFragment.kt +++ b/feature/lists/src/main/kotlin/app/pachli/feature/lists/AccountsInListFragment.kt @@ -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)