Save and restore state important values

This commit is contained in:
Naveen 2022-07-20 23:53:17 +05:30
parent 4375284612
commit d70e844448
3 changed files with 62 additions and 10 deletions

View File

@ -77,6 +77,7 @@ class EventActivity : SimpleActivity() {
private var mOriginalTimeZone = DateTimeZone.getDefault().id
private var mOriginalStartTS = 0L
private var mOriginalEndTS = 0L
private var mIsNewEvent = true
private lateinit var mEventStartDateTime: DateTime
private lateinit var mEventEndDateTime: DateTime
@ -353,6 +354,9 @@ class EventActivity : SimpleActivity() {
putLong(EVENT_TYPE_ID, mEventTypeId)
putInt(EVENT_CALENDAR_ID, mEventCalendarId)
putBoolean(IS_NEW_EVENT, mIsNewEvent)
putLong(ORIGINAL_START_TS, mOriginalStartTS)
putLong(ORIGINAL_END_TS, mOriginalEndTS)
}
}
@ -389,6 +393,9 @@ class EventActivity : SimpleActivity() {
mEventTypeId = getLong(EVENT_TYPE_ID)
mEventCalendarId = getInt(EVENT_CALENDAR_ID)
mIsNewEvent = getBoolean(IS_NEW_EVENT)
mOriginalStartTS = getLong(ORIGINAL_START_TS)
mOriginalEndTS = getLong(ORIGINAL_END_TS)
}
checkRepeatTexts(mRepeatInterval)
@ -397,6 +404,7 @@ class EventActivity : SimpleActivity() {
updateEventType()
updateCalDAVCalendar()
checkAttendees()
updateActionBarTitle()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
@ -420,6 +428,7 @@ class EventActivity : SimpleActivity() {
}
private fun setupEditEvent() {
mIsNewEvent = false
val realStart = if (mEventOccurrenceTS == 0L) mEvent.startTS else mEventOccurrenceTS
val duration = mEvent.endTS - mEvent.startTS
mOriginalStartTS = realStart
@ -1105,15 +1114,15 @@ class EventActivity : SimpleActivity() {
val reminders = getReminders()
if (!event_all_day.isChecked) {
if (reminders.getOrNull(2)?.minutes ?: 0 < -1) {
if ((reminders.getOrNull(2)?.minutes ?: 0) < -1) {
reminders.removeAt(2)
}
if (reminders.getOrNull(1)?.minutes ?: 0 < -1) {
if ((reminders.getOrNull(1)?.minutes ?: 0) < -1) {
reminders.removeAt(1)
}
if (reminders.getOrNull(0)?.minutes ?: 0 < -1) {
if ((reminders.getOrNull(0)?.minutes ?: 0) < -1) {
reminders.removeAt(0)
}
}
@ -1701,4 +1710,12 @@ class EventActivity : SimpleActivity() {
it.applyColorFilter(textColor)
}
}
private fun updateActionBarTitle() {
if (mIsNewEvent) {
updateActionBarTitle(getString(R.string.new_event))
} else {
updateActionBarTitle(getString(R.string.edit_event))
}
}
}

View File

@ -25,9 +25,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.EVERY_DAY_BIT
import com.simplemobiletools.commons.helpers.SAVE_DISCARD_PROMPT_INTERVAL
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.RadioItem
import kotlinx.android.synthetic.main.activity_task.*
import org.joda.time.DateTime
@ -53,6 +51,7 @@ class TaskActivity : SimpleActivity() {
private var mOriginalStartTS = 0L
private var mTaskCompleted = false
private var mLastSavePromptTS = 0L
private var mIsNewEvent = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -167,6 +166,8 @@ class TaskActivity : SimpleActivity() {
putLong(REPEAT_LIMIT, mRepeatLimit)
putLong(EVENT_TYPE_ID, mEventTypeId)
putBoolean(IS_NEW_EVENT, mIsNewEvent)
putLong(ORIGINAL_START_TS, mOriginalStartTS)
}
}
@ -191,10 +192,16 @@ class TaskActivity : SimpleActivity() {
mRepeatRule = getInt(REPEAT_RULE)
mRepeatLimit = getLong(REPEAT_LIMIT)
mEventTypeId = getLong(EVENT_TYPE_ID)
mIsNewEvent = getBoolean(IS_NEW_EVENT)
mOriginalStartTS = getLong(ORIGINAL_START_TS)
}
updateEventType()
updateTexts()
setupMarkCompleteButton()
checkRepeatTexts(mRepeatInterval)
checkRepeatRule()
updateActionBarTitle()
}
private fun gotTask(savedInstanceState: Bundle?, localEventType: EventType?, task: Event?) {
@ -264,6 +271,7 @@ class TaskActivity : SimpleActivity() {
}
private fun setupEditTask() {
mIsNewEvent = false
val realStart = if (mTaskOccurrenceTS == 0L) mTask.startTS else mTaskOccurrenceTS
mOriginalStartTS = realStart
mTaskDateTime = Formatter.getDateTimeFromTS(realStart)
@ -453,6 +461,7 @@ class TaskActivity : SimpleActivity() {
private fun dateSet(year: Int, month: Int, day: Int) {
mTaskDateTime = mTaskDateTime.withDate(year, month + 1, day)
updateDateText()
checkRepeatRule()
}
private fun timeSet(hours: Int, minutes: Int) {
@ -467,6 +476,21 @@ class TaskActivity : SimpleActivity() {
updateRepetitionText()
}
private fun checkRepeatRule() {
if (mRepeatInterval.isXWeeklyRepetition()) {
val day = mRepeatRule
if (day == MONDAY_BIT || day == TUESDAY_BIT || day == WEDNESDAY_BIT || day == THURSDAY_BIT || day == FRIDAY_BIT || day == SATURDAY_BIT || day == SUNDAY_BIT) {
setRepeatRule(2.0.pow((mTaskDateTime.dayOfWeek - 1).toDouble()).toInt())
}
} else if (mRepeatInterval.isXMonthlyRepetition() || mRepeatInterval.isXYearlyRepetition()) {
if (mRepeatRule == REPEAT_LAST_DAY && !isLastDayOfTheMonth()) {
mRepeatRule = REPEAT_SAME_DAY
}
checkRepetitionRuleText()
}
}
private fun updateDateText() {
task_date.text = Formatter.getDate(this, mTaskDateTime)
}
@ -483,17 +507,17 @@ class TaskActivity : SimpleActivity() {
private fun setupMarkCompleteButton() {
toggle_mark_complete.setOnClickListener { toggleCompletion() }
toggle_mark_complete.beVisibleIf(mTask.id != null)
updateTaskCompleted()
updateTaskCompletedButton()
ensureBackgroundThread {
// the stored value might be incorrect so update it (e.g. user completed the task via notification action before editing)
mTaskCompleted = isTaskCompleted(mTask.copy(startTS = mOriginalStartTS, endTS = mOriginalStartTS))
mTaskCompleted = isTaskCompleted(mTask.copy(startTS = mOriginalStartTS))
runOnUiThread {
updateTaskCompleted()
updateTaskCompletedButton()
}
}
}
private fun updateTaskCompleted() {
private fun updateTaskCompletedButton() {
if (mTaskCompleted) {
toggle_mark_complete.background = ContextCompat.getDrawable(this, R.drawable.button_background_stroke)
toggle_mark_complete.setText(R.string.mark_incomplete)
@ -847,4 +871,12 @@ class TaskActivity : SimpleActivity() {
private fun updateRepetitionText() {
task_repetition.text = getRepetitionText(mRepeatInterval)
}
private fun updateActionBarTitle() {
if (mIsNewEvent) {
updateActionBarTitle(getString(R.string.new_task))
} else {
updateActionBarTitle(getString(R.string.edit_task))
}
}
}

View File

@ -207,6 +207,8 @@ const val EVENT = "EVENT"
const val TASK = "TASK"
const val START_TS = "START_TS"
const val END_TS = "END_TS"
const val ORIGINAL_START_TS = "ORIGINAL_START_TS"
const val ORIGINAL_END_TS = "ORIGINAL_END_TS"
const val REMINDER_1_MINUTES = "REMINDER_1_MINUTES"
const val REMINDER_2_MINUTES = "REMINDER_2_MINUTES"
const val REMINDER_3_MINUTES = "REMINDER_3_MINUTES"
@ -220,6 +222,7 @@ const val ATTENDEES = "ATTENDEES"
const val AVAILABILITY = "AVAILABILITY"
const val EVENT_TYPE_ID = "EVENT_TYPE_ID"
const val EVENT_CALENDAR_ID = "EVENT_CALENDAR_ID"
const val IS_NEW_EVENT = "IS_NEW_EVENT"
// actions
const val ACTION_MARK_COMPLETED = "ACTION_MARK_COMPLETED"