couple smaller code style updates

This commit is contained in:
tibbi 2018-10-24 22:28:15 +02:00
parent 4caacb8173
commit 89cbad89a9
6 changed files with 63 additions and 79 deletions

View File

@ -133,7 +133,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, r
private fun shareEvents() = activity.shareEvents(selectedKeys.distinct()) private fun shareEvents() = activity.shareEvents(selectedKeys.distinct())
private fun askConfirmDelete() { private fun askConfirmDelete() {
val eventIds = selectedKeys.toList() val eventIds = selectedKeys.toMutableList()
val eventsToDelete = events.filter { selectedKeys.contains(it.id) } val eventsToDelete = events.filter { selectedKeys.contains(it.id) }
val timestamps = eventsToDelete.map { it.startTS } val timestamps = eventsToDelete.map { it.startTS }
val positions = getSelectedItemPositions() val positions = getSelectedItemPositions()

View File

@ -198,7 +198,7 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListIt
private fun shareEvents() = activity.shareEvents(selectedKeys.distinct()) private fun shareEvents() = activity.shareEvents(selectedKeys.distinct())
private fun askConfirmDelete() { private fun askConfirmDelete() {
val eventIds = selectedKeys.toList() val eventIds = selectedKeys.toMutableList()
val eventsToDelete = listItems.filter { selectedKeys.contains((it as? ListEvent)?.id) } as List<ListEvent> val eventsToDelete = listItems.filter { selectedKeys.contains((it as? ListEvent)?.id) } as List<ListEvent>
val timestamps = eventsToDelete.map { (it as? ListEvent)?.startTS }.filterNotNull() val timestamps = eventsToDelete.map { (it as? ListEvent)?.startTS }.filterNotNull()

View File

@ -6,7 +6,10 @@ import android.graphics.Rect
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.util.SparseIntArray import android.util.SparseIntArray
import android.view.* import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
@ -60,6 +63,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
lateinit var mScrollView: MyScrollView lateinit var mScrollView: MyScrollView
lateinit var mCalendar: WeeklyCalendarImpl lateinit var mCalendar: WeeklyCalendarImpl
lateinit var mRes: Resources lateinit var mRes: Resources
lateinit var mConfig: Config
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -67,12 +71,13 @@ class WeekFragment : Fragment(), WeeklyCalendar {
it.map { eventTypeColors.put(it.id, it.color) } it.map { eventTypeColors.put(it.id, it.color) }
} }
mRowHeight = context!!.resources.getDimension(R.dimen.weekly_view_row_height) mRes = context!!.resources
minScrollY = (mRowHeight * context!!.config.startWeeklyAt).toInt() mConfig = context!!.config
mRowHeight = mRes.getDimension(R.dimen.weekly_view_row_height)
minScrollY = (mRowHeight * mConfig.startWeeklyAt).toInt()
mWeekTimestamp = arguments!!.getInt(WEEK_START_TIMESTAMP) mWeekTimestamp = arguments!!.getInt(WEEK_START_TIMESTAMP)
dimPastEvents = context!!.config.dimPastEvents dimPastEvents = mConfig.dimPastEvents
primaryColor = context!!.getAdjustedPrimaryColor() primaryColor = context!!.getAdjustedPrimaryColor()
mRes = resources
allDayRows.add(HashSet()) allDayRows.add(HashSet())
mCalendar = WeeklyCalendarImpl(this, context!!) mCalendar = WeeklyCalendarImpl(this, context!!)
} }
@ -96,11 +101,6 @@ class WeekFragment : Fragment(), WeeklyCalendar {
return mView return mView
} }
override fun onPause() {
super.onPause()
wasExtraHeightAdded = true
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
setupDayLabels() setupDayLabels()
@ -111,8 +111,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
return@onGlobalLayout return@onGlobalLayout
} }
minScrollY = (mRowHeight * context!!.config.startWeeklyAt).toInt() minScrollY = (mRowHeight * mConfig.startWeeklyAt).toInt()
maxScrollY = (mRowHeight * context!!.config.endWeeklyAt).toInt() maxScrollY = (mRowHeight * mConfig.endWeeklyAt).toInt()
val bounds = Rect() val bounds = Rect()
week_events_holder.getGlobalVisibleRect(bounds) week_events_holder.getGlobalVisibleRect(bounds)
@ -124,6 +124,16 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} }
} }
override fun onPause() {
super.onPause()
wasExtraHeightAdded = true
}
override fun onDestroyView() {
super.onDestroyView()
mWasDestroyed = true
}
override fun setMenuVisibility(menuVisible: Boolean) { override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
isFragmentVisible = menuVisible isFragmentVisible = menuVisible
@ -139,11 +149,11 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private fun setupDayLabels() { private fun setupDayLabels() {
var curDay = Formatter.getDateTimeFromTS(mWeekTimestamp) var curDay = Formatter.getDateTimeFromTS(mWeekTimestamp)
val textColor = context!!.config.textColor val textColor = mConfig.textColor
val todayCode = Formatter.getDayCodeFromDateTime(DateTime()) val todayCode = Formatter.getDayCodeFromDateTime(DateTime())
for (i in 0..6) { for (i in 0..6) {
val dayCode = Formatter.getDayCodeFromDateTime(curDay) val dayCode = Formatter.getDayCodeFromDateTime(curDay)
val dayLetters = mRes.getStringArray(R.array.week_day_letters).toList() as ArrayList<String> val dayLetters = mRes.getStringArray(R.array.week_day_letters).toMutableList() as ArrayList<String>
val dayLetter = dayLetters[curDay.dayOfWeek - 1] val dayLetter = dayLetters[curDay.dayOfWeek - 1]
mView.findViewById<TextView>(mRes.getIdentifier("week_day_label_$i", "id", context!!.packageName)).apply { mView.findViewById<TextView>(mRes.getIdentifier("week_day_label_$i", "id", context!!.packageName)).apply {
@ -186,7 +196,6 @@ class WeekFragment : Fragment(), WeeklyCalendar {
selectedGrid?.animation?.cancel() selectedGrid?.animation?.cancel()
selectedGrid?.beGone() selectedGrid?.beGone()
//val rowHeight = resources.getDimension(R.dimen.weekly_view_row_height)
val hour = (event.y / mRowHeight).toInt() val hour = (event.y / mRowHeight).toInt()
selectedGrid = (inflater.inflate(R.layout.week_grid_item, null, false) as ImageView).apply { selectedGrid = (inflater.inflate(R.layout.week_grid_item, null, false) as ImageView).apply {
view.addView(this) view.addView(this)
@ -214,24 +223,13 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} }
override fun updateWeeklyCalendar(events: ArrayList<Event>) { override fun updateWeeklyCalendar(events: ArrayList<Event>) {
if (context == null) {
return
}
val newHash = events.hashCode() val newHash = events.hashCode()
if (newHash == lastHash) { if (newHash == lastHash || mWasDestroyed || context == null) {
return return
} }
lastHash = newHash lastHash = newHash
this.events = events this.events = events
updateEvents()
}
private fun updateEvents() {
if (mWasDestroyed) {
return
}
activity!!.runOnUiThread { activity!!.runOnUiThread {
if (context != null && activity != null && isAdded) { if (context != null && activity != null && isAdded) {
@ -254,7 +252,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val minimalHeight = mRes.getDimension(R.dimen.weekly_view_minimal_event_height).toInt() val minimalHeight = mRes.getDimension(R.dimen.weekly_view_minimal_event_height).toInt()
var hadAllDayEvent = false var hadAllDayEvent = false
val replaceDescription = context!!.config.replaceDescription val replaceDescription = mConfig.replaceDescription
val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { if (replaceDescription) it.location else it.description })) val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { if (replaceDescription) it.location else it.description }))
for (event in sorted) { for (event in sorted) {
if (event.getIsAllDay() || Formatter.getDayCodeFromTS(event.startTS) != Formatter.getDayCodeFromTS(event.endTS)) { if (event.getIsAllDay() || Formatter.getDayCodeFromTS(event.startTS) != Formatter.getDayCodeFromTS(event.endTS)) {
@ -263,7 +261,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} else { } else {
val startDateTime = Formatter.getDateTimeFromTS(event.startTS) val startDateTime = Formatter.getDateTimeFromTS(event.startTS)
val endDateTime = Formatter.getDateTimeFromTS(event.endTS) val endDateTime = Formatter.getDateTimeFromTS(event.endTS)
val dayOfWeek = startDateTime.plusDays(if (context!!.config.isSundayFirst) 1 else 0).dayOfWeek - 1 val dayOfWeek = startDateTime.plusDays(if (mConfig.isSundayFirst) 1 else 0).dayOfWeek - 1
val layout = getColumnWithId(dayOfWeek) val layout = getColumnWithId(dayOfWeek)
val startMinutes = startDateTime.minuteOfDay val startMinutes = startDateTime.minuteOfDay
@ -318,7 +316,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
applyColorFilter(primaryColor) applyColorFilter(primaryColor)
mView.week_events_holder.addView(this, 0) mView.week_events_holder.addView(this, 0)
val extraWidth = (todayColumn.width * 0.3).toInt() val extraWidth = (todayColumn.width * 0.3).toInt()
val markerHeight = resources.getDimension(R.dimen.weekly_view_now_height).toInt() val markerHeight = mRes.getDimension(R.dimen.weekly_view_now_height).toInt()
(layoutParams as RelativeLayout.LayoutParams).apply { (layoutParams as RelativeLayout.LayoutParams).apply {
width = todayColumn.width + extraWidth width = todayColumn.width + extraWidth
height = markerHeight height = markerHeight
@ -330,14 +328,11 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} }
private fun checkTopHolderHeight() { private fun checkTopHolderHeight() {
mView.week_top_holder.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { mView.week_top_holder.onGlobalLayout {
override fun onGlobalLayout() { if (isFragmentVisible && activity != null && !mWasDestroyed) {
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this) mListener?.updateHoursTopMargin(mView.week_top_holder.height)
if (isFragmentVisible && activity != null) {
mListener?.updateHoursTopMargin(mView.week_top_holder.height)
}
} }
}) }
} }
private fun addAllDayEvent(event: Event) { private fun addAllDayEvent(event: Event) {
@ -361,7 +356,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS).toLocalDate(), Formatter.getDateTimeFromTS(maxTS).toLocalDate()).days val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS).toLocalDate(), Formatter.getDateTimeFromTS(maxTS).toLocalDate()).days
val startDateTimeInWeek = Formatter.getDateTimeFromTS(minTS) val startDateTimeInWeek = Formatter.getDateTimeFromTS(minTS)
val firstDayIndex = (startDateTimeInWeek.dayOfWeek - if (context!!.config.isSundayFirst) 0 else 1) % 7 val firstDayIndex = (startDateTimeInWeek.dayOfWeek - if (mConfig.isSundayFirst) 0 else 1) % 7
var doesEventFit: Boolean var doesEventFit: Boolean
val cnt = allDayRows.size - 1 val cnt = allDayRows.size - 1
var wasEventHandled = false var wasEventHandled = false
@ -415,12 +410,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} }
private fun calculateExtraHeight() { private fun calculateExtraHeight() {
mView.week_top_holder.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { mView.week_top_holder.onGlobalLayout {
override fun onGlobalLayout() { if (activity != null && !mWasDestroyed) {
if (activity == null)
return
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (isFragmentVisible) { if (isFragmentVisible) {
mListener?.updateHoursTopMargin(mView.week_top_holder.height) mListener?.updateHoursTopMargin(mView.week_top_holder.height)
} }
@ -430,12 +421,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
wasExtraHeightAdded = true wasExtraHeightAdded = true
} }
} }
}) }
}
override fun onDestroyView() {
super.onDestroyView()
mWasDestroyed = true
} }
private fun getColumnWithId(id: Int) = mView.findViewById<ViewGroup>(mRes.getIdentifier("week_column_$id", "id", context!!.packageName)) private fun getColumnWithId(id: Int) = mView.findViewById<ViewGroup>(mRes.getIdentifier("week_column_$id", "id", context!!.packageName))

View File

@ -2,11 +2,11 @@ package com.simplemobiletools.calendar.fragments
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.MainActivity import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter
@ -67,11 +67,9 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener {
weekHolder!!.week_view_view_pager.apply { weekHolder!!.week_view_view_pager.apply {
adapter = weeklyAdapter adapter = weeklyAdapter
addOnPageChangeListener(object : ViewPager.OnPageChangeListener { addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) { override fun onPageScrollStateChanged(state: Int) {}
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
}
override fun onPageSelected(position: Int) { override fun onPageSelected(position: Int) {
currentWeekTS = weekTSs[position] currentWeekTS = weekTSs[position]

View File

@ -302,7 +302,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
} }
private fun initWeekDayLetters() { private fun initWeekDayLetters() {
dayLetters = context.resources.getStringArray(R.array.week_day_letters).toList() as ArrayList<String> dayLetters = context.resources.getStringArray(R.array.week_day_letters).toMutableList() as ArrayList<String>
if (context.config.isSundayFirst) { if (context.config.isSundayFirst) {
dayLetters.moveLastItemToFront() dayLetters.moveLastItemToFront()
} }

View File

@ -851,27 +851,6 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout
android:id="@+id/settings_customize_widget_colors_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingLeft="@dimen/normal_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingRight="@dimen/normal_margin"
android:paddingBottom="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_customize_widget_colors_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingStart="@dimen/medium_margin"
android:paddingLeft="@dimen/medium_margin"
android:text="@string/customize_widget_colors"/>
</RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/settings_list_widget_view_to_open_holder" android:id="@+id/settings_list_widget_view_to_open_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -906,6 +885,27 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout
android:id="@+id/settings_customize_widget_colors_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingLeft="@dimen/normal_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingRight="@dimen/normal_margin"
android:paddingBottom="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_customize_widget_colors_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingStart="@dimen/medium_margin"
android:paddingLeft="@dimen/medium_margin"
android:text="@string/customize_widget_colors"/>
</RelativeLayout>
<View <View
android:id="@+id/events_divider" android:id="@+id/events_divider"
android:layout_width="match_parent" android:layout_width="match_parent"