From 1390acd4ec2a893bb9e786587450c2ed3543f5c5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 22 Jan 2018 15:16:21 +0100 Subject: [PATCH] fix #96, add a Daily View --- .../calendar/activities/MainActivity.kt | 4 +- .../calendar/fragments/DayFragmentsHolder.kt | 107 ++++++++++++++++++ .../calendar/helpers/Constants.kt | 1 + app/src/main/res/layout/fragment_day.xml | 3 +- .../main/res/layout/fragment_days_holder.xml | 8 ++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragmentsHolder.kt create mode 100644 app/src/main/res/layout/fragment_days_holder.xml 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 5bca78a35..c8ceaac78 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -264,6 +264,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private fun showViewDialog() { val res = resources val items = arrayListOf( + RadioItem(DAILY_VIEW, res.getString(R.string.daily_view)), RadioItem(WEEKLY_VIEW, res.getString(R.string.weekly_view)), RadioItem(MONTHLY_VIEW, res.getString(R.string.monthly_view)), RadioItem(YEARLY_VIEW, res.getString(R.string.yearly_view)), @@ -497,7 +498,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { val bundle = Bundle() when (config.storedView) { - MONTHLY_VIEW -> bundle.putString(DAY_CODE, Formatter.getTodayCode(applicationContext)) + DAILY_VIEW, MONTHLY_VIEW -> bundle.putString(DAY_CODE, Formatter.getTodayCode(applicationContext)) WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, getThisWeekDateTime()) } @@ -525,6 +526,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } private fun getFragmentsHolder() = when (config.storedView) { + DAILY_VIEW -> DayFragmentsHolder() MONTHLY_VIEW -> MonthFragmentsHolder() YEARLY_VIEW -> YearFragmentsHolder() EVENTS_LIST_VIEW -> EventListFragment() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragmentsHolder.kt new file mode 100644 index 000000000..4f885226c --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragmentsHolder.kt @@ -0,0 +1,107 @@ +package com.simplemobiletools.calendar.fragments + +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.support.v4.view.ViewPager +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.MainActivity +import com.simplemobiletools.calendar.adapters.MyDayPagerAdapter +import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.helpers.DAY_CODE +import com.simplemobiletools.calendar.helpers.Formatter +import com.simplemobiletools.calendar.interfaces.NavigationListener +import com.simplemobiletools.commons.views.MyViewPager +import kotlinx.android.synthetic.main.fragment_days_holder.view.* +import org.joda.time.DateTime +import java.util.* + +class DayFragmentsHolder : MyFragmentHolder(), NavigationListener { + private val PREFILLED_DAYS = 121 + + private var viewPager: MyViewPager? = null + private var defaultDaylyPage = 0 + private var todayDayCode = "" + private var currentDayCode = "" + private var isGoToTodayVisible = false + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + currentDayCode = arguments?.getString(DAY_CODE) ?: "" + todayDayCode = Formatter.getTodayCode(context!!) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + val view = inflater.inflate(R.layout.fragment_days_holder, container, false) + view.background = ColorDrawable(context!!.config.backgroundColor) + viewPager = view.fragment_days_viewpager + setupFragment() + return view + } + + private fun setupFragment() { + val codes = getDays(currentDayCode) + val dailyAdapter = MyDayPagerAdapter(activity!!.supportFragmentManager, codes, this) + defaultDaylyPage = codes.size / 2 + + viewPager!!.apply { + adapter = dailyAdapter + addOnPageChangeListener(object : ViewPager.OnPageChangeListener { + override fun onPageScrollStateChanged(state: Int) { + } + + override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { + } + + override fun onPageSelected(position: Int) { + currentDayCode = codes[position] + val shouldGoToTodayBeVisible = shouldGoToTodayBeVisible() + if (isGoToTodayVisible != shouldGoToTodayBeVisible) { + (activity as? MainActivity)?.toggleGoToTodayVisibility(shouldGoToTodayBeVisible) + isGoToTodayVisible = shouldGoToTodayBeVisible + } + } + }) + currentItem = defaultDaylyPage + } + } + + private fun getDays(code: String): List { + val days = ArrayList(PREFILLED_DAYS) + val today = Formatter.getDateTimeFromCode(code) + for (i in -PREFILLED_DAYS / 2..PREFILLED_DAYS / 2) { + days.add(Formatter.getDayCodeFromDateTime(today.plusDays(i))) + } + return days + } + + override fun goLeft() { + viewPager!!.currentItem = viewPager!!.currentItem - 1 + } + + override fun goRight() { + viewPager!!.currentItem = viewPager!!.currentItem + 1 + } + + override fun goToDateTime(dateTime: DateTime) { + currentDayCode = Formatter.getDayCodeFromDateTime(dateTime) + setupFragment() + } + + override fun goToToday() { + currentDayCode = todayDayCode + setupFragment() + } + + override fun refreshEvents() { + setupFragment() + } + + override fun shouldGoToTodayBeVisible() = currentDayCode != todayDayCode + + override fun updateActionBarTitle() { + (activity as MainActivity).supportActionBar?.title = getString(R.string.app_launcher_name) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index a0f969dd9..23408fca5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -20,6 +20,7 @@ val MONTHLY_VIEW = 1 val YEARLY_VIEW = 2 val EVENTS_LIST_VIEW = 3 val WEEKLY_VIEW = 4 +val DAILY_VIEW = 5 val REMINDER_OFF = -1 diff --git a/app/src/main/res/layout/fragment_day.xml b/app/src/main/res/layout/fragment_day.xml index 8f13c2f1e..4ea4e4a0b 100644 --- a/app/src/main/res/layout/fragment_day.xml +++ b/app/src/main/res/layout/fragment_day.xml @@ -4,7 +4,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/day_holder" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="wrap_content" + android:padding="@dimen/medium_margin"> diff --git a/app/src/main/res/layout/fragment_days_holder.xml b/app/src/main/res/layout/fragment_days_holder.xml new file mode 100644 index 000000000..686ac134e --- /dev/null +++ b/app/src/main/res/layout/fragment_days_holder.xml @@ -0,0 +1,8 @@ + +