Merge pull request #1582 from Aga-C/keep-selected-date

Added keeping date when changing views (#1403)
This commit is contained in:
Tibor Kaputa 2021-12-20 20:45:22 +01:00 committed by GitHub
commit a382c73073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 121 additions and 27 deletions

View File

@ -799,16 +799,44 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun updateView(view: Int) { private fun updateView(view: Int) {
calendar_fab.beVisibleIf(view != YEARLY_VIEW && view != WEEKLY_VIEW) calendar_fab.beVisibleIf(view != YEARLY_VIEW && view != WEEKLY_VIEW)
val dateCode = getDateCodeToDisplay(view)
config.storedView = view config.storedView = view
checkSwipeRefreshAvailability() checkSwipeRefreshAvailability()
updateViewPager() updateViewPager(dateCode)
if (goToTodayButton?.isVisible == true) { if (goToTodayButton?.isVisible == true) {
shouldGoToTodayBeVisible = false shouldGoToTodayBeVisible = false
invalidateOptionsMenu() 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() val fragment = getFragmentsHolder()
currentFragments.forEach { currentFragments.forEach {
try { try {
@ -823,8 +851,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
val bundle = Bundle() val bundle = Bundle()
when (config.storedView) { when (config.storedView) {
DAILY_VIEW, MONTHLY_VIEW, MONTHLY_DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode) DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode ?: Formatter.getTodayCode())
WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, getThisWeekDateTime()) 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 fragment.arguments = bundle
@ -865,22 +895,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) { private fun getFragmentsHolder() = when (config.storedView) {
DAILY_VIEW -> DayFragmentsHolder() DAILY_VIEW -> DayFragmentsHolder()
MONTHLY_VIEW -> MonthFragmentsHolder() MONTHLY_VIEW -> MonthFragmentsHolder()

View File

@ -38,6 +38,8 @@ import com.simplemobiletools.calendar.pro.receivers.NotificationReceiver
import com.simplemobiletools.calendar.pro.services.SnoozeService import com.simplemobiletools.calendar.pro.services.SnoozeService
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.LocalDate import org.joda.time.LocalDate
import java.util.* import java.util.*
@ -602,3 +604,19 @@ fun Context.editEvent(event: ListEvent) {
startActivity(this) 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()
}
}

View File

@ -12,6 +12,7 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.MainActivity import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.adapters.MyDayPagerAdapter import com.simplemobiletools.calendar.pro.adapters.MyDayPagerAdapter
import com.simplemobiletools.calendar.pro.extensions.config 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.DAY_CODE
import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
@ -32,6 +33,8 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener {
private var currentDayCode = "" private var currentDayCode = ""
private var isGoToTodayVisible = false private var isGoToTodayVisible = false
override val viewType = DAILY_VIEW
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
currentDayCode = arguments?.getString(DAY_CODE) ?: "" currentDayCode = arguments?.getString(DAY_CODE) ?: ""
@ -108,7 +111,7 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener {
val view = layoutInflater.inflate(R.layout.date_picker, null) val view = layoutInflater.inflate(R.layout.date_picker, null)
val datePicker = view.findViewById<DatePicker>(R.id.date_picker) val datePicker = view.findViewById<DatePicker>(R.id.date_picker)
val dateTime = Formatter.getDateTimeFromCode(currentDayCode) val dateTime = getCurrentDate()!!
datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null) datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null)
AlertDialog.Builder(requireContext()) AlertDialog.Builder(requireContext())
@ -142,4 +145,12 @@ class DayFragmentsHolder : MyFragmentHolder(), NavigationListener {
override fun printView() { override fun printView() {
(viewPager?.adapter as? MyDayPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) (viewPager?.adapter as? MyDayPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0)
} }
override fun getCurrentDate(): DateTime? {
return if (currentDayCode != "") {
Formatter.getDateTimeFromCode(currentDayCode)
} else {
null
}
}
} }

View File

@ -13,6 +13,7 @@ import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.adapters.EventListAdapter import com.simplemobiletools.calendar.pro.adapters.EventListAdapter
import com.simplemobiletools.calendar.pro.extensions.* 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.helpers.Formatter
import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.ListEvent import com.simplemobiletools.calendar.pro.models.ListEvent
@ -46,6 +47,8 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
lateinit var mView: View lateinit var mView: View
override val viewType = EVENTS_LIST_VIEW
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mView = inflater.inflate(R.layout.fragment_event_list, container, false) mView = inflater.inflate(R.layout.fragment_event_list, container, false)
mView.background = ColorDrawable(requireContext().config.backgroundColor) mView.background = ColorDrawable(requireContext().config.backgroundColor)
@ -248,4 +251,6 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
}, 1000) }, 1000)
} }
} }
override fun getCurrentDate() = null
} }

View File

@ -16,6 +16,7 @@ import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.getMonthCode import com.simplemobiletools.calendar.pro.extensions.getMonthCode
import com.simplemobiletools.calendar.pro.helpers.DAY_CODE import com.simplemobiletools.calendar.pro.helpers.DAY_CODE
import com.simplemobiletools.calendar.pro.helpers.Formatter 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.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.getDialogTheme import com.simplemobiletools.commons.extensions.getDialogTheme
@ -34,6 +35,8 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener {
private var currentDayCode = "" private var currentDayCode = ""
private var isGoToTodayVisible = false private var isGoToTodayVisible = false
override val viewType = MONTHLY_DAILY_VIEW
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
currentDayCode = arguments?.getString(DAY_CODE) ?: "" currentDayCode = arguments?.getString(DAY_CODE) ?: ""
@ -111,7 +114,7 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener {
val datePicker = view.findViewById<DatePicker>(R.id.date_picker) val datePicker = view.findViewById<DatePicker>(R.id.date_picker)
datePicker.findViewById<View>(Resources.getSystem().getIdentifier("day", "id", "android")).beGone() datePicker.findViewById<View>(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) datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null)
AlertDialog.Builder(requireContext()) AlertDialog.Builder(requireContext())
@ -147,4 +150,12 @@ class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener {
} }
override fun printView() {} override fun printView() {}
override fun getCurrentDate(): DateTime? {
return if (currentDayCode != "") {
DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString())
} else {
null
}
}
} }

View File

@ -16,6 +16,7 @@ import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.getMonthCode import com.simplemobiletools.calendar.pro.extensions.getMonthCode
import com.simplemobiletools.calendar.pro.helpers.DAY_CODE import com.simplemobiletools.calendar.pro.helpers.DAY_CODE
import com.simplemobiletools.calendar.pro.helpers.Formatter 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.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.getDialogTheme import com.simplemobiletools.commons.extensions.getDialogTheme
@ -34,6 +35,8 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener {
private var currentDayCode = "" private var currentDayCode = ""
private var isGoToTodayVisible = false private var isGoToTodayVisible = false
override val viewType = MONTHLY_VIEW
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
currentDayCode = arguments?.getString(DAY_CODE) ?: "" currentDayCode = arguments?.getString(DAY_CODE) ?: ""
@ -111,7 +114,7 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener {
val datePicker = view.findViewById<DatePicker>(R.id.date_picker) val datePicker = view.findViewById<DatePicker>(R.id.date_picker)
datePicker.findViewById<View>(Resources.getSystem().getIdentifier("day", "id", "android")).beGone() datePicker.findViewById<View>(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) datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null)
AlertDialog.Builder(requireContext()) AlertDialog.Builder(requireContext())
@ -144,4 +147,12 @@ class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener {
override fun printView() { override fun printView() {
(viewPager?.adapter as? MyMonthPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) (viewPager?.adapter as? MyMonthPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0)
} }
override fun getCurrentDate(): DateTime? {
return if (currentDayCode != "") {
DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString())
} else {
null
}
}
} }

View File

@ -1,8 +1,11 @@
package com.simplemobiletools.calendar.pro.fragments package com.simplemobiletools.calendar.pro.fragments
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.joda.time.DateTime
abstract class MyFragmentHolder : Fragment() { abstract class MyFragmentHolder : Fragment() {
abstract val viewType: Int
abstract fun goToToday() abstract fun goToToday()
abstract fun showGoToDateDialog() abstract fun showGoToDateDialog()
@ -16,4 +19,6 @@ abstract class MyFragmentHolder : Fragment() {
abstract fun getNewEventDayCode(): String abstract fun getNewEventDayCode(): String
abstract fun printView() abstract fun printView()
abstract fun getCurrentDate(): DateTime?
} }

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.adapters.MyWeekPagerAdapter import com.simplemobiletools.calendar.pro.adapters.MyWeekPagerAdapter
import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.helpers.Formatter 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.helpers.WEEK_START_DATE_TIME
import com.simplemobiletools.calendar.pro.interfaces.WeekFragmentListener import com.simplemobiletools.calendar.pro.interfaces.WeekFragmentListener
import com.simplemobiletools.calendar.pro.views.MyScrollView import com.simplemobiletools.calendar.pro.views.MyScrollView
@ -39,11 +40,13 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener {
private var isGoToTodayVisible = false private var isGoToTodayVisible = false
private var weekScrollY = 0 private var weekScrollY = 0
override val viewType = WEEKLY_VIEW
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val dateTimeString = arguments?.getString(WEEK_START_DATE_TIME) ?: return val dateTimeString = arguments?.getString(WEEK_START_DATE_TIME) ?: return
currentWeekTS = (DateTime.parse(dateTimeString) ?: DateTime()).seconds() 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? { 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 view = layoutInflater.inflate(R.layout.date_picker, null)
val datePicker = view.findViewById<DatePicker>(R.id.date_picker) val datePicker = view.findViewById<DatePicker>(R.id.date_picker)
val dateTime = Formatter.getUTCDateTimeFromTS(currentWeekTS) val dateTime = getCurrentDate()!!
datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null) datePicker.init(dateTime.year, dateTime.monthOfYear - 1, dateTime.dayOfMonth, null)
AlertDialog.Builder(requireContext()) AlertDialog.Builder(requireContext())
@ -309,4 +312,12 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener {
}, 1000) }, 1000)
} }
} }
override fun getCurrentDate(): DateTime? {
return if (currentWeekTS != 0L) {
Formatter.getUTCDateTimeFromTS(currentWeekTS)
} else {
null
}
}
} }

View File

@ -14,6 +14,8 @@ import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.adapters.MyYearPagerAdapter import com.simplemobiletools.calendar.pro.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.YEARLY_VIEW
import com.simplemobiletools.calendar.pro.helpers.YEAR_TO_OPEN
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.getDialogTheme import com.simplemobiletools.commons.extensions.getDialogTheme
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
@ -31,10 +33,13 @@ class YearFragmentsHolder : MyFragmentHolder() {
private var currentYear = 0 private var currentYear = 0
private var isGoToTodayVisible = false private var isGoToTodayVisible = false
override val viewType = YEARLY_VIEW
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
currentYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt() val dateTimeString = arguments?.getString(YEAR_TO_OPEN)
todayYear = currentYear 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? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -130,4 +135,6 @@ class YearFragmentsHolder : MyFragmentHolder() {
override fun printView() { override fun printView() {
(viewPager?.adapter as? MyYearPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0) (viewPager?.adapter as? MyYearPagerAdapter)?.printCurrentView(viewPager?.currentItem ?: 0)
} }
override fun getCurrentDate() = null
} }

View File

@ -13,6 +13,7 @@ const val NEW_EVENT_START_TS = "new_event_start_ts"
const val WEEK_START_TIMESTAMP = "week_start_timestamp" const val WEEK_START_TIMESTAMP = "week_start_timestamp"
const val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration" const val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration"
const val WEEK_START_DATE_TIME = "week_start_date_time" const val WEEK_START_DATE_TIME = "week_start_date_time"
const val YEAR_TO_OPEN = "year_to_open"
const val CALDAV = "Caldav" const val CALDAV = "Caldav"
const val VIEW_TO_OPEN = "view_to_open" const val VIEW_TO_OPEN = "view_to_open"
const val SHORTCUT_NEW_EVENT = "shortcut_new_event" const val SHORTCUT_NEW_EVENT = "shortcut_new_event"