Skip to content

Commit

Permalink
bugfix: date picker misbehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
adrcotfas committed Mar 19, 2023
1 parent 1d17bb9 commit 245ca2a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.apps.adrcotfas.goodtime"
minSdkVersion 23
targetSdkVersion 33
versionCode 149
versionName "2.5.6"
versionCode 151
versionName "2.5.8"
resConfigs "ar-rSA",
"bn-rBD",
"bs-rBA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.view.View
import com.apps.adrcotfas.goodtime.statistics.main.SelectLabelDialog
import com.apps.adrcotfas.goodtime.statistics.main.StatisticsActivity
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.viewModels
import com.apps.adrcotfas.goodtime.database.Label
import com.apps.adrcotfas.goodtime.database.Session
Expand All @@ -37,6 +38,7 @@ import com.apps.adrcotfas.goodtime.util.*
import dagger.hilt.android.AndroidEntryPoint
import java.lang.Integer.min
import java.time.LocalTime
import java.util.*

@AndroidEntryPoint
class AddEditEntryDialog : BottomSheetDialogFragment(), OnLabelSelectedListener {
Expand Down Expand Up @@ -115,22 +117,34 @@ class AddEditEntryDialog : BottomSheetDialogFragment(), OnLabelSelectedListener
if (label != null && label != getString(R.string.label_unlabeled)) {
binding.labelChip.text = label
labelsViewModel.getColorOfLabel(label)
.observe(viewLifecycleOwner, { color: Int? ->
.observe(viewLifecycleOwner) { color: Int? ->
binding.labelChip.chipBackgroundColor = ColorStateList.valueOf(
ThemeHelper.getColor(
requireContext(), color!!
)
)
})
binding.labelDrawable.setImageDrawable(resources.getDrawable(R.drawable.ic_label))
}
binding.labelDrawable.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_label,
null
)
)
} else {
binding.labelChip.text = resources.getString(R.string.label_add)
binding.labelChip.chipBackgroundColor = ColorStateList.valueOf(
ThemeHelper.getColor(
requireContext(), ThemeHelper.COLOR_INDEX_UNLABELED
)
)
binding.labelDrawable.setImageDrawable(resources.getDrawable(R.drawable.ic_label_off))
binding.labelDrawable.setImageDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_label_off,
null
)
)
}
}

Expand Down Expand Up @@ -161,9 +175,13 @@ class AddEditEntryDialog : BottomSheetDialogFragment(), OnLabelSelectedListener
}

binding.editDate.setOnClickListener {
val picker = DatePickerDialogHelper.buildDatePicker(timestamp)
val picker =
DatePickerDialogHelper.buildDatePicker(
timestamp.toUtcMillis()
)
picker.addOnPositiveButtonClickListener {
val newLocalDate = it.toLocalDate()
val newLocalDate =
it.toZoneLocalDateTime()
viewModel.session.timestamp = Pair(newLocalDate, localTime).toLocalDateTime().millis
binding.editDate.text =
TimeUtils.formatDateLong(newLocalDate) //TODO: extract as extension function
Expand Down
63 changes: 41 additions & 22 deletions app/src/main/java/com/apps/adrcotfas/goodtime/util/TimeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ class TimeUtils {
return time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))
}

fun firstDayOfCurrentWeekMillis() : Long {
fun firstDayOfCurrentWeekMillis(): Long {
return LocalDate.now()
.atStartOfDay()
.with(TemporalAdjusters.previousOrSame(firstDayOfWeek()))
.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()
.atStartOfDay()
.with(TemporalAdjusters.previousOrSame(firstDayOfWeek()))
.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()
}

fun firstDayOfLastWeekMillis(): Long {
return LocalDate.now()
.minus(1, ChronoUnit.WEEKS)
.atStartOfDay()
.with(TemporalAdjusters.previousOrSame(firstDayOfWeek()))
.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()
.minus(1, ChronoUnit.WEEKS)
.atStartOfDay()
.with(TemporalAdjusters.previousOrSame(firstDayOfWeek()))
.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()
}

fun nowMillis(): Long {
return LocalDateTime.now()
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli()
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli()
}
}
}
Expand All @@ -80,31 +80,50 @@ fun secondsOfDayToTimerFormat(seconds: Int, is24HourFormat: Boolean = true): Str
)
}

fun Long.toLocalDateTime() : LocalDateTime {
fun Long.toLocalDateTime(): LocalDateTime {
return Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault()).toLocalDateTime()
}

fun Long.toLocalDate() : LocalDate {
fun Long.toLocalDate(): LocalDate {
return Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault()).toLocalDate()
}

fun Long.toLocalTime() : LocalTime {
// The following two functions are required to convert from Zone date time to UTC date time and back
// MaterialDatePicker uses UTC time
fun Long.toUtcLocalDateTime(): LocalDateTime =
Instant.ofEpochMilli(this).atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC)).toLocalDateTime()

fun Long.toZoneLocalDateTime(): LocalDate =
toUtcLocalDateTime().atZone(ZoneId.systemDefault()).toLocalDate()

fun Long.toLocalTime(): LocalTime {
return Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault()).toLocalTime()
}

fun Pair<LocalDate, LocalTime>.toLocalDateTime() : LocalDateTime {
fun Long.toUtcMillis() = toLocalDateTime().atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC))
.toInstant().toEpochMilli()

fun Pair<LocalDate, LocalTime>.toLocalDateTime(): LocalDateTime {
return LocalDateTime.of(this.first, this.second)
}

val LocalDateTime.millis
get() = this.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()

fun LocalTime.toFormattedTime(): String = this.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))
fun LocalDate.toFormattedDate(): String = this.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))
fun LocalDateTime.toFormattedDateTime(): String = this.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM))
fun LocalTime.toFormattedTime(): String =
this.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))

fun LocalDate.toFormattedDate(): String =
this.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))

fun LocalDateTime.toFormattedDateTime(): String =
this.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM))

fun startOfTodayMillis() =
LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()

fun startOfTodayMillis() = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
fun startOfTomorrowMillis() = LocalDate.now().plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
fun startOfTomorrowMillis() =
LocalDate.now().plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()

fun firstDayOfWeek(): DayOfWeek =
WeekFields.of(Locale.getDefault()).firstDayOfWeek
Expand Down

0 comments on commit 245ca2a

Please sign in to comment.