rewrite MainActivity, split views into standalone fragments

This commit is contained in:
tibbi 2018-01-21 19:10:29 +01:00
parent 708e32c341
commit 80d403f2b1
19 changed files with 514 additions and 366 deletions

View file

@ -12,32 +12,24 @@ import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.ContactsContract import android.provider.ContactsContract
import android.support.v4.view.MenuItemCompat import android.support.v4.view.MenuItemCompat
import android.support.v4.view.ViewPager
import android.support.v7.widget.SearchView import android.support.v7.widget.SearchView
import android.util.SparseIntArray import android.util.SparseIntArray
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import com.simplemobiletools.calendar.BuildConfig import com.simplemobiletools.calendar.BuildConfig
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.EventListAdapter import com.simplemobiletools.calendar.adapters.EventListAdapter
import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter
import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter
import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.dialogs.ExportEventsDialog import com.simplemobiletools.calendar.dialogs.ExportEventsDialog
import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog
import com.simplemobiletools.calendar.dialogs.ImportEventsDialog import com.simplemobiletools.calendar.dialogs.ImportEventsDialog
import com.simplemobiletools.calendar.extensions.* import com.simplemobiletools.calendar.extensions.*
import com.simplemobiletools.calendar.fragments.EventListFragment import com.simplemobiletools.calendar.fragments.*
import com.simplemobiletools.calendar.fragments.WeekFragment
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.models.EventType import com.simplemobiletools.calendar.models.EventType
import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListEvent
import com.simplemobiletools.calendar.views.MyScrollView
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
@ -52,11 +44,8 @@ import java.text.SimpleDateFormat
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewListener { class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private val CALDAV_SYNC_DELAY = 1000L private val CALDAV_SYNC_DELAY = 1000L
private val PREFILLED_MONTHS = 97
private val PREFILLED_YEARS = 31
private val PREFILLED_WEEKS = 61
private var showCalDAVRefreshToast = false private var showCalDAVRefreshToast = false
private var mIsMonthSelected = false private var mIsMonthSelected = false
@ -65,11 +54,10 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
private var mLatestSearchQuery = "" private var mLatestSearchQuery = ""
private var mCalDAVSyncHandler = Handler() private var mCalDAVSyncHandler = Handler()
private var mSearchMenuItem: MenuItem? = null private var mSearchMenuItem: MenuItem? = null
private var mIsGoToTodayVisible = true private var currentFragment: MyFragmentHolder? = null
private var shouldGoToTodayBeVisible = false
private var mDefaultWeeklyPage = 0 private var eventTypeColors = SparseIntArray()
private var mDefaultMonthlyPage = 0 private var goToTodayButton: MenuItem? = null
private var mDefaultYearlyPage = 0
private var mStoredTextColor = 0 private var mStoredTextColor = 0
private var mStoredBackgroundColor = 0 private var mStoredBackgroundColor = 0
@ -79,19 +67,15 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
private var mStoredUse24HourFormat = false private var mStoredUse24HourFormat = false
private var mStoredUseEnglish = false private var mStoredUseEnglish = false
companion object {
var mWeekScrollY = 0
var eventTypeColors = SparseIntArray()
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
appLaunched() appLaunched()
checkWhatsNewDialog() checkWhatsNewDialog()
if (resources.getBoolean(R.bool.portrait_only)) if (resources.getBoolean(R.bool.portrait_only)) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) { if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
val uri = intent.data val uri = intent.data
@ -145,7 +129,7 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
if (config.storedView == WEEKLY_VIEW) { if (config.storedView == WEEKLY_VIEW) {
if (mStoredIsSundayFirst != config.isSundayFirst || mStoredUse24HourFormat != config.use24hourFormat) { if (mStoredIsSundayFirst != config.isSundayFirst || mStoredUse24HourFormat != config.use24hourFormat) {
fillWeeklyViewPager()
} }
} }
@ -169,10 +153,10 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu) menuInflater.inflate(R.menu.menu_main, menu)
mIsGoToTodayVisible = shouldGoToTodayBeVisible()
menu.apply { menu.apply {
goToTodayButton = findItem(R.id.go_to_today)
findItem(R.id.filter).isVisible = mShouldFilterBeVisible findItem(R.id.filter).isVisible = mShouldFilterBeVisible
findItem(R.id.go_to_today).isVisible = mIsGoToTodayVisible findItem(R.id.go_to_today).isVisible = shouldGoToTodayBeVisible && config.storedView != EVENTS_LIST_VIEW
findItem(R.id.refresh_caldav_calendars).isVisible = config.caldavSync findItem(R.id.refresh_caldav_calendars).isVisible = config.caldavSync
} }
@ -286,31 +270,23 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
RadioItem(EVENTS_LIST_VIEW, res.getString(R.string.simple_event_list))) RadioItem(EVENTS_LIST_VIEW, res.getString(R.string.simple_event_list)))
RadioGroupDialog(this, items, config.storedView) { RadioGroupDialog(this, items, config.storedView) {
mIsMonthSelected = false
calendar_fab.beVisibleIf(it as Int != YEARLY_VIEW)
resetActionBarTitle()
closeSearch() closeSearch()
updateView(it as Int) updateView(it)
shouldGoToTodayBeVisible = false
invalidateOptionsMenu() invalidateOptionsMenu()
} }
} }
private fun goToToday() { private fun goToToday() {
if (config.storedView == WEEKLY_VIEW) { currentFragment?.goToToday()
week_view_view_pager.currentItem = mDefaultWeeklyPage
} else if (config.storedView == MONTHLY_VIEW) {
main_view_pager.currentItem = mDefaultMonthlyPage
} else if (config.storedView == YEARLY_VIEW) {
if (mIsMonthSelected) {
openMonthlyToday()
} else {
main_view_pager.currentItem = mDefaultYearlyPage
}
}
} }
private fun shouldGoToTodayBeVisible() = when (config.storedView) { private fun resetActionBarTitle() {
WEEKLY_VIEW -> week_view_view_pager.currentItem != mDefaultWeeklyPage supportActionBar?.title = getString(R.string.app_launcher_name)
MONTHLY_VIEW -> main_view_pager.currentItem != mDefaultMonthlyPage supportActionBar?.subtitle = ""
YEARLY_VIEW -> main_view_pager.currentItem != mDefaultYearlyPage
else -> false
} }
private fun showFilterDialog() { private fun showFilterDialog() {
@ -319,6 +295,11 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
} }
} }
fun toggleGoToTodayVisibility(beVisible: Boolean) {
shouldGoToTodayBeVisible = beVisible
invalidateOptionsMenu()
}
private fun refreshCalDAVCalendars(showRefreshToast: Boolean) { private fun refreshCalDAVCalendars(showRefreshToast: Boolean) {
showCalDAVRefreshToast = showRefreshToast showCalDAVRefreshToast = showRefreshToast
if (showRefreshToast) { if (showRefreshToast) {
@ -357,6 +338,7 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color)) val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color))
eventTypeId = dbHelper.insertEventType(eventType) eventTypeId = dbHelper.insertEventType(eventType)
} }
val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0) val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0)
handleParseResult(result) handleParseResult(result)
if (result != IcsImporter.ImportResult.IMPORT_FAIL) { if (result != IcsImporter.ImportResult.IMPORT_FAIL) {
@ -499,32 +481,43 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
mIsMonthSelected = view == MONTHLY_VIEW mIsMonthSelected = view == MONTHLY_VIEW
config.storedView = view config.storedView = view
updateViewPager() updateViewPager()
if (goToTodayButton?.isVisible == true) {
shouldGoToTodayBeVisible = false
invalidateOptionsMenu()
}
} }
private fun updateViewPager() { private fun updateViewPager() {
resetTitle() val fragment = getFragmentsHolder()
when { currentFragment = fragment
config.storedView == YEARLY_VIEW -> fillYearlyViewPager() val bundle = Bundle()
config.storedView == EVENTS_LIST_VIEW -> fillEventsList()
config.storedView == WEEKLY_VIEW -> fillWeeklyViewPager() when (config.storedView) {
else -> openMonthlyToday() MONTHLY_VIEW -> bundle.putString(DAY_CODE, Formatter.getTodayCode(applicationContext))
WEEKLY_VIEW -> bundle.putString(WEEK_START_DATE_TIME, getThisWeekDateTime())
} }
mWeekScrollY = 0 fragment.arguments = bundle
supportFragmentManager.beginTransaction().replace(R.id.fragments_holder, fragment, "").commit()
} }
private fun openMonthlyToday() { private fun getThisWeekDateTime(): String {
val targetDay = DateTime().toString(Formatter.DAYCODE_PATTERN) var thisweek = DateTime().withDayOfWeek(1).withTimeAtStartOfDay().minusDays(if (config.isSundayFirst) 1 else 0)
fillMonthlyViewPager(targetDay) if (DateTime().minusDays(7).seconds() > thisweek.seconds()) {
thisweek = thisweek.plusDays(7)
}
return thisweek.toString()
}
private fun getFragmentsHolder() = when (config.storedView) {
MONTHLY_VIEW -> MonthFragmentsHolder()
YEARLY_VIEW -> YearFragmentsHolder()
EVENTS_LIST_VIEW -> EventListFragment()
else -> WeekFragmentsHolder()
} }
private fun refreshViewPager() { private fun refreshViewPager() {
when { currentFragment?.refreshEvents()
config.storedView == YEARLY_VIEW && !mIsMonthSelected -> (main_view_pager.adapter as? MyYearPagerAdapter)?.refreshEvents(main_view_pager.currentItem)
config.storedView == EVENTS_LIST_VIEW -> fillEventsList()
config.storedView == WEEKLY_VIEW -> (week_view_view_pager.adapter as? MyWeekPagerAdapter)?.refreshEvents(week_view_view_pager.currentItem)
else -> (main_view_pager.adapter as? MyMonthPagerAdapter)?.refreshEvents(main_view_pager.currentItem)
}
} }
private fun tryImportEvents() { private fun tryImportEvents() {
@ -609,188 +602,6 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
BuildConfig.VERSION_NAME) BuildConfig.VERSION_NAME)
} }
private fun resetTitle() {
supportActionBar?.title = getString(R.string.app_launcher_name)
supportActionBar?.subtitle = ""
}
private fun fillMonthlyViewPager(targetDay: String) {
main_weekly_scrollview.beGone()
calendar_fab.beVisible()
val codes = getMonths(targetDay)
val monthlyAdapter = MyMonthPagerAdapter(supportFragmentManager, codes, this)
mDefaultMonthlyPage = codes.size / 2
main_view_pager.apply {
adapter = monthlyAdapter
beVisible()
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
if (mIsGoToTodayVisible != shouldGoToTodayBeVisible()) {
invalidateOptionsMenu()
}
if (config.storedView == YEARLY_VIEW) {
val dateTime = Formatter.getDateTimeFromCode(codes[position])
supportActionBar?.title = "${getString(R.string.app_launcher_name)} - ${Formatter.getYear(dateTime)}"
}
}
})
currentItem = mDefaultMonthlyPage
}
calendar_event_list_holder.beGone()
}
private fun getMonths(code: String): List<String> {
val months = ArrayList<String>(PREFILLED_MONTHS)
val today = Formatter.getDateTimeFromCode(code)
for (i in -PREFILLED_MONTHS / 2..PREFILLED_MONTHS / 2) {
months.add(Formatter.getDayCodeFromDateTime(today.plusMonths(i)))
}
return months
}
private fun fillWeeklyViewPager() {
var thisweek = DateTime().withDayOfWeek(1).withTimeAtStartOfDay().minusDays(if (config.isSundayFirst) 1 else 0)
if (DateTime().minusDays(7).seconds() > thisweek.seconds()) {
thisweek = thisweek.plusDays(7)
}
val weekTSs = getWeekTimestamps(thisweek.seconds())
val weeklyAdapter = MyWeekPagerAdapter(supportFragmentManager, weekTSs, object : WeekFragment.WeekScrollListener {
override fun scrollTo(y: Int) {
week_view_hours_scrollview.scrollY = y
mWeekScrollY = y
}
})
main_view_pager.beGone()
calendar_event_list_holder.beGone()
main_weekly_scrollview.beVisible()
week_view_hours_holder.removeAllViews()
val hourDateTime = DateTime().withDate(2000, 1, 1).withTime(0, 0, 0, 0)
for (i in 1..23) {
val formattedHours = Formatter.getHours(applicationContext, hourDateTime.withHourOfDay(i))
(layoutInflater.inflate(R.layout.weekly_view_hour_textview, null, false) as TextView).apply {
text = formattedHours
setTextColor(mStoredTextColor)
week_view_hours_holder.addView(this)
}
}
mDefaultWeeklyPage = weekTSs.size / 2
week_view_view_pager.apply {
adapter = weeklyAdapter
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
if (mIsGoToTodayVisible != shouldGoToTodayBeVisible()) {
invalidateOptionsMenu()
}
setupWeeklyActionbarTitle(weekTSs[position])
}
})
currentItem = mDefaultWeeklyPage
}
week_view_hours_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
override fun onScrollChanged(scrollView: MyScrollView, x: Int, y: Int, oldx: Int, oldy: Int) {
mWeekScrollY = y
weeklyAdapter.updateScrollY(week_view_view_pager.currentItem, y)
}
})
week_view_hours_scrollview.setOnTouchListener { view, motionEvent -> true }
}
fun updateHoursTopMargin(margin: Int) {
week_view_hours_divider.layoutParams.height = margin
week_view_hours_scrollview.requestLayout()
}
private fun getWeekTimestamps(targetWeekTS: Int): List<Int> {
val weekTSs = ArrayList<Int>(PREFILLED_WEEKS)
for (i in -PREFILLED_WEEKS / 2..PREFILLED_WEEKS / 2) {
weekTSs.add(Formatter.getDateTimeFromTS(targetWeekTS).plusWeeks(i).seconds())
}
return weekTSs
}
private fun setupWeeklyActionbarTitle(timestamp: Int) {
val startDateTime = Formatter.getDateTimeFromTS(timestamp)
val endDateTime = Formatter.getDateTimeFromTS(timestamp + WEEK_SECONDS)
val startMonthName = Formatter.getMonthName(applicationContext, startDateTime.monthOfYear)
if (startDateTime.monthOfYear == endDateTime.monthOfYear) {
var newTitle = startMonthName
if (startDateTime.year != DateTime().year)
newTitle += " - ${startDateTime.year}"
supportActionBar?.title = newTitle
} else {
val endMonthName = Formatter.getMonthName(applicationContext, endDateTime.monthOfYear)
supportActionBar?.title = "$startMonthName - $endMonthName"
}
supportActionBar?.subtitle = "${getString(R.string.week)} ${startDateTime.plusDays(3).weekOfWeekyear}"
}
private fun fillYearlyViewPager() {
main_weekly_scrollview.beGone()
calendar_fab.beGone()
val targetYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt()
val years = getYears(targetYear)
val yearlyAdapter = MyYearPagerAdapter(supportFragmentManager, years, this)
mDefaultYearlyPage = years.size / 2
main_view_pager.apply {
adapter = yearlyAdapter
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
if (mIsGoToTodayVisible != shouldGoToTodayBeVisible()) {
invalidateOptionsMenu()
}
if (position < years.size) {
supportActionBar?.title = "${getString(R.string.app_launcher_name)} - ${years[position]}"
}
}
})
currentItem = mDefaultYearlyPage
beVisible()
}
supportActionBar?.title = "${getString(R.string.app_launcher_name)} - ${years[years.size / 2]}"
calendar_event_list_holder.beGone()
}
private fun getYears(targetYear: Int): List<Int> {
val years = ArrayList<Int>(PREFILLED_YEARS)
years += targetYear - PREFILLED_YEARS / 2..targetYear + PREFILLED_YEARS / 2
return years
}
private fun fillEventsList() {
main_view_pager.adapter = null
main_view_pager.beGone()
main_weekly_scrollview.beGone()
calendar_event_list_holder.beVisible()
supportFragmentManager.beginTransaction().replace(R.id.calendar_event_list_holder, EventListFragment(), "").commit()
}
private fun searchQueryChanged(text: String) { private fun searchQueryChanged(text: String) {
mLatestSearchQuery = text mLatestSearchQuery = text
search_placeholder_2.beGoneIf(text.length >= 2) search_placeholder_2.beGoneIf(text.length >= 2)
@ -826,17 +637,15 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
refreshViewPager() refreshViewPager()
} }
override fun goLeft() { fun openMonthFromYearly(dateTime: DateTime) {
main_view_pager.currentItem = main_view_pager.currentItem - 1
}
override fun goRight() {
main_view_pager.currentItem = main_view_pager.currentItem + 1
}
override fun goToDateTime(dateTime: DateTime) {
fillMonthlyViewPager(Formatter.getDayCodeFromDateTime(dateTime))
mIsMonthSelected = true mIsMonthSelected = true
val fragment = MonthFragmentsHolder()
currentFragment = fragment
val bundle = Bundle()
bundle.putString(DAY_CODE, Formatter.getDayCodeFromDateTime(dateTime))
fragment.arguments = bundle
supportFragmentManager.beginTransaction().replace(R.id.fragments_holder, fragment, "").commit()
resetActionBarTitle()
} }
private fun openDayAt(timestamp: Long) { private fun openDayAt(timestamp: Long) {

View file

@ -4,14 +4,11 @@ import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray
import com.simplemobiletools.calendar.fragments.MonthFragment import com.simplemobiletools.calendar.fragments.MonthFragment
import com.simplemobiletools.calendar.helpers.DAY_CODE import com.simplemobiletools.calendar.helpers.DAY_CODE
import com.simplemobiletools.calendar.interfaces.NavigationListener import com.simplemobiletools.calendar.interfaces.NavigationListener
class MyMonthPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) { class MyMonthPagerAdapter(fm: FragmentManager, private val mCodes: List<String>, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<MonthFragment>()
override fun getCount() = mCodes.size override fun getCount() = mCodes.size
override fun getItem(position: Int): Fragment { override fun getItem(position: Int): Fragment {
@ -23,13 +20,6 @@ class MyMonthPagerAdapter(fm: FragmentManager, private val mCodes: List<String>,
fragment.arguments = bundle fragment.arguments = bundle
fragment.listener = mListener fragment.listener = mListener
mFragments.put(position, fragment)
return fragment return fragment
} }
fun refreshEvents(pos: Int) {
for (i in -1..1) {
mFragments[pos + i]?.updateCalendar()
}
}
} }

View file

@ -7,8 +7,9 @@ import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray import android.util.SparseArray
import com.simplemobiletools.calendar.fragments.WeekFragment import com.simplemobiletools.calendar.fragments.WeekFragment
import com.simplemobiletools.calendar.helpers.WEEK_START_TIMESTAMP import com.simplemobiletools.calendar.helpers.WEEK_START_TIMESTAMP
import com.simplemobiletools.calendar.interfaces.WeekFragmentListener
class MyWeekPagerAdapter(fm: FragmentManager, val mWeekTimestamps: List<Int>, val mListener: WeekFragment.WeekScrollListener) : FragmentStatePagerAdapter(fm) { class MyWeekPagerAdapter(fm: FragmentManager, val mWeekTimestamps: List<Int>, val mListener: WeekFragmentListener) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<WeekFragment>() private val mFragments = SparseArray<WeekFragment>()
override fun getCount() = mWeekTimestamps.size override fun getCount() = mWeekTimestamps.size
@ -30,10 +31,4 @@ class MyWeekPagerAdapter(fm: FragmentManager, val mWeekTimestamps: List<Int>, va
mFragments[pos - 1]?.updateScrollY(y) mFragments[pos - 1]?.updateScrollY(y)
mFragments[pos + 1]?.updateScrollY(y) mFragments[pos + 1]?.updateScrollY(y)
} }
fun refreshEvents(pos: Int) {
for (i in -1..1) {
mFragments[pos + i]?.updateEvents()
}
}
} }

View file

@ -4,14 +4,10 @@ import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray
import com.simplemobiletools.calendar.fragments.YearFragment import com.simplemobiletools.calendar.fragments.YearFragment
import com.simplemobiletools.calendar.helpers.YEAR_LABEL import com.simplemobiletools.calendar.helpers.YEAR_LABEL
import com.simplemobiletools.calendar.interfaces.NavigationListener
class MyYearPagerAdapter(fm: FragmentManager, val mYears: List<Int>, val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<YearFragment>()
class MyYearPagerAdapter(fm: FragmentManager, val mYears: List<Int>) : FragmentStatePagerAdapter(fm) {
override fun getCount() = mYears.size override fun getCount() = mYears.size
override fun getItem(position: Int): Fragment { override fun getItem(position: Int): Fragment {
@ -21,15 +17,7 @@ class MyYearPagerAdapter(fm: FragmentManager, val mYears: List<Int>, val mListen
val fragment = YearFragment() val fragment = YearFragment()
fragment.arguments = bundle fragment.arguments = bundle
fragment.mListener = mListener
mFragments.put(position, fragment)
return fragment return fragment
} }
fun refreshEvents(pos: Int) {
for (i in -1..1) {
mFragments[pos + i]?.updateEvents()
}
}
} }

View file

@ -6,3 +6,5 @@ fun String.substringTo(cnt: Int): String {
} else } else
substring(0, Math.min(length, cnt)) substring(0, Math.min(length, cnt))
} }
fun String.getMonthCode() = if (length == 8) substring(0, 6) else ""

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.calendar.fragments
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -22,7 +21,7 @@ import kotlinx.android.synthetic.main.fragment_event_list.view.*
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.* import java.util.*
class EventListFragment : Fragment(), RefreshRecyclerViewListener { class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
private var mEvents: List<Event> = ArrayList() private var mEvents: List<Event> = ArrayList()
private var prevEventsHash = 0 private var prevEventsHash = 0
private var lastHash = 0 private var lastHash = 0
@ -60,8 +59,9 @@ class EventListFragment : Fragment(), RefreshRecyclerViewListener {
val filtered = context!!.getFilteredEvents(events) val filtered = context!!.getFilteredEvents(events)
val hash = filtered.hashCode() val hash = filtered.hashCode()
if (prevEventsHash == hash) if (prevEventsHash == hash) {
return return
}
prevEventsHash = hash prevEventsHash = hash
mEvents = filtered mEvents = filtered
@ -97,4 +97,11 @@ class EventListFragment : Fragment(), RefreshRecyclerViewListener {
override fun refreshItems() { override fun refreshItems() {
checkEvents() checkEvents()
} }
override fun goToToday() {
}
override fun refreshEvents() {
checkEvents()
}
} }

View file

@ -47,7 +47,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
val view = inflater.inflate(R.layout.fragment_month, container, false) val view = inflater.inflate(R.layout.fragment_month, container, false)
mRes = resources mRes = resources
mPackageName = activity!!.packageName mPackageName = activity!!.packageName
mHolder = view.calendar_holder mHolder = view.month_calendar_holder
mDayCode = arguments!!.getString(DAY_CODE) mDayCode = arguments!!.getString(DAY_CODE)
mConfig = context!!.config mConfig = context!!.config
mSundayFirst = mConfig.isSundayFirst mSundayFirst = mConfig.isSundayFirst
@ -74,7 +74,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
updateCalendar() updateCalendar()
} }
fun updateCalendar() { private fun updateCalendar() {
mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode)) mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
} }

View file

@ -0,0 +1,101 @@
package com.simplemobiletools.calendar.fragments
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.MyMonthPagerAdapter
import com.simplemobiletools.calendar.extensions.getMonthCode
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_viewpager_holder.view.*
import org.joda.time.DateTime
class MonthFragmentsHolder : MyFragmentHolder(), NavigationListener {
private val PREFILLED_MONTHS = 97
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(context!!)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_viewpager_holder, container, false)
viewPager = view.fragment_viewpager
setupFragment()
return view
}
private fun shouldGoToTodayBeVisible() = currentDayCode.getMonthCode() != todayDayCode.getMonthCode()
private fun setupFragment() {
val codes = getMonths(currentDayCode)
val monthlyAdapter = MyMonthPagerAdapter(activity!!.supportFragmentManager, codes, this)
defaultMonthlyPage = codes.size / 2
viewPager!!.apply {
adapter = monthlyAdapter
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
}
}
private fun getMonths(code: String): List<String> {
val months = ArrayList<String>(PREFILLED_MONTHS)
val today = Formatter.getDateTimeFromCode(code)
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 refreshEvents() {
setupFragment()
}
}

View file

@ -0,0 +1,9 @@
package com.simplemobiletools.calendar.fragments
import android.support.v4.app.Fragment
abstract class MyFragmentHolder : Fragment() {
abstract fun goToToday()
abstract fun refreshEvents()
}

View file

@ -6,18 +6,20 @@ import android.graphics.Rect
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.util.SparseIntArray
import android.view.* import android.view.*
import android.widget.ImageView import android.widget.ImageView
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getFilteredEvents import com.simplemobiletools.calendar.extensions.getFilteredEvents
import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.interfaces.WeekFragmentListener
import com.simplemobiletools.calendar.interfaces.WeeklyCalendar import com.simplemobiletools.calendar.interfaces.WeeklyCalendar
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.views.MyScrollView import com.simplemobiletools.calendar.views.MyScrollView
@ -35,7 +37,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private val CLICK_DURATION_THRESHOLD = 150 private val CLICK_DURATION_THRESHOLD = 150
private val PLUS_FADEOUT_DELAY = 5000L private val PLUS_FADEOUT_DELAY = 5000L
var mListener: WeekScrollListener? = null var mListener: WeekFragmentListener? = null
private var mWeekTimestamp = 0 private var mWeekTimestamp = 0
private var mRowHeight = 0 private var mRowHeight = 0
private var minScrollY = -1 private var minScrollY = -1
@ -52,20 +54,30 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private var events = ArrayList<Event>() private var events = ArrayList<Event>()
private var allDayHolders = ArrayList<RelativeLayout>() private var allDayHolders = ArrayList<RelativeLayout>()
private var allDayRows = ArrayList<HashSet<Int>>() private var allDayRows = ArrayList<HashSet<Int>>()
private var eventTypeColors = SparseIntArray()
lateinit var inflater: LayoutInflater lateinit var inflater: LayoutInflater
lateinit var mView: View lateinit var mView: View
lateinit var mCalendar: WeeklyCalendarImpl lateinit var mCalendar: WeeklyCalendarImpl
lateinit var mRes: Resources lateinit var mRes: Resources
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreate(savedInstanceState: Bundle?) {
this.inflater = inflater super.onCreate(savedInstanceState)
context!!.dbHelper.getEventTypes {
it.map { eventTypeColors.put(it.id, it.color) }
}
mRowHeight = (context!!.resources.getDimension(R.dimen.weekly_view_row_height)).toInt() mRowHeight = (context!!.resources.getDimension(R.dimen.weekly_view_row_height)).toInt()
minScrollY = mRowHeight * context!!.config.startWeeklyAt minScrollY = mRowHeight * context!!.config.startWeeklyAt
mWeekTimestamp = arguments!!.getInt(WEEK_START_TIMESTAMP) mWeekTimestamp = arguments!!.getInt(WEEK_START_TIMESTAMP)
primaryColor = context!!.getAdjustedPrimaryColor() primaryColor = context!!.getAdjustedPrimaryColor()
mRes = resources mRes = resources
allDayRows.add(HashSet()) allDayRows.add(HashSet())
mCalendar = WeeklyCalendarImpl(this, context!!)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
this.inflater = inflater
mView = inflater.inflate(R.layout.fragment_week, container, false).apply { mView = inflater.inflate(R.layout.fragment_week, container, false).apply {
week_events_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener { week_events_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
@ -77,7 +89,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
week_events_scrollview.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { week_events_scrollview.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() { override fun onGlobalLayout() {
week_events_scrollview.viewTreeObserver.removeOnGlobalLayoutListener(this) week_events_scrollview.viewTreeObserver.removeOnGlobalLayoutListener(this)
updateScrollY(Math.max(MainActivity.mWeekScrollY, minScrollY)) updateScrollY(Math.max(mListener?.getCurrScrollY() ?: 0, minScrollY))
} }
}) })
} }
@ -85,7 +97,6 @@ class WeekFragment : Fragment(), WeeklyCalendar {
(0..6).map { inflater.inflate(R.layout.stroke_vertical_divider, mView.week_vertical_grid_holder) } (0..6).map { inflater.inflate(R.layout.stroke_vertical_divider, mView.week_vertical_grid_holder) }
(0..23).map { inflater.inflate(R.layout.stroke_horizontal_divider, mView.week_horizontal_grid_holder) } (0..23).map { inflater.inflate(R.layout.stroke_horizontal_divider, mView.week_horizontal_grid_holder) }
mCalendar = WeeklyCalendarImpl(this, context!!)
wasFragmentInit = true wasFragmentInit = true
return mView return mView
} }
@ -94,7 +105,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
isFragmentVisible = menuVisible isFragmentVisible = menuVisible
if (isFragmentVisible && wasFragmentInit) { if (isFragmentVisible && wasFragmentInit) {
(activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height) mListener?.updateHoursTopMargin(mView.week_top_holder.height)
checkScrollLimits(mView.week_events_scrollview.scrollY) checkScrollLimits(mView.week_events_scrollview.scrollY)
} }
} }
@ -269,7 +280,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val duration = endDateTime.minuteOfDay - startMinutes val duration = endDateTime.minuteOfDay - startMinutes
(inflater.inflate(R.layout.week_event_marker, null, false) as TextView).apply { (inflater.inflate(R.layout.week_event_marker, null, false) as TextView).apply {
val backgroundColor = MainActivity.eventTypeColors.get(event.eventType, primaryColor) val backgroundColor = eventTypeColors.get(event.eventType, primaryColor)
background = ColorDrawable(backgroundColor) background = ColorDrawable(backgroundColor)
setTextColor(backgroundColor.getContrastColor()) setTextColor(backgroundColor.getContrastColor())
text = event.title text = event.title
@ -327,7 +338,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
override fun onGlobalLayout() { override fun onGlobalLayout() {
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this) mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (isFragmentVisible && activity != null) { if (isFragmentVisible && activity != null) {
(activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height) mListener?.updateHoursTopMargin(mView.week_top_holder.height)
} }
} }
}) })
@ -338,7 +349,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
if (activity == null) if (activity == null)
return return
val backgroundColor = MainActivity.eventTypeColors.get(event.eventType, primaryColor) val backgroundColor = eventTypeColors.get(event.eventType, primaryColor)
background = ColorDrawable(backgroundColor) background = ColorDrawable(backgroundColor)
setTextColor(backgroundColor.getContrastColor()) setTextColor(backgroundColor.getContrastColor())
text = event.title text = event.title
@ -413,7 +424,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this) mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (isFragmentVisible) { if (isFragmentVisible) {
(activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height) mListener?.updateHoursTopMargin(mView.week_top_holder.height)
} }
if (!wasExtraHeightAdded) { if (!wasExtraHeightAdded) {
@ -435,8 +446,4 @@ class WeekFragment : Fragment(), WeeklyCalendar {
if (wasFragmentInit) if (wasFragmentInit)
mView.week_events_scrollview.scrollY = y mView.week_events_scrollview.scrollY = y
} }
interface WeekScrollListener {
fun scrollTo(y: Int)
}
} }

View file

@ -0,0 +1,142 @@
package com.simplemobiletools.calendar.fragments
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.WEEK_SECONDS
import com.simplemobiletools.calendar.helpers.WEEK_START_DATE_TIME
import com.simplemobiletools.calendar.interfaces.WeekFragmentListener
import com.simplemobiletools.calendar.views.MyScrollView
import kotlinx.android.synthetic.main.fragment_week_holder.*
import kotlinx.android.synthetic.main.fragment_week_holder.view.*
import org.joda.time.DateTime
class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener {
private val PREFILLED_WEEKS = 61
private var weekHolder: ViewGroup? = null
private var defaultWeeklyPage = 0
private var thisWeekTS = 0
private var currentWeekTS = 0
private var isGoToTodayVisible = false
private var weekScrollY = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dateTimeString = arguments?.getString(WEEK_START_DATE_TIME) ?: ""
currentWeekTS = (DateTime.parse(dateTimeString) ?: DateTime()).seconds()
thisWeekTS = currentWeekTS
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
weekHolder = inflater.inflate(R.layout.fragment_week_holder, container, false) as ViewGroup
setupFragment()
return weekHolder
}
private fun shouldGoToTodayBeVisible() = currentWeekTS != thisWeekTS
private fun setupFragment() {
val weekTSs = getWeekTimestamps(currentWeekTS)
val weeklyAdapter = MyWeekPagerAdapter(activity!!.supportFragmentManager, weekTSs, this)
val textColor = context!!.config.textColor
weekHolder!!.week_view_hours_holder.removeAllViews()
val hourDateTime = DateTime().withDate(2000, 1, 1).withTime(0, 0, 0, 0)
for (i in 1..23) {
val formattedHours = Formatter.getHours(context!!, hourDateTime.withHourOfDay(i))
(layoutInflater.inflate(R.layout.weekly_view_hour_textview, null, false) as TextView).apply {
text = formattedHours
setTextColor(textColor)
weekHolder!!.week_view_hours_holder.addView(this)
}
}
defaultWeeklyPage = weekTSs.size / 2
weekHolder!!.week_view_view_pager.apply {
adapter = weeklyAdapter
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
currentWeekTS = weekTSs[position]
val shouldGoToTodayBeVisible = shouldGoToTodayBeVisible()
if (isGoToTodayVisible != shouldGoToTodayBeVisible) {
(activity as? MainActivity)?.toggleGoToTodayVisibility(shouldGoToTodayBeVisible)
isGoToTodayVisible = shouldGoToTodayBeVisible
}
setupWeeklyActionbarTitle(weekTSs[position])
}
})
currentItem = defaultWeeklyPage
}
weekHolder!!.week_view_hours_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
override fun onScrollChanged(scrollView: MyScrollView, x: Int, y: Int, oldx: Int, oldy: Int) {
weekScrollY = y
weeklyAdapter.updateScrollY(week_view_view_pager.currentItem, y)
}
})
weekHolder!!.week_view_hours_scrollview.setOnTouchListener { view, motionEvent -> true }
}
private fun getWeekTimestamps(targetSeconds: Int): List<Int> {
val weekTSs = ArrayList<Int>(PREFILLED_WEEKS)
for (i in -PREFILLED_WEEKS / 2..PREFILLED_WEEKS / 2) {
weekTSs.add(Formatter.getDateTimeFromTS(targetSeconds).plusWeeks(i).seconds())
}
return weekTSs
}
private fun setupWeeklyActionbarTitle(timestamp: Int) {
val startDateTime = Formatter.getDateTimeFromTS(timestamp)
val endDateTime = Formatter.getDateTimeFromTS(timestamp + WEEK_SECONDS)
val startMonthName = Formatter.getMonthName(context!!, startDateTime.monthOfYear)
if (startDateTime.monthOfYear == endDateTime.monthOfYear) {
var newTitle = startMonthName
if (startDateTime.year != DateTime().year) {
newTitle += " - ${startDateTime.year}"
}
(activity as? MainActivity)?.supportActionBar?.title = newTitle
} else {
val endMonthName = Formatter.getMonthName(context!!, endDateTime.monthOfYear)
(activity as? MainActivity)?.supportActionBar?.title = "$startMonthName - $endMonthName"
}
(activity as? MainActivity)?.supportActionBar?.subtitle = "${getString(R.string.week)} ${startDateTime.plusDays(3).weekOfWeekyear}"
}
override fun goToToday() {
currentWeekTS = thisWeekTS
setupFragment()
}
override fun refreshEvents() {
setupFragment()
}
override fun scrollTo(y: Int) {
weekHolder!!.week_view_hours_scrollview.scrollY = y
weekScrollY = y
}
override fun updateHoursTopMargin(margin: Int) {
weekHolder?.week_view_hours_divider?.layoutParams?.height = margin
weekHolder?.week_view_hours_scrollview?.requestLayout()
}
override fun getCurrScrollY() = weekScrollY
}

View file

@ -9,10 +9,10 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.YEAR_LABEL import com.simplemobiletools.calendar.helpers.YEAR_LABEL
import com.simplemobiletools.calendar.helpers.YearlyCalendarImpl import com.simplemobiletools.calendar.helpers.YearlyCalendarImpl
import com.simplemobiletools.calendar.interfaces.NavigationListener
import com.simplemobiletools.calendar.interfaces.YearlyCalendar import com.simplemobiletools.calendar.interfaces.YearlyCalendar
import com.simplemobiletools.calendar.models.DayYearly import com.simplemobiletools.calendar.models.DayYearly
import com.simplemobiletools.calendar.views.SmallMonthView import com.simplemobiletools.calendar.views.SmallMonthView
@ -23,7 +23,6 @@ import org.joda.time.DateTime
import java.util.* import java.util.*
class YearFragment : Fragment(), YearlyCalendar { class YearFragment : Fragment(), YearlyCalendar {
var mListener: NavigationListener? = null
private var mYear = 0 private var mYear = 0
private var mSundayFirst = false private var mSundayFirst = false
private var lastHash = 0 private var lastHash = 0
@ -38,7 +37,6 @@ class YearFragment : Fragment(), YearlyCalendar {
setupMonths() setupMonths()
mCalendar = YearlyCalendarImpl(this, context!!, mYear) mCalendar = YearlyCalendarImpl(this, context!!, mYear)
return mView return mView
} }
@ -56,7 +54,7 @@ class YearFragment : Fragment(), YearlyCalendar {
mCalendar?.getEvents(mYear) mCalendar?.getEvents(mYear)
} }
fun setupMonths() { private fun setupMonths() {
val dateTime = DateTime().withDate(mYear, 2, 1).withHourOfDay(12) val dateTime = DateTime().withDate(mYear, 2, 1).withHourOfDay(12)
val days = dateTime.dayOfMonth().maximumValue val days = dateTime.dayOfMonth().maximumValue
mView.month_2.setDays(days) mView.month_2.setDays(days)
@ -67,12 +65,13 @@ class YearFragment : Fragment(), YearlyCalendar {
for (i in 1..12) { for (i in 1..12) {
val monthView = mView.findViewById<SmallMonthView>(res.getIdentifier("month_" + i, "id", context!!.packageName)) val monthView = mView.findViewById<SmallMonthView>(res.getIdentifier("month_" + i, "id", context!!.packageName))
var dayOfWeek = dateTime.withMonthOfYear(i).dayOfWeek().get() var dayOfWeek = dateTime.withMonthOfYear(i).dayOfWeek().get()
if (!mSundayFirst) if (!mSundayFirst) {
dayOfWeek-- dayOfWeek--
}
monthView.firstDay = dayOfWeek monthView.firstDay = dayOfWeek
monthView.setOnClickListener { monthView.setOnClickListener {
mListener?.goToDateTime(DateTime().withDate(mYear, i, 1)) (activity as MainActivity).openMonthFromYearly(DateTime().withDate(mYear, i, 1))
} }
} }
} }

View file

@ -0,0 +1,87 @@
package com.simplemobiletools.calendar.fragments
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.MyYearPagerAdapter
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.commons.views.MyViewPager
import kotlinx.android.synthetic.main.fragment_viewpager_holder.view.*
import org.joda.time.DateTime
class YearFragmentsHolder : MyFragmentHolder() {
private val PREFILLED_YEARS = 31
private var viewPager: MyViewPager? = null
private var defaultYearlyPage = 0
private var todayYear = 0
private var currentYear = 0
private var isGoToTodayVisible = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
currentYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt()
todayYear = currentYear
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_viewpager_holder, container, false)
viewPager = view.fragment_viewpager
setupFragment()
return view
}
private fun shouldGoToTodayBeVisible() = currentYear != todayYear
private fun setupFragment() {
val years = getYears(currentYear)
val yearlyAdapter = MyYearPagerAdapter(activity!!.supportFragmentManager, years)
defaultYearlyPage = years.size / 2
viewPager?.apply {
adapter = yearlyAdapter
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
currentYear = years[position]
val shouldGoToTodayBeVisible = shouldGoToTodayBeVisible()
if (isGoToTodayVisible != shouldGoToTodayBeVisible) {
(activity as? MainActivity)?.toggleGoToTodayVisibility(shouldGoToTodayBeVisible)
isGoToTodayVisible = shouldGoToTodayBeVisible
}
if (position < years.size) {
(activity as? MainActivity)?.supportActionBar?.title = "${getString(R.string.app_launcher_name)} - ${years[position]}"
}
}
})
currentItem = defaultYearlyPage
}
(activity as? MainActivity)?.supportActionBar?.title = "${getString(R.string.app_launcher_name)} - ${years[years.size / 2]}"
}
private fun getYears(targetYear: Int): List<Int> {
val years = ArrayList<Int>(PREFILLED_YEARS)
years += targetYear - PREFILLED_YEARS / 2..targetYear + PREFILLED_YEARS / 2
return years
}
override fun goToToday() {
currentYear = todayYear
setupFragment()
}
override fun refreshEvents() {
setupFragment()
}
}

View file

@ -13,6 +13,7 @@ val EVENT_OCCURRENCE_TS = "event_occurrence_ts"
val NEW_EVENT_START_TS = "new_event_start_ts" val NEW_EVENT_START_TS = "new_event_start_ts"
val WEEK_START_TIMESTAMP = "week_start_timestamp" val WEEK_START_TIMESTAMP = "week_start_timestamp"
val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration" val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration"
val WEEK_START_DATE_TIME = "week_start_date_time"
val CALDAV = "Caldav" val CALDAV = "Caldav"
val MONTHLY_VIEW = 1 val MONTHLY_VIEW = 1

View file

@ -0,0 +1,9 @@
package com.simplemobiletools.calendar.interfaces
interface WeekFragmentListener {
fun scrollTo(y: Int)
fun updateHoursTopMargin(margin: Int)
fun getCurrScrollY(): Int
}

View file

@ -6,66 +6,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout <FrameLayout
android:id="@+id/main_weekly_scrollview" android:id="@+id/fragments_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:visibility="gone">
<RelativeLayout
android:id="@+id/week_view_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/week_view_hours_divider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignRight="@+id/week_view_hours_scrollview"
android:background="@drawable/stroke_bottom_right"
android:importantForAccessibility="no"/>
<com.simplemobiletools.calendar.views.MyScrollView
android:id="@+id/week_view_hours_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/week_view_hours_divider"
android:background="@drawable/stroke_right"
android:overScrollMode="never"
android:paddingTop="@dimen/medium_margin"
android:scrollbars="none">
<LinearLayout
android:id="@+id/week_view_hours_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/weekly_view_row_height"/>
</com.simplemobiletools.calendar.views.MyScrollView>
<com.simplemobiletools.commons.views.MyViewPager
android:id="@+id/week_view_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/week_view_hours_scrollview"/>
</RelativeLayout>
</LinearLayout>
<com.simplemobiletools.commons.views.MyViewPager
android:id="@+id/main_view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/calendar_event_list_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<com.simplemobiletools.commons.views.MyFloatingActionButton <com.simplemobiletools.commons.views.MyFloatingActionButton
android:id="@+id/calendar_fab" android:id="@+id/calendar_fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -81,6 +26,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clickable="true" android:clickable="true"
android:focusable="true"
android:visibility="gone"> android:visibility="gone">
<com.simplemobiletools.commons.views.MyTextView <com.simplemobiletools.commons.views.MyTextView

View file

@ -2,7 +2,7 @@
<RelativeLayout <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/calendar_holder" android:id="@+id/month_calendar_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="@dimen/medium_margin"> android:padding="@dimen/medium_margin">

View file

@ -0,0 +1,6 @@
<?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_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/week_view_main_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/week_view_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/week_view_hours_divider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignRight="@+id/week_view_hours_scrollview"
android:background="@drawable/stroke_bottom_right"
android:importantForAccessibility="no"/>
<com.simplemobiletools.calendar.views.MyScrollView
android:id="@+id/week_view_hours_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/week_view_hours_divider"
android:background="@drawable/stroke_right"
android:overScrollMode="never"
android:paddingTop="@dimen/medium_margin"
android:scrollbars="none">
<LinearLayout
android:id="@+id/week_view_hours_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/weekly_view_row_height"/>
</com.simplemobiletools.calendar.views.MyScrollView>
<com.simplemobiletools.commons.views.MyViewPager
android:id="@+id/week_view_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/week_view_hours_scrollview"/>
</RelativeLayout>
</LinearLayout>