More refactoring of sharedpreferences into the relevant class

This commit is contained in:
ottoptj 2024-08-09 20:43:45 +03:00
commit 240bf4463b
13 changed files with 70 additions and 55 deletions

View file

@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter
import android.animation.ArgbEvaluator import android.animation.ArgbEvaluator
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.app.Activity import android.app.Activity
import android.content.SharedPreferences import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.view.View import android.view.View
import eu.ottop.yamlauncher.databinding.ActivityMainBinding import eu.ottop.yamlauncher.databinding.ActivityMainBinding
class Animations { class Animations (context: Context) {
private val sharedPreferenceManager = SharedPreferenceManager(context)
fun fadeViewIn(view: View, duration: Long = 100) { fun fadeViewIn(view: View, duration: Long = 100) {
view.fadeIn(duration) view.fadeIn(duration)
@ -30,8 +32,8 @@ class Animations {
binding.homeView.fadeOut() binding.homeView.fadeOut()
} }
fun backgroundIn(activity: Activity, preferences: SharedPreferences, duration: Long = 100) { fun backgroundIn(activity: Activity, duration: Long = 100) {
val originalColor = Color.parseColor(preferences.getString("bgColor", "#00000000")) val originalColor = sharedPreferenceManager.getBgColor()
val newColor: Int = if (originalColor == Color.parseColor("#00000000")) { val newColor: Int = if (originalColor == Color.parseColor("#00000000")) {
Color.parseColor("#3F000000") Color.parseColor("#3F000000")
@ -51,8 +53,8 @@ class Animations {
backgroundColorAnimator.start() backgroundColorAnimator.start()
} }
fun backgroundOut(activity: Activity, preferences: SharedPreferences, duration: Long = 100) { fun backgroundOut(activity: Activity, duration: Long = 100) {
val newColor = Color.parseColor(preferences.getString("bgColor", "#00000000")) val newColor = sharedPreferenceManager.getBgColor()
val originalColor: Int = if (newColor == Color.parseColor("#00000000")) { val originalColor: Int = if (newColor == Color.parseColor("#00000000")) {
Color.parseColor("#3F000000") Color.parseColor("#3F000000")

View file

@ -24,8 +24,6 @@ import kotlinx.coroutines.launch
class AppActionMenu { class AppActionMenu {
private val animations = Animations()
fun setActionListeners( fun setActionListeners(
activity: MainActivity, activity: MainActivity,
binding: ActivityMainBinding, binding: ActivityMainBinding,
@ -39,7 +37,7 @@ class AppActionMenu {
launcherApps: LauncherApps, launcherApps: LauncherApps,
mainActivity: LauncherActivityInfo? mainActivity: LauncherActivityInfo?
){ ){
val animations = Animations(activity)
val sharedPreferenceManager = SharedPreferenceManager(activity) val sharedPreferenceManager = SharedPreferenceManager(activity)
actionMenu.findViewById<TextView>(R.id.info).setOnClickListener { actionMenu.findViewById<TextView>(R.id.info).setOnClickListener {

View file

@ -36,7 +36,7 @@ class AppMenuAdapter(
private val sharedPreferenceManager = SharedPreferenceManager(context) private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils() private val uiUtils = UIUtils(context)
private val appUtils = AppUtils(context) private val appUtils = AppUtils(context)
interface OnItemClickListener { interface OnItemClickListener {

View file

@ -22,7 +22,7 @@ class GestureAppsAdapter(
private val sharedPreferenceManager = SharedPreferenceManager(context) private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils() private val uiUtils = UIUtils(context)
interface OnItemClickListener { interface OnItemClickListener {
fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) fun onItemClick(appInfo: LauncherActivityInfo, profile: Int)

View file

@ -40,8 +40,9 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
sharedPreferenceManager = SharedPreferenceManager(requireContext())
appUtils = AppUtils(requireContext()) appUtils = AppUtils(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext())
lifecycleScope.launch { lifecycleScope.launch {
@ -57,7 +58,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
} }
val recyclerView = view.findViewById<RecyclerView>(R.id.gesture_app_recycler) val recyclerView = view.findViewById<RecyclerView>(R.id.gesture_app_recycler)
val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity())
val uiUtils = UIUtils() val uiUtils = UIUtils(requireContext())
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
recyclerView.edgeEffectFactory = appMenuEdgeFactory recyclerView.edgeEffectFactory = appMenuEdgeFactory

View file

@ -23,7 +23,7 @@ class HiddenAppsAdapter(
private val sharedPreferenceManager = SharedPreferenceManager(context) private val sharedPreferenceManager = SharedPreferenceManager(context)
private var preferences = PreferenceManager.getDefaultSharedPreferences(context) private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils() private val uiUtils = UIUtils(context)
interface OnItemClickListener { interface OnItemClickListener {
fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) fun onItemClick(appInfo: LauncherActivityInfo, profile: Int)

View file

@ -21,7 +21,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
private lateinit var sharedPreferenceManager: SharedPreferenceManager private lateinit var sharedPreferenceManager: SharedPreferenceManager
private var adapter: HiddenAppsAdapter? = null private var adapter: HiddenAppsAdapter? = null
private var stringUtils = StringUtils() private var stringUtils = StringUtils()
private val uiUtils = UIUtils() private lateinit var uiUtils: UIUtils
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -33,6 +33,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
uiUtils = UIUtils(requireContext())
appUtils = AppUtils(requireContext()) appUtils = AppUtils(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext()) sharedPreferenceManager = SharedPreferenceManager(requireContext())

View file

@ -24,7 +24,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
private lateinit var weatherSystem: WeatherSystem private lateinit var weatherSystem: WeatherSystem
private lateinit var sharedPreferenceManager: SharedPreferenceManager private lateinit var sharedPreferenceManager: SharedPreferenceManager
private val stringUtils = StringUtils() private val stringUtils = StringUtils()
private val uiUtils = UIUtils() private lateinit var uiUtils: UIUtils
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -35,6 +35,9 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
uiUtils = UIUtils(requireContext())
weatherSystem = WeatherSystem(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext())
val searchView = view.findViewById<TextInputEditText>(R.id.locationSearch) val searchView = view.findViewById<TextInputEditText>(R.id.locationSearch)
@ -42,10 +45,6 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
weatherSystem = WeatherSystem(requireContext())
sharedPreferenceManager = SharedPreferenceManager(requireContext())
stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link)) stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link))
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {

View file

@ -11,14 +11,14 @@ import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
class LocationListAdapter( class LocationListAdapter(
private val activity: Context, private val context: Context,
private var apps: MutableList<Map<String, String>>, private var apps: MutableList<Map<String, String>>,
private val itemClickListener: OnItemClickListener private val itemClickListener: OnItemClickListener
) : ) :
RecyclerView.Adapter<LocationListAdapter.AppViewHolder>() { RecyclerView.Adapter<LocationListAdapter.AppViewHolder>() {
private var preferences = PreferenceManager.getDefaultSharedPreferences(activity) private var preferences = PreferenceManager.getDefaultSharedPreferences(context)
private val uiUtils = UIUtils() private val uiUtils = UIUtils(context)
interface OnItemClickListener { interface OnItemClickListener {
fun onItemClick(name: String?, latitude: String?, longitude: String?) fun onItemClick(name: String?, latitude: String?, longitude: String?)
@ -51,12 +51,12 @@ class LocationListAdapter(
override fun onBindViewHolder(holder: AppViewHolder, position: Int) { override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
val app = apps[position] 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) uiUtils.setAppSize(preferences, holder.textView, null, holder.regionText)
holder.textView.text = app["name"] 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 holder.textView.visibility = View.VISIBLE
} }

View file

@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private lateinit var weatherSystem: WeatherSystem private lateinit var weatherSystem: WeatherSystem
private lateinit var appUtils: AppUtils private lateinit var appUtils: AppUtils
private val stringUtils = StringUtils() private val stringUtils = StringUtils()
private val uiUtils = UIUtils() private lateinit var uiUtils: UIUtils
private lateinit var gestureUtils: GestureUtils private lateinit var gestureUtils: GestureUtils
private var appActionMenu = AppActionMenu() private var appActionMenu = AppActionMenu()
@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private lateinit var sharedPreferenceManager: SharedPreferenceManager private lateinit var sharedPreferenceManager: SharedPreferenceManager
private val animations = Animations() private lateinit var animations: Animations
private lateinit var clock: TextClock private lateinit var clock: TextClock
private var clockMargin = 0 private var clockMargin = 0
@ -121,6 +121,13 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
} }
private fun setMainVariables() { 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()) gestureDetector = GestureDetector(this, GestureListener())
shortcutGestureDetector = GestureDetector(this, TextGestureListener()) shortcutGestureDetector = GestureDetector(this, TextGestureListener())
@ -136,22 +143,15 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps 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) preferences = PreferenceManager.getDefaultSharedPreferences(this)
} }
private fun setPreferences() { private fun setPreferences() {
uiUtils.setBackground(window, preferences) uiUtils.setBackground(window)
uiUtils.setTextColors(preferences, binding.homeView) uiUtils.setTextColors(binding.homeView)
uiUtils.setSearchColors(preferences, searchView) uiUtils.setSearchColors(searchView)
uiUtils.setClockAlignment(preferences, clock, dateText) uiUtils.setClockAlignment(clock, dateText)
uiUtils.setSearchAlignment(preferences, searchView) uiUtils.setSearchAlignment(preferences, searchView)
uiUtils.setClockSize(preferences, clock) uiUtils.setClockSize(preferences, clock)
@ -225,7 +225,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private fun toAppMenu() { private fun toAppMenu() {
animations.showApps(binding) animations.showApps(binding)
animations.backgroundIn(this@MainActivity, preferences) animations.backgroundIn(this@MainActivity)
if (preferences.getBoolean("autoKeyboard", false)) { if (preferences.getBoolean("autoKeyboard", false)) {
val imm = val imm =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
@ -297,16 +297,16 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
if (preferences != null) { if (preferences != null) {
when (key) { when (key) {
"bgColor" -> { "bgColor" -> {
uiUtils.setBackground(window, preferences) uiUtils.setBackground(window)
} }
"textColor" -> { "textColor" -> {
uiUtils.setTextColors(preferences, binding.homeView) uiUtils.setTextColors(binding.homeView)
uiUtils.setSearchColors(preferences, searchView) uiUtils.setSearchColors(searchView)
} }
"clockAlignment" -> { "clockAlignment" -> {
uiUtils.setClockAlignment(preferences, clock, dateText) uiUtils.setClockAlignment(clock, dateText)
} }
"shortcutAlignment" -> { "shortcutAlignment" -> {
@ -370,7 +370,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
fun backToHome() { fun backToHome() {
closeKeyboard() closeKeyboard()
animations.showHome(binding) animations.showHome(binding)
animations.backgroundOut(this@MainActivity, preferences) animations.backgroundOut(this@MainActivity)
val handler = Handler(Looper.getMainLooper()) val handler = Handler(Looper.getMainLooper())
handler.postDelayed({ handler.postDelayed({
try { try {

View file

@ -5,7 +5,6 @@ import androidx.fragment.app.clearFragmentResultListener
import androidx.fragment.app.setFragmentResultListener import androidx.fragment.app.setFragmentResultListener
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
class SettingsFragment : PreferenceFragmentCompat() { class SettingsFragment : PreferenceFragmentCompat() {

View file

@ -1,6 +1,7 @@
package eu.ottop.yamlauncher package eu.ottop.yamlauncher
import android.content.Context import android.content.Context
import android.graphics.Color
import android.widget.TextView import android.widget.TextView
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
@ -95,4 +96,16 @@ class SharedPreferenceManager (context: Context) {
return preferences.getString("${direction}SwipeApp", "")?.split("§splitter§") 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")
}
} }

View file

@ -20,22 +20,24 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.view.children import androidx.core.view.children
import com.google.android.material.textfield.TextInputEditText 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.background = ColorDrawable(Color.parseColor("#00000000"))
window.decorView.setBackgroundColor( window.decorView.setBackgroundColor(
Color.parseColor(preferences.getString("bgColor", "#00000000")) sharedPreferenceManager.getBgColor()
) )
} }
fun setTextColors(preferences: SharedPreferences, view: View) { fun setTextColors(view: View) {
val color = Color.parseColor(preferences.getString("textColor", "#FFF3F3F3")) val color = sharedPreferenceManager.getTextColor()
when { when {
view is ViewGroup -> { view is ViewGroup -> {
view.children.forEach { child -> view.children.forEach { child ->
setTextColors(preferences, child) setTextColors(child)
} }
} }
hasMethod(view, "setTextColor") -> { hasMethod(view, "setTextColor") -> {
@ -66,12 +68,12 @@ class UIUtils {
return Color.argb(newAlpha, r, g, b) return Color.argb(newAlpha, r, g, b)
} }
fun setSearchColors(preferences: SharedPreferences, searchView: TextInputEditText) { fun setSearchColors(searchView: TextInputEditText) {
val viewTreeObserver = searchView.viewTreeObserver val viewTreeObserver = searchView.viewTreeObserver
val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener { val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() { override fun onGlobalLayout() {
val color = Color.parseColor(preferences.getString("textColor", "#FFF3F3F3")) val color = sharedPreferenceManager.getTextColor()
searchView.setTextColor(color) searchView.setTextColor(color)
searchView.setHintTextColor(setAlpha(color, "A9")) searchView.setHintTextColor(setAlpha(color, "A9"))
searchView.compoundDrawables[0].mutate().colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP) searchView.compoundDrawables[0].mutate().colorFilter = BlendModeColorFilter(color, BlendMode.SRC_ATOP)
@ -87,8 +89,8 @@ class UIUtils {
} }
} }
fun setClockAlignment(preferences: SharedPreferences, clock: TextClock, dateText: TextClock) { fun setClockAlignment(clock: TextClock, dateText: TextClock) {
val alignment = preferences.getString("clockAlignment", "left") val alignment = sharedPreferenceManager.getClockAlignment()
setTextAlignment(clock, alignment) setTextAlignment(clock, alignment)
setTextAlignment(dateText, alignment) setTextAlignment(dateText, alignment)
} }