move a chunk of code from DayActivity to DayFragment

This commit is contained in:
tibbi 2016-09-17 21:53:17 +02:00
parent 8a4ae25cff
commit cfba78342a
3 changed files with 108 additions and 62 deletions

View File

@ -4,12 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AbsListView
import android.widget.AdapterView
import butterknife.OnClick
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.Formatter
@ -19,8 +14,7 @@ import kotlinx.android.synthetic.main.activity_day.*
import org.joda.time.DateTime
import java.util.*
class DayActivity : SimpleActivity(), NavigationListener, DBHelper.DBOperationsListener, AdapterView.OnItemClickListener,
AbsListView.MultiChoiceModeListener {
class DayActivity : SimpleActivity(), NavigationListener, DBHelper.DBOperationsListener {
private val PREFILLED_DAYS = 61
private var mDayCode: String? = null
@ -235,43 +229,6 @@ AbsListView.MultiChoiceModeListener {
//updateEvents(events)
}
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
//editEvent(getEventsToShow(mEvents)[position])
}
override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
if (checked) {
mSelectedItemsCnt++
} else {
mSelectedItemsCnt--
}
mode.title = mSelectedItemsCnt.toString()
mode.invalidate()
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
checkDeleteEvents()
val inflater = mode.menuInflater
inflater.inflate(R.menu.menu_day_cab, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
prepareDeleteEvents()
mode.finish()
return true
}
else -> return false
}
}
private fun prepareDeleteEvents() {
/*val checked = mEventsList!!.checkedItemPositions
for (i in mEvents!!.indices) {
@ -294,10 +251,6 @@ AbsListView.MultiChoiceModeListener {
updateEvents(mEvents)*/
}
override fun onDestroyActionMode(mode: ActionMode) {
mSelectedItemsCnt = 0
}
override fun goLeft() {
}

View File

@ -2,31 +2,124 @@ package com.simplemobiletools.calendar.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.calendar.Constants
import android.view.*
import android.widget.AbsListView
import android.widget.AdapterView
import android.widget.RelativeLayout
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.NavigationListener
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventsAdapter
import com.simplemobiletools.calendar.models.Event
import kotlinx.android.synthetic.main.day_fragment.view.*
import kotlinx.android.synthetic.main.top_navigation.view.*
import java.util.*
class DayFragment : Fragment() {
private var mCode: String = ""
class DayFragment : Fragment(), DBHelper.DBOperationsListener, AdapterView.OnItemClickListener,
AbsListView.MultiChoiceModeListener {
private var mDayCode: String = ""
private var mEvents: MutableList<Event>? = null
private var mListener: NavigationListener? = null
lateinit var mHolder: RelativeLayout
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.day_fragment, container, false)
mCode = arguments.getString(Constants.DAY_CODE)
mHolder = view.day_holder
mDayCode = arguments.getString(Constants.DAY_CODE)
val day = Formatter.getEventDate(activity.applicationContext, mCode)
val day = Formatter.getEventDate(activity.applicationContext, mDayCode)
view.month_value.text = day
checkEvents()
return view
}
fun setListener(listener: NavigationListener) {
mListener = listener
}
private fun checkEvents() {
val startTS = Formatter.getDayStartTS(mDayCode)
val endTS = Formatter.getDayEndTS(mDayCode)
DBHelper.newInstance(activity.applicationContext, this).getEvents(startTS, endTS)
}
private fun updateEvents(events: MutableList<Event>) {
mEvents = ArrayList(events)
val eventsToShow = getEventsToShow(events)
val eventsAdapter = EventsAdapter(activity.applicationContext, eventsToShow)
mHolder.day_events.apply {
adapter = eventsAdapter
onItemClickListener = this@DayFragment
setMultiChoiceModeListener(this@DayFragment)
}
}
private fun getEventsToShow(events: MutableList<Event>): List<Event> {
/*val cnt = events.size
for (i in cnt - 1 downTo 0) {
if (mToBeDeleted!!.contains(events[i].id)) {
events.removeAt(i)
}
}*/
return events
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
//prepareDeleteEvents()
mode.finish()
return true
}
else -> return false
}
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
//checkDeleteEvents()
val inflater = mode.menuInflater
inflater.inflate(R.menu.menu_day_cab, menu)
return true
}
override fun onDestroyActionMode(mode: ActionMode) {
//mSelectedItemsCnt = 0
}
override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
/*if (checked) {
mSelectedItemsCnt++
} else {
mSelectedItemsCnt--
}
mode.title = mSelectedItemsCnt.toString()
mode.invalidate()*/
}
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//editEvent(getEventsToShow(mEvents)[position])
}
override fun eventInserted(event: Event?) {
}
override fun eventUpdated(event: Event?) {
}
override fun eventsDeleted(cnt: Int) {
checkEvents()
}
override fun gotEvents(events: MutableList<Event>) {
updateEvents(events)
}
}

View File

@ -27,7 +27,7 @@ class MonthFragment : Fragment(), Calendar {
private var mTextColorWithEvent: Int = 0
private var mWeakTextColorWithEvent: Int = 0
private var mSundayFirst: Boolean = false
private var mCode: String = ""
private var mDayCode: String = ""
private var mCalendar: CalendarImpl? = null
private var mListener: NavigationListener? = null
@ -41,7 +41,7 @@ class MonthFragment : Fragment(), Calendar {
mRes = resources
mHolder = view.calendar_holder
mCode = arguments.getString(Constants.DAY_CODE)
mDayCode = arguments.getString(Constants.DAY_CODE)
mConfig = Config.newInstance(context)
mSundayFirst = mConfig.isSundayFirst
@ -58,7 +58,7 @@ class MonthFragment : Fragment(), Calendar {
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mCalendar = CalendarImpl(this, context)
mCalendar!!.updateCalendar(Formatter.getDateTimeFromCode(mCode))
mCalendar!!.updateCalendar(Formatter.getDateTimeFromCode(mDayCode))
}
override fun onResume() {