fill the list of event types

This commit is contained in:
tibbi 2017-02-11 13:55:23 +01:00
parent 2c71c1c01e
commit 20a835f304
5 changed files with 208 additions and 4 deletions

View File

@ -2,12 +2,15 @@ package com.simplemobiletools.calendar.activities
import android.os.Bundle import android.os.Bundle
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventTypeAdapter
import com.simplemobiletools.calendar.dialogs.EventTypeDialog import com.simplemobiletools.calendar.dialogs.EventTypeDialog
import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.views.RecyclerViewDivider
import kotlinx.android.synthetic.main.activity_manage_event_types.* import kotlinx.android.synthetic.main.activity_manage_event_types.*
class ManageEventTypesActivity : SimpleActivity() { class ManageEventTypesActivity : SimpleActivity(), EventTypeAdapter.DeleteEventTypeListener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_event_types) setContentView(R.layout.activity_manage_event_types)
@ -28,7 +31,24 @@ class ManageEventTypesActivity : SimpleActivity() {
private fun getEventTypes() { private fun getEventTypes() {
DBHelper.newInstance(applicationContext).getEventTypes { DBHelper.newInstance(applicationContext).getEventTypes {
runOnUiThread {
gotEventTypes(it)
}
} }
} }
private fun gotEventTypes(eventTypes: List<EventType>) {
val eventTypesAdapter = EventTypeAdapter(this, eventTypes, this) {
}
manage_event_types_list.apply {
this@apply.adapter = eventTypesAdapter
addItemDecoration(RecyclerViewDivider(context))
}
}
override fun eventTypeDeleted() {
getEventTypes()
}
} }

View File

@ -0,0 +1,139 @@
package com.simplemobiletools.calendar.adapters
import android.support.v7.view.ActionMode
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.activities.SimpleActivity
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
import kotlinx.android.synthetic.main.event_type_view.view.*
import java.util.*
class EventTypeAdapter(val activity: SimpleActivity, val mItems: List<EventType>, val listener: DeleteEventTypeListener?, val itemClick: (EventType) -> Unit) :
RecyclerView.Adapter<EventTypeAdapter.ViewHolder>() {
val multiSelector = MultiSelector()
val views = ArrayList<View>()
companion object {
var actMode: ActionMode? = null
val markedItems = HashSet<Int>()
var textColor = 0
fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) {
itemView.event_item_frame.isSelected = select
if (pos == -1)
return
if (select)
markedItems.add(pos)
else
markedItems.remove(pos)
}
}
init {
textColor = activity.config.textColor
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.cab_delete -> askConfirmDelete()
else -> return false
}
return true
}
override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean {
super.onCreateActionMode(actionMode, menu)
actMode = actionMode
activity.menuInflater.inflate(R.menu.cab_event_type, menu)
return true
}
override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu) = true
override fun onDestroyActionMode(actionMode: ActionMode?) {
super.onDestroyActionMode(actionMode)
views.forEach { toggleItemSelection(it, false) }
markedItems.clear()
}
}
private fun askConfirmDelete() {
ConfirmationDialog(activity) {
actMode?.finish()
deleteEventTypes()
}
}
private fun deleteEventTypes() {
listener?.eventTypeDeleted()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.event_type_view, parent, false)
return ViewHolder(activity, view, itemClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
views.add(holder.bindView(multiSelectorMode, multiSelector, mItems[position], position))
}
override fun getItemCount() = mItems.size
class ViewHolder(val activity: SimpleActivity, view: View, val itemClick: (EventType) -> (Unit)) : SwappingHolder(view, MultiSelector()) {
fun bindView(multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, eventType: EventType, pos: Int): View {
itemView.apply {
event_type_title.text = eventType.title
event_type_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor)
toggleItemSelection(this, markedItems.contains(pos), pos)
event_type_title.setTextColor(textColor)
setOnClickListener { viewClicked(multiSelector, eventType, 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, eventType: EventType, 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 {
itemClick(eventType)
}
}
}
interface DeleteEventTypeListener {
fun eventTypeDeleted()
}
}

View File

@ -9,9 +9,8 @@
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/manage_event_types_list" android:id="@+id/manage_event_types_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:clipToPadding="false" android:clipToPadding="false"
android:paddingLeft="@dimen/activity_margin"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/> app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<com.simplemobiletools.commons.views.MyFloatingActionButton <com.simplemobiletools.commons.views.MyFloatingActionButton

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/event_item_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:foreground="@drawable/selector">
<RelativeLayout
android:id="@+id/event_type_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/event_type_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/medium_margin"
android:paddingRight="@dimen/medium_margin"
android:text="@string/text_color"/>
<ImageView
android:id="@+id/event_type_color"
android:layout_width="@dimen/color_sample_size"
android:layout_height="@dimen/color_sample_size"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin"
android:clickable="false"/>
</RelativeLayout>
</FrameLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_delete"
android:icon="@drawable/ic_delete"
android:title="@string/delete"
app:showAsAction="ifRoom"/>
</menu>