diff --git a/app/src/main/java/com/events/calendarsample/MainActivity.kt b/app/src/main/java/com/events/calendarsample/MainActivity.kt index 6727812..4d40650 100644 --- a/app/src/main/java/com/events/calendarsample/MainActivity.kt +++ b/app/src/main/java/com/events/calendarsample/MainActivity.kt @@ -33,7 +33,7 @@ class MainActivity : AppCompatActivity(), EventsCalendar.Callback { val today = Calendar.getInstance() val end = Calendar.getInstance() end.add(Calendar.YEAR, 2) - eventsCalendar.setSelectionMode(eventsCalendar.SINGLE_SELECTION) + eventsCalendar.setSelectionMode(eventsCalendar.MULTIPLE_SELECTION) eventsCalendar.setToday(today) eventsCalendar.setMonthRange(today, end) eventsCalendar.setWeekStartDay(Calendar.SUNDAY, false) @@ -43,15 +43,6 @@ class MainActivity : AppCompatActivity(), EventsCalendar.Callback { eventsCalendar.setWeekHeaderTypeface(FontsManager.getTypeface(FontsManager.OPENSANS_SEMIBOLD, this)) eventsCalendar.setCallback(this) - -// val min = Calendar.getInstance() -// min.add(Calendar.MONTH, 1) -// val max = Calendar.getInstance() -// max.add(Calendar.MONTH, 1) -// max.add(Calendar.DAY_OF_YEAR, 5) -// eventsCalendar.selectDateRange(min, max) - - val c = Calendar.getInstance() c.add(Calendar.DAY_OF_MONTH, 2) eventsCalendar.addEvent(c) @@ -63,7 +54,9 @@ class MainActivity : AppCompatActivity(), EventsCalendar.Callback { eventsCalendar.addEvent(c) selected.setOnClickListener { - eventsCalendar.setCurrentSelectedDate(eventsCalendar.getCurrentSelectedDate()) + val dates = eventsCalendar.getDatesFromSelectedRange() + Log.e("SELECTED SIZE", dates.size.toString()) +// eventsCalendar.setCurrentSelectedDate(eventsCalendar.getCurrentSelectedDate()) } selected.typeface = FontsManager.getTypeface(FontsManager.OPENSANS_SEMIBOLD, this) diff --git a/eventscalendar/src/main/java/com/events/calendar/utils/EventsCalendarUtil.kt b/eventscalendar/src/main/java/com/events/calendar/utils/EventsCalendarUtil.kt index 7ea7244..e413081 100644 --- a/eventscalendar/src/main/java/com/events/calendar/utils/EventsCalendarUtil.kt +++ b/eventscalendar/src/main/java/com/events/calendar/utils/EventsCalendarUtil.kt @@ -14,6 +14,9 @@ import kotlin.properties.Delegates object EventsCalendarUtil { const val WEEK_MODE = 0 const val MONTH_MODE = 1 + val SINGLE_SELECTION = 0 + val RANGE_SELECTION = 1 + val MULTIPLE_SELECTION = 2 private const val MONTHS_IN_YEAR = 12 const val YYYY_MM_DD = 10 const val DD_MM_YYYY = 20 @@ -50,6 +53,11 @@ object EventsCalendarUtil { var minDateInRange: Calendar? = null var maxDateInRange: Calendar? = null + fun updateSelectedDates(c: Calendar) { + if (datesInSelectedRange.containsKey(getDateString(c, DD_MM_YYYY))) datesInSelectedRange.remove(getDateString(c, DD_MM_YYYY)) + else datesInSelectedRange[getDateString(c, DD_MM_YYYY)] = c + } + fun updateMinMaxDateInRange(c: Calendar) { when { areDatesSame(minDateInRange, c) -> { @@ -92,7 +100,6 @@ object EventsCalendarUtil { } } else datesInSelectedRange.clear() } - Log.e("RANGE SIZE", datesInSelectedRange.size.toString()) } val disabledDates: ArrayList = ArrayList() diff --git a/eventscalendar/src/main/java/com/events/calendar/views/DateText.kt b/eventscalendar/src/main/java/com/events/calendar/views/DateText.kt index cc59673..e6da107 100644 --- a/eventscalendar/src/main/java/com/events/calendar/views/DateText.kt +++ b/eventscalendar/src/main/java/com/events/calendar/views/DateText.kt @@ -186,39 +186,52 @@ class DateText : View { mDateTextPaint.color = disabledTextColor canvas.drawText("" + mDate.get(Calendar.DATE), mDateTextX.toFloat(), mDateTextY, mDateTextPaint) } else { - if ((isCurrentMonth && !isDisabled) || EventsCalendarUtil.datesInSelectedRange.contains(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY))) { + if (isCurrentMonth && !isDisabled) { if (isToday) canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mTodayCircleRadius, mTodayPaint) - when { - EventsCalendarUtil.datesInSelectedRange.contains(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) && !isDisabled -> { + when (EventsCalendarUtil.SELECTION_MODE) { + EventsCalendarUtil.SINGLE_SELECTION -> { + if (isSelected) { + mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled + mDateTextPaint.color = selectedTextColor + canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mBgCircleRadius, mSelectionPaint) + } else mDateTextPaint.color = defaultTextColor + } + EventsCalendarUtil.RANGE_SELECTION -> { when { - EventsCalendarUtil.datesInSelectedRange.keys.indexOf(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) == 0 -> { - mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled - mDateTextPaint.color = selectedTextColor - canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mFullCircleRadius, mRangeSelectionStartPaint) - canvas.drawRect(RectF((mWidth / 2).toFloat(), 0f, mWidth.toFloat(), mHeight.toFloat()), mRangeSelectionStartPaint) - RectF(1f, 2f, 3f, 4f) -// canvas.drawColor(eventDotColor) + EventsCalendarUtil.datesInSelectedRange.contains(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) -> when { + EventsCalendarUtil.datesInSelectedRange.keys.indexOf(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) == 0 -> { + mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled + mDateTextPaint.color = selectedTextColor + canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mFullCircleRadius, mRangeSelectionStartPaint) + canvas.drawRect(RectF((mWidth / 2).toFloat(), 0f, mWidth.toFloat(), mHeight.toFloat()), mRangeSelectionStartPaint) + } + EventsCalendarUtil.datesInSelectedRange.keys.indexOf(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) == EventsCalendarUtil.datesInSelectedRange.size - 1 -> { + mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled + mDateTextPaint.color = selectedTextColor + canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mFullCircleRadius, mRangeSelectionEndPaint) + canvas.drawRect(RectF(0f, 0f, (mWidth / 2).toFloat(), mHeight.toFloat()), mRangeSelectionEndPaint) + } + else -> { + mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled + mDateTextPaint.color = selectedTextColor + canvas.drawColor(mRangeSelectionPaint.color) + } } - EventsCalendarUtil.datesInSelectedRange.keys.indexOf(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY)) == EventsCalendarUtil.datesInSelectedRange.size - 1 -> { + isSelected -> { mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled mDateTextPaint.color = selectedTextColor - canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mFullCircleRadius, mRangeSelectionEndPaint) - canvas.drawRect(RectF(0f, 0f, (mWidth / 2).toFloat(), mHeight.toFloat()), mRangeSelectionEndPaint) -// canvas.drawColor(eventDotColor) - } - else -> { - mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled - mDateTextPaint.color = selectedTextColor - canvas.drawColor(mRangeSelectionPaint.color) + canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mBgCircleRadius, mSelectionPaint) } + else -> mDateTextPaint.color = defaultTextColor } } - isSelected -> { - mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled - mDateTextPaint.color = selectedTextColor - canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mBgCircleRadius, mSelectionPaint) + EventsCalendarUtil.MULTIPLE_SELECTION -> { + if (EventsCalendarUtil.datesInSelectedRange.contains(EventsCalendarUtil.getDateString(mDate, EventsCalendarUtil.DD_MM_YYYY))) { + mDateTextPaint.isFakeBoldText = EventsCalendarUtil.isBoldTextOnSelectionEnabled + mDateTextPaint.color = selectedTextColor + canvas.drawCircle(mCircleX.toFloat(), mCircleY.toFloat(), mBgCircleRadius, mSelectionPaint) + } else mDateTextPaint.color = defaultTextColor } - else -> mDateTextPaint.color = defaultTextColor } canvas.drawText("" + mDate.get(Calendar.DATE), mDateTextX.toFloat(), mDateTextY, mDateTextPaint) } else { @@ -302,6 +315,7 @@ class DateText : View { return super.onTouchEvent(event) } + private fun isPointerInsideArea(event: MotionEvent): Boolean { touchDown = false val locationOnScreen = IntArray(2) diff --git a/eventscalendar/src/main/java/com/events/calendar/views/EventsCalendar.kt b/eventscalendar/src/main/java/com/events/calendar/views/EventsCalendar.kt index 9459f75..a6c535a 100644 --- a/eventscalendar/src/main/java/com/events/calendar/views/EventsCalendar.kt +++ b/eventscalendar/src/main/java/com/events/calendar/views/EventsCalendar.kt @@ -234,6 +234,7 @@ class EventsCalendar : ViewPager, MonthView.Callback { fun getDatesFromSelectedRange(): ArrayList { val dates: ArrayList = ArrayList() + dates.addAll(EventsCalendarUtil.datesInSelectedRange.values) return dates } @@ -335,32 +336,42 @@ class EventsCalendar : ViewPager, MonthView.Callback { } fun setCurrentSelectedDate(selectedDate: Calendar?) { - if (EventsCalendarUtil.SELECTION_MODE != RANGE_SELECTION) { - val position: Int - if (isPagingEnabled) { - doFocus = false - if (EventsCalendarUtil.currentMode == EventsCalendarUtil.MONTH_MODE) { - position = EventsCalendarUtil.getMonthPositionForDay(selectedDate, mMinMonth) - setCurrentItem(position, false) - post { - EventsCalendarUtil.monthPos = currentItem - EventsCalendarUtil.selectedDate = selectedDate - mCalendarMonthsAdapter.getItem(currentItem)?.setSelectedDate(selectedDate!!) - doFocus = true - } - } else { - position = EventsCalendarUtil.getWeekPosition(selectedDate, mMinMonth) - setCurrentItem(position, false) - post { - mCalendarWeekPagerAdapter.getItem(currentItem)?.setSelectedDate(selectedDate!!) - doFocus = true + when (EventsCalendarUtil.SELECTION_MODE) { + SINGLE_SELECTION -> { + val position: Int + if (isPagingEnabled) { + doFocus = false + if (EventsCalendarUtil.currentMode == EventsCalendarUtil.MONTH_MODE) { + position = EventsCalendarUtil.getMonthPositionForDay(selectedDate, mMinMonth) + setCurrentItem(position, false) + post { + EventsCalendarUtil.monthPos = currentItem + EventsCalendarUtil.selectedDate = selectedDate + mCalendarMonthsAdapter.getItem(currentItem)?.setSelectedDate(selectedDate!!) + doFocus = true + } + } else { + position = EventsCalendarUtil.getWeekPosition(selectedDate, mMinMonth) + setCurrentItem(position, false) + post { + mCalendarWeekPagerAdapter.getItem(currentItem)?.setSelectedDate(selectedDate!!) + doFocus = true + } } } } - } else { - if (selectedDate != null) EventsCalendarUtil.updateMinMaxDateInRange(selectedDate) - reset() - refreshTodayDate() + RANGE_SELECTION -> { + if (selectedDate != null) EventsCalendarUtil.updateMinMaxDateInRange(selectedDate) + reset() + refreshTodayDate() + } + MULTIPLE_SELECTION -> { + if (selectedDate != null) EventsCalendarUtil.updateSelectedDates(selectedDate) + reset() + refreshTodayDate() + } + else -> { + } } }