From 17fb1f87a8ca955026b373551da47e19908ad3d7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 17 Oct 2016 19:23:06 +0200 Subject: [PATCH] fill MyYearPagerAdapter --- .../simplemobiletools/calendar/Constants.java | 1 + .../simplemobiletools/calendar/Formatter.java | 2 +- .../calendar/activities/MainActivity.kt | 28 +++++++++++++++---- .../calendar/adapters/MyYearPagerAdapter.kt | 27 ++++++++++++++++++ .../calendar/fragments/YearFragment.kt | 27 ++++++++++++++++++ app/src/main/res/layout/year_fragment.xml | 8 ++++++ 6 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyYearPagerAdapter.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt create mode 100644 app/src/main/res/layout/year_fragment.xml diff --git a/app/src/main/java/com/simplemobiletools/calendar/Constants.java b/app/src/main/java/com/simplemobiletools/calendar/Constants.java index 230316932..a28af7a98 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Constants.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Constants.java @@ -5,6 +5,7 @@ public class Constants { public static final float HIGH_ALPHA = .8f; public static final String DAY_CODE = "day_code"; + public static final String YEAR_LABEL = "year"; public static final String EVENT = "event"; public static final int MONTHLY_VIEW = 1; diff --git a/app/src/main/java/com/simplemobiletools/calendar/Formatter.java b/app/src/main/java/com/simplemobiletools/calendar/Formatter.java index 8d7cc9b35..9105c686b 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Formatter.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Formatter.java @@ -9,8 +9,8 @@ import org.joda.time.format.DateTimeFormatter; public class Formatter { public static final String DAYCODE_PATTERN = "YYYYMMdd"; + public static final String YEAR_PATTERN = "YYYY"; private static final String DAY_PATTERN = "d"; - private static final String YEAR_PATTERN = "YYYY"; private static final String EVENT_DATE_PATTERN = "d YYYY"; // MMMM doesn't give the proper month name in some languages private static final String EVENT_TIME_PATTERN = "HH:mm"; 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 fccfd2662..18b42db2c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -7,6 +7,7 @@ import android.view.MenuItem import com.simplemobiletools.calendar.* import com.simplemobiletools.calendar.Formatter import com.simplemobiletools.calendar.adapters.MyMonthPagerAdapter +import com.simplemobiletools.calendar.adapters.MyYearPagerAdapter import com.simplemobiletools.calendar.extensions.updateWidget import kotlinx.android.synthetic.main.activity_main.* import org.joda.time.DateTime @@ -15,6 +16,7 @@ import java.util.* class MainActivity : SimpleActivity(), NavigationListener { private val PREFILLED_MONTHS = 73 + private val PREFILLED_YEARS = 21 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -73,10 +75,10 @@ class MainActivity : SimpleActivity(), NavigationListener { private fun updateViewPager() { if (mConfig.view == Constants.MONTHLY_VIEW) { - val today = DateTime().toString(Formatter.DAYCODE_PATTERN) - fillMonthlyViewPager(today) + val targetDay = DateTime().toString(Formatter.DAYCODE_PATTERN) + fillMonthlyViewPager(targetDay) } else { - + fillYearlyViewPager() } } @@ -87,8 +89,8 @@ class MainActivity : SimpleActivity(), NavigationListener { startActivity(intent) } - private fun fillMonthlyViewPager(targetMonth: String) { - val codes = getMonths(targetMonth) + private fun fillMonthlyViewPager(targetDay: String) { + val codes = getMonths(targetDay) val adapter = MyMonthPagerAdapter(supportFragmentManager, codes, this) view_pager.adapter = adapter view_pager.currentItem = codes.size / 2 @@ -104,6 +106,22 @@ class MainActivity : SimpleActivity(), NavigationListener { return months } + private fun fillYearlyViewPager() { + val targetYear = DateTime().toString(Formatter.YEAR_PATTERN).toInt() + val years = getYears(targetYear) + val adapter = MyYearPagerAdapter(supportFragmentManager, years, this) + view_pager.adapter = adapter + view_pager.currentItem = years.size / 2 + } + + private fun getYears(targetYear: Int): List { + val years = ArrayList(PREFILLED_YEARS) + for (i in targetYear - PREFILLED_YEARS / 2..targetYear + PREFILLED_YEARS / 2) + years.add(i) + + return years + } + override fun goLeft() { view_pager.currentItem = view_pager.currentItem - 1 } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyYearPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyYearPagerAdapter.kt new file mode 100644 index 000000000..5c3e2998d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/MyYearPagerAdapter.kt @@ -0,0 +1,27 @@ +package com.simplemobiletools.calendar.adapters + +import android.os.Bundle +import android.support.v4.app.Fragment +import android.support.v4.app.FragmentManager +import android.support.v4.app.FragmentStatePagerAdapter +import com.simplemobiletools.calendar.Constants +import com.simplemobiletools.calendar.NavigationListener +import com.simplemobiletools.calendar.fragments.YearFragment + +class MyYearPagerAdapter(fm: FragmentManager, private val mYears: List, private val mListener: NavigationListener) : FragmentStatePagerAdapter(fm) { + + override fun getCount(): Int { + return mYears.size + } + + override fun getItem(position: Int): Fragment { + val bundle = Bundle() + val year = mYears[position] + bundle.putInt(Constants.YEAR_LABEL, year) + + val fragment = YearFragment() + fragment.arguments = bundle + fragment.setListener(mListener) + return fragment + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt new file mode 100644 index 000000000..ee0b01725 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt @@ -0,0 +1,27 @@ +package com.simplemobiletools.calendar.fragments + +import android.os.Bundle +import android.support.v4.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.calendar.Constants +import com.simplemobiletools.calendar.NavigationListener +import com.simplemobiletools.calendar.R + +class YearFragment : Fragment() { + private var mListener: NavigationListener? = null + private var mYear = 0 + + override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { + val view = inflater!!.inflate(R.layout.year_fragment, container, false) + + mYear = arguments.getInt(Constants.YEAR_LABEL) + + return view + } + + fun setListener(listener: NavigationListener) { + mListener = listener + } +} diff --git a/app/src/main/res/layout/year_fragment.xml b/app/src/main/res/layout/year_fragment.xml new file mode 100644 index 000000000..96108a88d --- /dev/null +++ b/app/src/main/res/layout/year_fragment.xml @@ -0,0 +1,8 @@ + + + +