Skip to content

Commit

Permalink
Add preference to supply the maximum size of a grid cell
Browse files Browse the repository at this point in the history
  • Loading branch information
meikpiep committed Jan 21, 2024
1 parent 15060db commit a6b0e14
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added all difficulty levels of 4x4 and 5x5 grids.
- Added support of dynamic colors.
- Added new preference to set the maximum size of a grid cell, from 24 dp to 96 dp.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ApplicationPreferencesImpl(
return enumValueOf(themePref)
}

override fun maximumCellSizeInDP(): Int {
return preferences.getInt("maximumCellSize", 72)
}

override fun showDupedDigits(): Boolean {
return preferences.getBoolean("duplicates", true)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.piepmeyer.gauguin.ui.grid

fun interface GridCellSizeListener {
fun cellSizeChanged(cellSizePercent: Int)
fun cellSizeChanged()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package org.piepmeyer.gauguin.ui.grid

class GridCellSizeService {
var cellSizePercent = 100
set(cellSizePercent) {
field = cellSizePercent
listener?.cellSizeChanged(cellSizePercent)
}

private var listener: GridCellSizeListener? = null

fun setCellSizeListener(listener: GridCellSizeListener?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class GridUI : View, OnTouchListener, GridView, KoinComponent {
private val paintHolder = GridPaintHolder(this)
var isPreviewMode = false
private var previewStillCalculating = false
private var cellSizePercent = 100
private var maximumCellSizeInDP = gridUiInjectionStrategy.maximumCellSizeInDP()

private var padding = Pair(0, 0)

constructor(context: Context?) : super(context) {
Expand Down Expand Up @@ -98,11 +99,9 @@ class GridUI : View, OnTouchListener, GridView, KoinComponent {
widthMeasureSpec: Int,
heightMeasureSpec: Int,
) {
val measuredWidth = measure(widthMeasureSpec)
val measuredHeight = measure(heightMeasureSpec)
setMeasuredDimension(
measuredWidth * cellSizePercent / 100,
measuredHeight * cellSizePercent / 100,
measure(widthMeasureSpec),
measure(heightMeasureSpec),
)
}

Expand Down Expand Up @@ -225,8 +224,7 @@ class GridUI : View, OnTouchListener, GridView, KoinComponent {

val cellSizeSquare = min(cellSizeWidth, cellSizeHeight)

// 96dp is the current size of a large FAB in Material 3, which should be big enough.
val maximumCellSize = 96 * resources.displayMetrics.density
val maximumCellSize = maximumCellSizeInDP * resources.displayMetrics.density

return min(cellSizeSquare, maximumCellSize).toInt()
}
Expand Down Expand Up @@ -275,10 +273,6 @@ class GridUI : View, OnTouchListener, GridView, KoinComponent {
this.previewStillCalculating = previewStillCalculating
}

fun setCellSizePercent(cellSizePercent: Int) {
this.cellSizePercent = cellSizePercent
}

companion object {
const val BORDER_WIDTH = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class GridUiInjectionDefaultStrategy : GridUiInjectionStrategy, KoinComponent {
override fun numeralSystem() = game.grid.variant.options.numeralSystem

override fun markDuplicatedInRowOrColumn() = applicationPreferences.showDupedDigits()

override fun maximumCellSizeInDP(): Int = applicationPreferences.maximumCellSizeInDP()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class GridUiInjectionEditModeStrategy : GridUiInjectionStrategy {
override fun numeralSystem() = NumeralSystem.Decimal

override fun markDuplicatedInRowOrColumn() = false

override fun maximumCellSizeInDP(): Int = 48
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ interface GridUiInjectionStrategy {
fun showOperators(): Boolean

fun markDuplicatedInRowOrColumn(): Boolean

fun maximumCellSizeInDP(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ class MainActivity : AppCompatActivity(), GridCreationListener {
ft.replace(R.id.gameTopFrame, topFragment)
ft.commit()

cellSizeService.setCellSizeListener { cellSizePercent ->
binding.gridview.setCellSizePercent(cellSizePercent)
cellSizeService.setCellSizeListener {
binding.gridview.invalidate()
binding.gridview.forceLayout()
binding.gridview.invalidate()
}

game.setSolvedHandler { reveal -> gameSolved(reveal) }
Expand All @@ -91,7 +92,7 @@ class MainActivity : AppCompatActivity(), GridCreationListener {

specialListener =
OnSharedPreferenceChangeListener { _: SharedPreferences, key: String? ->
if (key == "theme") {
if (key == "theme" || key == "maximumCellSize") {
this.recreate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.view.marginStart
import androidx.core.view.updateLayoutParams
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.slider.Slider
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.DividerDrawerItem
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem
Expand All @@ -27,14 +26,11 @@ import org.piepmeyer.gauguin.ui.LoadGameListActivity
import org.piepmeyer.gauguin.ui.MainDialogs
import org.piepmeyer.gauguin.ui.SettingsActivity
import org.piepmeyer.gauguin.ui.StatisticsActivity
import org.piepmeyer.gauguin.ui.grid.GridCellSizeService
import kotlin.math.roundToInt

class MainNavigationViewService(
private val mainActivity: MainActivity,
private val binding: ActivityMainBinding,
) : KoinComponent {
private val cellSizeService: GridCellSizeService by inject()
private val savedGamesService: SavedGamesService by inject()

fun initialize() {
Expand Down Expand Up @@ -173,18 +169,6 @@ class MainNavigationViewService(
true
}

val gridScaleSlider = header.findViewById<Slider>(R.id.gridScaleSlider)
gridScaleSlider.addOnChangeListener(
Slider.OnChangeListener { _: Slider?, value: Float, fromUser: Boolean ->
if (fromUser) {
cellSizeService.cellSizePercent = value.roundToInt()
}
},
)

cellSizeService.cellSizePercent = 100
gridScaleSlider.value = cellSizeService.cellSizePercent.toFloat()

binding.mainBottomAppBar.setOnMenuItemClickListener(
BottomAppBarItemClickListener(
binding.mainConstraintLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@
app:layout_constraintTop_toTopOf="parent"
/>

<com.google.android.material.slider.Slider
android:id="@+id/gridScaleSlider"
android:layout_width="match_parent"
android:layout_height="18dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/navigation_drawer_picture"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:valueFrom="50.0"
android:valueTo="100.0"
android:stepSize="10.0" />

<ImageView
android:id="@+id/navigation_drawer_app_icon"
android:layout_width="48dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,4 @@
android:textColor="@color/CustomColor1"
style="@style/TextAppearance.Material3.HeadlineMedium" />

<com.google.android.material.slider.Slider
android:id="@+id/gridScaleSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/navigation_drawer_picture"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:valueFrom="50.0"
android:valueTo="100.0"
android:stepSize="10.0" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions gauguin-app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,6 @@
<string name="setting_fill_single_cages_at_start_title">Fülle Einzelgrppen beim Start</string>
<string name="setting_fill_single_cages_at_start_summary_on">Beim Spielstart werden alle Einzelgruppen automatisch gefüllt.</string>
<string name="setting_fill_single_cages_at_start_summary_off">Beim Spielstart bleiben Einzelgruppen leer.</string>
<string name="setting_maximum_cell_size">Maximale Größe einer Zelle</string>
<string name="setting_maximum_cell_size_summary">Maximale Größe einer Zelle des Spielfelds in \'dp\'</string>
</resources>
2 changes: 2 additions & 0 deletions gauguin-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@
<string name="setting_fill_single_cages_at_start_title">Fill single cages at start</string>
<string name="setting_fill_single_cages_at_start_summary_on">Single cages will be filled automatically.</string>
<string name="setting_fill_single_cages_at_start_summary_off">Single cages will be left empty</string>
<string name="setting_maximum_cell_size">Maximum cell size</string>
<string name="setting_maximum_cell_size_summary">Maximum cell size of the grid in \'dp\'</string>
</resources>
13 changes: 11 additions & 2 deletions gauguin-app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory android:title="@string/settings_group_display" android:key="Display">
<ListPreference
android:key="theme"
android:title="@string/setting_theme_title"
android:entries="@array/setting_theme_entries"
android:entryValues="@array/setting_theme_values"
android:defaultValue="DARK" />
<SeekBarPreference
android:key="maximumCellSize"
android:title="@string/setting_maximum_cell_size"
android:summary="@string/setting_maximum_cell_size_summary"
android:defaultValue="72"
android:max="96"
app:min="24"
app:showSeekBarValue="true"
/>
<CheckBoxPreference
android:key="pencilatstart"
android:title="@string/setting_pencil_at_start_title"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.piepmeyer.gauguin.options.SingleCageUsage
interface ApplicationPreferences {
val theme: Theme

fun maximumCellSizeInDP(): Int

fun showDupedDigits(): Boolean

fun setShowOperators(showOperators: Boolean)
Expand Down

0 comments on commit a6b0e14

Please sign in to comment.