Use TimePickerButton

This commit is contained in:
Tibo De Peuter 2023-05-01 13:32:52 +02:00
parent 3c73c5a853
commit 89b9c23123
2 changed files with 20 additions and 28 deletions

View file

@ -16,7 +16,6 @@ import androidx.compose.ui.unit.dp
import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds
import java.util.* import java.util.*
// TODO codeduplicatie met Tibo, later wegdoen
@Composable @Composable
fun TimePickerButton( fun TimePickerButton(
hoursMinutesSeconds: HoursMinutesSeconds, hoursMinutesSeconds: HoursMinutesSeconds,
@ -37,11 +36,13 @@ fun TimePickerButton(
} }
} }
// TODO idem codedup Tibo
private fun pickDuration(context: Context, listener: OnTimeSetListener) { private fun pickDuration(context: Context, listener: OnTimeSetListener) {
val mCalendar = Calendar.getInstance() val timePickerDialog = TimePickerDialog(
val mHour = mCalendar[Calendar.HOUR] context,
val mMinute = mCalendar[Calendar.MINUTE] listener,
val mTimePickerDialog = TimePickerDialog(context, listener, mHour, mMinute, true) 0,
mTimePickerDialog.show() 0,
true
)
timePickerDialog.show()
} }

View file

@ -1,7 +1,6 @@
package be.ugent.sel.studeez.screens.timer_selection package be.ugent.sel.studeez.screens.timer_selection
import android.app.TimePickerDialog import android.widget.TimePicker
import android.content.Context
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -9,10 +8,12 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import be.ugent.sel.studeez.R import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.NotInternationalisedButton
import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate
import be.ugent.sel.studeez.common.composable.StealthButton import be.ugent.sel.studeez.common.composable.StealthButton
import be.ugent.sel.studeez.common.composable.TimePickerButton
import be.ugent.sel.studeez.common.composable.TimerEntry import be.ugent.sel.studeez.common.composable.TimerEntry
import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds
import be.ugent.sel.studeez.data.local.models.timer_functional.Time
import be.ugent.sel.studeez.data.local.models.timer_info.CustomTimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.CustomTimerInfo
import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo
import be.ugent.sel.studeez.resources import be.ugent.sel.studeez.resources
@ -22,7 +23,7 @@ import kotlinx.coroutines.flow.flowOf
data class TimerSelectionActions( data class TimerSelectionActions(
val getAllTimers: () -> Flow<List<TimerInfo>>, val getAllTimers: () -> Flow<List<TimerInfo>>,
val startSession: (TimerInfo) -> Unit, val startSession: (TimerInfo) -> Unit,
val pickDuration: (Context) -> Unit, val pickDuration: (TimePicker?, Int, Int) -> Unit,
val customTimeStudyTime: Int val customTimeStudyTime: Int
) )
@ -33,19 +34,9 @@ fun getTimerSelectionActions(
return TimerSelectionActions( return TimerSelectionActions(
getAllTimers = viewModel::getAllTimers, getAllTimers = viewModel::getAllTimers,
startSession = { viewModel.startSession(open, it) }, startSession = { viewModel.startSession(open, it) },
pickDuration = { context -> pickDuration = { _, hour: Int, minute: Int ->
val dialog = TimePickerDialog(
context,
{ _, hour: Int, minute: Int ->
viewModel.customTimerStudyTime.value = hour * 60 * 60 + minute * 60 viewModel.customTimerStudyTime.value = hour * 60 * 60 + minute * 60
}, },
0,
0,
true
)
dialog.show()
},
customTimeStudyTime = viewModel.customTimerStudyTime.value customTimeStudyTime = viewModel.customTimerStudyTime.value
) )
} }
@ -103,7 +94,7 @@ fun CustomTimerEntry(
description = resources().getString(R.string.custom_description), description = resources().getString(R.string.custom_description),
studyTime = timerSelectionActions.customTimeStudyTime studyTime = timerSelectionActions.customTimeStudyTime
) )
val context = LocalContext.current val hms: HoursMinutesSeconds = Time(timerInfo.studyTime).getAsHMS()
TimerEntry( TimerEntry(
timerInfo = timerInfo, timerInfo = timerInfo,
@ -114,9 +105,9 @@ fun CustomTimerEntry(
) )
}, },
rightButton = { rightButton = {
NotInternationalisedButton( TimePickerButton(
text = String.format("%02d:%02d", timerInfo.studyTime / 3600, (timerInfo.studyTime % 3600) / 60), hoursMinutesSeconds = hms,
onClick = { timerSelectionActions.pickDuration(context) } onTimeSetListener = timerSelectionActions.pickDuration
) )
} }
) )
@ -126,7 +117,7 @@ fun CustomTimerEntry(
@Composable @Composable
fun TimerSelectionPreview() { fun TimerSelectionPreview() {
TimerSelectionScreen( TimerSelectionScreen(
timerSelectionActions = TimerSelectionActions({ flowOf() }, {}, { {} }, 0), timerSelectionActions = TimerSelectionActions({ flowOf() }, {}, { _, _, _ -> {} }, 0),
popUp = {} popUp = {}
) )
} }