Reworked meeting scheduler, now correctly handles selected date & time in local timezone to the one selected if different

This commit is contained in:
Sylvain Berfini 2024-12-18 11:06:02 +01:00
parent a90dd53c9c
commit 679f125870

View file

@ -45,7 +45,6 @@ import org.linphone.ui.main.model.SelectedAddressModel
import org.linphone.utils.Event import org.linphone.utils.Event
import org.linphone.utils.LinphoneUtils import org.linphone.utils.LinphoneUtils
import org.linphone.utils.TimestampUtils import org.linphone.utils.TimestampUtils
import java.util.Locale
class ScheduleMeetingViewModel class ScheduleMeetingViewModel
@UiThread @UiThread
@ -64,8 +63,6 @@ class ScheduleMeetingViewModel
val fromDate = MutableLiveData<String>() val fromDate = MutableLiveData<String>()
val toDate = MutableLiveData<String>()
val fromTime = MutableLiveData<String>() val fromTime = MutableLiveData<String>()
val toTime = MutableLiveData<String>() val toTime = MutableLiveData<String>()
@ -90,6 +87,9 @@ class ScheduleMeetingViewModel
private var startTimestamp = 0L private var startTimestamp = 0L
private var endTimestamp = 0L private var endTimestamp = 0L
internal var startDayOfYear = 0
internal var startYear = 0
internal var startHour = 0 internal var startHour = 0
internal var startMinutes = 0 internal var startMinutes = 0
@ -212,6 +212,10 @@ class ScheduleMeetingViewModel
TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id) TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id)
) )
cal.timeInMillis = now cal.timeInMillis = now
startYear = cal.get(Calendar.YEAR)
startDayOfYear = cal.get(Calendar.DAY_OF_YEAR)
cal.add(Calendar.HOUR, 1) cal.add(Calendar.HOUR, 1)
cal.set(Calendar.MINUTE, 0) cal.set(Calendar.MINUTE, 0)
cal.set(Calendar.SECOND, 0) cal.set(Calendar.SECOND, 0)
@ -220,12 +224,12 @@ class ScheduleMeetingViewModel
startMinutes = 0 startMinutes = 0
cal.add(Calendar.HOUR, 1) cal.add(Calendar.HOUR, 1)
val twoHoursLater = cal.timeInMillis val oneHourLater = cal.timeInMillis
endHour = cal.get(Calendar.HOUR_OF_DAY) endHour = cal.get(Calendar.HOUR_OF_DAY)
endMinutes = 0 endMinutes = 0
startTimestamp = nextFullHour startTimestamp = nextFullHour
endTimestamp = twoHoursLater endTimestamp = oneHourLater
Log.i( Log.i(
"$TAG Default start time is [$startHour:$startMinutes], default end time is [$startHour:$startMinutes]" "$TAG Default start time is [$startHour:$startMinutes], default end time is [$startHour:$startMinutes]"
@ -288,35 +292,34 @@ class ScheduleMeetingViewModel
@UiThread @UiThread
fun getCurrentlySelectedStartDate(): Long { fun getCurrentlySelectedStartDate(): Long {
return startTimestamp val cal = Calendar.getInstance()
cal.set(Calendar.YEAR, startYear)
cal.set(Calendar.DAY_OF_YEAR, startDayOfYear)
return cal.timeInMillis
} }
@UiThread @UiThread
fun setStartDate(timestamp: Long) { fun setStartDate(timestamp: Long) {
val cal = Calendar.getInstance( val cal = Calendar.getInstance()
TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id)
)
cal.timeInMillis = timestamp cal.timeInMillis = timestamp
cal.set(Calendar.HOUR_OF_DAY, startHour)
cal.set(Calendar.MINUTE, startMinutes)
startTimestamp = cal.timeInMillis
cal.set(Calendar.HOUR_OF_DAY, endHour) startYear = cal.get(Calendar.YEAR)
cal.set(Calendar.MINUTE, endMinutes) startDayOfYear = cal.get(Calendar.DAY_OF_YEAR)
endTimestamp = cal.timeInMillis Log.i("$TAG Date picker returned timestamp [$timestamp], day of year is now [$startDayOfYear] for year [$startYear], updating displayed date")
computeDateLabels() computeDateLabels()
computeTimeLabels()
} }
@UiThread @UiThread
fun updateTimeZone(timeZone: TimeZoneModel) { fun updateTimeZone(timeZone: TimeZoneModel) {
selectedTimeZone.value = timeZone selectedTimeZone.value = timeZone
computeTimeLabels() Log.i("$TAG Newly selected time zone is [$timeZone]")
} }
@UiThread @UiThread
fun setStartTime(hours: Int, minutes: Int) { fun setStartTime(hours: Int, minutes: Int) {
Log.i("$TAG Newly selected start time is [$hours:$minutes], updating displayed start/end hours")
startHour = hours startHour = hours
startMinutes = minutes startMinutes = minutes
@ -328,6 +331,7 @@ class ScheduleMeetingViewModel
@UiThread @UiThread
fun setEndTime(hours: Int, minutes: Int) { fun setEndTime(hours: Int, minutes: Int) {
Log.i("$TAG Newly selected end time is [$hours:$minutes], updating displayed end hours")
endHour = hours endHour = hours
endMinutes = minutes endMinutes = minutes
@ -414,6 +418,9 @@ class ScheduleMeetingViewModel
// Allows to have a chat room within the conference // Allows to have a chat room within the conference
conferenceInfo.setCapability(StreamType.Text, true) conferenceInfo.setCapability(StreamType.Text, true)
Log.i("$TAG Computing timestamps")
computeTimestampsForSelectedTimezone()
val startTime = startTimestamp / 1000 // Linphone expects timestamp in seconds val startTime = startTimestamp / 1000 // Linphone expects timestamp in seconds
val duration = val duration =
(((endTimestamp - startTimestamp) / 1000) / 60).toInt() // Linphone expects duration in minutes (((endTimestamp - startTimestamp) / 1000) / 60).toInt() // Linphone expects duration in minutes
@ -470,6 +477,9 @@ class ScheduleMeetingViewModel
conferenceInfo.subject = subject.value conferenceInfo.subject = subject.value
conferenceInfo.description = description.value conferenceInfo.description = description.value
Log.i("$TAG Computing timestamps")
computeTimestampsForSelectedTimezone()
val startTime = startTimestamp / 1000 // Linphone expects timestamp in seconds val startTime = startTimestamp / 1000 // Linphone expects timestamp in seconds
conferenceInfo.dateTime = startTime conferenceInfo.dateTime = startTime
val duration = (((endTimestamp - startTimestamp) / 1000) / 60).toInt() // Linphone expects duration in minutes val duration = (((endTimestamp - startTimestamp) / 1000) / 60).toInt() // Linphone expects duration in minutes
@ -525,8 +535,13 @@ class ScheduleMeetingViewModel
TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id) TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id)
) )
cal.timeInMillis = startTimestamp cal.timeInMillis = startTimestamp
startYear = cal.get(Calendar.YEAR)
startDayOfYear = cal.get(Calendar.DAY_OF_YEAR)
startHour = cal.get(Calendar.HOUR_OF_DAY) startHour = cal.get(Calendar.HOUR_OF_DAY)
startMinutes = cal.get(Calendar.MINUTE) startMinutes = cal.get(Calendar.MINUTE)
cal.timeInMillis = endTimestamp cal.timeInMillis = endTimestamp
endHour = cal.get(Calendar.HOUR_OF_DAY) endHour = cal.get(Calendar.HOUR_OF_DAY)
endMinutes = cal.get(Calendar.MINUTE) endMinutes = cal.get(Calendar.MINUTE)
@ -564,66 +579,65 @@ class ScheduleMeetingViewModel
} }
@AnyThread @AnyThread
private fun computeDateLabels() { private fun computeTimestampsForSelectedTimezone() {
val start = TimestampUtils.toString( val timeZone = TimeZone.getTimeZone(selectedTimeZone.value?.id ?: TimeZone.getDefault().id)
startTimestamp, Log.i("$TAG Computing timestamps using time zone [$timeZone]")
onlyDate = true,
timestampInSecs = false,
shortDate = false,
hideYear = false
)
fromDate.postValue(start)
Log.i("$TAG Computed start date for timestamp [$startTimestamp] is [$start]")
val end = TimestampUtils.toString( val cal = Calendar.getInstance(timeZone)
endTimestamp, cal.set(Calendar.YEAR, startYear)
cal.set(Calendar.DAY_OF_YEAR, startDayOfYear)
cal.set(Calendar.HOUR_OF_DAY, startHour)
cal.set(Calendar.MINUTE, startMinutes)
startTimestamp = cal.timeInMillis
Log.i("$TAG Computed start timestamp is [$startTimestamp]")
cal.set(Calendar.HOUR_OF_DAY, endHour)
cal.set(Calendar.MINUTE, endMinutes)
if (endHour < startHour || (endHour == startHour && endMinutes <= startMinutes)) {
// Make sure if endTime is after startTime that it is on the next day
if (cal.timeInMillis <= startTimestamp) {
Log.i("$TAG endTime < startTime, adding 1 day to endTimestamp")
cal.add(Calendar.DAY_OF_YEAR, 1)
}
}
endTimestamp = cal.timeInMillis
Log.i("$TAG Computed end timestamp is [$endTimestamp]")
}
@AnyThread
private fun computeDateLabels() {
val cal = Calendar.getInstance()
cal.set(Calendar.YEAR, startYear)
cal.set(Calendar.DAY_OF_YEAR, startDayOfYear)
val date = TimestampUtils.toString(
cal.timeInMillis,
onlyDate = true, onlyDate = true,
timestampInSecs = false, timestampInSecs = false,
shortDate = false, shortDate = false,
hideYear = false hideYear = false
) )
toDate.postValue(end) fromDate.postValue(date)
Log.i("$TAG Computed end date for timestamp [$endTimestamp] is [$end]") Log.i("$TAG Computed date is [$date]")
} }
@AnyThread @AnyThread
private fun computeTimeLabels() { private fun computeTimeLabels() {
val timeZoneId = selectedTimeZone.value?.id ?: TimeZone.getDefault().id val cal = Calendar.getInstance()
Log.i("$TAG Updating timestamps using time zone [${selectedTimeZone.value}]($timeZoneId)")
val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId))
cal.timeInMillis = startTimestamp
if (startHour != -1 && startMinutes != -1) { if (startHour != -1 && startMinutes != -1) {
cal.set(Calendar.HOUR_OF_DAY, startHour) cal.set(Calendar.HOUR_OF_DAY, startHour)
cal.set(Calendar.MINUTE, startMinutes) cal.set(Calendar.MINUTE, startMinutes)
} }
startTimestamp = cal.timeInMillis val startTime = TimestampUtils.timeToString(cal.timeInMillis, timestampInSecs = false)
fromTime.postValue(startTime)
// Manually printing calendar hour & minute to prevent timezone selection to convert selected time
val startHoursToDisplay = String.format(Locale.getDefault(), "%02d", cal.get(Calendar.HOUR_OF_DAY))
val startMinutesToDisplay = String.format(Locale.getDefault(), "%02d", cal.get(Calendar.MINUTE))
fromTime.postValue("$startHoursToDisplay:$startMinutesToDisplay")
cal.timeInMillis = endTimestamp
if (endHour != -1 && endMinutes != -1) { if (endHour != -1 && endMinutes != -1) {
cal.set(Calendar.HOUR_OF_DAY, endHour) cal.set(Calendar.HOUR_OF_DAY, endHour)
cal.set(Calendar.MINUTE, endMinutes) cal.set(Calendar.MINUTE, endMinutes)
if (endHour < startHour || (endHour == startHour && endMinutes <= startMinutes)) {
// Make sure if endTime is after startTime that it is on the next day
if (cal.timeInMillis <= startTimestamp) {
Log.i("$TAG endTime < startTime, adding 1 day to endTimestamp")
cal.add(Calendar.DAY_OF_YEAR, 1)
}
}
} }
endTimestamp = cal.timeInMillis val endTime = TimestampUtils.timeToString(cal.timeInMillis, timestampInSecs = false)
toTime.postValue(endTime)
// Manually printing calendar hour & minute to prevent timezone selection to convert selected time Log.i("$TAG Computed start time is [$startTime] and end time is [$endTime]")
val endHoursToDisplay = String.format(Locale.getDefault(), "%02d", cal.get(Calendar.HOUR_OF_DAY))
val endMinutesToDisplay = String.format(Locale.getDefault(), "%02d", cal.get(Calendar.MINUTE))
toTime.postValue("$endHoursToDisplay:$endMinutesToDisplay")
Log.i("$TAG Start timestamp is now [$startTimestamp] and end timestamp is now [$endTimestamp]")
} }
} }