From ca963765e6cdbea2eabf79514b04211b425297eb Mon Sep 17 00:00:00 2001 From: Agnieszka C <85929121+Aga-C@users.noreply.github.com> Date: Sat, 18 Dec 2021 18:13:16 +0100 Subject: [PATCH] Added keeping date when changing views (#1403) --- .../calendar/pro/activities/MainActivity.kt | 54 ++++++++++++------- .../calendar/pro/extensions/Context.kt | 18 +++++++ .../pro/fragments/DayFragmentsHolder.kt | 9 +++- .../pro/fragments/EventListFragment.kt | 7 +++ .../pro/fragments/MonthDayFragmentsHolder.kt | 9 +++- .../pro/fragments/MonthFragmentsHolder.kt | 9 +++- .../pro/fragments/MyFragmentHolder.kt | 5 ++ .../pro/fragments/WeekFragmentsHolder.kt | 11 +++- .../pro/fragments/YearFragmentsHolder.kt | 13 +++-- .../calendar/pro/helpers/Constants.kt | 1 + 10 files changed, 108 insertions(+), 28 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 541a84b24..653b9087b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -799,16 +799,44 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private fun updateView(view: Int) { calendar_fab.beVisibleIf(view != YEARLY_VIEW && view != WEEKLY_VIEW) + val dateCode = getDateCodeToDisplay(view) config.storedView = view checkSwipeRefreshAvailability() - updateViewPager() + updateViewPager(dateCode) if (goToTodayButton?.isVisible == true) { shouldGoToTodayBeVisible = false invalidateOptionsMenu() } } - private fun updateViewPager(dayCode: String? = Formatter.getTodayCode()) { + private fun getDateCodeToDisplay(newView: Int): String? { + val fragment = currentFragments.last() + val currentView = fragment.viewType + if (newView == EVENTS_LIST_VIEW || currentView == EVENTS_LIST_VIEW) { + return null + } + + val fragmentDate: DateTime? = fragment.getCurrentDate() + val viewOrder = arrayListOf(DAILY_VIEW, WEEKLY_VIEW, MONTHLY_VIEW, YEARLY_VIEW) + val currentViewIndex = viewOrder.indexOf(if (currentView == MONTHLY_DAILY_VIEW) MONTHLY_VIEW else currentView) + val newViewIndex = viewOrder.indexOf(if (newView == MONTHLY_DAILY_VIEW) MONTHLY_VIEW else newView) + + return if (fragmentDate != null && currentViewIndex <= newViewIndex) { + getDateCodeFormatForView(newView, fragmentDate!!) + } else { + getDateCodeFormatForView(newView, DateTime()) + } + } + + private fun getDateCodeFormatForView(view: Int, date: DateTime): String { + return when (view) { + WEEKLY_VIEW -> getDatesWeekDateTime(date) + YEARLY_VIEW -> date.toString() + else -> Formatter.getDayCodeFromDateTime(date) + } + } + + private fun updateViewPager(dayCode: String? = null) { val fragment = getFragmentsHolder() currentFragments.forEach { try { @@ -823,8 +851,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { val bundle = Bundle() when (config.storedView) { - DAILY_VIEW, MONTHLY_VIEW, MONTHLY_DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode) - WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, getThisWeekDateTime()) + DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode ?: Formatter.getTodayCode()) + WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, dayCode ?: getDatesWeekDateTime(DateTime())) + MONTHLY_VIEW, MONTHLY_DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode ?: Formatter.getTodayCode()) + YEARLY_VIEW -> bundle.putString(YEAR_TO_OPEN, dayCode) } fragment.arguments = bundle @@ -864,22 +894,6 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } } - private fun getThisWeekDateTime(): String { - return if (!config.startWeekWithCurrentDay) { - val currentOffsetHours = TimeZone.getDefault().rawOffset / 1000 / 60 / 60 - - // not great, not terrible - val useHours = if (currentOffsetHours >= 10) 8 else 12 - var thisweek = DateTime().withZone(DateTimeZone.UTC).withDayOfWeek(1).withHourOfDay(useHours).minusDays(if (config.isSundayFirst) 1 else 0) - if (DateTime().minusDays(7).seconds() > thisweek.seconds()) { - thisweek = thisweek.plusDays(7) - } - thisweek.toString() - } else { - DateTime().withZone(DateTimeZone.UTC).toString() - } - } - private fun getFragmentsHolder() = when (config.storedView) { DAILY_VIEW -> DayFragmentsHolder() MONTHLY_VIEW -> MonthFragmentsHolder() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 2c72c88be..48bcf0dd1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -38,6 +38,8 @@ import com.simplemobiletools.calendar.pro.receivers.NotificationReceiver import com.simplemobiletools.calendar.pro.services.SnoozeService import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* +import org.joda.time.DateTime +import org.joda.time.DateTimeZone import org.joda.time.LocalDate import java.util.* @@ -602,3 +604,19 @@ fun Context.editEvent(event: ListEvent) { startActivity(this) } } + +fun Context.getDatesWeekDateTime(date: DateTime): String { + return if (!config.startWeekWithCurrentDay) { + val currentOffsetHours = TimeZone.getDefault().rawOffset / 1000 / 60 / 60 + + // not great, not terrible + val useHours = if (currentOffsetHours >= 10) 8 else 12 + var thisweek = date.withZone(DateTimeZone.UTC).withDayOfWeek(1).withHourOfDay(useHours).minusDays(if (config.isSundayFirst) 1 else 0) + if (date.minusDays(7).seconds() > thisweek.seconds()) { + thisweek = thisweek.plusDays(7) + } + thisweek.toString() + } else { + date.withZone(DateTimeZone.UTC).toString() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/DayFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/DayFragmentsHolder.kt index 584679333..050ac31aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/DayFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/DayFragmentsHolder.kt @@ -12,6 +12,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.activities.MainActivity import com.simplemobiletools.calendar.pro.adapters.MyDayPagerAdapter import com.simplemobiletools.calendar.pro.extensions.config +import com.simplemobiletools.calendar.pro.helpers.DAILY_VIEW import com.simplemobiletools.calendar.pro.helpers.DAY_CODE import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.interfaces.NavigationListener @@ -32,6 +33,8 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener { private var currentDayCode = "" private var isGoToTodayVisible = false + override val viewType = DAILY_VIEW + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) currentDayCode = arguments?.getString(DAY_CODE) ?: "" @@ -108,7 +111,7 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener { val view = layoutInflater.inflate(R.layout.date_picker, null) val datePicker = view.findViewById(R.id.date_picker) - val dateTime = Formatter.getDateTimeFromCode(currentDayCode) + val dateTime = getCurrentDate()!! datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null) AlertDialog.Builder(requireContext()) @@ -142,4 +145,8 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener { override fun printView() { (viewPager?.adapter as? MyDayPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) } + + override fun getCurrentDate(): DateTime? { + return if (currentDayCode != "") Formatter.getDateTimeFromCode(currentDayCode) else null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt index e2bb197ca..c3df998ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt @@ -13,6 +13,7 @@ import com.simplemobiletools.calendar.pro.activities.MainActivity import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.adapters.EventListAdapter import com.simplemobiletools.calendar.pro.extensions.* +import com.simplemobiletools.calendar.pro.helpers.EVENTS_LIST_VIEW import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.ListEvent @@ -46,6 +47,8 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { lateinit var mView: View + override val viewType = EVENTS_LIST_VIEW + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { mView = inflater.inflate(R.layout.fragment_event_list, container, false) mView.background = ColorDrawable(requireContext().config.backgroundColor) @@ -248,4 +251,8 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { }, 1000) } } + + override fun getCurrentDate(): DateTime? { + return null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthDayFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthDayFragmentsHolder.kt index 5d3672b25..2b2a96036 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthDayFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthDayFragmentsHolder.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.getMonthCode import com.simplemobiletools.calendar.pro.helpers.DAY_CODE import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.helpers.MONTHLY_DAILY_VIEW import com.simplemobiletools.calendar.pro.interfaces.NavigationListener import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.getDialogTheme @@ -34,6 +35,8 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener { private var currentDayCode = "" private var isGoToTodayVisible = false + override val viewType = MONTHLY_DAILY_VIEW + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) currentDayCode = arguments?.getString(DAY_CODE) ?: "" @@ -111,7 +114,7 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener { val datePicker = view.findViewById(R.id.date_picker) datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).beGone() - val dateTime = DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString()) + val dateTime = getCurrentDate()!! datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null) AlertDialog.Builder(requireContext()) @@ -147,4 +150,8 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener { } override fun printView() {} + + override fun getCurrentDate(): DateTime? { + return if (currentDayCode != "") DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString()) else null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthFragmentsHolder.kt index 4a8eb35a5..5ec58f13c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MonthFragmentsHolder.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.getMonthCode import com.simplemobiletools.calendar.pro.helpers.DAY_CODE import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.helpers.MONTHLY_VIEW import com.simplemobiletools.calendar.pro.interfaces.NavigationListener import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.getDialogTheme @@ -34,6 +35,8 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener { private var currentDayCode = "" private var isGoToTodayVisible = false + override val viewType = MONTHLY_VIEW + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) currentDayCode = arguments?.getString(DAY_CODE) ?: "" @@ -111,7 +114,7 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener { val datePicker = view.findViewById(R.id.date_picker) datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).beGone() - val dateTime = DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString()) + val dateTime = getCurrentDate()!! datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null) AlertDialog.Builder(requireContext()) @@ -144,4 +147,8 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener { override fun printView() { (viewPager?.adapter as? MyMonthPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) } + + override fun getCurrentDate(): DateTime? { + return if (currentDayCode != "") DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString()) else null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MyFragmentHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MyFragmentHolder.kt index faf2008b6..12c707f6a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MyFragmentHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/MyFragmentHolder.kt @@ -1,8 +1,11 @@ package com.simplemobiletools.calendar.pro.fragments import androidx.fragment.app.Fragment +import org.joda.time.DateTime abstract class MyFragmentHolder : Fragment() { + abstract val viewType: Int + abstract fun goToToday() abstract fun showGoToDateDialog() @@ -16,4 +19,6 @@ abstract class MyFragmentHolder : Fragment() { abstract fun getNewEventDayCode(): String abstract fun printView() + + abstract fun getCurrentDate(): DateTime? } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt index e59f12df1..bd208badb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.calendar.pro.activities.MainActivity import com.simplemobiletools.calendar.pro.adapters.MyWeekPagerAdapter import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.helpers.WEEKLY_VIEW import com.simplemobiletools.calendar.pro.helpers.WEEK_START_DATE_TIME import com.simplemobiletools.calendar.pro.interfaces.WeekFragmentListener import com.simplemobiletools.calendar.pro.views.MyScrollView @@ -39,11 +40,13 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener { private var isGoToTodayVisible = false private var weekScrollY = 0 + override val viewType = WEEKLY_VIEW + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val dateTimeString = arguments?.getString(WEEK_START_DATE_TIME) ?: return currentWeekTS = (DateTime.parse(dateTimeString) ?: DateTime()).seconds() - thisWeekTS = currentWeekTS + thisWeekTS = DateTime.parse(requireContext().getDatesWeekDateTime(DateTime())).seconds() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -179,7 +182,7 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener { val view = layoutInflater.inflate(R.layout.date_picker, null) val datePicker = view.findViewById(R.id.date_picker) - val dateTime = Formatter.getUTCDateTimeFromTS(currentWeekTS) + val dateTime = getCurrentDate()!! datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null) AlertDialog.Builder(requireContext()) @@ -309,4 +312,8 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener { }, 1000) } } + + override fun getCurrentDate(): DateTime? { + return if (currentWeekTS != 0L) Formatter.getUTCDateTimeFromTS(currentWeekTS) else null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/YearFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/YearFragmentsHolder.kt index f49d9df7f..1964cc445 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/YearFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/YearFragmentsHolder.kt @@ -13,7 +13,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.activities.MainActivity import com.simplemobiletools.calendar.pro.adapters.MyYearPagerAdapter import com.simplemobiletools.calendar.pro.extensions.config -import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.helpers.* import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.getDialogTheme import com.simplemobiletools.commons.extensions.setupDialogStuff @@ -31,10 +31,13 @@ class YearFragmentsHolder : MyFragmentHolder() { private var currentYear = 0 private var isGoToTodayVisible = false + override val viewType = YEARLY_VIEW + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - currentYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt() - todayYear = currentYear + val dateTimeString = arguments?.getString(YEAR_TO_OPEN) + currentYear = (if (dateTimeString != null) DateTime.parse(dateTimeString) else DateTime()).toString(Formatter.YEAR_PATTERN).toInt() + todayYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -130,4 +133,8 @@ class YearFragmentsHolder : MyFragmentHolder() { override fun printView() { (viewPager?.adapter as? MyYearPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) } + + override fun getCurrentDate(): DateTime? { + return null + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 1d9948d78..a638b3f63 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -13,6 +13,7 @@ const val NEW_EVENT_START_TS = "new_event_start_ts" const val WEEK_START_TIMESTAMP = "week_start_timestamp" const val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration" const val WEEK_START_DATE_TIME = "week_start_date_time" +const val YEAR_TO_OPEN = "year_to_open" const val CALDAV = "Caldav" const val VIEW_TO_OPEN = "view_to_open" const val SHORTCUT_NEW_EVENT = "shortcut_new_event"