Skip to content

Commit

Permalink
Switch from SharedPref to proto preference storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester committed Jan 2, 2024
1 parent d505fa0 commit e3d3ab7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +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.models.Alignment
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 var alignment: Alignment,
private val unlauncherDataSource: UnlauncherDataSource
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val WORK_APP_PREFIX = "\uD83C\uDD46 " //Unicode for boxed w
private val regex = Regex("[!@#\$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>/? ]")
private var apps: List<UnlauncherApp> = listOf()
private var filteredApps: List<AppDrawerRow> = 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()
}
}
Expand Down Expand Up @@ -88,14 +87,11 @@ class AppDrawerAdapter(
updateFilteredApps(filterQuery)
}

fun setGravity(gravity: Alignment) {
this.alignment = gravity
}

@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, "")
Expand Down Expand Up @@ -159,7 +155,7 @@ class AppDrawerAdapter(

fun bind(item: UnlauncherApp) {
this.item.text = item.displayName
this.item.gravity = alignment.value;
this.item.gravity = gravity
}
}

Expand Down
30 changes: 10 additions & 20 deletions app/src/main/java/com/sduduzog/slimlauncher/adapters/HomeAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.models.Alignment
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
import com.sduduzog.slimlauncher.models.HomeApp
import com.sduduzog.slimlauncher.models.fromGravity
import com.sduduzog.slimlauncher.utils.OnLaunchAppListener
import com.sduduzog.slimlauncher.ui.main.HomeFragment
import com.sduduzog.slimlauncher.utils.gravity

class HomeAdapter(private val listener: OnLaunchAppListener)
: RecyclerView.Adapter<HomeAdapter.ViewHolder>() {
class HomeAdapter(
private val listener: HomeFragment,
private val unlauncherDataSource: UnlauncherDataSource
) : RecyclerView.Adapter<HomeAdapter.ViewHolder>() {

private var apps: List<HomeApp> = 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)
Expand All @@ -29,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
Expand All @@ -42,16 +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 = fromGravity(alignment)
}

inner class ViewHolder(mView: View) : RecyclerView.ViewHolder(mView) {
val mLabelView: TextView = mView.findViewById(R.id.home_fragment_list_item_app_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,4 +100,12 @@ class CorePreferencesRepository(
}
}
}

fun updateAlignmentFormat(alignmentFormat: AlignmentFormat) {
lifecycleScope.launch {
corePreferencesStore.updateData {
it.toBuilder().setAlignmentFormat(alignmentFormat).build()
}
}
}
}
18 changes: 0 additions & 18 deletions app/src/main/java/com/sduduzog/slimlauncher/models/Alignment.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +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.models.Alignment
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), Alignment.LEFT.value)
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()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ import com.jkuester.unlauncher.datastore.ClockType
import com.jkuester.unlauncher.datastore.SearchBarPosition
import com.jkuester.unlauncher.datastore.UnlauncherApp
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.models.Alignment
import com.sduduzog.slimlauncher.adapters.AppDrawerAdapter
import com.sduduzog.slimlauncher.adapters.HomeAdapter
import com.sduduzog.slimlauncher.models.fromGravity
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
import com.sduduzog.slimlauncher.datasource.quickbuttonprefs.QuickButtonPreferencesRepository
import com.sduduzog.slimlauncher.models.HomeApp
Expand Down Expand Up @@ -69,10 +67,7 @@ import java.util.Date
import java.util.Locale
import javax.inject.Inject

private val DEFAULT_ALIGNMENT: Int = Alignment.LEFT.value
// magic value... not sure what the point of it is.
// Happens to be the same value as the default alignment ordinal
private const val FILTER_START: Int = 3
private const val APP_TILE_SIZE: Int = 3

@AndroidEntryPoint
class HomeFragment : BaseFragment(), OnLaunchAppListener {
Expand Down Expand Up @@ -103,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, DEFAULT_ALIGNMENT)

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

Expand All @@ -118,10 +108,10 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
viewModel.apps.observe(viewLifecycleOwner) { list ->
list?.let { apps ->
adapter1.setItems(apps.filter {
it.sortingIndex < FILTER_START
it.sortingIndex < APP_TILE_SIZE
})
adapter2.setItems(apps.filter {
it.sortingIndex >= FILTER_START
it.sortingIndex >= APP_TILE_SIZE
})

// Set the home apps in the Unlauncher data
Expand All @@ -134,9 +124,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
appDrawerAdapter = AppDrawerAdapter(
AppDrawerListener(),
viewLifecycleOwner,
unlauncherAppsRepo,
unlauncherDataSource.corePreferencesRepo,
fromGravity(alignment),
unlauncherDataSource
)

setEventListeners()
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/sduduzog/slimlauncher/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
(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
}
7 changes: 7 additions & 0 deletions app/src/main/proto/core_preferences.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,3 +25,9 @@ enum ClockType {
analog = 2;
binary = 3;
}

enum AlignmentFormat {
left = 0;
center = 1;
right = 2;
}
11 changes: 5 additions & 6 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<string name="app_name" translatable="false">Unlauncher</string>
<string name="main_placeholder_clock" translatable="false">00:00</string>
<string name="main_placeholder_date" translatable="false">Sat, Apr 20</string>
<string name="prefs_settings" translatable="false">settings</string>
<string name="prefs_settings_key_theme" translatable="false">key_theme</string>
<string name="prefs_settings_key_time_format" translatable="false">time_format</string>
<string name="prefs_settings_key_toggle_status_bar" translatable="false">hide_status_bar</string>
<string name="slim_url" translatable="false">https://github.com/jkuester/unlauncher</string>

<string-array name="alignment_format_array">
Expand Down Expand Up @@ -74,7 +78,7 @@
<string name="menu_reset">Reset</string>
<string name="open_app">Open App</string>
<string name="options_fragment_change_theme">Change Theme</string>
<string name="options_fragment_choose_alignment">Choose Alignment</string>
<string name="options_fragment_choose_alignment">Choose App Alignment</string>
<string name="options_fragment_choose_clock_type">Choose Clock Type</string>
<string name="options_fragment_choose_time_format">Choose Time Format</string>
<string name="options_fragment_customise_apps">Customise Apps</string>
Expand All @@ -84,11 +88,6 @@
<string name="options_fragment_hide_status_bar">Hide Status Bar</string>
<string name="options_fragment_show_status_bar">Show Status Bar</string>
<string name="options_fragment_toggle_status_bar">Toggle Status Bar</string>
<string name="prefs_settings" translatable="false">settings</string>
<string name="prefs_settings_alignment" translatable="false">alignment</string>
<string name="prefs_settings_key_theme" translatable="false">key_theme</string>
<string name="prefs_settings_key_time_format" translatable="false">time_format</string>
<string name="prefs_settings_key_toggle_status_bar" translatable="false">hide_status_bar</string>
<string name="remove_all_apps_dialog_message">"This action will not uninstall your apps."
"It is just to confirm if you're clearing this list on purpose"</string>
<string name="remove_all_apps_dialog_title">Remove All Apps</string>
Expand Down

0 comments on commit e3d3ab7

Please sign in to comment.