Skip to content

Commit

Permalink
Add counter
Browse files Browse the repository at this point in the history
Closes #244
  • Loading branch information
mueller-ma committed Dec 2, 2023
1 parent 16d328b commit 8d591ef
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() {
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_dice,
R.id.nav_counter,
R.id.nav_random_list,
R.id.nav_rot13,
R.id.nav_alphabet,
Expand All @@ -57,6 +58,7 @@ class MainActivity : AppCompatActivity() {
private fun getDefaultStartDestinationId(): Int {
return when (Prefs(this).startPage) {
getString(R.string.menu_dice_value) -> R.id.nav_dice
getString(R.string.menu_counter_value) -> R.id.nav_counter
getString(R.string.menu_random_list_value) -> R.id.nav_random_list
getString(R.string.menu_rot13_value) -> R.id.nav_rot13
getString(R.string.menu_alphabet_value) -> R.id.nav_alphabet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.muellerma.tabletoptools.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doOnTextChanged
import com.github.muellerma.tabletoptools.databinding.CounterBinding
import com.github.muellerma.tabletoptools.databinding.FragmentCounterBinding


class CounterFragment : AbstractBaseFragment() {
private lateinit var binding: FragmentCounterBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentCounterBinding.inflate(inflater, container, false)

setupScreenOn(binding.root)

return binding.root
}

override fun onResume() {
super.onResume()

setupCounter(1, binding.counter1)
setupCounter(2, binding.counter2)
setupCounter(3, binding.counter3)
setupCounter(4, binding.counter4)
setupCounter(5, binding.counter5)
}

private fun setupCounter(id: Int, counter: CounterBinding) {
fun getInput() = counter.count.text.toString().toIntOrNull() ?: 1

counter.less.setOnClickListener {
counter.count.setText(getInput().dec().toString())
}
counter.more.setOnClickListener {
counter.count.setText(getInput().inc().toString())
}

val prefs = prefs.Counter(id)
counter.label.setText(prefs.label)
counter.label.doOnTextChanged { text, _, _, _ -> prefs.label = text?.toString() }

counter.count.setText(prefs.count.toString())
counter.count.doOnTextChanged { _, _, _, _ -> prefs.count = getInput() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.preference.PreferenceManager
import com.github.muellerma.tabletoptools.R
import com.github.muellerma.tabletoptools.ui.fragments.TimerFragment

class Prefs(private val context: Context) {
class Prefs(val context: Context) {
private var sharedPrefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

val maxDiceCount: Int
Expand Down Expand Up @@ -62,4 +62,22 @@ class Prefs(private val context: Context) {
else -> null
}
}

inner class Counter(private val id: Int) {
var label: String?
get() = sharedPrefs.getString("counter_${id}_label", null)
set(value) {
sharedPrefs.edit {
putString("counter_${id}_label", value)
}
}

var count: Int
get() = sharedPrefs.getInt("counter_${id}_count", 1)
set(value) {
sharedPrefs.edit {
putInt("counter_${id}_count", value)
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_exposure_plus_1_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,7L8,7v4L4,11v2h4v4h2v-4h4v-2h-4L10,7zM20,18h-2L18,7.38L15,8.4L15,6.7L19.7,5h0.3v13z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_keyboard_arrow_left_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15.41,16.59L10.83,12l4.58,-4.59L14,6l-6,6 6,6 1.41,-1.41z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M8.59,16.59L13.17,12 8.59,7.41 10,6l6,6 -6,6 -1.41,-1.41z"/>
</vector>
51 changes: 51 additions & 0 deletions app/src/main/res/layout/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/counter_label"
android:gravity="center"
android:maxLines="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<ImageButton
android:id="@+id/less"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_keyboard_arrow_left_24"
app:layout_constraintTop_toBottomOf="@+id/label"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp" />

<EditText
android:id="@+id/count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="1"
android:gravity="center"
android:maxLines="1"
android:inputType="number"
app:layout_constraintTop_toBottomOf="@+id/label"
android:layout_marginTop="16dp"
app:layout_constraintStart_toEndOf="@+id/less"
app:layout_constraintEnd_toStartOf="@+id/more"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />

<ImageButton
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_keyboard_arrow_right_24"
app:layout_constraintTop_toBottomOf="@+id/label"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
21 changes: 21 additions & 0 deletions app/src/main/res/layout/fragment_counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:orientation="vertical">

<include android:id="@+id/counter1" layout="@layout/counter" android:layout_margin="8dp" />
<include android:id="@+id/counter2" layout="@layout/counter" android:layout_margin="8dp" />
<include android:id="@+id/counter3" layout="@layout/counter" android:layout_margin="8dp" />
<include android:id="@+id/counter4" layout="@layout/counter" android:layout_margin="8dp" />
<include android:id="@+id/counter5" layout="@layout/counter" android:layout_margin="8dp" />

</LinearLayout>
</ScrollView>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
android:id="@+id/nav_dice"
android:icon="@drawable/ic_menu_dice"
android:title="@string/menu_dice" />
<item
android:id="@+id/nav_counter"
android:icon="@drawable/baseline_exposure_plus_1_24"
android:title="@string/menu_counter" />
<item
android:id="@+id/nav_random_list"
android:icon="@drawable/ic_baseline_format_list_bulleted_24"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
android:label="@string/menu_dice"
tools:layout="@layout/fragment_dice" />

<fragment
android:id="@+id/nav_counter"
android:name="com.github.muellerma.tabletoptools.ui.fragments.CounterFragment"
android:label="@string/menu_counter"
tools:layout="@layout/fragment_counter" />

<fragment
android:id="@+id/nav_random_list"
android:name="com.github.muellerma.tabletoptools.ui.fragments.RandomListFragment"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</string-array>
<string-array name="pages">
<item>@string/menu_dice</item>
<item>@string/menu_counter</item>
<item>@string/menu_random_list</item>
<item>@string/menu_rot13</item>
<item>@string/menu_alphabet</item>
Expand All @@ -19,6 +20,7 @@
</string-array>
<string-array name="pages_values">
<item>@string/menu_dice_value</item>
<item>@string/menu_counter_value</item>
<item>@string/menu_random_list_value</item>
<item>@string/menu_rot13_value</item>
<item>@string/menu_alphabet_value</item>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<item quantity="other">All latin letters will be rotated by %d places (can be set below)</item>
</plurals>

<!-- Counter -->
<string name="counter_label">Counter name</string>
<string name="menu_counter">Counter</string>

<!-- Alphabet -->
<string name="menu_alphabet">Alphabet</string>
<string name="alphabet_letter">Letter</string>
Expand All @@ -42,6 +46,7 @@
<string name="menu_alphabet_value" translatable="false">alphabet</string>
<string name="menu_number_converter_value" translatable="false">converter</string>
<string name="menu_prime_fac_value" translatable="false">prime_fac</string>
<string name="menu_counter_value" translatable="false">counter</string>

<!-- Dice -->
<string name="menu_dice">Dice</string>
Expand Down
Loading

0 comments on commit 8d591ef

Please sign in to comment.