avoid unnecessary fragment redraws

This commit is contained in:
tibbi 2017-10-21 18:14:01 +02:00
parent 125fcc3055
commit 387269a58a
11 changed files with 41 additions and 9 deletions

View File

@ -188,7 +188,7 @@ class WidgetMonthlyConfigureActivity : AppCompatActivity(), MonthlyCalendar {
}
}
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>) {
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean) {
runOnUiThread {
mDays = days
top_value.text = month

View File

@ -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<Event>) {
val newHash = events.hashCode()
if (newHash == lastHash) {
return
}
lastHash = newHash
val replaceDescription = context.config.replaceDescription
val sorted = ArrayList<Event>(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, {
if (replaceDescription) it.location else it.description

View File

@ -32,6 +32,7 @@ import java.util.*
class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListener {
private var mEvents: List<Event> = 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)

View File

@ -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<DayMonthly>) {
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, 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

View File

@ -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<Event>) {
val newHash = events.hashCode()
if (newHash == lastHash) {
return
}
lastHash = newHash
this.events = events
updateEvents()
}

View File

@ -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<ArrayList<DayYearly>>) {
override fun updateYearlyCalendar(events: SparseArray<ArrayList<DayYearly>>, 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

View File

@ -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)
}
}

View File

@ -180,7 +180,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() {
}
private val monthlyCalendar = object : MonthlyCalendar {
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>) {
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean) {
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.fragment_month_widget)

View File

@ -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<ArrayList<DayYearly>>, dateTime: DateTime, event: Event) {

View File

@ -4,5 +4,5 @@ import android.content.Context
import com.simplemobiletools.calendar.models.DayMonthly
interface MonthlyCalendar {
fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>)
fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean)
}

View File

@ -5,5 +5,5 @@ import com.simplemobiletools.calendar.models.DayYearly
import java.util.*
interface YearlyCalendar {
fun updateYearlyCalendar(events: SparseArray<ArrayList<DayYearly>>)
fun updateYearlyCalendar(events: SparseArray<ArrayList<DayYearly>>, hashCode: Int)
}