diff --git a/app/src/main/java/com/sduduzog/slimlauncher/adapters/AppDrawerAdapter.kt b/app/src/main/java/com/sduduzog/slimlauncher/adapters/AppDrawerAdapter.kt index d6cba92c..d3b419dc 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/adapters/AppDrawerAdapter.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/adapters/AppDrawerAdapter.kt @@ -11,29 +11,30 @@ import androidx.lifecycle.LifecycleOwner import androidx.recyclerview.widget.RecyclerView import com.jkuester.unlauncher.datastore.UnlauncherApp import com.sduduzog.slimlauncher.R -import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsRepository -import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository +import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource import com.sduduzog.slimlauncher.ui.main.HomeFragment import com.sduduzog.slimlauncher.utils.firstUppercase +import com.sduduzog.slimlauncher.utils.gravity class AppDrawerAdapter( - private val listener: HomeFragment.AppDrawerListener, - lifecycleOwner: LifecycleOwner, - appsRepo: UnlauncherAppsRepository, - private val corePreferencesRepo: CorePreferencesRepository + private val listener: HomeFragment.AppDrawerListener, + lifecycleOwner: LifecycleOwner, + private val unlauncherDataSource: UnlauncherDataSource ) : RecyclerView.Adapter() { private val WORK_APP_PREFIX = "\uD83C\uDD46 " //Unicode for boxed w private val regex = Regex("[!@#\$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>/? ]") private var apps: List = listOf() private var filteredApps: List = listOf() + private var gravity = 3 init { - appsRepo.liveData().observe(lifecycleOwner) { unlauncherApps -> + unlauncherDataSource.unlauncherAppsRepo.liveData().observe(lifecycleOwner) { unlauncherApps -> apps = unlauncherApps.appsList updateFilteredApps() } - corePreferencesRepo.liveData().observe(lifecycleOwner) { _ -> + unlauncherDataSource.corePreferencesRepo.liveData().observe(lifecycleOwner) { corePrefs -> + gravity = corePrefs.alignmentFormat.gravity() updateFilteredApps() } } @@ -88,8 +89,9 @@ class AppDrawerAdapter( @SuppressLint("NotifyDataSetChanged") private fun updateFilteredApps(filterQuery: String = "") { - val showDrawerHeadings = corePreferencesRepo.get().showDrawerHeadings - val searchAllApps = corePreferencesRepo.get().searchAllAppsInDrawer && filterQuery != "" + val corePreferences = unlauncherDataSource.corePreferencesRepo.get() + val showDrawerHeadings = corePreferences.showDrawerHeadings + val searchAllApps = corePreferences.searchAllAppsInDrawer && filterQuery != "" val displayableApps = apps .filter { app -> (app.displayInDrawer || searchAllApps) && regex.replace(app.displayName, "") @@ -153,6 +155,7 @@ class AppDrawerAdapter( fun bind(item: UnlauncherApp) { this.item.text = item.displayName + this.item.gravity = gravity } } diff --git a/app/src/main/java/com/sduduzog/slimlauncher/adapters/HomeAdapter.kt b/app/src/main/java/com/sduduzog/slimlauncher/adapters/HomeAdapter.kt index fc2ef427..7289fed3 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/adapters/HomeAdapter.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/adapters/HomeAdapter.kt @@ -6,27 +6,18 @@ import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.sduduzog.slimlauncher.R +import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource import com.sduduzog.slimlauncher.models.HomeApp -import com.sduduzog.slimlauncher.utils.OnLaunchAppListener +import com.sduduzog.slimlauncher.ui.main.HomeFragment +import com.sduduzog.slimlauncher.utils.gravity -/** - * Corresponding to the resulting gravity, not the option key - */ -enum class Alignment (val value: Int) { - LEFT(3), - RIGHT(5), - CENTER(1) -} - -class HomeAdapter(private val listener: OnLaunchAppListener) - : RecyclerView.Adapter() { +class HomeAdapter( + private val listener: HomeFragment, + private val unlauncherDataSource: UnlauncherDataSource +) : RecyclerView.Adapter() { private var apps: List = listOf() - private var gravity: Alignment = Alignment.LEFT - constructor(listener: OnLaunchAppListener, alignment: Int) : this(listener) { - setAlignment(alignment) - } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.main_fragment_list_item, parent, false) @@ -36,10 +27,12 @@ class HomeAdapter(private val listener: OnLaunchAppListener) override fun onBindViewHolder(holder: ViewHolder, position: Int) { val item = apps.elementAt(position) holder.mLabelView.text = item.appNickname ?: item.appName - holder.mLabelView.gravity = gravity.value holder.mLabelView.setOnClickListener { listener.onLaunch(item, it) } + unlauncherDataSource.corePreferencesRepo.liveData().observe(listener.viewLifecycleOwner) { + holder.mLabelView.gravity = it.alignmentFormat.gravity() + } } override fun getItemCount(): Int = apps.size @@ -49,20 +42,6 @@ class HomeAdapter(private val listener: OnLaunchAppListener) notifyDataSetChanged() } - fun getGravity(): Alignment = gravity - - fun setGravity(gravity: Alignment) { - this.gravity = gravity - } - - private fun setAlignment(alignment: Int) { - gravity = when (alignment) { - 2 -> Alignment.RIGHT - 1 -> Alignment.CENTER - else -> Alignment.LEFT - } - } - inner class ViewHolder(mView: View) : RecyclerView.ViewHolder(mView) { val mLabelView: TextView = mView.findViewById(R.id.home_fragment_list_item_app_name) diff --git a/app/src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesRepository.kt b/app/src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesRepository.kt index 5caab5fc..a67d7a6d 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesRepository.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesRepository.kt @@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore import androidx.lifecycle.LifecycleCoroutineScope import androidx.lifecycle.LiveData import androidx.lifecycle.asLiveData +import com.jkuester.unlauncher.datastore.AlignmentFormat import com.jkuester.unlauncher.datastore.ClockType import com.jkuester.unlauncher.datastore.CorePreferences import com.jkuester.unlauncher.datastore.SearchBarPosition @@ -99,4 +100,12 @@ class CorePreferencesRepository( } } } + + fun updateAlignmentFormat(alignmentFormat: AlignmentFormat) { + lifecycleScope.launch { + corePreferencesStore.updateData { + it.toBuilder().setAlignmentFormat(alignmentFormat).build() + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/sduduzog/slimlauncher/ui/dialogs/ChooseAlignmentDialog.kt b/app/src/main/java/com/sduduzog/slimlauncher/ui/dialogs/ChooseAlignmentDialog.kt index 81269932..7611ed4e 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/ui/dialogs/ChooseAlignmentDialog.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/ui/dialogs/ChooseAlignmentDialog.kt @@ -2,38 +2,35 @@ package com.sduduzog.slimlauncher.ui.dialogs import android.app.AlertDialog import android.app.Dialog -import android.content.Context.MODE_PRIVATE -import android.content.SharedPreferences import android.os.Bundle -import androidx.core.content.edit import androidx.fragment.app.DialogFragment +import com.jkuester.unlauncher.datastore.AlignmentFormat import com.sduduzog.slimlauncher.R +import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource +import dagger.hilt.android.AndroidEntryPoint +import javax.inject.Inject +@AndroidEntryPoint class ChooseAlignmentDialog : DialogFragment() { - private lateinit var settings: SharedPreferences + @Inject + lateinit var unlauncherDataSource: UnlauncherDataSource override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val builder = AlertDialog.Builder(requireContext()) - settings = requireContext().getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE) - val active = settings.getInt(getString(R.string.prefs_settings_alignment), 3) + val repo = unlauncherDataSource.corePreferencesRepo + val active = repo.get().alignmentFormat.number builder.setTitle(R.string.choose_alignment_dialog_title) builder.setSingleChoiceItems(R.array.alignment_format_array, active) { dialogInterface, i -> dialogInterface.dismiss() - settings.edit { - putInt(getString(R.string.prefs_settings_alignment), i) - } - + repo.updateAlignmentFormat(AlignmentFormat.forNumber(i)) } return builder.create() } - companion object { - fun getInstance(): ChooseAlignmentDialog{ - return ChooseAlignmentDialog() - } + fun getInstance(): ChooseAlignmentDialog = ChooseAlignmentDialog() } } diff --git a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt index 59ccd153..6bf7471c 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt @@ -67,6 +67,8 @@ import java.util.Date import java.util.Locale import javax.inject.Inject +private const val APP_TILE_SIZE: Int = 3 + @AndroidEntryPoint class HomeFragment : BaseFragment(), OnLaunchAppListener { @Inject @@ -96,13 +98,8 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val settingsKey = getString(R.string.prefs_settings) - val alignmentKey: String = getString(R.string.prefs_settings_alignment) - val preferences = requireContext().getSharedPreferences(settingsKey, Context.MODE_PRIVATE) - val alignment = preferences.getInt(alignmentKey, 3) - - val adapter1 = HomeAdapter(this, alignment) - val adapter2 = HomeAdapter(this, alignment) + val adapter1 = HomeAdapter(this, unlauncherDataSource) + val adapter2 = HomeAdapter(this, unlauncherDataSource) home_fragment_list.adapter = adapter1 home_fragment_list_exp.adapter = adapter2 @@ -111,10 +108,10 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { viewModel.apps.observe(viewLifecycleOwner) { list -> list?.let { apps -> adapter1.setItems(apps.filter { - it.sortingIndex < 3 + it.sortingIndex < APP_TILE_SIZE }) adapter2.setItems(apps.filter { - it.sortingIndex >= 3 + it.sortingIndex >= APP_TILE_SIZE }) // Set the home apps in the Unlauncher data @@ -127,8 +124,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { appDrawerAdapter = AppDrawerAdapter( AppDrawerListener(), viewLifecycleOwner, - unlauncherAppsRepo, - unlauncherDataSource.corePreferencesRepo + unlauncherDataSource ) setEventListeners() diff --git a/app/src/main/java/com/sduduzog/slimlauncher/utils/Utils.kt b/app/src/main/java/com/sduduzog/slimlauncher/utils/Utils.kt index af144585..3887cf06 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/utils/Utils.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/utils/Utils.kt @@ -14,6 +14,7 @@ import android.text.style.TextAppearanceSpan import android.util.DisplayMetrics import android.view.WindowInsets import androidx.annotation.StringRes +import com.jkuester.unlauncher.datastore.AlignmentFormat import com.sduduzog.slimlauncher.R @@ -97,4 +98,10 @@ fun createTitleAndSubtitleText(context: Context, title: CharSequence, subtitle: fun String.firstUppercase() = this.first().uppercase() fun ApplicationInfo.isSystemApp(): Boolean = (this.flags and ApplicationInfo.FLAG_SYSTEM != 0) || - (this.flags and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP != 0) \ No newline at end of file + (this.flags and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP != 0) + +fun AlignmentFormat.gravity(): Int = when (this.number) { + 2 -> 5 // RIGHT + 1 -> 1 // CENTER + else -> 3 // LEFT +} diff --git a/app/src/main/proto/core_preferences.proto b/app/src/main/proto/core_preferences.proto index 448b0e3e..8b9d0493 100644 --- a/app/src/main/proto/core_preferences.proto +++ b/app/src/main/proto/core_preferences.proto @@ -11,6 +11,7 @@ message CorePreferences { bool show_drawer_headings = 5; bool search_all_apps_in_drawer = 6; optional ClockType clock_type = 7; + AlignmentFormat alignment_format = 8; } enum SearchBarPosition { @@ -24,3 +25,9 @@ enum ClockType { analog = 2; binary = 3; } + +enum AlignmentFormat { + left = 0; + center = 1; + right = 2; +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c9eda3ac..6c050d69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,10 @@ Unlauncher 00:00 Sat, Apr 20 + settings + key_theme + time_format + hide_status_bar https://github.com/jkuester/unlauncher @@ -74,7 +78,7 @@ Reset Open App Change Theme - Choose Alignment + Choose App Alignment Choose Clock Type Choose Time Format Customise Apps @@ -84,11 +88,6 @@ Hide Status Bar Show Status Bar Toggle Status Bar - settings - alignment - key_theme - time_format - hide_status_bar "This action will not uninstall your apps." "It is just to confirm if you're clearing this list on purpose" Remove All Apps