Skip to content

Commit

Permalink
Switch to using proto preference
Browse files Browse the repository at this point in the history
Eliminate excess state
  • Loading branch information
jkuester committed Dec 29, 2023
1 parent 5ba6747 commit 793bb75
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.jkuester.unlauncher.datastore.UnlauncherApps
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsMigrations
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsRepository
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsSerializer
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesMigrations
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesSerializer
import com.sduduzog.slimlauncher.datasource.quickbuttonprefs.QuickButtonPreferencesMigrations
Expand All @@ -30,7 +31,8 @@ private val Context.unlauncherAppsStore: DataStore<UnlauncherApps> by dataStore(

private val Context.corePreferencesStore: DataStore<CorePreferences> by dataStore(
fileName = "core_preferences.proto",
serializer = CorePreferencesSerializer
serializer = CorePreferencesSerializer,
produceMigrations = { _ -> CorePreferencesMigrations().get() }
)

class UnlauncherDataSource(context: Context, lifecycleScope: LifecycleCoroutineScope) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sduduzog.slimlauncher.datasource.coreprefs

import androidx.datastore.core.DataMigration
import com.jkuester.unlauncher.datastore.ClockType
import com.jkuester.unlauncher.datastore.CorePreferences

class CorePreferencesMigrations {
fun get(): List<DataMigration<CorePreferences>> {
return listOf(
object : DataMigration<CorePreferences> {
override suspend fun shouldMigrate(currentData: CorePreferences) = !currentData.hasClockType()

override suspend fun migrate(currentData: CorePreferences): CorePreferences {
val prefBuilder = currentData.toBuilder()
prefBuilder.clockType = ClockType.digital
return prefBuilder.build()
}

override suspend fun cleanUp() {}
}
)
}
}
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.ClockType
import com.jkuester.unlauncher.datastore.CorePreferences
import com.jkuester.unlauncher.datastore.SearchBarPosition
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -101,4 +102,12 @@ class CorePreferencesRepository(
}
}
}

fun updateClockType(clockType: ClockType) {
lifecycleScope.launch {
corePreferencesStore.updateData {
it.toBuilder().setClockType(clockType).build()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@ 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.ClockType
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class ChooseClockTypeDialog : 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_key_clock_type), 0)
val repo = unlauncherDataSource.corePreferencesRepo
val active = repo.get().clockType.number
builder.setTitle(R.string.choose_clock_type_dialog_title)
builder.setSingleChoiceItems(R.array.clock_type_array, active) {dialogInterface, i ->
dialogInterface.dismiss()
settings.edit {
putInt(getString(R.string.prefs_settings_key_clock_type), i)
}

repo.updateClockType(ClockType.forNumber(i))
}
return builder.create()
}


companion object {
fun getInstance(): ChooseClockTypeDialog{
return ChooseClockTypeDialog()
}
fun getInstance(): ChooseClockTypeDialog = ChooseClockTypeDialog()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class AnalogClockView(context: Context, attrs: AttributeSet) : ClockView(context
override fun onDraw(canvas : Canvas)
{
super.onDraw(canvas)
if (hidden) return

val calendar = Calendar.getInstance()

val hour = calendar[Calendar.HOUR]
Expand Down Expand Up @@ -86,11 +84,6 @@ class AnalogClockView(context: Context, attrs: AttributeSet) : ClockView(context
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// if hidden set to 0
if (hidden) {
setMeasuredDimension(0, 0)
return
}
val dim = max(min(suggestedMinimumWidth, suggestedMinimumHeight),
2 * radius.toInt()) + 4 * border.toInt()
val minw: Int = dim + paddingLeft + paddingRight + marginStart + marginEnd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.text.format.DateFormat
import android.util.AttributeSet
import com.sduduzog.slimlauncher.R
import java.util.Calendar
Expand All @@ -21,7 +20,6 @@ class BinaryClockView(context: Context, attrs: AttributeSet)
private var is24Hour: Boolean = false

init {
is24Hour = is24HourTimeFormat(context)
onPaint.style = Paint.Style.FILL_AND_STROKE
offPaint.style = Paint.Style.STROKE
context.theme.obtainStyledAttributes(
Expand All @@ -41,8 +39,6 @@ class BinaryClockView(context: Context, attrs: AttributeSet)
override fun onDraw(canvas : Canvas)
{
super.onDraw(canvas)
if (hidden) return

val calendar = Calendar.getInstance()

val middle = if (distance > 0) distance * 3 + bitSize * 2 else height.toFloat() / 2
Expand Down Expand Up @@ -84,31 +80,16 @@ class BinaryClockView(context: Context, attrs: AttributeSet)

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// Try for a width based on your minimum.
if (hidden) {
setMeasuredDimension(0, 0)
} else {
val minw: Int = paddingLeft + paddingRight + suggestedMinimumWidth +
12 * bitSize.toInt() + 7 * distance.toInt()
val w: Int = resolveSizeAndState(minw, widthMeasureSpec, 0)
val minw: Int = paddingLeft + paddingRight + suggestedMinimumWidth +
12 * bitSize.toInt() + 7 * distance.toInt()
val w: Int = resolveSizeAndState(minw, widthMeasureSpec, 0)

// Whatever the width is, ask for a height that lets the pie get as big as
// it can.
val minh: Int = paddingBottom + paddingTop +
4 * bitSize.toInt() + 5 * distance.toInt()
val h: Int = resolveSizeAndState(minh, heightMeasureSpec, 0)
// Whatever the width is, ask for a height that lets the pie get as big as
// it can.
val minh: Int = paddingBottom + paddingTop +
4 * bitSize.toInt() + 5 * distance.toInt()
val h: Int = resolveSizeAndState(minh, heightMeasureSpec, 0)

setMeasuredDimension(w, h)
}
}

private fun is24HourTimeFormat(context: Context): Boolean {
val settingsKey = context.getString(R.string.prefs_settings)
val timeFormatKey = context.getString(R.string.prefs_settings_key_time_format);
val preferences = context.getSharedPreferences(settingsKey, Context.MODE_PRIVATE)
return when (preferences.getInt(timeFormatKey, 0)) {
1 -> true
2 -> false
else -> DateFormat.is24HourFormat(context)
}
setMeasuredDimension(w, h)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import android.util.TypedValue
import android.view.View

abstract class ClockView(context: Context, attrs: AttributeSet) : View(context, attrs) {
protected var hidden: Boolean = true
fun setHiddenState(isHidden: Boolean) {
hidden = isHidden
}

private fun getColor(constant: Int): Int {
val tv = TypedValue()
Expand Down
50 changes: 25 additions & 25 deletions app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.jkuester.unlauncher.datastore.ClockType
import com.jkuester.unlauncher.datastore.SearchBarPosition
import com.jkuester.unlauncher.datastore.UnlauncherApp
import com.sduduzog.slimlauncher.R
Expand All @@ -45,8 +46,18 @@ import com.sduduzog.slimlauncher.ui.dialogs.RenameAppDisplayNameDialog
import com.sduduzog.slimlauncher.utils.BaseFragment
import com.sduduzog.slimlauncher.utils.OnLaunchAppListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.home_fragment_default.*
import kotlinx.android.synthetic.main.home_fragment_content.*
import kotlinx.android.synthetic.main.home_fragment_content.app_drawer_edit_text
import kotlinx.android.synthetic.main.home_fragment_content.app_drawer_fragment_list
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_analog_time
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_bin_time
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_call
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_camera
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_date
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_list
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_list_exp
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_options
import kotlinx.android.synthetic.main.home_fragment_content.home_fragment_time
import kotlinx.android.synthetic.main.home_fragment_default.home_fragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
Expand All @@ -70,14 +81,6 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
uninstallAppLauncher = registerForActivityResult(StartActivityForResult()) { refreshApps() }
}

enum class ClockType {
NOCLOCK,
DIGITAL,
ANALOG,
BINARY
}
private var clockType = ClockType.DIGITAL

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val coreRepository = unlauncherDataSource.corePreferencesRepo
val layout = when (coreRepository.get().searchBarPosition) {
Expand All @@ -95,17 +98,6 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
val alignmentKey: String = getString(R.string.prefs_settings_alignment)
val preferences = requireContext().getSharedPreferences(settingsKey, Context.MODE_PRIVATE)
val alignment = preferences.getInt(alignmentKey, 3)
clockType = ClockType.values()[preferences.getInt(getString(R.string.prefs_settings_key_clock_type), ClockType.DIGITAL.ordinal)]
home_fragment_analog_time.setHiddenState(clockType != ClockType.ANALOG)
home_fragment_bin_time.setHiddenState(clockType != ClockType.BINARY)

if (clockType != ClockType.DIGITAL) {
home_fragment_time.height = 0
}
// Should this be configurable independent of clockface?
if (clockType == ClockType.NOCLOCK) {
home_fragment_date.height = 0
}

val adapter1 = HomeAdapter(this, alignment)
val adapter2 = HomeAdapter(this, alignment)
Expand Down Expand Up @@ -143,6 +135,14 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {

val showSearchBar = unlauncherDataSource.corePreferencesRepo.showSearchField
app_drawer_edit_text.visibility = if (showSearchBar) View.VISIBLE else View.GONE

unlauncherDataSource.corePreferencesRepo.liveData().observe(viewLifecycleOwner){ corePreferences ->
val clockType = corePreferences.clockType
home_fragment_time.visibility = if(clockType == ClockType.digital) View.VISIBLE else View.GONE
home_fragment_analog_time.visibility = if(clockType == ClockType.analog) View.VISIBLE else View.GONE
home_fragment_bin_time.visibility = if(clockType == ClockType.binary) View.VISIBLE else View.GONE
home_fragment_date.visibility = if(clockType != ClockType.none) View.VISIBLE else View.GONE
}
}

override fun onStart() {
Expand Down Expand Up @@ -310,10 +310,10 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {

fun updateClock() {
updateDate()
when (clockType) {
ClockType.DIGITAL -> updateClockDigital()
ClockType.ANALOG -> home_fragment_analog_time.updateClock()
ClockType.BINARY -> home_fragment_bin_time.updateClock()
when (unlauncherDataSource.corePreferencesRepo.get().clockType) {
ClockType.digital -> updateClockDigital()
ClockType.analog -> home_fragment_analog_time.updateClock()
ClockType.binary -> home_fragment_bin_time.updateClock()
else -> {}
}
}
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/proto/core_preferences.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ message CorePreferences {
SearchBarPosition search_bar_position = 4;
bool show_drawer_headings = 5;
bool search_all_apps_in_drawer = 6;
optional ClockType clock_type = 7;
}

enum SearchBarPosition {
top = 0;
bottom = 1;
}
}

enum ClockType {
none = 0;
digital = 1;
analog = 2;
binary = 3;
}
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<string name="prefs_settings_key_toggle_status_bar" translatable="false">hide_status_bar</string>
<string name="prefs_settings_alignment" translatable="false">alignment</string>
<string name="content_description_back">Back</string>
<string name="prefs_settings_key_clock_type" translatable="false">clock_type</string>
<string name="choose_time_format_dialog_title">Choose Time Format</string>
<string name="main_fragment_options">Options</string>
<string name="options_fragment_device_settings">Device Settings</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/home_motion_scene.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_22sdp"
app:visibilityMode="ignore"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -28,6 +29,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_42sdp"
android:padding="@dimen/_4sdp"
app:visibilityMode="ignore"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -37,6 +39,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_42sdp"
app:visibilityMode="ignore"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -48,6 +51,7 @@
android:padding="@dimen/_4sdp"
android:textSize="@dimen/font_size_home_date"
android:layout_marginTop="@dimen/_94sdp"
app:visibilityMode="ignore"
app:layout_constraintEnd_toEndOf="@+id/home_fragment_time"
app:layout_constraintStart_toStartOf="@+id/home_fragment_time"
app:layout_constraintTop_toTopOf="parent" />
Expand Down Expand Up @@ -140,6 +144,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_44sdp"
android:alpha="-1"
app:visibilityMode="ignore"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
Expand All @@ -150,6 +155,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="0px"
android:alpha="0"
app:visibilityMode="ignore"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -161,6 +167,7 @@
android:layout_height="0px"
android:layout_marginTop="0px"
android:alpha="-1"
app:visibilityMode="ignore"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
Expand All @@ -170,6 +177,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="-1"
app:visibilityMode="ignore"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="@+id/home_fragment_time"
app:layout_constraintStart_toStartOf="@+id/home_fragment_time" />
Expand Down

0 comments on commit 793bb75

Please sign in to comment.