Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vsnappy1 committed May 25, 2023
1 parent ef814fa commit 785a331
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import androidx.compose.ui.unit.dp
import com.vsnappy1.composecalendarexample.ui.theme.ComposeCalendarExampleTheme
import com.vsnappy1.datepicker.DatePicker
import com.vsnappy1.timepicker.TimePicker
import com.vsnappy1.timepicker.data.model.ComposeTimePickerTime
import com.vsnappy1.timepicker.data.model.TimePickerTime
import com.vsnappy1.timepicker.enums.MinuteGap

class MainActivity : ComponentActivity() {
Expand All @@ -45,7 +45,7 @@ class MainActivity : ComponentActivity() {
onTimeSelected = { hour, minute ->
text = "$hour : $minute "
},
time = ComposeTimePickerTime(5, 5),
time = TimePickerTime(5, 5),
minuteGap = MinuteGap.FIVE
)
Text(text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import com.vsnappy1.component.AnimatedFadeVisibility
import com.vsnappy1.composedatepicker.R
import com.vsnappy1.datepicker.ui.model.DatePickerConfiguration
import com.vsnappy1.datepicker.data.Constant
import com.vsnappy1.datepicker.data.model.ComposeDatePickerDate
import com.vsnappy1.datepicker.data.model.DatePickerDate
import com.vsnappy1.datepicker.data.model.DefaultDate
import com.vsnappy1.datepicker.data.model.Month
import com.vsnappy1.datepicker.data.model.SelectionLimiter
Expand All @@ -67,7 +67,7 @@ import kotlin.math.ceil
fun DatePicker(
modifier: Modifier = Modifier,
onDateSelected: (Int, Int, Int) -> Unit,
date: ComposeDatePickerDate = DefaultDate.defaultDate,
date: DatePickerDate = DefaultDate.defaultDate,
selectionLimiter: SelectionLimiter = SelectionLimiter(),
configuration: DatePickerConfiguration = DatePickerConfiguration.Builder().build(),
id: Int = 1
Expand Down Expand Up @@ -363,7 +363,7 @@ private fun DateViewBodyItem(
val day = value - currentVisibleMonth.firstDayOfMonth.number + 2
val isSelected = day == selectedDayOfMonth && selectedMonth == currentVisibleMonth
val isWithinRange = selectionLimiter.isWithinRange(
ComposeDatePickerDate(
DatePickerDate(
selectedYear,
currentVisibleMonth.number,
day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package com.vsnappy1.datepicker.data.model

import android.icu.util.Calendar

data class ComposeDatePickerDate(
data class DatePickerDate(
val year: Int,
val month: Int,
val day: Int
)

object DefaultDate {
private val calendar = Calendar.getInstance()
val defaultDate = ComposeDatePickerDate(
val defaultDate = DatePickerDate(
calendar[Calendar.YEAR],
calendar[Calendar.MONTH],
calendar[Calendar.DAY_OF_MONTH]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.vsnappy1.extension.isEqual
import java.util.Calendar

class SelectionLimiter(
private val fromDate: ComposeDatePickerDate? = null,
private val toDate: ComposeDatePickerDate? = null
private val fromDate: DatePickerDate? = null,
private val toDate: DatePickerDate? = null
) {
fun isWithinRange(date: ComposeDatePickerDate): Boolean {
fun isWithinRange(date: DatePickerDate): Boolean {


val fromDate =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vsnappy1.datepicker.data.Constant
import com.vsnappy1.datepicker.data.model.ComposeDatePickerDate
import com.vsnappy1.datepicker.data.model.DatePickerDate
import com.vsnappy1.datepicker.data.model.Month
import com.vsnappy1.datepicker.ui.model.DatePickerUiState
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -130,7 +130,7 @@ internal class DatePickerViewModel : ViewModel() {
}

fun setDate(
date: ComposeDatePickerDate,
date: DatePickerDate,
calendar: Calendar = Calendar.getInstance()
) {
val yearMin = Constant.years.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.vsnappy1.extension.noRippleClickable
import com.vsnappy1.extension.toDp
import com.vsnappy1.theme.Size.extraLarge
import com.vsnappy1.theme.Size.medium
import com.vsnappy1.timepicker.data.model.ComposeTimePickerTime
import com.vsnappy1.timepicker.data.model.TimePickerTime
import com.vsnappy1.timepicker.enums.MinuteGap
import com.vsnappy1.timepicker.ui.model.TimePickerConfiguration
import com.vsnappy1.timepicker.ui.viewmodel.TimePickerViewModel
Expand All @@ -46,12 +46,12 @@ fun TimePicker(
onTimeSelected: (Int, Int) -> Unit,
is24Hour: Boolean? = null,
minuteGap: MinuteGap = MinuteGap.ONE,
time: ComposeTimePickerTime? = null,
time: TimePickerTime? = null,
configuration: TimePickerConfiguration = TimePickerConfiguration.Builder().build(),
id: Int = 1
) {
val viewModel: TimePickerViewModel = viewModel(key = "TimePickerViewModel$id")
val timePickerTime = time ?: ComposeTimePickerTime.getTime()
val timePickerTime = time ?: TimePickerTime.getTime()
val is24: Boolean = is24Hour ?: DateFormat.is24HourFormat(LocalContext.current)
val timePickerUiState = viewModel.getUiStateTimeProvided(timePickerTime, minuteGap, is24)
val uiState by viewModel.uiState.observeAsState(timePickerUiState)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.vsnappy1.timepicker.data.model

import com.vsnappy1.timepicker.data.Constant
import com.vsnappy1.timepicker.enums.MinuteGap
import java.util.Calendar

class ComposeTimePickerTime(val hour: Int, val minute: Int) {
class TimePickerTime(val hour: Int, val minute: Int) {

companion object DefaultTime {
fun getTime(): ComposeTimePickerTime {
return ComposeTimePickerTime(
fun getTime(): TimePickerTime {
return TimePickerTime(
Calendar.getInstance()[Calendar.HOUR_OF_DAY],
Calendar.getInstance()[Calendar.MINUTE]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vsnappy1.timepicker.data.Constant
import com.vsnappy1.timepicker.data.model.ComposeTimePickerTime
import com.vsnappy1.timepicker.data.model.TimePickerTime
import com.vsnappy1.timepicker.enums.MinuteGap
import com.vsnappy1.timepicker.ui.model.TimePickerUiState
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -43,13 +43,13 @@ internal class TimePickerViewModel : ViewModel() {
}
}

fun getSelectedTime(): ComposeTimePickerTime? {
fun getSelectedTime(): TimePickerTime? {
val time = _uiState.value?.let {
var hour = it.hours[it.selectedHourIndex].toInt()
if (!it.is24Hour) {
hour = hour % 12 + if (it.selectedTimeOfDayIndex == 1) 12 else 0
}
ComposeTimePickerTime(
TimePickerTime(
hour,
it.minutes[it.selectedMinuteIndex].toInt()
)
Expand All @@ -58,15 +58,15 @@ internal class TimePickerViewModel : ViewModel() {
}

fun updateUiState(
timePickerTime: ComposeTimePickerTime,
timePickerTime: TimePickerTime,
minuteGap: MinuteGap,
is24: Boolean
) {
_uiState.value = getUiStateTimeProvided(timePickerTime, minuteGap, is24)
}

fun getUiStateTimeProvided(
timePickerTime: ComposeTimePickerTime,
timePickerTime: TimePickerTime,
minuteGap: MinuteGap,
is24: Boolean
): TimePickerUiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.icu.util.Calendar
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.vsnappy1.MainCoroutineRule
import com.vsnappy1.datepicker.data.Constant
import com.vsnappy1.datepicker.data.model.ComposeDatePickerDate
import com.vsnappy1.datepicker.data.model.DatePickerDate
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -179,7 +179,7 @@ class DatePickerViewModelTest {
fun setDate_when_givenYearIsNotWithinRange_shouldThrowAndIllegalArgumentException() {
//Given
val lastYear = Constant.years.last()
val composeDatePickerDate = ComposeDatePickerDate(lastYear + 1, 0, 1)
val composeDatePickerDate = DatePickerDate(lastYear + 1, 0, 1)

//When
viewModel.setDate(composeDatePickerDate, calendar = calendar)
Expand All @@ -189,7 +189,7 @@ class DatePickerViewModelTest {
fun setDate_when_givenMonthIsNotWithinRange_shouldThrowAndIllegalArgumentException() {
//Given
val lastYear = Constant.years.last()
val composeDatePickerDate = ComposeDatePickerDate(lastYear, 12, 1)
val composeDatePickerDate = DatePickerDate(lastYear, 12, 1)

//When
viewModel.setDate(composeDatePickerDate, calendar = calendar)
Expand All @@ -199,7 +199,7 @@ class DatePickerViewModelTest {
fun setDate_when_givenDayIsZero_shouldThrowAndIllegalArgumentException() {
//Given
val lastYear = Constant.years.last()
val composeDatePickerDate = ComposeDatePickerDate(lastYear, 5, 0)
val composeDatePickerDate = DatePickerDate(lastYear, 5, 0)

//When
viewModel.setDate(composeDatePickerDate, calendar = calendar)
Expand All @@ -209,7 +209,7 @@ class DatePickerViewModelTest {
fun setDate_when_givenDayIsMoreThenMaxDayOfMonth_shouldThrowAndIllegalArgumentException() {
//Given
val lastYear = Constant.years.last()
val composeDatePickerDate = ComposeDatePickerDate(lastYear, 5, 31)
val composeDatePickerDate = DatePickerDate(lastYear, 5, 31)

//When
viewModel.setDate(composeDatePickerDate, calendar = calendar)
Expand All @@ -225,7 +225,7 @@ class DatePickerViewModelTest {
val day = 7
val month = 5
val year = Constant.years.last()
val composeDatePickerDate = ComposeDatePickerDate(year, month, day)
val composeDatePickerDate = DatePickerDate(year, month, day)

Mockito.`when`(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)).thenReturn(30)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.vsnappy1.timepicker.ui.viewmodel

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.vsnappy1.timepicker.data.model.ComposeTimePickerTime
import com.vsnappy1.MainCoroutineRule
import com.vsnappy1.timepicker.data.model.TimePickerTime
import com.vsnappy1.timepicker.enums.MinuteGap
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
Expand All @@ -16,6 +20,9 @@ class TimePickerViewModelTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@get:Rule
val mainCoroutineRule = MainCoroutineRule()

private lateinit var viewModel: TimePickerViewModel

@Before
Expand All @@ -26,7 +33,7 @@ class TimePickerViewModelTest {
@Test(expected = IllegalArgumentException::class)
fun getUiStateTimeProvided_when_hourIsLessThanZero_shouldThrowException() {
//Given
val time = ComposeTimePickerTime(-1, 45)
val time = TimePickerTime(-1, 45)

//When
viewModel.getUiStateTimeProvided(time, MinuteGap.FIVE, true)
Expand All @@ -35,7 +42,7 @@ class TimePickerViewModelTest {
@Test(expected = IllegalArgumentException::class)
fun getUiStateTimeProvided_when_hourIsMoreThan23_shouldThrowException() {
//Given
val time = ComposeTimePickerTime(24, 45)
val time = TimePickerTime(24, 45)

//When
viewModel.getUiStateTimeProvided(time, MinuteGap.FIVE, true)
Expand All @@ -44,7 +51,7 @@ class TimePickerViewModelTest {
@Test(expected = IllegalArgumentException::class)
fun getUiStateTimeProvided_when_minuteIsLessThanZero_shouldThrowException() {
//Given
val time = ComposeTimePickerTime(12, -1)
val time = TimePickerTime(12, -1)

//When
viewModel.getUiStateTimeProvided(time, MinuteGap.FIVE, true)
Expand All @@ -53,7 +60,7 @@ class TimePickerViewModelTest {
@Test(expected = IllegalArgumentException::class)
fun getUiStateTimeProvided_when_minuteIsMoreThan59_shouldThrowException() {
//Given
val time = ComposeTimePickerTime(12, 60)
val time = TimePickerTime(12, 60)

//When
viewModel.getUiStateTimeProvided(time, MinuteGap.FIVE, true)
Expand All @@ -63,7 +70,7 @@ class TimePickerViewModelTest {
@Test
fun getUiStateTimeProvided_when_is24HourIsFalse_shouldUpdateTheUiStateAccordingly() {
//Given
val time = ComposeTimePickerTime(20, 55)
val time = TimePickerTime(20, 55)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand All @@ -81,7 +88,7 @@ class TimePickerViewModelTest {
@Test
fun getUiStateTimeProvided_when_is24HourIsTrue_shouldUpdateTheUiStateAccordingly() {
//Given
val time = ComposeTimePickerTime(20, 55)
val time = TimePickerTime(20, 55)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, true)
Expand All @@ -98,7 +105,7 @@ class TimePickerViewModelTest {
@Test
fun getUiStateTimeProvided_when_minuteProvidedIsNotMultipleOfMinuteGap_shouldUpdateTheUiStateAccordingly() {
//Given
val time = ComposeTimePickerTime(20, 51)
val time = TimePickerTime(20, 51)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, true)
Expand All @@ -112,16 +119,18 @@ class TimePickerViewModelTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun updateSelectedHourIndex_when_13HoursAdded_shouldChangeTimeOfDay() {
fun updateSelectedHourIndex_when_13HoursAdded_shouldChangeTimeOfDay() = runTest{
//Given
val time = ComposeTimePickerTime(10, 55)
val time = TimePickerTime(0, 55)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
viewModel.uiState.value?.let {
viewModel.updateSelectedHourIndex(it.selectedHourIndex + 13)
}
advanceTimeBy(250)

//Then
viewModel.uiState.value?.let {
Expand All @@ -132,7 +141,7 @@ class TimePickerViewModelTest {
@Test
fun getSelectedTime_when_is24HourIsFalse_shouldReturnCorrectHoursAndMinutes() {
//Given
val time = ComposeTimePickerTime(20, 55)
val time = TimePickerTime(20, 55)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand All @@ -147,7 +156,7 @@ class TimePickerViewModelTest {
@Test
fun getSelectedTime_when_minuteGapIsFive_shouldCeilTheMinuteToNearestMultipleOfMinuteGap() {
//Given
val time = ComposeTimePickerTime(20, 53)
val time = TimePickerTime(20, 53)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand All @@ -162,7 +171,7 @@ class TimePickerViewModelTest {
@Test
fun getSelectedTime_when_minuteGapIsFiveAndCurrentMinuteIsMoreThan55_shouldMakeTheMinuteZeroAndIncrementTheHour() {
//Given
val time = ComposeTimePickerTime(20, 57)
val time = TimePickerTime(20, 57)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand All @@ -177,7 +186,7 @@ class TimePickerViewModelTest {
@Test
fun getSelectedTime_when_minuteGapIsFiveItIsElevenOClockAMAndCurrentMinuteIsMoreThan55_shouldMakeTheMinuteZeroAndIncrementTheHourAndChangeTimeOfTheDay() {
//Given
val time = ComposeTimePickerTime(11, 57)
val time = TimePickerTime(11, 57)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand All @@ -194,7 +203,7 @@ class TimePickerViewModelTest {
@Test
fun getSelectedTime_when_minuteGapIsFiveItIsElevenOClockPMAndCurrentMinuteIsMoreThan55_shouldMakeTheMinuteZeroAndIncrementTheHourAndChangeTimeOfTheDay() {
//Given
val time = ComposeTimePickerTime(23, 57)
val time = TimePickerTime(23, 57)

//When
viewModel.updateUiState(time, MinuteGap.FIVE, false)
Expand Down

0 comments on commit 785a331

Please sign in to comment.