use real week timestamps at week fragments

This commit is contained in:
tibbi 2017-01-17 23:28:42 +01:00
parent 378abf5c98
commit 1fb9299aee
7 changed files with 56 additions and 28 deletions

View File

@ -16,6 +16,8 @@ import com.simplemobiletools.calendar.adapters.MyWeekPagerAdapter
import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter
import com.simplemobiletools.calendar.dialogs.ChangeViewDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.extensions.secondsInWeek
import com.simplemobiletools.calendar.extensions.updateWidgets
import com.simplemobiletools.calendar.fragments.EventListFragment
import com.simplemobiletools.calendar.fragments.WeekFragment
@ -36,6 +38,7 @@ import java.util.*
class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
private val PREFILLED_MONTHS = 73
private val PREFILLED_YEARS = 21
private val PREFILLED_WEEKS = 41
private var mIsMonthSelected = false
private var mSnackbar: Snackbar? = null
@ -169,7 +172,9 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
}
private fun fillWeeklyViewPager() {
val weeklyAdapter = MyWeekPagerAdapter(supportFragmentManager, object : WeekFragment.WeekScrollListener {
val thisweek = DateTime().withDayOfWeek(1).withTime(0, 0, 0, 0).seconds()
val weekTSs = getWeekTimestamps(thisweek)
val weeklyAdapter = MyWeekPagerAdapter(supportFragmentManager, weekTSs, object : WeekFragment.WeekScrollListener {
override fun scrollTo(y: Int) {
week_view_hours_scrollview.scrollY = y
mWeekScrollY = y
@ -187,7 +192,10 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
week_view_hours_holder.addView(view)
}
week_view_view_pager.adapter = weeklyAdapter
week_view_view_pager.apply {
adapter = weeklyAdapter
currentItem = weekTSs.size / 2
}
week_view_hours_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
override fun onScrollChanged(scrollView: MyScrollView, x: Int, y: Int, oldx: Int, oldy: Int) {
@ -197,6 +205,14 @@ class MainActivity : SimpleActivity(), EventListFragment.DeleteListener {
})
}
private fun getWeekTimestamps(targetWeekTS: Int): List<Int> {
val weekTSs = ArrayList<Int>(PREFILLED_WEEKS)
for (i in -PREFILLED_WEEKS / 2..PREFILLED_WEEKS / 2) {
weekTSs.add(targetWeekTS + i * secondsInWeek)
}
return weekTSs
}
private fun fillYearlyViewPager() {
main_weekly_scrollview.visibility = View.GONE
calendar_fab.visibility = View.GONE

View File

@ -6,14 +6,18 @@ import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.util.SparseArray
import com.simplemobiletools.calendar.fragments.WeekFragment
import com.simplemobiletools.calendar.helpers.WEEK_START_TIMESTAMP
class MyWeekPagerAdapter(fm: FragmentManager, private val mListener: WeekFragment.WeekScrollListener) : FragmentStatePagerAdapter(fm) {
class MyWeekPagerAdapter(fm: FragmentManager, val mWeekTimestamps: List<Int>, val mListener: WeekFragment.WeekScrollListener) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<WeekFragment>()
override fun getCount() = 1
override fun getCount() = mWeekTimestamps.size
override fun getItem(position: Int): Fragment {
val bundle = Bundle()
val weekTimestamp = mWeekTimestamps[position]
bundle.putInt(WEEK_START_TIMESTAMP, weekTimestamp)
val fragment = WeekFragment()
fragment.arguments = bundle
fragment.setListener(mListener)

View File

@ -8,7 +8,7 @@ import com.simplemobiletools.calendar.fragments.YearFragment
import com.simplemobiletools.calendar.helpers.YEAR_LABEL
import com.simplemobiletools.calendar.interfaces.NavigationListener
class MyYearPagerAdapter(fm: FragmentManager, private val mYears: List<Int>, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
class MyYearPagerAdapter(fm: FragmentManager, val mYears: List<Int>, val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) {
override fun getCount() = mYears.size

View File

@ -99,3 +99,5 @@ fun Context.getAppropriateTheme(): Int {
}
val Context.config: Config get() = Config.newInstance(this)
val Context.secondsInWeek: Int get() = 7 * 24 * 60 * 60

View File

@ -13,42 +13,51 @@ import com.simplemobiletools.calendar.activities.MainActivity
import com.simplemobiletools.calendar.adapters.WeekEventsAdapter
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.calendar.helpers.WEEK_START_TIMESTAMP
import com.simplemobiletools.calendar.helpers.WeeklyCalendarImpl
import com.simplemobiletools.calendar.interfaces.WeeklyCalendar
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.views.MyScrollView
import kotlinx.android.synthetic.main.fragment_week.view.*
import org.joda.time.DateTime
class WeekFragment : Fragment(), WeeklyCalendar {
private var mListener: WeekScrollListener? = null
private var mWeekTimestamp = 0
lateinit var mView: View
lateinit var mCalendar: WeeklyCalendarImpl
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mView = inflater.inflate(R.layout.fragment_week, container, false)
mView = inflater.inflate(R.layout.fragment_week, container, false).apply {
mView.week_events_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
override fun onScrollChanged(scrollView: MyScrollView, x: Int, y: Int, oldx: Int, oldy: Int) {
mListener?.scrollTo(y)
}
})
week_events_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener {
override fun onScrollChanged(scrollView: MyScrollView, x: Int, y: Int, oldx: Int, oldy: Int) {
mListener?.scrollTo(y)
}
})
mView.week_events_scrollview.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
updateScrollY(MainActivity.mWeekScrollY)
mView.week_events_scrollview.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
week_events_scrollview.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
updateScrollY(MainActivity.mWeekScrollY)
mView.week_events_scrollview.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
mView.week_events_grid.adapter = WeekEventsAdapter(context)
week_events_grid.adapter = WeekEventsAdapter(context)
}
mWeekTimestamp = arguments.getInt(WEEK_START_TIMESTAMP)
mCalendar = WeeklyCalendarImpl(this, context)
setupDayLabels()
return mView
}
private fun setupDayLabels() {
}
override fun onResume() {
super.onResume()
mCalendar.updateWeeklyCalendar(DateTime())
mCalendar.updateWeeklyCalendar(mWeekTimestamp)
}
override fun updateWeeklyCalendar(events: List<Event>) {

View File

@ -8,6 +8,7 @@ val MEDIUM_ALPHA = .6f
val DAY_CODE = "day_code"
val YEAR_LABEL = "year"
val EVENT_ID = "event_id"
val WEEK_START_TIMESTAMP = "week_start_timestamp"
val MONTHLY_VIEW = 1
val YEARLY_VIEW = 2

View File

@ -1,25 +1,21 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.extensions.secondsInWeek
import com.simplemobiletools.calendar.interfaces.WeeklyCalendar
import com.simplemobiletools.calendar.models.Event
import org.joda.time.DateTime
import java.util.*
class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) : DBHelper.GetEventsListener {
var mEvents: List<Event>
lateinit var mTargetDate: DateTime
init {
mEvents = ArrayList<Event>()
}
fun updateWeeklyCalendar(targetDate: DateTime) {
mTargetDate = targetDate
val startTS = mTargetDate.seconds()
val endTS = mTargetDate.plusWeeks(1).seconds()
fun updateWeeklyCalendar(weekStartTS: Int) {
val startTS = weekStartTS
val endTS = startTS + mContext.secondsInWeek
DBHelper(mContext).getEvents(startTS, endTS, this)
}