mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
change the day labels color if it contains an event
This commit is contained in:
@@ -1,31 +1,37 @@
|
|||||||
package com.simplemobiletools.calendar;
|
package com.simplemobiletools.calendar;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
import com.simplemobiletools.calendar.models.Day;
|
import com.simplemobiletools.calendar.models.Day;
|
||||||
|
import com.simplemobiletools.calendar.models.Event;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.DateTimeZone;
|
|
||||||
|
|
||||||
import java.text.DateFormatSymbols;
|
import java.text.DateFormatSymbols;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CalendarImpl {
|
public class CalendarImpl implements DBHelper.DBOperationsListener {
|
||||||
public static final int DAYS_CNT = 42;
|
public static final int DAYS_CNT = 42;
|
||||||
private static final String YEAR_PATTERN = "YYYY";
|
private static final String YEAR_PATTERN = "YYYY";
|
||||||
|
|
||||||
private final Calendar mCallback;
|
private final Calendar mCallback;
|
||||||
private final String mToday;
|
private final String mToday;
|
||||||
|
private final Context mContext;
|
||||||
private DateTime mTargetDate;
|
private DateTime mTargetDate;
|
||||||
|
private List<Event> mEvents;
|
||||||
|
|
||||||
public CalendarImpl(Calendar callback) {
|
public CalendarImpl(Calendar callback, Context context) {
|
||||||
this.mCallback = callback;
|
this.mCallback = callback;
|
||||||
mToday = new DateTime().toString(Constants.DATE_PATTERN);
|
mContext = context;
|
||||||
|
mToday = new DateTime().toString(Formatter.DAYCODE_PATTERN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCalendar(DateTime targetDate) {
|
public void updateCalendar(DateTime targetDate) {
|
||||||
this.mTargetDate = targetDate;
|
mTargetDate = targetDate;
|
||||||
getMonthName();
|
final int startTS = Formatter.getDayStartTS(Formatter.getDayCodeFromDateTime(mTargetDate.minusMonths(1)));
|
||||||
getDays(targetDate);
|
final int endTS = Formatter.getDayEndTS(Formatter.getDayCodeFromDateTime(mTargetDate.plusMonths(1)));
|
||||||
|
DBHelper.newInstance(mContext, this).getEvents(startTS, endTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getPrevMonth() {
|
public void getPrevMonth() {
|
||||||
@@ -36,35 +42,35 @@ public class CalendarImpl {
|
|||||||
updateCalendar(mTargetDate.plusMonths(1));
|
updateCalendar(mTargetDate.plusMonths(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getDays(DateTime targetDate) {
|
private void getDays() {
|
||||||
final List<Day> days = new ArrayList<>(DAYS_CNT);
|
final List<Day> days = new ArrayList<>(DAYS_CNT);
|
||||||
|
|
||||||
final int currMonthDays = targetDate.dayOfMonth().getMaximumValue();
|
final int currMonthDays = mTargetDate.dayOfMonth().getMaximumValue();
|
||||||
final int firstDayIndex = targetDate.withDayOfMonth(1).getDayOfWeek() - 1;
|
final int firstDayIndex = mTargetDate.withDayOfMonth(1).getDayOfWeek() - 1;
|
||||||
final int prevMonthDays = targetDate.minusMonths(1).dayOfMonth().getMaximumValue();
|
final int prevMonthDays = mTargetDate.minusMonths(1).dayOfMonth().getMaximumValue();
|
||||||
|
|
||||||
boolean isThisMonth = false;
|
boolean isThisMonth = false;
|
||||||
boolean isToday;
|
boolean isToday;
|
||||||
int value = prevMonthDays - firstDayIndex + 1;
|
int value = prevMonthDays - firstDayIndex + 1;
|
||||||
DateTime curDay = targetDate;
|
DateTime curDay = mTargetDate;
|
||||||
|
|
||||||
for (int i = 0; i < DAYS_CNT; i++) {
|
for (int i = 0; i < DAYS_CNT; i++) {
|
||||||
if (i < firstDayIndex) {
|
if (i < firstDayIndex) {
|
||||||
isThisMonth = false;
|
isThisMonth = false;
|
||||||
curDay = targetDate.minusMonths(1);
|
curDay = mTargetDate.minusMonths(1);
|
||||||
} else if (i == firstDayIndex) {
|
} else if (i == firstDayIndex) {
|
||||||
value = 1;
|
value = 1;
|
||||||
isThisMonth = true;
|
isThisMonth = true;
|
||||||
curDay = targetDate;
|
curDay = mTargetDate;
|
||||||
} else if (value == currMonthDays + 1) {
|
} else if (value == currMonthDays + 1) {
|
||||||
value = 1;
|
value = 1;
|
||||||
isThisMonth = false;
|
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));
|
final Day day = new Day(value, isThisMonth, isToday, dayCode, hasEvent(dayCode));
|
||||||
days.add(day);
|
days.add(day);
|
||||||
value++;
|
value++;
|
||||||
@@ -74,11 +80,16 @@ public class CalendarImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasEvent(String dayCode) {
|
private boolean hasEvent(String dayCode) {
|
||||||
|
for (Event e : mEvents) {
|
||||||
|
if (Formatter.getDayCodeFromTS(e.getStartTS()).equals(dayCode)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isToday(DateTime targetDate, int curDayInMonth) {
|
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() {
|
private String getMonthName() {
|
||||||
@@ -94,4 +105,15 @@ public class CalendarImpl {
|
|||||||
public DateTime getTargetDate() {
|
public DateTime getTargetDate() {
|
||||||
return mTargetDate;
|
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 IS_FIRST_RUN = "is_first_run";
|
||||||
public static final String WIDGET_BG_COLOR = "widget_bg_color";
|
public static final String WIDGET_BG_COLOR = "widget_bg_color";
|
||||||
public static final String WIDGET_TEXT_COLOR = "widget_text_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 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;
|
import org.joda.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
public class Formatter {
|
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_DATE_PATTERN = "MMMM d YYYY";
|
||||||
private static final String EVENT_TIME_PATTERN = "HH:mm";
|
private static final String EVENT_TIME_PATTERN = "HH:mm";
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ public class Formatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DateTime getDateTimeFromCode(String dayCode) {
|
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);
|
return dateTimeFormatter.parseDateTime(dayCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,4 +41,13 @@ public class Formatter {
|
|||||||
final DateTime dateTime = getDateTimeFromCode(dayCode);
|
final DateTime dateTime = getDateTimeFromCode(dayCode);
|
||||||
return (int) (dateTime.plusDays(1).minusMinutes(1).getMillis() / 1000);
|
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);
|
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
|
||||||
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
||||||
|
|
||||||
mCalendar = new CalendarImpl(this);
|
mCalendar = new CalendarImpl(this, mContext);
|
||||||
mCalendar.updateCalendar(new DateTime());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -51,8 +51,8 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||||||
|
|
||||||
private int mTextColor;
|
private int mTextColor;
|
||||||
private int mWeakTextColor;
|
private int mWeakTextColor;
|
||||||
private int mTextColorWithNote;
|
private int mTextColorWithEvent;
|
||||||
private int mWeakTextColorWithNote;
|
private int mWeakTextColorWithEvent;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -63,9 +63,9 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||||||
mRes = getResources();
|
mRes = getResources();
|
||||||
Locale.setDefault(Locale.ENGLISH);
|
Locale.setDefault(Locale.ENGLISH);
|
||||||
mTextColor = Utils.adjustAlpha(Color.BLACK, Constants.HIGH_ALPHA);
|
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);
|
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);
|
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
|
||||||
mRightArrow.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();
|
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams();
|
||||||
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
|
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());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,11 +119,11 @@ public class MainActivity extends AppCompatActivity implements Calendar {
|
|||||||
if (dayTV == null)
|
if (dayTV == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int curTextColor = mWeakTextColor;
|
int curTextColor = day.getHasEvent() ? mWeakTextColorWithEvent : mWeakTextColor;
|
||||||
float curTextSize = mDayTextSize;
|
float curTextSize = mDayTextSize;
|
||||||
|
|
||||||
if (day.getIsThisMonth()) {
|
if (day.getIsThisMonth()) {
|
||||||
curTextColor = mTextColor;
|
curTextColor = day.getHasEvent() ? mTextColorWithEvent : mTextColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (day.getIsToday()) {
|
if (day.getIsToday()) {
|
||||||
|
@@ -99,7 +99,7 @@ public class WidgetConfigureActivity extends AppCompatActivity implements Calend
|
|||||||
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
||||||
updateBgColor();
|
updateBgColor();
|
||||||
|
|
||||||
mCalendar = new CalendarImpl(this);
|
mCalendar = new CalendarImpl(this, getApplicationContext());
|
||||||
mCalendar.updateCalendar(new DateTime());
|
mCalendar.updateCalendar(new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,15 +4,15 @@ public class Day {
|
|||||||
private final int mValue;
|
private final int mValue;
|
||||||
private final boolean mIsThisMonth;
|
private final boolean mIsThisMonth;
|
||||||
private final boolean mIsToday;
|
private final boolean mIsToday;
|
||||||
private final boolean mHasNote;
|
private final boolean mHasEvent;
|
||||||
private final String mCode;
|
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;
|
mValue = value;
|
||||||
mIsThisMonth = isThisMonth;
|
mIsThisMonth = isThisMonth;
|
||||||
mIsToday = isToday;
|
mIsToday = isToday;
|
||||||
mCode = code;
|
mCode = code;
|
||||||
mHasNote = hasNote;
|
mHasEvent = hasEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getValue() {
|
public int getValue() {
|
||||||
@@ -31,7 +31,7 @@ public class Day {
|
|||||||
return mCode;
|
return mCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getHasNote() {
|
public boolean getHasEvent() {
|
||||||
return mHasNote;
|
return mHasEvent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user