fill MyYearPagerAdapter

This commit is contained in:
tibbi 2016-10-17 19:23:06 +02:00
parent f7fd91def6
commit 17fb1f87a8
6 changed files with 87 additions and 6 deletions

View File

@ -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;

View File

@ -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";

View File

@ -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<Int> {
val years = ArrayList<Int>(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
}

View File

@ -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<Int>, 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
}
}

View File

@ -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
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>