rewrite MonthFragment to Kotlin

This commit is contained in:
tibbi 2016-09-15 23:36:01 +02:00
parent 668e130ed9
commit 09ef991010
8 changed files with 207 additions and 246 deletions

View File

@ -167,7 +167,7 @@ public class DBHelper extends SQLiteOpenHelper {
}
public void getEvents(int fromTS, int toTS) {
List<Event> events = new ArrayList<>();
final List<Event> events = new ArrayList<>();
for (int ts = fromTS; ts <= toTS; ts += Constants.DAY) {
events.addAll(getEventsFor(ts));
}

View File

@ -104,7 +104,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private void setupButtons() {
setupIntent(PREV, R.id.top_left_arrow);
setupIntent(NEXT, R.id.top_right_arrow);
setupAppOpenIntent(R.id.top_text);
setupAppOpenIntent(R.id.month_value);
}
private SharedPreferences initPrefs(Context context) {
@ -161,7 +161,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
private void updateTopViews() {
mRemoteViews.setInt(R.id.top_text, "setTextColor", mTextColor);
mRemoteViews.setInt(R.id.month_value, "setTextColor", mTextColor);
Bitmap bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_left);
mRemoteViews.setImageViewBitmap(R.id.top_left_arrow, bmp);
@ -171,7 +171,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
public void updateMonth(String month) {
mRemoteViews.setTextViewText(R.id.top_text, month);
mRemoteViews.setTextViewText(R.id.month_value, month);
}
@Override

View File

@ -43,7 +43,7 @@ import butterknife.OnClick;
public class DayActivity extends SimpleActivity
implements DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {
@BindView(R.id.top_text) TextView mDateTV;
@BindView(R.id.month_value) TextView mDateTV;
@BindView(R.id.day_events) ListView mEventsList;
@BindView(R.id.day_coordinator) CoordinatorLayout mCoordinatorLayout;
@BindView(R.id.top_left_arrow) ImageView mLeftArrow;
@ -123,7 +123,7 @@ public class DayActivity extends SimpleActivity
switchToDay(tomorrowCode);
}
@OnClick(R.id.top_text)
@OnClick(R.id.month_value)
public void pickDay() {
final int theme = mConfig.getIsDarkTheme() ? R.style.MyAlertDialog_Dark : R.style.MyAlertDialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, theme);

View File

@ -1,233 +0,0 @@
package com.simplemobiletools.calendar.fragments;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import com.simplemobiletools.calendar.Calendar;
import com.simplemobiletools.calendar.CalendarImpl;
import com.simplemobiletools.calendar.Config;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.activities.DayActivity;
import com.simplemobiletools.calendar.models.Day;
import org.joda.time.DateTime;
import java.util.List;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MonthFragment extends Fragment implements Calendar {
@BindView(R.id.top_text) TextView mMonthTV;
@BindView(R.id.top_left_arrow) ImageView mLeftArrow;
@BindView(R.id.top_right_arrow) ImageView mRightArrow;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
private View mView;
private CalendarImpl mCalendar;
private Resources mRes;
private Config mConfig;
private NavigationListener mListener;
private String mPackageName;
private String mCode;
private int mTextColor;
private int mWeakTextColor;
private int mTextColorWithEvent;
private int mWeakTextColorWithEvent;
private boolean mSundayFirst;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.calendar_layout, container, false);
ButterKnife.bind(this, mView);
mRes = getResources();
mCode = getArguments().getString(Constants.DAY_CODE);
mConfig = Config.newInstance(getContext());
mSundayFirst = mConfig.getIsSundayFirst();
setupColors();
mPackageName = getActivity().getPackageName();
mDayTextSize /= mRes.getDisplayMetrics().density;
mTodayTextSize /= mRes.getDisplayMetrics().density;
setupLabels();
return mView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCalendar = new CalendarImpl(this, getContext());
mCalendar.updateCalendar(Formatter.getDateTimeFromCode(mCode));
}
@Override
public void onResume() {
super.onResume();
if (mConfig.getIsSundayFirst() != mSundayFirst) {
mSundayFirst = mConfig.getIsSundayFirst();
setupLabels();
}
}
@Override
public void updateCalendar(String month, List<Day> days) {
mMonthTV.setText(month);
updateDays(days);
}
public void setListener(NavigationListener listener) {
mListener = listener;
}
private void setupColors() {
final int baseColor = mConfig.getIsDarkTheme() ? Color.WHITE : Color.BLACK;
mTextColor = Utils.adjustAlpha(baseColor, Constants.HIGH_ALPHA);
mTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.HIGH_ALPHA);
mWeakTextColor = Utils.adjustAlpha(baseColor, Constants.LOW_ALPHA);
mWeakTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.LOW_ALPHA);
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mLeftArrow.setBackground(null);
mRightArrow.setBackground(null);
}
@OnClick(R.id.top_left_arrow)
public void leftArrowClicked() {
if (mListener != null)
mListener.goLeft();
}
@OnClick(R.id.top_right_arrow)
public void rightArrowClicked() {
if (mListener != null)
mListener.goRight();
}
@OnClick(R.id.top_text)
public void pickMonth() {
final int theme = mConfig.getIsDarkTheme() ? R.style.MyAlertDialog_Dark : R.style.MyAlertDialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(), theme);
final View view = getLayoutInflater(getArguments()).inflate(R.layout.date_picker, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
hideDayPicker(datePicker);
final DateTime dateTime = new DateTime(mCalendar.getTargetDate().toString());
datePicker.init(dateTime.getYear(), dateTime.getMonthOfYear() - 1, 1, null);
alertDialog.setView(view);
alertDialog.setNegativeButton(R.string.cancel, null);
alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final int month = datePicker.getMonth() + 1;
final int year = datePicker.getYear();
if (mListener != null)
mListener.goToDateTime(new DateTime().withMonthOfYear(month).withYear(year));
}
});
alertDialog.show();
}
private void hideDayPicker(DatePicker datePicker) {
final LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
final LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
final NumberPicker picker1 = (NumberPicker) ll2.getChildAt(0);
final NumberPicker picker2 = (NumberPicker) ll2.getChildAt(1);
final NumberPicker dayPicker = (picker1.getMaxValue() > picker2.getMaxValue()) ? picker1 : picker2;
dayPicker.setVisibility(View.GONE);
}
private void setupLabels() {
int letters[] = Utils.getLetterIDs();
for (int i = 0; i < 7; i++) {
final TextView dayTV = (TextView) mView.findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName));
if (dayTV != null) {
dayTV.setTextSize(mDayTextSize);
dayTV.setTextColor(mWeakTextColor);
int index = i;
if (!mSundayFirst)
index = (index + 1) % letters.length;
dayTV.setText(getString(letters[index]));
}
}
}
private void updateDays(List<Day> days) {
final int len = days.size();
for (int i = 0; i < len; i++) {
final Day day = days.get(i);
final TextView dayTV = (TextView) mView.findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName));
if (dayTV == null)
continue;
int curTextColor = day.getHasEvent() ? mWeakTextColorWithEvent : mWeakTextColor;
float curTextSize = mDayTextSize;
if (day.getIsThisMonth()) {
curTextColor = day.getHasEvent() ? mTextColorWithEvent : mTextColor;
}
if (day.getIsToday()) {
curTextSize = mTodayTextSize;
}
dayTV.setText(String.valueOf(day.getValue()));
dayTV.setTextColor(curTextColor);
dayTV.setTextSize(curTextSize);
dayTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDay(day.getCode());
}
});
}
}
private void openDay(String code) {
if (code.isEmpty())
return;
final Intent intent = new Intent(getContext(), DayActivity.class);
intent.putExtra(Constants.DAY_CODE, code);
startActivity(intent);
}
public interface NavigationListener {
void goLeft();
void goRight();
void goToDateTime(DateTime dateTime);
}
}

View File

@ -0,0 +1,194 @@
package com.simplemobiletools.calendar.fragments
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.activities.DayActivity
import com.simplemobiletools.calendar.models.Day
import kotlinx.android.synthetic.main.calendar_layout.view.*
import kotlinx.android.synthetic.main.top_navigation.view.*
import org.joda.time.DateTime
class MonthFragment : Fragment(), Calendar {
private var mDayTextSize: Float = 0f
private var mTodayTextSize: Float = 0f
private var mPackageName: String = ""
private var mTextColor: Int = 0
private var mWeakTextColor: Int = 0
private var mTextColorWithEvent: Int = 0
private var mWeakTextColorWithEvent: Int = 0
private var mSundayFirst: Boolean = false
private var mCode: String = ""
private var mCalendar: CalendarImpl? = null
private var mListener: 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.calendar_layout, container, false)
mRes = resources
mHolder = view.calendar_holder
mCode = arguments.getString(Constants.DAY_CODE)
mConfig = Config.newInstance(context)
mSundayFirst = mConfig.isSundayFirst
setupColors()
mPackageName = activity.packageName
mDayTextSize = mRes.getDimension(R.dimen.day_text_size) / mRes.displayMetrics.density
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size) / mRes.displayMetrics.density
setupLabels()
return view
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mCalendar = CalendarImpl(this, context)
mCalendar!!.updateCalendar(Formatter.getDateTimeFromCode(mCode))
}
override fun onResume() {
super.onResume()
if (mConfig.isSundayFirst != mSundayFirst) {
mSundayFirst = mConfig.isSundayFirst
setupLabels()
}
}
override fun updateCalendar(month: String, days: List<Day>) {
mHolder.month_value.text = month
updateDays(days)
}
fun setListener(listener: NavigationListener) {
mListener = listener
}
private fun setupColors() {
val baseColor = if (mConfig.isDarkTheme) Color.WHITE else Color.BLACK
mTextColor = Utils.adjustAlpha(baseColor, Constants.HIGH_ALPHA)
mTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.HIGH_ALPHA)
mWeakTextColor = Utils.adjustAlpha(baseColor, Constants.LOW_ALPHA)
mWeakTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.LOW_ALPHA)
mHolder.top_left_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
mHolder.top_right_arrow.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP)
mHolder.top_left_arrow.background = null
mHolder.top_right_arrow.background = null
mHolder.top_left_arrow.setOnClickListener {
if (mListener != null)
mListener!!.goLeft()
}
mHolder.top_right_arrow.setOnClickListener {
if (mListener != null)
mListener!!.goRight()
}
mHolder.month_value.setOnClickListener { showMonthDialog() }
}
fun showMonthDialog() {
val theme = if (mConfig.isDarkTheme) R.style.MyAlertDialog_Dark else R.style.MyAlertDialog
val alertDialog = AlertDialog.Builder(context, theme)
val view = getLayoutInflater(arguments).inflate(R.layout.date_picker, null)
val datePicker = view.findViewById(R.id.date_picker) as DatePicker
hideDayPicker(datePicker)
val dateTime = DateTime(mCalendar!!.targetDate.toString())
datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null)
alertDialog.setView(view)
alertDialog.setNegativeButton(R.string.cancel, null)
alertDialog.setPositiveButton(R.string.ok) { dialog, id ->
val month = datePicker.month + 1
val year = datePicker.year
if (mListener != null)
mListener!!.goToDateTime(DateTime().withMonthOfYear(month).withYear(year))
}
alertDialog.show()
}
private fun hideDayPicker(datePicker: DatePicker) {
val ll = datePicker.getChildAt(0) as LinearLayout
val ll2 = ll.getChildAt(0) as LinearLayout
val picker1 = ll2.getChildAt(0) as NumberPicker
val picker2 = ll2.getChildAt(1) as NumberPicker
val dayPicker = if (picker1.maxValue > picker2.maxValue) picker1 else picker2
dayPicker.visibility = View.GONE
}
private fun setupLabels() {
val letters = Utils.getLetterIDs()
for (i in 0..6) {
val dayTV = mHolder.findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName)) as TextView
dayTV.textSize = mDayTextSize
dayTV.setTextColor(mWeakTextColor)
var index = i
if (!mSundayFirst)
index = (index + 1) % letters.size
dayTV.text = getString(letters[index])
}
}
private fun updateDays(days: List<Day>) {
val len = days.size
for (i in 0..len - 1) {
val day = days[i]
val dayTV = mHolder.findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName)) as TextView
var curTextColor = if (day.hasEvent) mWeakTextColorWithEvent else mWeakTextColor
var curTextSize = mDayTextSize
if (day.isThisMonth) {
curTextColor = if (day.hasEvent) mTextColorWithEvent else mTextColor
}
if (day.isToday) {
curTextSize = mTodayTextSize
}
dayTV.text = day.value.toString()
dayTV.setTextColor(curTextColor)
dayTV.textSize = curTextSize
dayTV.setOnClickListener { openDay(day.code) }
}
}
private fun openDay(code: String) {
if (code.isEmpty())
return
val intent = Intent(context, DayActivity::class.java)
intent.putExtra(Constants.DAY_CODE, code)
startActivity(intent)
}
interface NavigationListener {
fun goLeft()
fun goRight()
fun goToDateTime(dateTime: DateTime)
}
}

View File

@ -18,7 +18,7 @@
android:id="@+id/day_events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/top_text"
android:layout_below="@+id/month_value"
android:choiceMode="multipleChoiceModal"
android:clipToPadding="false"
android:paddingLeft="@dimen/activity_margin"/>

View File

@ -12,7 +12,7 @@
android:id="@+id/table_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/top_text"
android:layout_below="@+id/month_value"
android:gravity="center"
android:orientation="vertical">

View File

@ -8,13 +8,13 @@
style="@style/ArrowStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/top_text"
android:layout_alignTop="@+id/top_text"
android:layout_alignBottom="@+id/month_value"
android:layout_alignTop="@+id/month_value"
android:padding="@dimen/activity_margin"
android:src="@mipmap/arrow_left"/>
<TextView
android:id="@+id/top_text"
android:id="@+id/month_value"
style="@style/MonthStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -31,9 +31,9 @@
style="@style/ArrowStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/top_text"
android:layout_alignBottom="@+id/month_value"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/top_text"
android:layout_alignTop="@+id/month_value"
android:padding="@dimen/activity_margin"
android:src="@mipmap/arrow_right"/>