From 240bf4463b92bbf53bbe16bcf1656a93533971d5 Mon Sep 17 00:00:00 2001 From: ottoptj Date: Fri, 9 Aug 2024 20:43:45 +0300 Subject: [PATCH] More refactoring of sharedpreferences into the relevant class --- .../java/eu/ottop/yamlauncher/Animations.kt | 14 ++++--- .../eu/ottop/yamlauncher/AppActionMenu.kt | 4 +- .../eu/ottop/yamlauncher/AppMenuAdapter.kt | 2 +- .../ottop/yamlauncher/GestureAppsAdapter.kt | 2 +- .../ottop/yamlauncher/GestureAppsFragment.kt | 5 ++- .../eu/ottop/yamlauncher/HiddenAppsAdapter.kt | 2 +- .../ottop/yamlauncher/HiddenAppsFragment.kt | 3 +- .../eu/ottop/yamlauncher/LocationFragment.kt | 9 ++--- .../ottop/yamlauncher/LocationListAdapter.kt | 10 ++--- .../java/eu/ottop/yamlauncher/MainActivity.kt | 38 +++++++++---------- .../eu/ottop/yamlauncher/SettingsFragment.kt | 1 - .../yamlauncher/SharedPreferenceManager.kt | 13 +++++++ .../main/java/eu/ottop/yamlauncher/UIUtils.kt | 22 ++++++----- 13 files changed, 70 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/Animations.kt b/app/src/main/java/eu/ottop/yamlauncher/Animations.kt index 4b21271..1e4805e 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/Animations.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/Animations.kt @@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter import android.animation.ArgbEvaluator import android.animation.ValueAnimator import android.app.Activity -import android.content.SharedPreferences +import android.content.Context import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.view.View import eu.ottop.yamlauncher.databinding.ActivityMainBinding -class Animations { +class Animations (context: Context) { + + private val sharedPreferenceManager = SharedPreferenceManager(context) fun fadeViewIn(view: View, duration: Long = 100) { view.fadeIn(duration) @@ -30,8 +32,8 @@ class Animations { binding.homeView.fadeOut() } - fun backgroundIn(activity: Activity, preferences: SharedPreferences, duration: Long = 100) { - val originalColor = Color.parseColor(preferences.getString("bgColor", "#00000000")) + fun backgroundIn(activity: Activity, duration: Long = 100) { + val originalColor = sharedPreferenceManager.getBgColor() val newColor: Int = if (originalColor == Color.parseColor("#00000000")) { Color.parseColor("#3F000000") @@ -51,8 +53,8 @@ class Animations { backgroundColorAnimator.start() } - fun backgroundOut(activity: Activity, preferences: SharedPreferences, duration: Long = 100) { - val newColor = Color.parseColor(preferences.getString("bgColor", "#00000000")) + fun backgroundOut(activity: Activity, duration: Long = 100) { + val newColor = sharedPreferenceManager.getBgColor() val originalColor: Int = if (newColor == Color.parseColor("#00000000")) { Color.parseColor("#3F000000") diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 3aae1d3..6f86f50 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -24,8 +24,6 @@ import kotlinx.coroutines.launch class AppActionMenu { - private val animations = Animations() - fun setActionListeners( activity: MainActivity, binding: ActivityMainBinding, @@ -39,7 +37,7 @@ class AppActionMenu { launcherApps: LauncherApps, mainActivity: LauncherActivityInfo? ){ - + val animations = Animations(activity) val sharedPreferenceManager = SharedPreferenceManager(activity) actionMenu.findViewById(R.id.info).setOnClickListener { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index d3d9d4f..332303c 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -36,7 +36,7 @@ class AppMenuAdapter( private val sharedPreferenceManager = SharedPreferenceManager(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context) - private val uiUtils = UIUtils() + private val uiUtils = UIUtils(context) private val appUtils = AppUtils(context) interface OnItemClickListener { diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt index 7a276ff..0002ab7 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt @@ -22,7 +22,7 @@ class GestureAppsAdapter( private val sharedPreferenceManager = SharedPreferenceManager(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context) - private val uiUtils = UIUtils() + private val uiUtils = UIUtils(context) interface OnItemClickListener { fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt index 7e09004..36d2123 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt @@ -40,8 +40,9 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - sharedPreferenceManager = SharedPreferenceManager(requireContext()) + appUtils = AppUtils(requireContext()) + sharedPreferenceManager = SharedPreferenceManager(requireContext()) lifecycleScope.launch { @@ -57,7 +58,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { } val recyclerView = view.findViewById(R.id.gesture_app_recycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) - val uiUtils = UIUtils() + val uiUtils = UIUtils(requireContext()) val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) recyclerView.edgeEffectFactory = appMenuEdgeFactory diff --git a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt index aa592bf..6508239 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsAdapter.kt @@ -23,7 +23,7 @@ class HiddenAppsAdapter( private val sharedPreferenceManager = SharedPreferenceManager(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context) - private val uiUtils = UIUtils() + private val uiUtils = UIUtils(context) interface OnItemClickListener { fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) diff --git a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt index 2c535c3..beed689 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt @@ -21,7 +21,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { private lateinit var sharedPreferenceManager: SharedPreferenceManager private var adapter: HiddenAppsAdapter? = null private var stringUtils = StringUtils() - private val uiUtils = UIUtils() + private lateinit var uiUtils: UIUtils override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -33,6 +33,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + uiUtils = UIUtils(requireContext()) appUtils = AppUtils(requireContext()) sharedPreferenceManager = SharedPreferenceManager(requireContext()) diff --git a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt index 6644598..10be865 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt @@ -24,7 +24,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { private lateinit var weatherSystem: WeatherSystem private lateinit var sharedPreferenceManager: SharedPreferenceManager private val stringUtils = StringUtils() - private val uiUtils = UIUtils() + private lateinit var uiUtils: UIUtils override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -35,6 +35,9 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + uiUtils = UIUtils(requireContext()) + weatherSystem = WeatherSystem(requireContext()) + sharedPreferenceManager = SharedPreferenceManager(requireContext()) val searchView = view.findViewById(R.id.locationSearch) @@ -42,10 +45,6 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener { val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) - weatherSystem = WeatherSystem(requireContext()) - - sharedPreferenceManager = SharedPreferenceManager(requireContext()) - stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link)) lifecycleScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt index 520796a..f46bd1b 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/LocationListAdapter.kt @@ -11,14 +11,14 @@ import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView class LocationListAdapter( - private val activity: Context, + private val context: Context, private var apps: MutableList>, private val itemClickListener: OnItemClickListener ) : RecyclerView.Adapter() { - private var preferences = PreferenceManager.getDefaultSharedPreferences(activity) - private val uiUtils = UIUtils() + private var preferences = PreferenceManager.getDefaultSharedPreferences(context) + private val uiUtils = UIUtils(context) interface OnItemClickListener { fun onItemClick(name: String?, latitude: String?, longitude: String?) @@ -51,12 +51,12 @@ class LocationListAdapter( override fun onBindViewHolder(holder: AppViewHolder, position: Int) { val app = apps[position] - uiUtils.setAppAlignment(activity, preferences, holder.textView, null ,holder.regionText) + uiUtils.setAppAlignment(context, preferences, holder.textView, null ,holder.regionText) uiUtils.setAppSize(preferences, holder.textView, null, holder.regionText) holder.textView.text = app["name"] - holder.regionText.text = activity.getString(R.string.region_text, app["region"], app["country"]) + holder.regionText.text = context.getString(R.string.region_text, app["region"], app["country"]) holder.textView.visibility = View.VISIBLE } diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 3c7a6c6..11374c4 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private lateinit var weatherSystem: WeatherSystem private lateinit var appUtils: AppUtils private val stringUtils = StringUtils() - private val uiUtils = UIUtils() + private lateinit var uiUtils: UIUtils private lateinit var gestureUtils: GestureUtils private var appActionMenu = AppActionMenu() @@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private lateinit var sharedPreferenceManager: SharedPreferenceManager - private val animations = Animations() + private lateinit var animations: Animations private lateinit var clock: TextClock private var clockMargin = 0 @@ -121,6 +121,13 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } private fun setMainVariables() { + weatherSystem = WeatherSystem(this@MainActivity) + appUtils = AppUtils(this@MainActivity) + uiUtils = UIUtils(this@MainActivity) + gestureUtils = GestureUtils(this@MainActivity) + sharedPreferenceManager = SharedPreferenceManager(this@MainActivity) + animations = Animations(this@MainActivity) + gestureDetector = GestureDetector(this, GestureListener()) shortcutGestureDetector = GestureDetector(this, TextGestureListener()) @@ -136,22 +143,15 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps - weatherSystem = WeatherSystem(this@MainActivity) - - appUtils = AppUtils(this@MainActivity) - - sharedPreferenceManager = SharedPreferenceManager(this@MainActivity) - gestureUtils = GestureUtils(this@MainActivity) - preferences = PreferenceManager.getDefaultSharedPreferences(this) } private fun setPreferences() { - uiUtils.setBackground(window, preferences) - uiUtils.setTextColors(preferences, binding.homeView) - uiUtils.setSearchColors(preferences, searchView) + uiUtils.setBackground(window) + uiUtils.setTextColors(binding.homeView) + uiUtils.setSearchColors(searchView) - uiUtils.setClockAlignment(preferences, clock, dateText) + uiUtils.setClockAlignment(clock, dateText) uiUtils.setSearchAlignment(preferences, searchView) uiUtils.setClockSize(preferences, clock) @@ -225,7 +225,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private fun toAppMenu() { animations.showApps(binding) - animations.backgroundIn(this@MainActivity, preferences) + animations.backgroundIn(this@MainActivity) if (preferences.getBoolean("autoKeyboard", false)) { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -297,16 +297,16 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh if (preferences != null) { when (key) { "bgColor" -> { - uiUtils.setBackground(window, preferences) + uiUtils.setBackground(window) } "textColor" -> { - uiUtils.setTextColors(preferences, binding.homeView) - uiUtils.setSearchColors(preferences, searchView) + uiUtils.setTextColors(binding.homeView) + uiUtils.setSearchColors(searchView) } "clockAlignment" -> { - uiUtils.setClockAlignment(preferences, clock, dateText) + uiUtils.setClockAlignment(clock, dateText) } "shortcutAlignment" -> { @@ -370,7 +370,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh fun backToHome() { closeKeyboard() animations.showHome(binding) - animations.backgroundOut(this@MainActivity, preferences) + animations.backgroundOut(this@MainActivity) val handler = Handler(Looper.getMainLooper()) handler.postDelayed({ try { diff --git a/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt index f8b6afc..a5194eb 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt @@ -5,7 +5,6 @@ import androidx.fragment.app.clearFragmentResultListener import androidx.fragment.app.setFragmentResultListener import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat -import androidx.preference.PreferenceManager import androidx.preference.SwitchPreference class SettingsFragment : PreferenceFragmentCompat() { diff --git a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt index 398f29c..18345bb 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt @@ -1,6 +1,7 @@ package eu.ottop.yamlauncher import android.content.Context +import android.graphics.Color import android.widget.TextView import androidx.preference.PreferenceManager @@ -95,4 +96,16 @@ class SharedPreferenceManager (context: Context) { return preferences.getString("${direction}SwipeApp", "")?.split("§splitter§") } + fun getBgColor(): Int { + return Color.parseColor(preferences.getString("bgColor", "#00000000")) + } + + fun getTextColor(): Int { + return Color.parseColor(preferences.getString("textColor", "#FFF3F3F3")) + } + + fun getClockAlignment(): String? { + return preferences.getString("clockAlignment", "left") + } + } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt index f1cda61..feea72c 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt @@ -20,22 +20,24 @@ import androidx.core.content.res.ResourcesCompat import androidx.core.view.children import com.google.android.material.textfield.TextInputEditText -class UIUtils { +class UIUtils(context: Context) { - fun setBackground(window: Window, preferences: SharedPreferences) { + private val sharedPreferenceManager = SharedPreferenceManager(context) + + fun setBackground(window: Window) { window.decorView.background = ColorDrawable(Color.parseColor("#00000000")) window.decorView.setBackgroundColor( - Color.parseColor(preferences.getString("bgColor", "#00000000")) + sharedPreferenceManager.getBgColor() ) } - fun setTextColors(preferences: SharedPreferences, view: View) { - val color = Color.parseColor(preferences.getString("textColor", "#FFF3F3F3")) + fun setTextColors(view: View) { + val color = sharedPreferenceManager.getTextColor() when { view is ViewGroup -> { view.children.forEach { child -> - setTextColors(preferences, child) + setTextColors(child) } } hasMethod(view, "setTextColor") -> { @@ -66,12 +68,12 @@ class UIUtils { return Color.argb(newAlpha, r, g, b) } - fun setSearchColors(preferences: SharedPreferences, searchView: TextInputEditText) { + fun setSearchColors(searchView: TextInputEditText) { val viewTreeObserver = searchView.viewTreeObserver val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { - val color = Color.parseColor(preferences.getString("textColor", "#FFF3F3F3")) + val color = sharedPreferenceManager.getTextColor() searchView.setTextColor(color) searchView.setHintTextColor(setAlpha(color, "A9")) searchView.compoundDrawables[0].mutate().colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP) @@ -87,8 +89,8 @@ class UIUtils { } } - fun setClockAlignment(preferences: SharedPreferences, clock: TextClock, dateText: TextClock) { - val alignment = preferences.getString("clockAlignment", "left") + fun setClockAlignment(clock: TextClock, dateText: TextClock) { + val alignment = sharedPreferenceManager.getClockAlignment() setTextAlignment(clock, alignment) setTextAlignment(dateText, alignment) }