Skip to content

Commit c917c16

Browse files
committed
fix: minor input bugs
1 parent 6f484bf commit c917c16

4 files changed

Lines changed: 130 additions & 107 deletions

File tree

app/src/main/java/dev/randombits/intervaltimer/HiitTimer.kt

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,106 @@ enum class TimerStatus {
88
REST
99
}
1010

11-
abstract class HiitTimer(val activeTime: Int, val restTime: Int) {
12-
11+
abstract class HiitTimer(private val activeTime: Int, val restTime: Int) {
1312
private var set: Int = 0;
1413
private var status: TimerStatus = TimerStatus.PREPARE;
1514
private var timer: CountDownTimer? = null;
1615
private var remainingTime: Long = 0;
16+
private var isRunning = false;
1717

18-
public abstract fun onUpdate(millisRemaining: Long);
19-
public abstract fun onStatusChange(status: TimerStatus, set: Int);
18+
abstract fun onUpdate(millisRemaining: Long);
19+
abstract fun onStatusChange(status: TimerStatus, set: Int);
2020

21-
public fun start() {
21+
fun start() {
22+
isRunning = true;
2223
status = TimerStatus.PREPARE;
2324
onStatusChange(status, set);
25+
2426
timer = object : CountDownTimer(5 * 1000, 50) {
2527
override fun onTick(millisRemaining: Long) {
26-
onUpdate(millisRemaining);
28+
if (isRunning)
29+
onUpdate(millisRemaining);
30+
else
31+
this.cancel();
2732
}
2833

2934
override fun onFinish() {
30-
startActive();
35+
if (isRunning)
36+
startActive();
37+
else
38+
this.cancel();
3139
}
3240
}.start()
3341
}
3442

35-
36-
public fun pause() {
43+
fun pause() {
44+
isRunning = false;
3745
timer?.cancel();
3846
}
3947

40-
public fun resume() {
48+
fun resume() {
49+
isRunning = true;
50+
4151
when (status) {
4252
TimerStatus.PREPARE -> start();
4353
TimerStatus.ACTIVE -> startActive();
4454
TimerStatus.REST -> start();
4555
}
4656
}
4757

48-
public fun stop() {
58+
fun stop() {
59+
isRunning = false;
4960
timer?.cancel();
5061
timer = null;
5162
}
5263

5364
private fun startActive() {
5465
var time = remainingTime;
66+
5567
if (remainingTime == 0L) {
5668
time = activeTime.toLong() * 1000;
5769
status = TimerStatus.ACTIVE;
5870
set++;
5971
onStatusChange(status, set);
6072
}
73+
6174
timer = object : CountDownTimer(time, 50) {
6275
override fun onTick(millisRemaining: Long) {
6376
remainingTime = millisRemaining;
64-
onUpdate(millisRemaining);
77+
if (isRunning)
78+
onUpdate(millisRemaining);
79+
else
80+
this.cancel();
6581
}
6682

6783
override fun onFinish() {
6884
remainingTime = 0;
69-
startRest();
85+
if (isRunning)
86+
startRest();
87+
else
88+
this.cancel();
7089
}
71-
}.start()
90+
}.start();
7291
}
7392

7493
private fun startRest() {
7594
status = TimerStatus.REST;
7695
onStatusChange(status, set);
96+
7797
timer = object : CountDownTimer(restTime.toLong() * 1000, 50) {
7898
override fun onTick(millisRemaining: Long) {
79-
onUpdate(millisRemaining);
99+
if (isRunning)
100+
onUpdate(millisRemaining);
101+
else
102+
this.cancel();
80103
}
81104

82105
override fun onFinish() {
83-
startActive();
106+
if (isRunning)
107+
startActive();
108+
else
109+
this.cancel();
84110
}
85-
}.start()
111+
}.start();
86112
}
87113
}

app/src/main/java/dev/randombits/intervaltimer/MainActivity.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import android.os.Handler
77
import android.os.Looper
88
import androidx.appcompat.app.AppCompatActivity
99

10-
1110
class MainActivity : AppCompatActivity() {
1211
private var alarmSound = 0
1312
private var playingSound = 0;
1413

15-
val attributes = AudioAttributes.Builder()
14+
private val attributes: AudioAttributes = AudioAttributes.Builder()
1615
.setUsage(AudioAttributes.USAGE_MEDIA)
1716
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
1817
.build()
19-
val soundPool = SoundPool.Builder()
18+
19+
private val soundPool: SoundPool = SoundPool.Builder()
2020
.setAudioAttributes(attributes)
2121
.setMaxStreams(1)
2222
.build()
@@ -25,7 +25,6 @@ class MainActivity : AppCompatActivity() {
2525
super.onCreate(savedInstanceState)
2626
setContentView(R.layout.layout_main);
2727

28-
2928
val prefs = getSharedPreferences(packageName, MODE_PRIVATE);
3029
val activeTime = prefs.getInt("activeTime", 45);
3130
val restTime = prefs.getInt("restTime", 15);
@@ -35,6 +34,7 @@ class MainActivity : AppCompatActivity() {
3534
.replace(R.id.mainFrame, SettingsFragment.newInstance(activeTime, restTime))
3635
.commit();
3736
}
37+
3838
alarmSound = soundPool.load(this, R.raw.threesecbeep, 1);
3939
}
4040

@@ -57,22 +57,18 @@ class MainActivity : AppCompatActivity() {
5757
}
5858

5959
fun cancelAlarm() {
60-
if (playingSound == 0) {
60+
if (playingSound == 0)
6161
return;
62-
}
62+
6363
soundPool.stop(playingSound);
6464
playingSound = 0;
6565
}
6666

6767
fun soundAlarm() {
68-
if (playingSound > 0) {
68+
if (playingSound > 0)
6969
return;
70-
}
71-
playingSound = soundPool.play(alarmSound, 1f, 1f, 1, 0, 1f);
7270

73-
Handler(Looper.getMainLooper()).postDelayed(
74-
{ playingSound = 0 },
75-
3000
76-
)
71+
playingSound = soundPool.play(alarmSound, 1f, 1f, 1, 0, 1f);
72+
Handler(Looper.getMainLooper()).postDelayed({ playingSound = 0 }, 3000);
7773
}
7874
}

app/src/main/java/dev/randombits/intervaltimer/SettingsFragment.kt

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,115 @@
11
package dev.randombits.intervaltimer
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.os.Bundle
6+
import android.text.InputFilter
57
import android.view.LayoutInflater
68
import android.view.View
79
import android.view.ViewGroup
810
import android.widget.EditText
11+
import androidx.compose.ui.semantics.text
912
import androidx.fragment.app.Fragment
1013

1114
private const val ARG_PARAM1 = "active";
12-
private const val ARG_PARAM2 = "rest"
15+
private const val ARG_PARAM2 = "rest";
1316

1417
class SettingsFragment : Fragment() {
15-
// TODO: Rename and change types of parameters
16-
private var activeTime: Int? = 45
17-
private var restTime: Int? = 15
18+
private var defaultActiveTime: Int = 45;
19+
private var defaultRestTime: Int = 15;
1820
private var mainActivity: MainActivity? = null;
1921
private var activeInput: EditText? = null;
2022
private var restInput: EditText? = null;
2123

24+
private val rangeFilter = InputFilter { source, _, _, dest, _, _ ->
25+
when (val input = (dest.toString() + source.toString()).toIntOrNull()) {
26+
null, 0 -> "";
27+
else -> if (input >= 1) null else "";
28+
}
29+
};
30+
2231
override fun onAttach(context: Context) {
2332
super.onAttach(context);
2433
mainActivity = context as MainActivity;
2534
}
2635

2736
override fun onCreate(savedInstanceState: Bundle?) {
28-
super.onCreate(savedInstanceState)
37+
super.onCreate(savedInstanceState);
2938

3039
arguments?.let {
31-
activeTime = it.getInt(ARG_PARAM1)
32-
restTime = it.getInt(ARG_PARAM2)
40+
defaultActiveTime = it.getInt(ARG_PARAM1);
41+
defaultRestTime = it.getInt(ARG_PARAM2);
3342
}
3443
}
3544

3645
override fun onCreateView(
3746
inflater: LayoutInflater, container: ViewGroup?,
3847
savedInstanceState: Bundle?
39-
): View? {
48+
): View {
4049
val view = inflater.inflate(R.layout.fragment_settings, container, false) as View;
50+
setupFields(view);
51+
return view;
52+
}
4153

42-
activeInput = view.findViewById(R.id.activeTime)
43-
activeInput!!.setText(activeTime.toString())
44-
restInput = view.findViewById(R.id.restTime)
45-
restInput!!.setText(restTime.toString())
54+
@SuppressLint("SetTextI18n")
55+
private fun setupFields(view: View) {
56+
activeInput = view.findViewById(R.id.activeTime);
57+
activeInput!!.setText(defaultActiveTime.toString());
58+
activeInput!!.filters = arrayOf(rangeFilter);
59+
restInput = view.findViewById(R.id.restTime);
60+
restInput!!.setText(defaultRestTime.toString());
61+
restInput!!.filters = arrayOf(rangeFilter);
4662

4763
view.findViewById<View>(R.id.activeTime_less).setOnClickListener {
48-
val activeTime = activeInput!!.text.toString();
49-
if (activeTime.isBlank()) {
50-
activeInput!!.setText("45");
51-
} else {
52-
if (Integer.parseInt(activeTime) >= 5) {
53-
activeInput!!.setText((Integer.parseInt(activeTime) - 5).toString());
54-
}
55-
}
56-
57-
}
58-
view.findViewById<View>(R.id.activeTime_more).setOnClickListener {
59-
val activeTime = activeInput!!.text.toString();
60-
if (activeTime.isBlank()) {
61-
activeInput!!.setText("45");
62-
} else {
63-
activeInput!!.setText((Integer.parseInt(activeTime) + 5).toString());
64-
}
64+
decreaseTimerValue(activeInput!!, defaultActiveTime, -5);
6565
}
6666

6767
view.findViewById<View>(R.id.restTime_less).setOnClickListener {
68-
val activeTime = restInput!!.text.toString();
69-
if (activeTime.isBlank()) {
70-
restInput!!.setText("45");
71-
} else {
72-
if (Integer.parseInt(activeTime) >= 5) {
73-
restInput!!.setText((Integer.parseInt(activeTime) - 5).toString());
74-
}
75-
}
68+
decreaseTimerValue(restInput!!, defaultRestTime, -5);
69+
}
7670

71+
view.findViewById<View>(R.id.activeTime_more).setOnClickListener {
72+
increaseTimerValue(activeInput!!, defaultActiveTime, 5);
7773
}
74+
7875
view.findViewById<View>(R.id.restTime_more).setOnClickListener {
79-
val activeTime = restInput!!.text.toString();
80-
if (activeTime.isBlank()) {
81-
restInput!!.setText("45");
82-
} else {
83-
restInput!!.setText((Integer.parseInt(activeTime) + 5).toString());
84-
}
76+
increaseTimerValue(restInput!!, defaultRestTime, 5);
8577
}
8678

87-
view.findViewById<View>(R.id.beginBtn).setOnClickListener { startTimer() }
88-
return view;
79+
view.findViewById<View>(R.id.beginBtn).setOnClickListener { startTimer(); };
80+
}
81+
82+
@SuppressLint("SetTextI18n")
83+
private fun decreaseTimerValue(editText: EditText, defaultValue: Int, change: Int) {
84+
val currentValue = editText.text.toString().toIntOrNull() ?: defaultValue;
85+
val newValue = (currentValue + change).coerceAtLeast(5);
86+
editText.setText(newValue.toString());
87+
}
88+
89+
@SuppressLint("SetTextI18n")
90+
private fun increaseTimerValue(editText: EditText, defaultValue: Int, change: Int) {
91+
val currentValue = editText.text.toString().toIntOrNull() ?: defaultValue;
92+
val newValue = currentValue + change;
93+
editText.setText(newValue.toString());
8994
}
9095

9196
override fun onSaveInstanceState(outState: Bundle) {
9297
outState.putInt(
9398
"settings.activeTime",
9499
Integer.parseInt(activeInput!!.text.toString())
95-
)
100+
);
101+
96102
outState.putInt(
97103
"settings.restTime",
98104
Integer.parseInt(restInput!!.text.toString())
99-
)
105+
);
106+
100107
super.onSaveInstanceState(outState);
101108
}
102109

103110
override fun onDestroy() {
104111
super.onDestroy();
112+
105113
mainActivity!!.savePreferences(
106114
Integer.parseInt(activeInput!!.text.toString()),
107115
Integer.parseInt(restInput!!.text.toString())
@@ -113,10 +121,10 @@ class SettingsFragment : Fragment() {
113121
fun newInstance(active: Int, rest: Int) =
114122
SettingsFragment().apply {
115123
arguments = Bundle().apply {
116-
putInt(ARG_PARAM1, active)
117-
putInt(ARG_PARAM2, rest)
124+
putInt(ARG_PARAM1, active);
125+
putInt(ARG_PARAM2, rest);
118126
}
119-
}
127+
};
120128
}
121129

122130
private fun startTimer() {

0 commit comments

Comments
 (0)