convert MonthlyCalendar to kotlin

This commit is contained in:
tibbi 2016-11-03 19:04:14 +01:00
parent 635e646d82
commit bf74b34cb7
5 changed files with 114 additions and 131 deletions

View File

@ -1,9 +0,0 @@
package com.simplemobiletools.calendar;
import com.simplemobiletools.calendar.models.Day;
import java.util.List;
public interface MonthlyCalendar {
void updateMonthlyCalendar(String month, List<Day> days);
}

View File

@ -1,120 +0,0 @@
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 java.util.ArrayList;
import java.util.List;
public class MonthlyCalendarImpl implements DBHelper.GetEventsListener {
private static final int DAYS_CNT = 42;
private static final String YEAR_PATTERN = "YYYY";
private final MonthlyCalendar mCallback;
private final String mToday;
private final Context mContext;
private DateTime mTargetDate;
private List<Event> mEvents;
public MonthlyCalendarImpl(MonthlyCalendar callback, Context context) {
mCallback = callback;
mContext = context;
mToday = new DateTime().toString(Formatter.DAYCODE_PATTERN);
mEvents = new ArrayList<>();
}
public void updateMonthlyCalendar(DateTime targetDate) {
mTargetDate = targetDate;
final int startTS = Formatter.getDayStartTS(Formatter.getDayCodeFromDateTime(mTargetDate.minusMonths(1)));
final int endTS = Formatter.getDayEndTS(Formatter.getDayCodeFromDateTime(mTargetDate.plusMonths(1)));
new DBHelper(mContext).getEvents(startTS, endTS, this);
}
public void setTargetDate(DateTime dateTime) {
mTargetDate = dateTime;
}
void getPrevMonth() {
updateMonthlyCalendar(mTargetDate.minusMonths(1));
}
void getNextMonth() {
updateMonthlyCalendar(mTargetDate.plusMonths(1));
}
public void getDays() {
final List<Day> days = new ArrayList<>(DAYS_CNT);
final int currMonthDays = mTargetDate.dayOfMonth().getMaximumValue();
int firstDayIndex = mTargetDate.withDayOfMonth(1).getDayOfWeek();
if (!Config.newInstance(mContext).getIsSundayFirst())
firstDayIndex -= 1;
final int prevMonthDays = mTargetDate.minusMonths(1).dayOfMonth().getMaximumValue();
boolean isThisMonth = false;
boolean isToday;
int value = prevMonthDays - firstDayIndex + 1;
DateTime curDay = mTargetDate;
for (int i = 0; i < DAYS_CNT; i++) {
if (i < firstDayIndex) {
isThisMonth = false;
curDay = mTargetDate.minusMonths(1);
} else if (i == firstDayIndex) {
value = 1;
isThisMonth = true;
curDay = mTargetDate;
} else if (value == currMonthDays + 1) {
value = 1;
isThisMonth = false;
curDay = mTargetDate.plusMonths(1);
}
isToday = isThisMonth && isToday(mTargetDate, value);
final DateTime newDay = curDay.withDayOfMonth(value);
final String dayCode = Formatter.getDayCodeFromDateTime(newDay);
final Day day = new Day(value, isThisMonth, isToday, dayCode, hasEvent(dayCode), newDay.getWeekOfWeekyear());
days.add(day);
value++;
}
mCallback.updateMonthlyCalendar(getMonthName(), days);
}
private boolean hasEvent(String dayCode) {
for (Event event : mEvents) {
if (Formatter.getDayCodeFromTS(event.getStartTS()).equals(dayCode)) {
return true;
}
}
return false;
}
private boolean isToday(DateTime targetDate, int curDayInMonth) {
return targetDate.withDayOfMonth(curDayInMonth).toString(Formatter.DAYCODE_PATTERN).equals(mToday);
}
private String getMonthName() {
String month = Formatter.getMonthName(mContext, mTargetDate.getMonthOfYear() - 1);
final String targetYear = mTargetDate.toString(YEAR_PATTERN);
if (!targetYear.equals(new DateTime().toString(YEAR_PATTERN))) {
month += " " + targetYear;
}
return month;
}
public DateTime getTargetDate() {
return mTargetDate;
}
@Override
public void gotEvents(List<Event> events) {
mEvents = events;
getDays();
}
}

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.calendar
import com.simplemobiletools.calendar.models.Day
interface MonthlyCalendar {
fun updateMonthlyCalendar(month: String, days: List<Day>)
}

View File

@ -0,0 +1,105 @@
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 java.util.*
class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) : DBHelper.GetEventsListener {
private val DAYS_CNT = 42
private val YEAR_PATTERN = "YYYY"
private val mToday: String
var mEvents: List<Event>
lateinit var mTargetDate: DateTime
init {
mToday = DateTime().toString(Formatter.DAYCODE_PATTERN)
mEvents = ArrayList<Event>()
}
fun updateMonthlyCalendar(targetDate: DateTime) {
mTargetDate = targetDate
val startTS = Formatter.getDayStartTS(Formatter.getDayCodeFromDateTime(mTargetDate.minusMonths(1)))
val endTS = Formatter.getDayEndTS(Formatter.getDayCodeFromDateTime(mTargetDate.plusMonths(1)))
DBHelper(mContext).getEvents(startTS, endTS, this)
}
fun getPrevMonth() {
updateMonthlyCalendar(mTargetDate.minusMonths(1))
}
fun getNextMonth() {
updateMonthlyCalendar(mTargetDate.plusMonths(1))
}
fun getDays() {
val days = ArrayList<Day>(DAYS_CNT)
val currMonthDays = mTargetDate.dayOfMonth().maximumValue
var firstDayIndex = mTargetDate.withDayOfMonth(1).dayOfWeek
if (!Config.newInstance(mContext).isSundayFirst)
firstDayIndex -= 1
val prevMonthDays = mTargetDate.minusMonths(1).dayOfMonth().maximumValue
var isThisMonth = false
var isToday: Boolean
var value = prevMonthDays - firstDayIndex + 1
var curDay: DateTime = mTargetDate
for (i in 0..DAYS_CNT - 1) {
if (i < firstDayIndex) {
isThisMonth = false
curDay = mTargetDate.minusMonths(1)
} else if (i == firstDayIndex) {
value = 1
isThisMonth = true
curDay = mTargetDate
} else if (value == currMonthDays + 1) {
value = 1
isThisMonth = false
curDay = mTargetDate.plusMonths(1)
}
isToday = isThisMonth && isToday(mTargetDate, value)
val newDay = curDay.withDayOfMonth(value)
val dayCode = Formatter.getDayCodeFromDateTime(newDay)
val day = Day(value, isThisMonth, isToday, dayCode, hasEvent(dayCode), newDay.weekOfWeekyear)
days.add(day)
value++
}
mCallback.updateMonthlyCalendar(monthName, days)
}
private fun hasEvent(dayCode: String): Boolean {
for (event in mEvents) {
if (Formatter.getDayCodeFromTS(event.startTS) == dayCode) {
return true
}
}
return false
}
private fun isToday(targetDate: DateTime, curDayInMonth: Int): Boolean {
return targetDate.withDayOfMonth(curDayInMonth).toString(Formatter.DAYCODE_PATTERN) == mToday
}
private val monthName: String
get() {
var month = Formatter.getMonthName(mContext, mTargetDate.monthOfYear - 1)
val targetYear = mTargetDate.toString(YEAR_PATTERN)
if (targetYear != DateTime().toString(YEAR_PATTERN)) {
month += " $targetYear"
}
return month
}
override fun gotEvents(events: MutableList<Event>) {
mEvents = events
getDays()
}
}

View File

@ -69,7 +69,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
mCalendar.apply {
targetDate = Formatter.getDateTimeFromCode(mDayCode)
mTargetDate = Formatter.getDateTimeFromCode(mDayCode)
getDays() // prefill the screen asap, even if without events
updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
}
@ -118,7 +118,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
val datePicker = view.findViewById(R.id.date_picker) as DatePicker
hideDayPicker(datePicker)
val dateTime = DateTime(mCalendar.targetDate.toString())
val dateTime = DateTime(mCalendar.mTargetDate.toString())
datePicker.init(dateTime.year, dateTime.monthOfYear - 1, 1, null)
alertDialog.apply {