bug fixes

This commit is contained in:
2026-05-29 20:30:41 -07:00
parent f87d23a922
commit 97b01437b2
5 changed files with 61 additions and 11 deletions
@@ -3,6 +3,9 @@ package com.example.countdowntimer
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
@@ -21,7 +24,9 @@ class MainActivity : AppCompatActivity() {
private lateinit var btnDurationLabel: Button
private lateinit var hsvDurationOptions: View
private lateinit var chipGroup: ChipGroup
private lateinit var vibrator: Vibrator
private var selectedDuration: Long = DEFAULT_DURATION
private var lastSelectedChipId: Int = -1
private fun durationToChipId(duration: Long): Int = when (duration) {
5000L -> R.id.chip5s
@@ -42,6 +47,16 @@ class MainActivity : AppCompatActivity() {
btnDurationLabel.text = "${selectedDuration / 1000}s"
}
private fun onChipClicked(chipId: Int) {
if (chipId == lastSelectedChipId) {
// Re-selection: provide feedback so user knows the selection is registered
vibrator.vibrate(VibrationEffect.createOneShot(15, VibrationEffect.DEFAULT_AMPLITUDE))
val originalText = btnDurationLabel.text
btnDurationLabel.text = "${selectedDuration / 1000}s!"
btnDurationLabel.postDelayed({ btnDurationLabel.text = originalText }, 300)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@@ -52,6 +67,8 @@ class MainActivity : AppCompatActivity() {
btnDurationLabel = findViewById(R.id.btnDurationLabel)
hsvDurationOptions = findViewById(R.id.hsvDurationOptions)
chipGroup = findViewById(R.id.chipGroup)
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibrator = vibratorManager.defaultVibrator
updateDurationLabel()
@@ -72,15 +89,27 @@ class MainActivity : AppCompatActivity() {
}
}
chipGroup.setOnCheckedChangeListener { _, checkedId ->
chipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
val checkedId = if (checkedIds.size > 0) checkedIds[0].toInt() else -1
if (checkedId != -1) {
selectedDuration = chipIdToDuration(checkedId)
prefs.edit().putLong(KEY_SELECTED_DURATION, selectedDuration).apply()
updateDurationLabel()
if (checkedId != lastSelectedChipId) {
// New selection: update duration, save, and refresh label
selectedDuration = chipIdToDuration(checkedId)
prefs.edit().putLong(KEY_SELECTED_DURATION, selectedDuration).apply()
updateDurationLabel()
lastSelectedChipId = checkedId
}
// Always close the panel on any selection
hsvDurationOptions.visibility = View.GONE
}
}
// Set individual click listeners to detect re-selection
findViewById<View>(R.id.chip5s).setOnClickListener { onChipClicked(R.id.chip5s) }
findViewById<View>(R.id.chip10s).setOnClickListener { onChipClicked(R.id.chip10s) }
findViewById<View>(R.id.chip15s).setOnClickListener { onChipClicked(R.id.chip15s) }
findViewById<View>(R.id.chip30s).setOnClickListener { onChipClicked(R.id.chip30s) }
findViewById<View>(R.id.btnStart).setOnClickListener {
CountdownDialog(this, selectedDuration, prefs).show()
}
+2 -2
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#81C784" />
<item android:color="#E8E8E8" />
<item android:state_checked="true" android:color="#2E7D32" />
<item android:color="#233024" />
</selector>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#C8E6C9" />
<item android:color="#2E7D32" />
</selector>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#FFFFFF" />
<item android:color="#C8E6C9" />
</selector>
+16 -5
View File
@@ -170,17 +170,22 @@
android:layout_height="wrap_content"
android:minWidth="56dp"
android:text="5s"
app:chipBackgroundColor="@color/chip_duration_bg" />
app:chipBackgroundColor="@color/chip_duration_bg"
app:chipStrokeColor="@color/chip_duration_stroke"
app:chipStrokeWidth="2dp"
android:textColor="@color/chip_duration_text" />
<com.google.android.material.chip.Chip
android:id="@+id/chip10s"
style="@style/Widget.Material3.Chip.Suggestion.Elevated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:minWidth="56dp"
android:text="10s"
app:chipBackgroundColor="@color/chip_duration_bg" />
app:chipBackgroundColor="@color/chip_duration_bg"
app:chipStrokeColor="@color/chip_duration_stroke"
app:chipStrokeWidth="2dp"
android:textColor="@color/chip_duration_text" />
<com.google.android.material.chip.Chip
android:id="@+id/chip15s"
@@ -189,7 +194,10 @@
android:layout_height="wrap_content"
android:minWidth="56dp"
android:text="15s"
app:chipBackgroundColor="@color/chip_duration_bg" />
app:chipBackgroundColor="@color/chip_duration_bg"
app:chipStrokeColor="@color/chip_duration_stroke"
app:chipStrokeWidth="2dp"
android:textColor="@color/chip_duration_text" />
<com.google.android.material.chip.Chip
android:id="@+id/chip30s"
@@ -198,7 +206,10 @@
android:layout_height="wrap_content"
android:minWidth="56dp"
android:text="30s"
app:chipBackgroundColor="@color/chip_duration_bg" />
app:chipBackgroundColor="@color/chip_duration_bg"
app:chipStrokeColor="@color/chip_duration_stroke"
app:chipStrokeWidth="2dp"
android:textColor="@color/chip_duration_text" />
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>