move the logic in an interface
This commit is contained in:
parent
c4e0212c1c
commit
90f1122eac
|
@ -0,0 +1,9 @@
|
|||
package calendar.simplemobiletools.com;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Calendar {
|
||||
void updateDays(List<Day> days);
|
||||
|
||||
void updateMonth(String month);
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package calendar.simplemobiletools.com;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CalendarImpl {
|
||||
public static final int DAYS_CNT = 42;
|
||||
private static final String DATE_PATTERN = "ddMMYYYY";
|
||||
private static final String YEAR_PATTERN = "YYYY";
|
||||
|
||||
private final Calendar callback;
|
||||
private final String today;
|
||||
private DateTime targetDate;
|
||||
|
||||
public CalendarImpl(Calendar callback, DateTime targetDate) {
|
||||
this.callback = callback;
|
||||
this.targetDate = targetDate;
|
||||
today = new DateTime().toString(DATE_PATTERN);
|
||||
}
|
||||
|
||||
public void updateCalendar(DateTime targetDateTime) {
|
||||
this.targetDate = targetDateTime;
|
||||
getDays(targetDateTime);
|
||||
getMonthName();
|
||||
}
|
||||
|
||||
public void getPrevMonth() {
|
||||
updateCalendar(targetDate.minusMonths(1));
|
||||
}
|
||||
|
||||
public void getNextMonth() {
|
||||
updateCalendar(targetDate.plusMonths(1));
|
||||
}
|
||||
|
||||
private void getDays(DateTime targetDate) {
|
||||
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();
|
||||
|
||||
boolean isThisMonth = false;
|
||||
boolean isToday;
|
||||
int value = prevMonthDays - firstDayIndex + 1;
|
||||
|
||||
for (int i = 0; i < DAYS_CNT; i++) {
|
||||
if (i < firstDayIndex) {
|
||||
isThisMonth = false;
|
||||
} else if (i == firstDayIndex) {
|
||||
value = 1;
|
||||
isThisMonth = true;
|
||||
} else if (value == currMonthDays + 1) {
|
||||
value = 1;
|
||||
isThisMonth = false;
|
||||
}
|
||||
|
||||
isToday = isThisMonth && isToday(targetDate, value);
|
||||
|
||||
final Day day = new Day(value, isThisMonth, isToday);
|
||||
days.add(day);
|
||||
value++;
|
||||
}
|
||||
|
||||
callback.updateDays(days);
|
||||
}
|
||||
|
||||
private boolean isToday(DateTime targetDate, int curDayInMonth) {
|
||||
return targetDate.withDayOfMonth(curDayInMonth).toString(DATE_PATTERN).equals(today);
|
||||
}
|
||||
|
||||
private void 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);
|
||||
}
|
||||
|
||||
public DateTime getTargetDate() {
|
||||
return targetDate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package calendar.simplemobiletools.com;
|
||||
|
||||
public class Day {
|
||||
private final int value;
|
||||
private final boolean isThisMonth;
|
||||
private final boolean isToday;
|
||||
|
||||
public Day(int value, boolean isThisMonth, boolean isToday) {
|
||||
this.value = value;
|
||||
this.isThisMonth = isThisMonth;
|
||||
this.isToday = isToday;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean getIsThisMonth() {
|
||||
return isThisMonth;
|
||||
}
|
||||
|
||||
public boolean getIsToday() {
|
||||
return isToday;
|
||||
}
|
||||
}
|
|
@ -1,15 +1,15 @@
|
|||
package calendar.simplemobiletools.com;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.BindColor;
|
||||
|
@ -17,22 +17,20 @@ import butterknife.BindDimen;
|
|||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements MyDatePickerDialog.DatePickedListener {
|
||||
public class MainActivity extends AppCompatActivity implements MyDatePickerDialog.DatePickedListener, Calendar {
|
||||
public static final String DATE = "date";
|
||||
private static final String DATE_PATTERN = "ddMMYYYY";
|
||||
private static final String YEAR_PATTERN = "YYYY";
|
||||
|
||||
@Bind(R.id.left_arrow) ImageView leftArrow;
|
||||
@Bind(R.id.right_arrow) ImageView rightArrow;
|
||||
@Bind(R.id.table_month) TextView monthTV;
|
||||
@Bind(R.id.table_holder) LinearLayout tableHolder;
|
||||
@BindColor(R.color.darkGrey) int darkGrey;
|
||||
@BindColor(R.color.lightGrey) int lightGrey;
|
||||
@BindDimen(R.dimen.day_text_size) float dayTextSize;
|
||||
@BindDimen(R.dimen.today_text_size) float todayTextSize;
|
||||
|
||||
private DateTime targetDate;
|
||||
private String today;
|
||||
private CalendarImpl calendar;
|
||||
private Resources res;
|
||||
private String packageName;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -42,92 +40,79 @@ public class MainActivity extends AppCompatActivity implements MyDatePickerDialo
|
|||
|
||||
leftArrow.getDrawable().mutate().setColorFilter(darkGrey, PorterDuff.Mode.SRC_ATOP);
|
||||
rightArrow.getDrawable().mutate().setColorFilter(darkGrey, PorterDuff.Mode.SRC_ATOP);
|
||||
targetDate = new DateTime();
|
||||
today = targetDate.toString(DATE_PATTERN);
|
||||
|
||||
res = getResources();
|
||||
packageName = getPackageName();
|
||||
dayTextSize /= getResources().getDisplayMetrics().density;
|
||||
todayTextSize /= getResources().getDisplayMetrics().density;
|
||||
|
||||
fillCalendar();
|
||||
DateTime now = new DateTime();
|
||||
calendar = new CalendarImpl(this, now);
|
||||
calendar.updateCalendar(now);
|
||||
|
||||
setupLabelSizes();
|
||||
}
|
||||
|
||||
private void fillCalendar() {
|
||||
monthTV.setText(getMonthName());
|
||||
final int currMonthDays = targetDate.dayOfMonth().getMaximumValue();
|
||||
final int firstDayIndex = targetDate.withDayOfMonth(1).getDayOfWeek() - 1;
|
||||
final int prevMonthDays = targetDate.minusMonths(1).dayOfMonth().getMaximumValue();
|
||||
final int prevMonthStart = prevMonthDays - firstDayIndex + 1;
|
||||
private void fillCalendar(List<Day> days) {
|
||||
final int len = days.size();
|
||||
|
||||
int cur = 0;
|
||||
int thisMonthDays = 1;
|
||||
int nextMonthsDay = 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
final Day day = days.get(i);
|
||||
final TextView dayTV = (TextView) findViewById(res.getIdentifier("day_" + i, "id", packageName));
|
||||
dayTV.setText(String.valueOf(day.getValue()));
|
||||
|
||||
final int rowsCnt = tableHolder.getChildCount();
|
||||
for (int i = 1; i < rowsCnt; i++) {
|
||||
final LinearLayout row = (LinearLayout) tableHolder.getChildAt(i);
|
||||
final int daysCnt = row.getChildCount();
|
||||
for (int j = 0; j < daysCnt; j++) {
|
||||
final TextView day = (TextView) row.getChildAt(j);
|
||||
day.setTextSize(dayTextSize);
|
||||
|
||||
int currDate = thisMonthDays;
|
||||
if (cur < firstDayIndex) {
|
||||
currDate = prevMonthStart + cur;
|
||||
day.setTextColor(lightGrey);
|
||||
} else if (currDate <= currMonthDays) {
|
||||
if (targetDate.withDayOfMonth(thisMonthDays).toString(DATE_PATTERN).equals(today)) {
|
||||
day.setTextSize(todayTextSize);
|
||||
}
|
||||
|
||||
thisMonthDays++;
|
||||
day.setTextColor(darkGrey);
|
||||
if (day.getIsThisMonth()) {
|
||||
dayTV.setTextColor(darkGrey);
|
||||
} else {
|
||||
currDate = nextMonthsDay++;
|
||||
day.setTextColor(lightGrey);
|
||||
dayTV.setTextColor(lightGrey);
|
||||
}
|
||||
|
||||
day.setText(String.valueOf(currDate));
|
||||
cur++;
|
||||
if (day.getIsToday()) {
|
||||
dayTV.setTextSize(todayTextSize);
|
||||
} else {
|
||||
dayTV.setTextSize(dayTextSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OnClick(R.id.left_arrow)
|
||||
public void leftArrowClicked() {
|
||||
targetDate = targetDate.minusMonths(1);
|
||||
fillCalendar();
|
||||
calendar.getPrevMonth();
|
||||
}
|
||||
|
||||
@OnClick(R.id.right_arrow)
|
||||
public void rightArrowClicked() {
|
||||
targetDate = targetDate.plusMonths(1);
|
||||
fillCalendar();
|
||||
calendar.getNextMonth();
|
||||
}
|
||||
|
||||
@OnClick(R.id.table_month)
|
||||
public void pickMonth() {
|
||||
final MyDatePickerDialog dialog = new MyDatePickerDialog();
|
||||
final Bundle bundle = new Bundle();
|
||||
bundle.putString(DATE, targetDate.toString());
|
||||
bundle.putString(DATE, calendar.getTargetDate().toString());
|
||||
dialog.setArguments(bundle);
|
||||
dialog.show(getSupportFragmentManager(), "datepicker");
|
||||
}
|
||||
|
||||
private String getMonthName() {
|
||||
final String[] months = new DateFormatSymbols().getMonths();
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(months[targetDate.getMonthOfYear() - 1]);
|
||||
|
||||
final String targetYear = targetDate.toString(YEAR_PATTERN);
|
||||
if (!targetYear.equals(new DateTime().toString(YEAR_PATTERN))) {
|
||||
sb.append(" ");
|
||||
sb.append(targetYear);
|
||||
}
|
||||
return sb.toString();
|
||||
@Override
|
||||
public void onDatePicked(DateTime dateTime) {
|
||||
calendar.updateCalendar(dateTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDatePicked(DateTime dateTime) {
|
||||
targetDate = dateTime;
|
||||
fillCalendar();
|
||||
public void updateDays(List<Day> days) {
|
||||
fillCalendar(days);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMonth(String month) {
|
||||
monthTV.setText(month);
|
||||
}
|
||||
|
||||
private void setupLabelSizes() {
|
||||
for (int i = 0; i < 7; i++) {
|
||||
final TextView dayTV = (TextView) findViewById(res.getIdentifier("label_" + i, "id", packageName));
|
||||
dayTV.setTextSize(dayTextSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<resources>
|
||||
<dimen name="activity_margin">16dp</dimen>
|
||||
<dimen name="day_text_size">16sp</dimen>
|
||||
<dimen name="today_text_size">22sp</dimen>
|
||||
<dimen name="today_text_size">30sp</dimen>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in New Issue