open the particular event from Event list view on click

This commit is contained in:
tibbi 2016-10-23 19:08:55 +02:00
parent e043e1b814
commit 4388e23ff1
1 changed files with 26 additions and 6 deletions

View File

@ -1,13 +1,17 @@
package com.simplemobiletools.calendar.fragments
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.DBHelper
import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.adapters.EventsListAdapter
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.ListEvent
@ -18,8 +22,11 @@ import org.joda.time.DateTime
import java.util.*
import kotlin.comparisons.compareBy
class EventListFragment : Fragment(), DBHelper.GetEventsListener {
class EventListFragment : Fragment(), DBHelper.GetEventsListener, AdapterView.OnItemClickListener {
private val EDIT_EVENT = 1
lateinit var mView: View
var mListItems: ArrayList<ListItem> = ArrayList()
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mView = inflater!!.inflate(R.layout.fragment_event_list, container, false)
@ -34,22 +41,35 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener {
}
override fun gotEvents(events: MutableList<Event>) {
val listItems = ArrayList<ListItem>(events.size)
mListItems = ArrayList<ListItem>(events.size)
val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }))
var prevCode = ""
sorted.forEach {
val code = Formatter.getDayCodeFromTS(it.startTS)
if (code != prevCode) {
val day = Formatter.getEventDate(context, code)
listItems.add(ListSection(day, false))
mListItems.add(ListSection(day, false))
prevCode = code
}
listItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description))
mListItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description))
}
val eventsAdapter = EventsListAdapter(context, listItems)
val eventsAdapter = EventsListAdapter(context, mListItems)
activity?.runOnUiThread {
mView.calendar_events_list.adapter = eventsAdapter
mView.calendar_events_list.apply {
adapter = eventsAdapter
onItemClickListener = this@EventListFragment
}
}
}
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
editEvent((mListItems[position] as ListEvent).id)
}
private fun editEvent(eventId: Int) {
val intent = Intent(activity.applicationContext, EventActivity::class.java)
intent.putExtra(Constants.EVENT_ID, eventId)
startActivityForResult(intent, EDIT_EVENT)
}
}