From 387269a58a4af8dcc87058a139a10e3d31000a65 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Oct 2017 18:14:01 +0200 Subject: [PATCH] avoid unnecessary fragment redraws --- .../activities/WidgetMonthlyConfigureActivity.kt | 2 +- .../simplemobiletools/calendar/fragments/DayFragment.kt | 7 +++++++ .../calendar/fragments/EventListFragment.kt | 7 +++++++ .../calendar/fragments/MonthFragment.kt | 9 ++++++++- .../simplemobiletools/calendar/fragments/WeekFragment.kt | 6 ++++++ .../simplemobiletools/calendar/fragments/YearFragment.kt | 7 ++++++- .../calendar/helpers/MonthlyCalendarImpl.kt | 4 ++-- .../calendar/helpers/MyWidgetMonthlyProvider.kt | 2 +- .../calendar/helpers/YearlyCalendarImpl.kt | 2 +- .../calendar/interfaces/MonthlyCalendar.kt | 2 +- .../calendar/interfaces/YearlyCalendar.kt | 2 +- 11 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt index 72d7472d7..bbfe5ec01 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/WidgetMonthlyConfigureActivity.kt @@ -188,7 +188,7 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar { } } - override fun updateMonthlyCalendar(context: Context, month: String, days: List) { + override fun updateMonthlyCalendar(context: Context, month: String, days: List, checkedEvents: Boolean) { runOnUiThread { mDays = days top_value.text = month diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index ddab4da6e..813c3c7c8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -36,6 +36,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen private var mTextColor = 0 private var mDayCode = "" private var mListener: NavigationListener? = null + private var lastHash = 0 lateinit var mRes: Resources lateinit var mHolder: RelativeLayout @@ -118,6 +119,12 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListen } private fun receivedEvents(events: List) { + val newHash = events.hashCode() + if (newHash == lastHash) { + return + } + lastHash = newHash + val replaceDescription = context.config.replaceDescription val sorted = ArrayList(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { if (replaceDescription) it.location else it.description diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index 11448b9c6..d17857395 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -32,6 +32,7 @@ import java.util.* class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListener { private var mEvents: List = ArrayList() private var prevEventsHash = 0 + private var lastHash = 0 lateinit var mView: View override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -62,6 +63,12 @@ class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEvents if (context == null || activity == null) return + val newHash = events.hashCode() + if (newHash == lastHash) { + return + } + lastHash = newHash + val filtered = context.getFilteredEvents(events) val hash = filtered.hashCode() if (prevEventsHash == hash) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt index 757060973..a35712742 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt @@ -39,6 +39,7 @@ class MonthFragment : Fragment(), MonthlyCalendar { private var mDayCode = "" private var mPackageName = "" private var dayLabelHeight = 0 + private var lastHash = 0L var listener: NavigationListener? = null @@ -83,7 +84,13 @@ class MonthFragment : Fragment(), MonthlyCalendar { mCalendar.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode)) } - override fun updateMonthlyCalendar(context: Context, month: String, days: List) { + override fun updateMonthlyCalendar(context: Context, month: String, days: List, checkedEvents: Boolean) { + val newHash = month.hashCode() + days.hashCode().toLong() + if ((lastHash != 0L && !checkedEvents) || lastHash == newHash) { + return + } + lastHash = newHash + activity?.runOnUiThread { mHolder.top_value.apply { text = month diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt index bec446a7c..4f05263a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt @@ -40,6 +40,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { private var maxScrollY = -1 private var mWasDestroyed = false private var primaryColor = 0 + private var lastHash = 0 private var isFragmentVisible = false private var wasFragmentInit = false private var wasExtraHeightAdded = false @@ -214,6 +215,11 @@ class WeekFragment : Fragment(), WeeklyCalendar { } override fun updateWeeklyCalendar(events: ArrayList) { + val newHash = events.hashCode() + if (newHash == lastHash) { + return + } + lastHash = newHash this.events = events updateEvents() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt index 720b245f6..7d3cbe172 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt @@ -25,6 +25,7 @@ class YearFragment : Fragment(), YearlyCalendar { private var mListener: NavigationListener? = null private var mYear = 0 private var mSundayFirst = false + private var lastHash = 0 lateinit var mView: View lateinit var mCalendar: YearlyCalendarImpl @@ -90,10 +91,14 @@ class YearFragment : Fragment(), YearlyCalendar { mListener = listener } - override fun updateYearlyCalendar(events: SparseArray>) { + override fun updateYearlyCalendar(events: SparseArray>, hashCode: Int) { if (!isAdded) return + if (hashCode == lastHash) { + return + } + lastHash = hashCode val res = resources for (i in 1..12) { val monthView = mView.findViewById(res.getIdentifier("month_$i", "id", context.packageName)) as SmallMonthView diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt index 660643f1c..5e43361e7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt @@ -79,7 +79,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) if (markDaysWithEvents) { markDaysWithEvents(days) } else { - mCallback.updateMonthlyCalendar(mContext, monthName, days) + mCallback.updateMonthlyCalendar(mContext, monthName, days, false) } } @@ -108,7 +108,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) days.filter { dayEvents.keys.contains(it.code) }.forEach { it.dayEvents = dayEvents[it.code]!! } - mCallback.updateMonthlyCalendar(mContext, monthName, days) + mCallback.updateMonthlyCalendar(mContext, monthName, days, true) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt index 3ec3353bf..525573fde 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt @@ -180,7 +180,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() { } private val monthlyCalendar = object : MonthlyCalendar { - override fun updateMonthlyCalendar(context: Context, month: String, days: List) { + override fun updateMonthlyCalendar(context: Context, month: String, days: List, checkedEvents: Boolean) { val appWidgetManager = AppWidgetManager.getInstance(context) appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { val views = RemoteViews(context.packageName, R.layout.fragment_month_widget) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt index 9c2ae2294..8fe807c01 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt @@ -41,7 +41,7 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val } } } - callback.updateYearlyCalendar(arr) + callback.updateYearlyCalendar(arr, events.hashCode()) } private fun markDay(arr: SparseArray>, dateTime: DateTime, event: Event) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt index c6ac7cb5a..1f7c188b5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/MonthlyCalendar.kt @@ -4,5 +4,5 @@ import android.content.Context import com.simplemobiletools.calendar.models.DayMonthly interface MonthlyCalendar { - fun updateMonthlyCalendar(context: Context, month: String, days: List) + fun updateMonthlyCalendar(context: Context, month: String, days: List, checkedEvents: Boolean) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt index 226fc2017..cf6ad5da6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt @@ -5,5 +5,5 @@ import com.simplemobiletools.calendar.models.DayYearly import java.util.* interface YearlyCalendar { - fun updateYearlyCalendar(events: SparseArray>) + fun updateYearlyCalendar(events: SparseArray>, hashCode: Int) }