11package dev.randombits.intervaltimer
22
3+ import android.annotation.SuppressLint
34import android.content.Context
45import android.os.Bundle
6+ import android.text.InputFilter
57import android.view.LayoutInflater
68import android.view.View
79import android.view.ViewGroup
810import android.widget.EditText
11+ import androidx.compose.ui.semantics.text
912import androidx.fragment.app.Fragment
1013
1114private const val ARG_PARAM1 = " active" ;
12- private const val ARG_PARAM2 = " rest"
15+ private const val ARG_PARAM2 = " rest" ;
1316
1417class 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