change the day labels color if it contains an event
This commit is contained in:
parent
5ecd7a11ee
commit
84d6010ffe
|
@ -1,31 +1,37 @@
|
|||
package com.simplemobiletools.calendar;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.simplemobiletools.calendar.models.Day;
|
||||
import com.simplemobiletools.calendar.models.Event;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CalendarImpl {
|
||||
public class CalendarImpl implements DBHelper.DBOperationsListener {
|
||||
public static final int DAYS_CNT = 42;
|
||||
private static final String YEAR_PATTERN = "YYYY";
|
||||
|
||||
private final Calendar mCallback;
|
||||
private final String mToday;
|
||||
private final Context mContext;
|
||||
private DateTime mTargetDate;
|
||||
private List<Event> mEvents;
|
||||
|
||||
public CalendarImpl(Calendar callback) {
|
||||
public CalendarImpl(Calendar callback, Context context) {
|
||||
this.mCallback = callback;
|
||||
mToday = new DateTime().toString(Constants.DATE_PATTERN);
|
||||
mContext = context;
|
||||
mToday = new DateTime().toString(Formatter.DAYCODE_PATTERN);
|
||||
}
|
||||
|
||||
public void updateCalendar(DateTime targetDate) {
|
||||
this.mTargetDate = targetDate;
|
||||
getMonthName();
|
||||
getDays(targetDate);
|
||||
mTargetDate = targetDate;
|
||||
final int startTS = Formatter.getDayStartTS(Formatter.getDayCodeFromDateTime(mTargetDate.minusMonths(1)));
|
||||
final int endTS = Formatter.getDayEndTS(Formatter.getDayCodeFromDateTime(mTargetDate.plusMonths(1)));
|
||||
DBHelper.newInstance(mContext, this).getEvents(startTS, endTS);
|
||||
}
|
||||
|
||||
public void getPrevMonth() {
|
||||
|
@ -36,35 +42,35 @@ public class CalendarImpl {
|
|||
updateCalendar(mTargetDate.plusMonths(1));
|
||||
}
|
||||
|
||||
private void getDays(DateTime targetDate) {
|
||||
private void getDays() {
|
||||
final List<Day> days = new ArrayList<>(DAYS_CNT);
|
||||
|
||||
final int currMonthDays = targetDate.dayOfMonth().getMaximumValue();
|
||||
final int firstDayIndex = targetDate.withDayOfMonth(1).getDayOfWeek() - 1;
|
||||
final int prevMonthDays = targetDate.minusMonths(1).dayOfMonth().getMaximumValue();
|
||||
final int currMonthDays = mTargetDate.dayOfMonth().getMaximumValue();
|
||||
final int firstDayIndex = mTargetDate.withDayOfMonth(1).getDayOfWeek() - 1;
|
||||
final int prevMonthDays = mTargetDate.minusMonths(1).dayOfMonth().getMaximumValue();
|
||||
|
||||
boolean isThisMonth = false;
|
||||
boolean isToday;
|
||||
int value = prevMonthDays - firstDayIndex + 1;
|
||||
DateTime curDay = targetDate;
|
||||
DateTime curDay = mTargetDate;
|
||||
|
||||
for (int i = 0; i < DAYS_CNT; i++) {
|
||||
if (i < firstDayIndex) {
|
||||
isThisMonth = false;
|
||||
curDay = targetDate.minusMonths(1);
|
||||
curDay = mTargetDate.minusMonths(1);
|
||||
} else if (i == firstDayIndex) {
|
||||
value = 1;
|
||||
isThisMonth = true;
|
||||
curDay = targetDate;
|
||||
curDay = mTargetDate;
|
||||
} else if (value == currMonthDays + 1) {
|
||||
value = 1;
|
||||
isThisMonth = false;
|
||||
curDay = targetDate.plusMonths(1);
|
||||
curDay = mTargetDate.plusMonths(1);
|
||||
}
|
||||
|
||||
isToday = isThisMonth && isToday(targetDate, value);
|
||||
isToday = isThisMonth && isToday(mTargetDate, value);
|
||||
|
||||
final String dayCode = curDay.withDayOfMonth(value).toDateTime(DateTimeZone.UTC).toString(Constants.DATE_PATTERN);
|
||||
final String dayCode = Formatter.getDayCodeFromDateTime(curDay.withDayOfMonth(value));
|
||||
final Day day = new Day(value, isThisMonth, isToday, dayCode, hasEvent(dayCode));
|
||||
days.add(day);
|
||||
value++;
|
||||
|
@ -74,11 +80,16 @@ public class CalendarImpl {
|
|||
}
|
||||
|
||||
private boolean hasEvent(String dayCode) {
|
||||
for (Event e : mEvents) {
|
||||
if (Formatter.getDayCodeFromTS(e.getStartTS()).equals(dayCode)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isToday(DateTime targetDate, int curDayInMonth) {
|
||||
return targetDate.withDayOfMonth(curDayInMonth).toString(Constants.DATE_PATTERN).equals(mToday);
|
||||
return targetDate.withDayOfMonth(curDayInMonth).toString(Formatter.DAYCODE_PATTERN).equals(mToday);
|
||||
}
|
||||
|
||||
private String getMonthName() {
|
||||
|
@ -94,4 +105,15 @@ public class CalendarImpl {
|
|||
public DateTime getTargetDate() {
|
||||
return mTargetDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventInserted() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gotEvents(List<Event> events) {
|
||||
mEvents = events;
|
||||
getDays();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,6 @@ public class Constants {
|
|||
public static final String IS_FIRST_RUN = "is_first_run";
|
||||
public static final String WIDGET_BG_COLOR = "widget_bg_color";
|
||||
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
|
||||
public static final String EVENTS = "events";
|
||||
|
||||
public static final String DAY_CODE = "day_code";
|
||||
public static final String DATE_PATTERN = "YYMMdd";
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.joda.time.format.DateTimeFormat;
|
|||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
public class Formatter {
|
||||
public static final String DAYCODE_PATTERN = "YYMMdd";
|
||||
private static final String EVENT_DATE_PATTERN = "MMMM d YYYY";
|
||||
private static final String EVENT_TIME_PATTERN = "HH:mm";
|
||||
|
||||
|
@ -22,7 +23,7 @@ public class Formatter {
|
|||
}
|
||||
|
||||
public static DateTime getDateTimeFromCode(String dayCode) {
|
||||
final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(Constants.DATE_PATTERN).withZone(DateTimeZone.UTC);
|
||||
final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.UTC);
|
||||
return dateTimeFormatter.parseDateTime(dayCode);
|
||||
}
|
||||
|
||||
|
@ -40,4 +41,13 @@ public class Formatter {
|
|||
final DateTime dateTime = getDateTimeFromCode(dayCode);
|
||||
return (int) (dateTime.plusDays(1).minusMinutes(1).getMillis() / 1000);
|
||||
}
|
||||
|
||||
public static String getDayCodeFromTS(int ts) {
|
||||
final DateTime dateTime = new DateTime(ts * 1000L, DateTimeZone.getDefault());
|
||||
return dateTime.toString(Formatter.DAYCODE_PATTERN);
|
||||
}
|
||||
|
||||
public static String getDayCodeFromDateTime(DateTime dateTime) {
|
||||
return dateTime.toDateTime(DateTimeZone.getDefault()).toString(Formatter.DAYCODE_PATTERN);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
|
|||
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
|
||||
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
||||
|
||||
mCalendar = new CalendarImpl(this);
|
||||
mCalendar = new CalendarImpl(this, mContext);
|
||||
mCalendar.updateCalendar(new DateTime());
|
||||
}
|
||||
|
||||
|
|
|
@ -51,8 +51,8 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||
|
||||
private int mTextColor;
|
||||
private int mWeakTextColor;
|
||||
private int mTextColorWithNote;
|
||||
private int mWeakTextColorWithNote;
|
||||
private int mTextColorWithEvent;
|
||||
private int mWeakTextColorWithEvent;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -63,9 +63,9 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||
mRes = getResources();
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
mTextColor = Utils.adjustAlpha(Color.BLACK, Constants.HIGH_ALPHA);
|
||||
mTextColorWithNote = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.HIGH_ALPHA);
|
||||
mTextColorWithEvent = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), Constants.HIGH_ALPHA);
|
||||
mWeakTextColor = Utils.adjustAlpha(Color.BLACK, Constants.LOW_ALPHA);
|
||||
mWeakTextColorWithNote = Utils.adjustAlpha(mRes.getColor(R.color.colorPrimary), 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);
|
||||
|
||||
|
@ -77,7 +77,12 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams();
|
||||
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
|
||||
|
||||
mCalendar = new CalendarImpl(this);
|
||||
mCalendar = new CalendarImpl(this, getApplicationContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mCalendar.updateCalendar(new DateTime());
|
||||
}
|
||||
|
||||
|
@ -114,11 +119,11 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||
if (dayTV == null)
|
||||
continue;
|
||||
|
||||
int curTextColor = mWeakTextColor;
|
||||
int curTextColor = day.getHasEvent() ? mWeakTextColorWithEvent : mWeakTextColor;
|
||||
float curTextSize = mDayTextSize;
|
||||
|
||||
if (day.getIsThisMonth()) {
|
||||
curTextColor = mTextColor;
|
||||
curTextColor = day.getHasEvent() ? mTextColorWithEvent : mTextColor;
|
||||
}
|
||||
|
||||
if (day.getIsToday()) {
|
||||
|
|
|
@ -99,7 +99,7 @@ public class WidgetConfigureActivity extends AppCompatActivity implements Calend
|
|||
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
||||
updateBgColor();
|
||||
|
||||
mCalendar = new CalendarImpl(this);
|
||||
mCalendar = new CalendarImpl(this, getApplicationContext());
|
||||
mCalendar.updateCalendar(new DateTime());
|
||||
}
|
||||
|
||||
|
|
|
@ -4,15 +4,15 @@ public class Day {
|
|||
private final int mValue;
|
||||
private final boolean mIsThisMonth;
|
||||
private final boolean mIsToday;
|
||||
private final boolean mHasNote;
|
||||
private final boolean mHasEvent;
|
||||
private final String mCode;
|
||||
|
||||
public Day(int value, boolean isThisMonth, boolean isToday, String code, boolean hasNote) {
|
||||
public Day(int value, boolean isThisMonth, boolean isToday, String code, boolean hasEvent) {
|
||||
mValue = value;
|
||||
mIsThisMonth = isThisMonth;
|
||||
mIsToday = isToday;
|
||||
mCode = code;
|
||||
mHasNote = hasNote;
|
||||
mHasEvent = hasEvent;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
|
@ -31,7 +31,7 @@ public class Day {
|
|||
return mCode;
|
||||
}
|
||||
|
||||
public boolean getHasNote() {
|
||||
return mHasNote;
|
||||
public boolean getHasEvent() {
|
||||
return mHasEvent;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue