From 6d79b686fa44cab39f6c4807ddcbf714d3f0d306 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 12 Feb 2017 18:49:41 +0100 Subject: [PATCH] add a filter to daily view too --- .../calendar/activities/DayActivity.kt | 32 +++++++++++++++++++ .../calendar/activities/MainActivity.kt | 2 +- .../calendar/adapters/MyDayPagerAdapter.kt | 5 +-- .../calendar/fragments/DayFragment.kt | 18 +++++------ app/src/main/res/menu/menu_day.xml | 9 ++++++ .../main/res/menu/{menu.xml => menu_main.xml} | 0 6 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 app/src/main/res/menu/menu_day.xml rename app/src/main/res/menu/{menu.xml => menu_main.xml} (100%) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt index c48388001..ce902de8f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/DayActivity.kt @@ -3,10 +3,15 @@ package com.simplemobiletools.calendar.activities import android.content.Intent import android.os.Bundle import android.support.v4.view.ViewPager +import android.util.SparseIntArray +import android.view.Menu +import android.view.MenuItem import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter +import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog import com.simplemobiletools.calendar.extensions.getNewEventTimestampFromCode import com.simplemobiletools.calendar.helpers.DAY_CODE +import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.NEW_EVENT_START_TS import com.simplemobiletools.calendar.interfaces.NavigationListener @@ -20,6 +25,7 @@ class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChange private var mDayCode = "" private var mPagerDays: MutableList? = null private var mPagerPos = 0 + private var eventTypeColors = SparseIntArray() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -34,6 +40,26 @@ class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChange day_fab.setOnClickListener { addNewEvent() } updateTextColors(day_coordinator) + + DBHelper.newInstance(applicationContext).getEventTypes { + eventTypeColors.clear() + it.map { eventTypeColors.put(it.id, it.color) } + invalidateOptionsMenu() + } + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.menu_day, menu) + menu.findItem(R.id.filter).isVisible = eventTypeColors.size() > 1 + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.filter -> showFilterDialog() + else -> return super.onOptionsItemSelected(item) + } + return true } private fun fillViewPager(targetDay: String) { @@ -47,6 +73,12 @@ class DayActivity : SimpleActivity(), NavigationListener, ViewPager.OnPageChange } } + private fun showFilterDialog() { + FilterEventTypesDialog(this) { + recheckEvents() + } + } + private fun addNewEvent() { Intent(applicationContext, EventActivity::class.java).apply { putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(mPagerDays?.get(view_pager.currentItem).toString())) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 66a717050..b72b7e960 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -98,7 +98,7 @@ class MainActivity : SimpleActivity(), NavigationListener { } override fun onCreateOptionsMenu(menu: Menu): Boolean { - menuInflater.inflate(R.menu.menu, menu) + menuInflater.inflate(R.menu.menu_main, menu) menu.findItem(R.id.filter).isVisible = eventTypeColors.size() > 1 return true } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt index a6df6c179..9cd8e7285 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyDayPagerAdapter.kt @@ -29,7 +29,8 @@ class MyDayPagerAdapter(fm: FragmentManager, private val mCodes: List, p } fun checkDayEvents(pos: Int) { - mFragments[pos - 1].checkEvents() - mFragments[pos + 1].checkEvents() + for (i in -1..1) { + mFragments[pos + i].checkEvents() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index 1088881a4..7275c081b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -18,7 +18,10 @@ import com.simplemobiletools.calendar.activities.SimpleActivity import com.simplemobiletools.calendar.adapters.DayEventsAdapter import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.getAppropriateTheme -import com.simplemobiletools.calendar.helpers.* +import com.simplemobiletools.calendar.extensions.getFilteredEvents +import com.simplemobiletools.calendar.helpers.DAY_CODE +import com.simplemobiletools.calendar.helpers.DBHelper +import com.simplemobiletools.calendar.helpers.EVENT_ID import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.interfaces.DeleteItemsListener import com.simplemobiletools.calendar.interfaces.NavigationListener @@ -38,21 +41,18 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents lateinit var mRes: Resources lateinit var mHolder: RelativeLayout - lateinit var mConfig: Config override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_day, container, false) mRes = resources mHolder = view.day_holder - mConfig = context.config mDayCode = arguments.getString(DAY_CODE) - val day = Formatter.getDayTitle(activity.applicationContext, mDayCode) mHolder.top_value.apply { text = day setOnClickListener { pickDay() } - setTextColor(mConfig.textColor) + setTextColor(context.config.textColor) } setupButtons() @@ -65,7 +65,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents } private fun setupButtons() { - mTextColor = mConfig.textColor + mTextColor = context.config.textColor mHolder.apply { top_left_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP) @@ -117,7 +117,7 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents DBHelper.newInstance(context, this).getEvents(startTS, endTS, this) } - private fun updateEvents(events: MutableList) { + private fun updateEvents(events: List) { if (activity == null) return @@ -149,14 +149,14 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents } override fun eventsDeleted(cnt: Int) { - checkEvents() (activity as DayActivity).recheckEvents() } override fun gotEvents(events: MutableList) { val sorted = ArrayList(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))) + val filtered = context.getFilteredEvents(sorted) activity?.runOnUiThread { - updateEvents(sorted) + updateEvents(filtered) } } } diff --git a/app/src/main/res/menu/menu_day.xml b/app/src/main/res/menu/menu_day.xml new file mode 100644 index 000000000..9940db810 --- /dev/null +++ b/app/src/main/res/menu/menu_day.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu_main.xml similarity index 100% rename from app/src/main/res/menu/menu.xml rename to app/src/main/res/menu/menu_main.xml