adding an initial implementation of the monthly + daily view

This commit is contained in:
tibbi 2021-02-21 17:58:55 +01:00
parent 545b393abb
commit 660582c41d
7 changed files with 317 additions and 1 deletions

View File

@ -409,6 +409,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
RadioItem(DAILY_VIEW, getString(R.string.daily_view)),
RadioItem(WEEKLY_VIEW, getString(R.string.weekly_view)),
RadioItem(MONTHLY_VIEW, getString(R.string.monthly_view)),
RadioItem(MONTHLY_DAILY_VIEW, getString(R.string.monthly_daily_view)),
RadioItem(YEARLY_VIEW, getString(R.string.yearly_view)),
RadioItem(EVENTS_LIST_VIEW, getString(R.string.simple_event_list)))
@ -748,7 +749,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
val bundle = Bundle()
when (config.storedView) {
DAILY_VIEW, MONTHLY_VIEW -> bundle.putString(DAY_CODE, dayCode)
DAILY_VIEW, MONTHLY_VIEW, MONTHLY_DAILY_VIEW -> bundle.putString(DAY_CODE, dayCode)
WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, getThisWeekDateTime())
}
@ -804,6 +805,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun getFragmentsHolder() = when (config.storedView) {
DAILY_VIEW -> DayFragmentsHolder()
MONTHLY_VIEW -> MonthFragmentsHolder()
MONTHLY_DAILY_VIEW -> MonthDayFragmentsHolder()
YEARLY_VIEW -> YearFragmentsHolder()
EVENTS_LIST_VIEW -> EventListFragment()
else -> WeekFragmentsHolder()

View File

@ -0,0 +1,39 @@
package com.simplemobiletools.calendar.pro.adapters
import android.os.Bundle
import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.simplemobiletools.calendar.pro.fragments.MonthDayFragment
import com.simplemobiletools.calendar.pro.helpers.DAY_CODE
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
class MyMonthDayPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<MonthDayFragment>()
override fun getCount() = mCodes.size
override fun getItem(position: Int): Fragment {
val bundle = Bundle()
val code = mCodes[position]
bundle.putString(DAY_CODE, code)
val fragment = MonthDayFragment()
fragment.arguments = bundle
fragment.listener = mListener
mFragments.put(position, fragment)
return fragment
}
fun updateCalendars(pos: Int) {
for (i in -1..1) {
mFragments[pos + i]?.updateCalendar()
}
}
fun printCurrentView(pos: Int) {
mFragments[pos].printCurrentView()
}
}

View File

@ -0,0 +1,109 @@
package com.simplemobiletools.calendar.pro.fragments
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.helpers.Config
import com.simplemobiletools.calendar.pro.helpers.DAY_CODE
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.MonthlyCalendarImpl
import com.simplemobiletools.calendar.pro.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.calendar.pro.models.DayMonthly
import kotlinx.android.synthetic.main.fragment_month_day.view.*
import org.joda.time.DateTime
class MonthDayFragment : Fragment(), MonthlyCalendar {
private var mTextColor = 0
private var mSundayFirst = false
private var mShowWeekNumbers = false
private var mDayCode = ""
private var mPackageName = ""
private var mLastHash = 0L
private var mCalendar: MonthlyCalendarImpl? = null
var listener: NavigationListener? = null
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_month_day, container, false)
mRes = resources
mPackageName = activity!!.packageName
mHolder = view.month_day_calendar_holder
mDayCode = arguments!!.getString(DAY_CODE)!!
mConfig = context!!.config
storeStateVariables()
setupButtons()
mCalendar = MonthlyCalendarImpl(this, context!!)
return view
}
override fun onPause() {
super.onPause()
storeStateVariables()
}
override fun onResume() {
super.onResume()
if (mConfig.showWeekNumbers != mShowWeekNumbers) {
mLastHash = -1L
}
mCalendar!!.apply {
mTargetDate = Formatter.getDateTimeFromCode(mDayCode)
getDays(false) // prefill the screen asap, even if without events
}
storeStateVariables()
updateCalendar()
}
private fun storeStateVariables() {
mConfig.apply {
mSundayFirst = isSundayFirst
mShowWeekNumbers = showWeekNumbers
}
}
fun updateCalendar() {
mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
}
override fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean, currTargetDate: DateTime) {
val newHash = month.hashCode() + days.hashCode().toLong()
if ((mLastHash != 0L && !checkedEvents) || mLastHash == newHash) {
return
}
mLastHash = newHash
activity?.runOnUiThread {
updateDays(days)
}
}
private fun setupButtons() {
mTextColor = mConfig.textColor
}
private fun updateDays(days: ArrayList<DayMonthly>) {
mHolder.month_day_view_wrapper.updateDays(days) {
(activity as MainActivity).openDayFromMonthly(Formatter.getDateTimeFromCode(it.code))
}
}
fun printCurrentView() {}
}

View File

@ -0,0 +1,145 @@
package com.simplemobiletools.calendar.pro.fragments
import android.content.res.Resources
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.DatePicker
import androidx.appcompat.app.AlertDialog
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.MainActivity
import com.simplemobiletools.calendar.pro.adapters.MyMonthDayPagerAdapter
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.getMonthCode
import com.simplemobiletools.calendar.pro.helpers.DAY_CODE
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.getDialogTheme
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.updateActionBarTitle
import com.simplemobiletools.commons.views.MyViewPager
import kotlinx.android.synthetic.main.fragment_months_days_holder.view.*
import org.joda.time.DateTime
class MonthDayFragmentsHolder : MyFragmentHolder(), NavigationListener {
private val PREFILLED_MONTHS = 251
private var viewPager: MyViewPager? = null
private var defaultMonthlyPage = 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()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_months_days_holder, container, false)
view.background = ColorDrawable(context!!.config.backgroundColor)
viewPager = view.fragment_months_days_viewpager
viewPager!!.id = (System.currentTimeMillis() % 100000).toInt()
setupFragment()
return view
}
private fun setupFragment() {
val codes = getMonths(currentDayCode)
val monthlyDailyAdapter = MyMonthDayPagerAdapter(activity!!.supportFragmentManager, codes, this)
defaultMonthlyPage = codes.size / 2
viewPager!!.apply {
adapter = monthlyDailyAdapter
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 = defaultMonthlyPage
}
updateActionBarTitle()
}
private fun getMonths(code: String): List<String> {
val months = ArrayList<String>(PREFILLED_MONTHS)
val today = Formatter.getDateTimeFromCode(code).withDayOfMonth(1)
for (i in -PREFILLED_MONTHS / 2..PREFILLED_MONTHS / 2) {
months.add(Formatter.getDayCodeFromDateTime(today.plusMonths(i)))
}
return months
}
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 showGoToDateDialog() {
activity!!.setTheme(context!!.getDialogTheme())
val view = layoutInflater.inflate(R.layout.date_picker, null)
val datePicker = view.findViewById<DatePicker>(R.id.date_picker)
datePicker.findViewById<View>(Resources.getSystem().getIdentifier("day", "id", "android")).beGone()
val dateTime = DateTime(Formatter.getDateTimeFromCode(currentDayCode).toString())
datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null)
AlertDialog.Builder(context!!)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.ok) { dialog, which -> datePicked(dateTime, datePicker) }
.create().apply {
activity?.setupDialogStuff(view, this)
}
}
private fun datePicked(dateTime: DateTime, datePicker: DatePicker) {
val month = datePicker.month + 1
val year = datePicker.year
val newDateTime = dateTime.withDate(year, month, 1)
goToDateTime(newDateTime)
}
override fun refreshEvents() {
(viewPager?.adapter as? MyMonthDayPagerAdapter)?.updateCalendars(viewPager?.currentItem ?: 0)
}
override fun shouldGoToTodayBeVisible() = currentDayCode.getMonthCode() != todayDayCode.getMonthCode()
override fun updateActionBarTitle() {
(activity as? MainActivity)?.updateActionBarTitle(getString(R.string.app_launcher_name))
}
override fun getNewEventDayCode() = if (shouldGoToTodayBeVisible()) currentDayCode else todayDayCode
override fun printView() {}
}

View File

@ -26,6 +26,7 @@ const val EVENTS_LIST_VIEW = 3
const val WEEKLY_VIEW = 4
const val DAILY_VIEW = 5
const val LAST_VIEW = 6
const val MONTHLY_DAILY_VIEW = 7
const val REMINDER_OFF = -1

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/month_day_calendar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/medium_margin">
<com.simplemobiletools.calendar.pro.views.MonthViewWrapper
android:id="@+id/month_day_view_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_months_days_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true" />