Skip to content

Commit

Permalink
fix: wrong chosen date in date picker (closes #334)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 14, 2024
1 parent 3782f0a commit d6fafff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.bnyro.contacts.obj.TranslatedType
import com.bnyro.contacts.obj.ValueWithType
import com.bnyro.contacts.ui.components.dialogs.DialogButton
import com.bnyro.contacts.util.CalendarUtils
import java.util.TimeZone

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -55,20 +56,22 @@ fun DatePickerEditor(
onDelete: () -> Unit,
shape: Shape,
) {
val datePickerOffset = 1000 * 3600 * 23
val datePickerOffset = remember {
TimeZone.getDefault().rawOffset
}

var showPicker by remember {
mutableStateOf(false)
}
val datePickerState = rememberDatePickerState(
runCatching {
CalendarUtils.dateToMillis(state.value.value)
}.getOrDefault(null)?.plus(datePickerOffset)
CalendarUtils.dateToMillis(state.value.value)?.plus(datePickerOffset)
}.getOrDefault(null)
)

LaunchedEffect(datePickerState.selectedDateMillis) {
state.value.value = datePickerState.selectedDateMillis?.let {
CalendarUtils.millisToDate(it, CalendarUtils.isoDateFormat)
CalendarUtils.millisToDate(it.minus(datePickerOffset), CalendarUtils.isoDateFormat)
}.orEmpty()
}

Expand Down Expand Up @@ -104,7 +107,7 @@ fun DatePickerEditor(
colors = ButtonDefaults.outlinedButtonColors(contentColor = MaterialTheme.colorScheme.onSurfaceVariant)
) {
Text(text = datePickerState.selectedDateMillis
?.let { CalendarUtils.millisToDate(it) }
?.let { CalendarUtils.millisToDate(it.minus(datePickerOffset)) }
?.takeIf { state.value.value.isNotEmpty() }
?: stringResource(R.string.date))
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/contacts/util/CalendarUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object CalendarUtils {
private val localizedFormat get() = DateFormat.getDateInstance()

fun millisToDate(milliSeconds: Long, formatter: DateFormat = localizedFormat): String {
val calendar: Calendar = Calendar.getInstance()
val calendar = Calendar.getInstance()
calendar.clear(Calendar.ZONE_OFFSET)
calendar.timeInMillis = milliSeconds
return formatter.format(calendar.time)
}
Expand Down

0 comments on commit d6fafff

Please sign in to comment.