Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject SharedPreferences #4441

Merged
merged 8 commits into from
May 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.keylesspalace.tusky

import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -25,7 +26,6 @@ import androidx.appcompat.widget.SearchView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
Expand All @@ -44,13 +44,17 @@ import com.keylesspalace.tusky.util.viewBinding
import com.keylesspalace.tusky.viewmodel.AccountsInListViewModel
import com.keylesspalace.tusky.viewmodel.State
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch

private typealias AccountInfo = Pair<TimelineAccount, Boolean>

@AndroidEntryPoint
class AccountsInListFragment : DialogFragment() {

@Inject
lateinit var preferences: SharedPreferences

private val viewModel: AccountsInListViewModel by viewModels()
private val binding by viewBinding(FragmentAccountsInListBinding::bind)

Expand All @@ -60,9 +64,6 @@ class AccountsInListFragment : DialogFragment() {
private val searchAdapter = SearchAdapter()

private val radius by unsafeLazy { resources.getDimensionPixelSize(R.dimen.avatar_radius_48dp) }
private val pm by unsafeLazy { PreferenceManager.getDefaultSharedPreferences(requireContext()) }
private val animateAvatar by unsafeLazy { pm.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false) }
private val animateEmojis by unsafeLazy { pm.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -206,6 +207,8 @@ class AccountsInListFragment : DialogFragment() {
position: Int
) {
val account = getItem(position)
val animateAvatar = preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
holder.binding.displayNameTextView.text = account.name.emojify(account.emojis, holder.binding.displayNameTextView, animateEmojis)
holder.binding.usernameTextView.text = account.username
loadAvatar(account.avatar, holder.binding.avatar, radius, animateAvatar)
Expand Down Expand Up @@ -257,6 +260,9 @@ class AccountsInListFragment : DialogFragment() {
) {
val (account, inAList) = getItem(position)

val animateAvatar = preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)

holder.binding.displayNameTextView.text = account.name.emojify(account.emojis, holder.binding.displayNameTextView, animateEmojis)
holder.binding.usernameTextView.text = account.username
loadAvatar(account.avatar, holder.binding.avatar, radius, animateAvatar)
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/keylesspalace/tusky/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public abstract class BaseActivity extends AppCompatActivity {
@NonNull
public AccountManager accountManager;

@Inject
@NonNull
public SharedPreferences preferences;

/**
* Allows overriding the default ViewModelProvider.Factory for testing purposes.
*/
Expand All @@ -93,8 +97,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
);
}

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

/* There isn't presently a way to globally change the theme of a whole application at
* runtime, just individual activities. So, each activity has to set its theme before any
* views are created. */
Expand Down Expand Up @@ -125,7 +127,8 @@ private boolean activityTransitionWasRequested() {

@Override
protected void attachBaseContext(Context newBase) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase);
// injected preferences not yet available in this point of the lifecycle
SharedPreferences preferences = ((TuskyApplication)newBase.getApplicationContext()).sharedPreferences;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too hacky?


// Scale text in the UI from PrefKeys.UI_TEXT_SCALE_RATIO
float uiScaleRatio = preferences.getFloat(PrefKeys.UI_TEXT_SCALE_RATIO, 100F);
Expand Down Expand Up @@ -245,7 +248,11 @@ public void showAccountChooserDialog(@Nullable CharSequence dialogTitle, boolean
if (!showActiveAccount && activeAccount != null) {
accounts.remove(activeAccount);
}
AccountSelectionAdapter adapter = new AccountSelectionAdapter(this);
AccountSelectionAdapter adapter = new AccountSelectionAdapter(
this,
preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false),
preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
);
adapter.addAll(accounts);

new AlertDialog.Builder(this)
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/keylesspalace/tusky/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import androidx.core.view.MenuProvider
import androidx.core.view.forEach
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.viewpager2.widget.MarginPageTransformer
import at.connyduck.calladapter.networkresult.fold
import com.bumptech.glide.Glide
Expand Down Expand Up @@ -111,7 +110,6 @@ import com.keylesspalace.tusky.util.overrideActivityTransitionCompat
import com.keylesspalace.tusky.util.reduceSwipeSensitivity
import com.keylesspalace.tusky.util.show
import com.keylesspalace.tusky.util.startActivityWithSlideInAnimation
import com.keylesspalace.tusky.util.unsafeLazy
import com.keylesspalace.tusky.util.viewBinding
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
Expand Down Expand Up @@ -182,8 +180,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {

private var unreadAnnouncementsCount = 0

private val preferences by unsafeLazy { PreferenceManager.getDefaultSharedPreferences(this) }

// We need to know if the emoji pack has been changed
private var selectedEmojiPack: String? = null

Expand Down Expand Up @@ -1182,7 +1178,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
header.clear()
header.profiles = profiles
header.setActiveProfile(accountManager.activeAccount!!.id)
binding.mainToolbar.subtitle = if (accountManager.shouldDisplaySelfUsername(this)) {
binding.mainToolbar.subtitle = if (accountManager.shouldDisplaySelfUsername()) {
accountManager.activeAccount!!.fullName
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.preference.PreferenceManager
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.ItemAutocompleteAccountBinding
import com.keylesspalace.tusky.db.entity.AccountEntity
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.util.emojify
import com.keylesspalace.tusky.util.loadAvatar

class AccountSelectionAdapter(context: Context) : ArrayAdapter<AccountEntity>(
class AccountSelectionAdapter(
context: Context,
private val animateAvatars: Boolean,
private val animateEmojis: Boolean
) : ArrayAdapter<AccountEntity>(
context,
R.layout.item_autocomplete_account
) {
Expand All @@ -42,17 +44,13 @@ class AccountSelectionAdapter(context: Context) : ArrayAdapter<AccountEntity>(

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(R.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import androidx.core.view.WindowInsetsCompat.Type.systemBars
import androidx.core.view.updatePadding
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.MarginPageTransformer
import com.bumptech.glide.Glide
Expand Down Expand Up @@ -92,7 +91,6 @@ import com.keylesspalace.tusky.util.reduceSwipeSensitivity
import com.keylesspalace.tusky.util.setClickableText
import com.keylesspalace.tusky.util.show
import com.keylesspalace.tusky.util.startActivityWithSlideInAnimation
import com.keylesspalace.tusky.util.unsafeLazy
import com.keylesspalace.tusky.util.viewBinding
import com.keylesspalace.tusky.util.visible
import com.keylesspalace.tusky.view.showMuteAccountDialog
Expand Down Expand Up @@ -121,8 +119,6 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide

private lateinit var accountFieldAdapter: AccountFieldAdapter

private val preferences by unsafeLazy { PreferenceManager.getDefaultSharedPreferences(this) }

private var followState: FollowState = FollowState.NOT_FOLLOWING
private var blocking: Boolean = false
private var muting: Boolean = false
Expand Down Expand Up @@ -173,10 +169,9 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
// Obtain information to fill out the profile.
viewModel.setAccountInfo(intent.getStringExtra(KEY_ACCOUNT_ID)!!)

val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
animateAvatar = sharedPrefs.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
animateEmojis = sharedPrefs.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
hideFab = sharedPrefs.getBoolean(PrefKeys.FAB_HIDE, false)
animateAvatar = preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
animateEmojis = preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
hideFab = preferences.getBoolean(PrefKeys.FAB_HIDE, false)

handleWindowInsets()
setupToolbar()
Expand Down Expand Up @@ -241,7 +236,6 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
}

// If wellbeing mode is enabled, follow stats and posts count should be hidden
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val wellbeingEnabled = preferences.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_PROFILE, false)

if (wellbeingEnabled) {
Expand Down Expand Up @@ -715,7 +709,6 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide
showingReblogs = relation.showingReblogs

// If wellbeing mode is enabled, "follows you" text should not be visible
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val wellbeingEnabled = preferences.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_PROFILE, false)

binding.accountFollowsYouTextView.visible(relation.followedBy && !wellbeingEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.keylesspalace.tusky.components.account.media

import android.content.SharedPreferences
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
Expand All @@ -28,7 +29,6 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.color.MaterialColors
import com.keylesspalace.tusky.R
Expand Down Expand Up @@ -64,6 +64,9 @@ class AccountMediaFragment :
@Inject
lateinit var accountManager: AccountManager

@Inject
lateinit var preferences: SharedPreferences

private val binding by viewBinding(FragmentTimelineBinding::bind)

private val viewModel: AccountMediaViewModel by viewModels()
Expand All @@ -78,7 +81,6 @@ class AccountMediaFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)

val preferences = PreferenceManager.getDefaultSharedPreferences(view.context)
val useBlurhash = preferences.getBoolean(PrefKeys.USE_BLURHASH, true)

adapter = AccountMediaGridAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

package com.keylesspalace.tusky.components.accountlist

import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -73,6 +73,9 @@ class AccountListFragment :
@Inject
lateinit var accountManager: AccountManager

@Inject
lateinit var preferences: SharedPreferences

private val binding by viewBinding(FragmentAccountListBinding::bind)

private lateinit var type: Type
Expand Down Expand Up @@ -101,10 +104,9 @@ class AccountListFragment :
binding.swipeRefreshLayout.setOnRefreshListener { fetchAccounts() }
binding.swipeRefreshLayout.setColorSchemeResources(R.color.tusky_blue)

val pm = PreferenceManager.getDefaultSharedPreferences(view.context)
val animateAvatar = pm.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = pm.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val showBotOverlay = pm.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, true)
val animateAvatar = preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false)
val animateEmojis = preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)
val showBotOverlay = preferences.getBoolean(PrefKeys.SHOW_BOT_OVERLAY, true)

val activeAccount = accountManager.activeAccount!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.keylesspalace.tusky.components.announcements

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
Expand All @@ -27,7 +26,6 @@ import android.widget.PopupWindow
import androidx.activity.viewModels
import androidx.core.view.MenuProvider
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.color.MaterialColors
Expand Down Expand Up @@ -100,7 +98,6 @@ class AnnouncementsActivity :
val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
binding.announcementsList.addItemDecoration(divider)

val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val wellbeingEnabled = preferences.getBoolean(PrefKeys.WELLBEING_HIDE_STATS_POSTS, false)
val animateEmojis = preferences.getBoolean(PrefKeys.ANIMATE_CUSTOM_EMOJIS, false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.transition.TransitionManager
import com.canhub.cropper.CropImage
Expand Down Expand Up @@ -104,7 +103,6 @@ import com.keylesspalace.tusky.util.loadAvatar
import com.keylesspalace.tusky.util.modernLanguageCode
import com.keylesspalace.tusky.util.setDrawableTint
import com.keylesspalace.tusky.util.show
import com.keylesspalace.tusky.util.unsafeLazy
import com.keylesspalace.tusky.util.viewBinding
import com.keylesspalace.tusky.util.visible
import com.mikepenz.iconics.IconicsDrawable
Expand Down Expand Up @@ -146,8 +144,6 @@ class ComposeActivity :

private var photoUploadUri: Uri? = null

private val preferences by unsafeLazy { PreferenceManager.getDefaultSharedPreferences(this) }

@VisibleForTesting
var maximumTootCharacters = InstanceInfoRepository.DEFAULT_CHARACTER_LIMIT
var charactersReservedPerUrl = InstanceInfoRepository.DEFAULT_CHARACTERS_RESERVED_PER_URL
Expand Down Expand Up @@ -290,7 +286,7 @@ class ComposeActivity :
setupButtons()
subscribeToUpdates(mediaAdapter)

if (accountManager.shouldDisplaySelfUsername(this)) {
if (accountManager.shouldDisplaySelfUsername()) {
binding.composeUsernameView.text = getString(
R.string.compose_active_account_description,
activeAccount.fullName
Expand Down
Loading
Loading