mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
update the EventListAdapter code
This commit is contained in:
parent
316832d49c
commit
9b24d4420e
@ -47,7 +47,7 @@ ext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.simplemobiletools:commons:2.38.11'
|
compile 'com.simplemobiletools:commons:2.39.0'
|
||||||
compile 'joda-time:joda-time:2.9.9'
|
compile 'joda-time:joda-time:2.9.9'
|
||||||
compile 'com.facebook.stetho:stetho:1.5.0'
|
compile 'com.facebook.stetho:stetho:1.5.0'
|
||||||
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
|
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
|
||||||
|
@ -143,8 +143,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateWidgets()
|
updateWidgets()
|
||||||
if (config.storedView != EVENTS_LIST_VIEW)
|
updateTextColors(calendar_coordinator)
|
||||||
updateTextColors(calendar_coordinator)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
|
@ -1,17 +1,11 @@
|
|||||||
package com.simplemobiletools.calendar.adapters
|
package com.simplemobiletools.calendar.adapters
|
||||||
|
|
||||||
import android.graphics.PorterDuff
|
import android.graphics.PorterDuff
|
||||||
import android.graphics.drawable.Drawable
|
import android.view.View
|
||||||
import android.support.v7.view.ActionMode
|
import android.view.ViewGroup
|
||||||
import android.support.v7.widget.RecyclerView
|
|
||||||
import android.view.*
|
|
||||||
import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback
|
|
||||||
import com.bignerdranch.android.multiselector.MultiSelector
|
|
||||||
import com.bignerdranch.android.multiselector.SwappingHolder
|
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
import com.simplemobiletools.calendar.activities.SimpleActivity
|
import com.simplemobiletools.calendar.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
|
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
|
||||||
import com.simplemobiletools.calendar.extensions.config
|
|
||||||
import com.simplemobiletools.calendar.extensions.shareEvents
|
import com.simplemobiletools.calendar.extensions.shareEvents
|
||||||
import com.simplemobiletools.calendar.helpers.Formatter
|
import com.simplemobiletools.calendar.helpers.Formatter
|
||||||
import com.simplemobiletools.calendar.interfaces.DeleteEventsListener
|
import com.simplemobiletools.calendar.interfaces.DeleteEventsListener
|
||||||
@ -23,97 +17,126 @@ import com.simplemobiletools.commons.extensions.beInvisibleIf
|
|||||||
import kotlinx.android.synthetic.main.event_list_item.view.*
|
import kotlinx.android.synthetic.main.event_list_item.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>, val listener: DeleteEventsListener?, val itemClick: (ListEvent) -> Unit) :
|
class EventListAdapter(activity: SimpleActivity, val listItems: List<ListItem>, val listener: DeleteEventsListener?, itemClick: (Any) -> Unit) :
|
||||||
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
MyAdapter(activity, itemClick) {
|
||||||
val multiSelector = MultiSelector()
|
|
||||||
val views = ArrayList<View>()
|
|
||||||
|
|
||||||
val ITEM_EVENT = 0
|
private val ITEM_EVENT = 0
|
||||||
val ITEM_HEADER = 1
|
private val ITEM_HEADER = 1
|
||||||
|
|
||||||
companion object {
|
private val topDivider = resources.getDrawable(R.drawable.divider_width)
|
||||||
var actMode: ActionMode? = null
|
private val allDayString = resources.getString(R.string.all_day)
|
||||||
val markedItems = HashSet<Int>()
|
private val replaceDescriptionWithLocation = config.replaceDescription
|
||||||
|
private val redTextColor = resources.getColor(R.color.red_text)
|
||||||
|
private val now = (System.currentTimeMillis() / 1000).toInt()
|
||||||
|
private val todayDate = Formatter.getDayTitle(activity, Formatter.getDayCodeFromTS(now))
|
||||||
|
|
||||||
var topDivider: Drawable? = null
|
override fun getActionMenuId() = R.menu.cab_event_list
|
||||||
var now = (System.currentTimeMillis() / 1000).toInt()
|
|
||||||
var primaryColor = 0
|
|
||||||
var textColor = 0
|
|
||||||
var redTextColor = 0
|
|
||||||
var todayDate = ""
|
|
||||||
var allDayString = ""
|
|
||||||
var replaceDescriptionWithLocation = false
|
|
||||||
|
|
||||||
fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) {
|
override fun markItemSelection(select: Boolean, pos: Int) {
|
||||||
itemView.event_item_frame.isSelected = select
|
itemViews[pos]?.event_item_frame?.isSelected = select
|
||||||
if (pos == -1)
|
}
|
||||||
return
|
|
||||||
|
|
||||||
if (select)
|
override fun actionItemPressed(id: Int) {
|
||||||
markedItems.add(pos)
|
when (id) {
|
||||||
else
|
R.id.cab_share -> shareEvents()
|
||||||
markedItems.remove(pos)
|
R.id.cab_delete -> askConfirmDelete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyAdapter.ViewHolder {
|
||||||
activity.resources.apply {
|
val layoutId = if (viewType == ITEM_EVENT) R.layout.event_list_item else R.layout.event_list_section
|
||||||
allDayString = getString(R.string.all_day)
|
val view = activity.layoutInflater.inflate(layoutId, parent, false)
|
||||||
topDivider = getDrawable(R.drawable.divider_width)
|
return createViewHolder(view)
|
||||||
redTextColor = getColor(R.color.red_text)
|
|
||||||
}
|
|
||||||
|
|
||||||
textColor = activity.config.textColor
|
|
||||||
primaryColor = activity.config.primaryColor
|
|
||||||
val mTodayCode = Formatter.getDayCodeFromTS(now)
|
|
||||||
todayDate = Formatter.getDayTitle(activity, mTodayCode)
|
|
||||||
replaceDescriptionWithLocation = activity.config.replaceDescription
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
|
override fun onBindViewHolder(holder: MyAdapter.ViewHolder, position: Int) {
|
||||||
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
|
val listItem = listItems[position]
|
||||||
when (item.itemId) {
|
val view = holder.bindView(listItem) {
|
||||||
R.id.cab_share -> shareEvents()
|
if (listItem is ListSection) {
|
||||||
R.id.cab_delete -> askConfirmDelete()
|
setupListSection(it, listItem, position)
|
||||||
else -> return false
|
} else if (listItem is ListEvent) {
|
||||||
|
setupListEvent(it, listItem)
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
itemViews.put(position, view)
|
||||||
|
toggleItemSelection(selectedPositions.contains(position), position)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
|
override fun getItemCount() = listItems.size
|
||||||
super.onCreateActionMode(actionMode, menu)
|
|
||||||
actMode = actionMode
|
override fun getItemViewType(position: Int) = if (listItems[position] is ListEvent) ITEM_EVENT else ITEM_HEADER
|
||||||
activity.menuInflater.inflate(R.menu.cab_event_list, menu)
|
|
||||||
return true
|
private fun setupListEvent(view: View, listEvent: ListEvent) {
|
||||||
|
view.apply {
|
||||||
|
event_item_title.text = listEvent.title
|
||||||
|
event_item_description.text = if (replaceDescriptionWithLocation) listEvent.location else listEvent.description
|
||||||
|
event_item_start.text = if (listEvent.isAllDay) allDayString else Formatter.getTimeFromTS(context, listEvent.startTS)
|
||||||
|
event_item_end.beInvisibleIf(listEvent.startTS == listEvent.endTS)
|
||||||
|
event_item_color.setColorFilter(listEvent.color, PorterDuff.Mode.SRC_IN)
|
||||||
|
|
||||||
|
if (listEvent.startTS != listEvent.endTS) {
|
||||||
|
val startCode = Formatter.getDayCodeFromTS(listEvent.startTS)
|
||||||
|
val endCode = Formatter.getDayCodeFromTS(listEvent.endTS)
|
||||||
|
|
||||||
|
event_item_end.apply {
|
||||||
|
text = Formatter.getTimeFromTS(context, listEvent.endTS)
|
||||||
|
if (startCode != endCode) {
|
||||||
|
if (listEvent.isAllDay) {
|
||||||
|
text = Formatter.getDateFromCode(context, endCode, true)
|
||||||
|
} else {
|
||||||
|
append(" (${Formatter.getDateFromCode(context, endCode, true)})")
|
||||||
|
}
|
||||||
|
} else if (listEvent.isAllDay) {
|
||||||
|
beInvisible()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var startTextColor = textColor
|
||||||
|
var endTextColor = textColor
|
||||||
|
if (listEvent.startTS <= now && listEvent.endTS <= now) {
|
||||||
|
if (listEvent.isAllDay) {
|
||||||
|
if (Formatter.getDayCodeFromTS(listEvent.startTS) == Formatter.getDayCodeFromTS(now))
|
||||||
|
startTextColor = primaryColor
|
||||||
|
} else {
|
||||||
|
startTextColor = redTextColor
|
||||||
|
}
|
||||||
|
endTextColor = redTextColor
|
||||||
|
} else if (listEvent.startTS <= now && listEvent.endTS >= now) {
|
||||||
|
startTextColor = primaryColor
|
||||||
|
}
|
||||||
|
|
||||||
|
event_item_start.setTextColor(startTextColor)
|
||||||
|
event_item_end.setTextColor(endTextColor)
|
||||||
|
event_item_title.setTextColor(startTextColor)
|
||||||
|
event_item_description.setTextColor(startTextColor)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu) = true
|
private fun setupListSection(view: View, listSection: ListSection, position: Int) {
|
||||||
|
view.event_item_title.apply {
|
||||||
override fun onDestroyActionMode(actionMode: ActionMode?) {
|
text = listSection.title
|
||||||
super.onDestroyActionMode(actionMode)
|
setCompoundDrawablesWithIntrinsicBounds(null, if (position == 0) null else topDivider, null, null)
|
||||||
views.forEach { toggleItemSelection(it, false) }
|
setTextColor(if (listSection.title == todayDate) primaryColor else textColor)
|
||||||
markedItems.clear()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shareEvents() {
|
private fun shareEvents() {
|
||||||
val selections = multiSelector.selectedPositions
|
val eventIds = ArrayList<Int>(selectedPositions.size)
|
||||||
val eventIds = ArrayList<Int>(selections.size)
|
selectedPositions.forEach {
|
||||||
selections.forEach {
|
eventIds.add((listItems[it] as ListEvent).id)
|
||||||
eventIds.add((mItems[it] as ListEvent).id)
|
|
||||||
}
|
}
|
||||||
activity.shareEvents(eventIds.distinct())
|
activity.shareEvents(eventIds.distinct())
|
||||||
actMode?.finish()
|
finishActMode()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun askConfirmDelete() {
|
private fun askConfirmDelete() {
|
||||||
val selections = multiSelector.selectedPositions
|
val eventIds = ArrayList<Int>(selectedPositions.size)
|
||||||
val eventIds = ArrayList<Int>(selections.size)
|
val timestamps = ArrayList<Int>(selectedPositions.size)
|
||||||
val timestamps = ArrayList<Int>(selections.size)
|
|
||||||
|
|
||||||
selections.forEach {
|
selectedPositions.forEach {
|
||||||
eventIds.add((mItems[it] as ListEvent).id)
|
eventIds.add((listItems[it] as ListEvent).id)
|
||||||
timestamps.add((mItems[it] as ListEvent).startTS)
|
timestamps.add((listItems[it] as ListEvent).startTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
DeleteEventDialog(activity, eventIds) {
|
DeleteEventDialog(activity, eventIds) {
|
||||||
@ -122,128 +145,7 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>,
|
|||||||
} else {
|
} else {
|
||||||
listener?.addEventRepeatException(eventIds, timestamps)
|
listener?.addEventRepeatException(eventIds, timestamps)
|
||||||
}
|
}
|
||||||
actMode?.finish()
|
finishActMode()
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemViewType(position: Int) = if (mItems[position] is ListEvent) ITEM_EVENT else ITEM_HEADER
|
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
|
|
||||||
val layoutId = if (viewType == ITEM_EVENT) R.layout.event_list_item else R.layout.event_list_section
|
|
||||||
val view = LayoutInflater.from(parent?.context).inflate(layoutId, parent, false)
|
|
||||||
|
|
||||||
return if (viewType == ITEM_EVENT) {
|
|
||||||
EventListAdapter.ViewHolder(activity, view, itemClick)
|
|
||||||
} else {
|
|
||||||
EventListAdapter.SectionHolder(view)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
||||||
if (holder.itemViewType == ITEM_EVENT) {
|
|
||||||
views.add((holder as ViewHolder).bindView(multiSelectorMode, multiSelector, mItems[position], position))
|
|
||||||
} else {
|
|
||||||
(holder as SectionHolder).bindView(mItems[position])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemCount() = mItems.size
|
|
||||||
|
|
||||||
class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (ListEvent) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
|
|
||||||
fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, listItem: ListItem, pos: Int): View {
|
|
||||||
val item = listItem as ListEvent
|
|
||||||
itemView.apply {
|
|
||||||
event_item_title.text = item.title
|
|
||||||
event_item_description.text = if (replaceDescriptionWithLocation) item.location else item.description
|
|
||||||
event_item_start.text = if (item.isAllDay) allDayString else Formatter.getTimeFromTS(context, item.startTS)
|
|
||||||
event_item_end.beInvisibleIf(item.startTS == item.endTS)
|
|
||||||
event_item_color.setColorFilter(item.color, PorterDuff.Mode.SRC_IN)
|
|
||||||
|
|
||||||
toggleItemSelection(this, markedItems.contains(pos), pos)
|
|
||||||
|
|
||||||
if (item.startTS != item.endTS) {
|
|
||||||
val startCode = Formatter.getDayCodeFromTS(item.startTS)
|
|
||||||
val endCode = Formatter.getDayCodeFromTS(item.endTS)
|
|
||||||
|
|
||||||
event_item_end.apply {
|
|
||||||
text = Formatter.getTimeFromTS(context, item.endTS)
|
|
||||||
if (startCode != endCode) {
|
|
||||||
if (item.isAllDay) {
|
|
||||||
text = Formatter.getDateFromCode(context, endCode, true)
|
|
||||||
} else {
|
|
||||||
append(" (${Formatter.getDateFromCode(context, endCode, true)})")
|
|
||||||
}
|
|
||||||
} else if (item.isAllDay) {
|
|
||||||
beInvisible()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var startTextColor = textColor
|
|
||||||
var endTextColor = textColor
|
|
||||||
if (item.startTS <= now && item.endTS <= now) {
|
|
||||||
if (item.isAllDay) {
|
|
||||||
if (Formatter.getDayCodeFromTS(item.startTS) == Formatter.getDayCodeFromTS(now))
|
|
||||||
startTextColor = primaryColor
|
|
||||||
} else {
|
|
||||||
startTextColor = redTextColor
|
|
||||||
}
|
|
||||||
endTextColor = redTextColor
|
|
||||||
} else if (item.startTS <= now && item.endTS >= now) {
|
|
||||||
startTextColor = primaryColor
|
|
||||||
}
|
|
||||||
|
|
||||||
event_item_start.setTextColor(startTextColor)
|
|
||||||
event_item_end.setTextColor(endTextColor)
|
|
||||||
event_item_title.setTextColor(startTextColor)
|
|
||||||
event_item_description.setTextColor(startTextColor)
|
|
||||||
|
|
||||||
setOnClickListener { viewClicked(multiSelector, listItem, pos) }
|
|
||||||
setOnLongClickListener {
|
|
||||||
if (!multiSelector.isSelectable) {
|
|
||||||
activity.startSupportActionMode(multiSelectorCallback)
|
|
||||||
multiSelector.setSelected(this@ViewHolder, true)
|
|
||||||
actMode?.title = multiSelector.selectedPositions.size.toString()
|
|
||||||
toggleItemSelection(itemView, true, pos)
|
|
||||||
actMode?.invalidate()
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemView
|
|
||||||
}
|
|
||||||
|
|
||||||
fun viewClicked(multiSelector: MultiSelector, listItem: ListItem, pos: Int) {
|
|
||||||
if (multiSelector.isSelectable) {
|
|
||||||
val isSelected = multiSelector.selectedPositions.contains(layoutPosition)
|
|
||||||
multiSelector.setSelected(this, !isSelected)
|
|
||||||
toggleItemSelection(itemView, !isSelected, pos)
|
|
||||||
|
|
||||||
val selectedCnt = multiSelector.selectedPositions.size
|
|
||||||
if (selectedCnt == 0) {
|
|
||||||
actMode?.finish()
|
|
||||||
} else {
|
|
||||||
actMode?.title = selectedCnt.toString()
|
|
||||||
}
|
|
||||||
actMode?.invalidate()
|
|
||||||
} else {
|
|
||||||
val listEvent = listItem as ListEvent
|
|
||||||
itemClick(listEvent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SectionHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
||||||
fun bindView(listItem: ListItem): View {
|
|
||||||
val item = listItem as ListSection
|
|
||||||
itemView.event_item_title.apply {
|
|
||||||
text = item.title
|
|
||||||
setCompoundDrawablesWithIntrinsicBounds(null, if (position == 0) null else topDivider, null, null)
|
|
||||||
setTextColor(if (item.title == todayDate) primaryColor else textColor)
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemView
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,8 +92,11 @@ class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEvents
|
|||||||
}
|
}
|
||||||
|
|
||||||
val eventsAdapter = EventListAdapter(activity as SimpleActivity, listItems, this) {
|
val eventsAdapter = EventListAdapter(activity as SimpleActivity, listItems, this) {
|
||||||
editEvent(it)
|
if (it is ListEvent) {
|
||||||
|
editEvent(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
activity?.runOnUiThread {
|
activity?.runOnUiThread {
|
||||||
mView.calendar_events_list.apply {
|
mView.calendar_events_list.apply {
|
||||||
this@apply.adapter = eventsAdapter
|
this@apply.adapter = eventsAdapter
|
||||||
|
Loading…
x
Reference in New Issue
Block a user