fill the widget with days

This commit is contained in:
tibbi 2016-01-31 11:36:14 +01:00
parent 975c625fb2
commit 1216682c3e
4 changed files with 51 additions and 18 deletions

View File

@ -3,7 +3,5 @@ package calendar.simplemobiletools.com;
import java.util.List;
public interface Calendar {
void updateDays(List<Day> days);
void updateMonth(String month);
void updateCalendar(String month, List<Day> days);
}

View File

@ -22,8 +22,8 @@ public class CalendarImpl {
public void updateCalendar(DateTime targetDate) {
this.targetDate = targetDate;
getDays(targetDate);
getMonthName();
getDays(targetDate);
}
public void getPrevMonth() {
@ -63,22 +63,21 @@ public class CalendarImpl {
value++;
}
callback.updateDays(days);
callback.updateCalendar(getMonthName(), days);
}
private boolean isToday(DateTime targetDate, int curDayInMonth) {
return targetDate.withDayOfMonth(curDayInMonth).toString(DATE_PATTERN).equals(today);
}
private void getMonthName() {
private String getMonthName() {
final String[] months = new DateFormatSymbols().getMonths();
String month = (months[targetDate.getMonthOfYear() - 1]);
final String targetYear = targetDate.toString(YEAR_PATTERN);
if (!targetYear.equals(new DateTime().toString(YEAR_PATTERN))) {
month += " " + targetYear;
}
callback.updateMonth(month);
return month;
}
public DateTime getTargetDate() {

View File

@ -43,8 +43,8 @@ public class MainActivity extends AppCompatActivity implements MyDatePickerDialo
res = getResources();
packageName = getPackageName();
dayTextSize /= getResources().getDisplayMetrics().density;
todayTextSize /= getResources().getDisplayMetrics().density;
dayTextSize /= res.getDisplayMetrics().density;
todayTextSize /= res.getDisplayMetrics().density;
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
@ -52,7 +52,7 @@ public class MainActivity extends AppCompatActivity implements MyDatePickerDialo
setupLabelSizes();
}
private void fillCalendar(List<Day> days) {
private void updateDays(List<Day> days) {
final int len = days.size();
for (int i = 0; i < len; i++) {
@ -99,12 +99,12 @@ public class MainActivity extends AppCompatActivity implements MyDatePickerDialo
}
@Override
public void updateDays(List<Day> days) {
fillCalendar(days);
public void updateCalendar(String month, List<Day> days) {
updateMonth(month);
updateDays(days);
}
@Override
public void updateMonth(String month) {
private void updateMonth(String month) {
monthTV.setText(month);
}

View File

@ -6,6 +6,7 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.widget.RemoteViews;
@ -22,6 +23,11 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private static Intent intent;
private static Context cxt;
private static CalendarImpl calendar;
private static Resources res;
private static int lightGrey;
private static int darkGrey;
private static float dayTextSize;
private static float todayTextSize;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
@ -30,6 +36,13 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private void initVariables(Context context) {
cxt = context;
res = cxt.getResources();
lightGrey = res.getColor(R.color.lightGrey);
darkGrey = res.getColor(R.color.darkGrey);
dayTextSize = res.getDimension(R.dimen.day_text_size) / res.getDisplayMetrics().density;
todayTextSize = res.getDimension(R.dimen.today_text_size) / res.getDisplayMetrics().density;
final ComponentName component = new ComponentName(cxt, MyWidgetProvider.class);
widgetManager = AppWidgetManager.getInstance(cxt);
widgetIds = widgetManager.getAppWidgetIds(component);
@ -75,14 +88,37 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
}
@Override
public void updateDays(List<Day> days) {
final String packageName = cxt.getPackageName();
final int len = days.size();
for (int i = 0; i < len; i++) {
final Day day = days.get(i);
final int id = res.getIdentifier("day_" + i, "id", packageName);
int textColor = lightGrey;
float textSize = dayTextSize;
if (day.getIsThisMonth()) {
textColor = darkGrey;
}
if (day.getIsToday()) {
textSize = todayTextSize;
}
remoteViews.setTextViewText(id, String.valueOf(day.getValue()));
remoteViews.setInt(id, "setTextColor", textColor);
remoteViews.setFloat(id, "setTextSize", textSize);
}
}
public void updateMonth(String month) {
remoteViews.setTextViewText(R.id.table_month, month);
}
@Override
public void updateMonth(String month) {
remoteViews.setTextViewText(R.id.table_month, month);
public void updateCalendar(String month, List<Day> days) {
updateMonth(month);
updateDays(days);
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
}