fix #1314, avoid switching day at creating a new event on some views

This commit is contained in:
tibbi
2021-03-21 19:28:44 +01:00
parent 39db1e0120
commit 1d0072b633
2 changed files with 10 additions and 4 deletions

View File

@@ -86,7 +86,9 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
checkWhatsNewDialog() checkWhatsNewDialog()
calendar_fab.beVisibleIf(config.storedView != YEARLY_VIEW && config.storedView != WEEKLY_VIEW) calendar_fab.beVisibleIf(config.storedView != YEARLY_VIEW && config.storedView != WEEKLY_VIEW)
calendar_fab.setOnClickListener { calendar_fab.setOnClickListener {
launchNewEventIntent(currentFragments.last().getNewEventDayCode()) val lastFragment = currentFragments.last()
val allowChangingDay = lastFragment !is DayFragmentsHolder && lastFragment !is MonthDayFragmentsHolder
launchNewEventIntent(lastFragment.getNewEventDayCode(), allowChangingDay)
} }
storeStateVariables() storeStateVariables()

View File

@@ -331,20 +331,24 @@ fun Context.rescheduleReminder(event: Event?, minutes: Int) {
} }
} }
fun Context.launchNewEventIntent(dayCode: String = Formatter.getTodayCode()) { // if the default event start time is set to "Next full hour" and the event is created before midnight, it could change the day
fun Context.launchNewEventIntent(dayCode: String = Formatter.getTodayCode(), allowChangingDay: Boolean = false) {
Intent(applicationContext, EventActivity::class.java).apply { Intent(applicationContext, EventActivity::class.java).apply {
putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(dayCode)) putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(dayCode, allowChangingDay))
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(this) startActivity(this)
} }
} }
fun Context.getNewEventTimestampFromCode(dayCode: String): Long { fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Boolean = false): Long {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
val defaultStartTime = config.defaultStartTime val defaultStartTime = config.defaultStartTime
val currHour = calendar.get(Calendar.HOUR_OF_DAY) val currHour = calendar.get(Calendar.HOUR_OF_DAY)
var dateTime = Formatter.getLocalDateTimeFromCode(dayCode).withHourOfDay(currHour) var dateTime = Formatter.getLocalDateTimeFromCode(dayCode).withHourOfDay(currHour)
var newDateTime = dateTime.plusHours(1).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0) var newDateTime = dateTime.plusHours(1).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0)
if (!allowChangingDay && dateTime.dayOfMonth() != newDateTime.dayOfMonth()) {
newDateTime = newDateTime.minusDays(1)
}
return if (defaultStartTime == -1) { return if (defaultStartTime == -1) {
newDateTime.seconds() newDateTime.seconds()