Skip to content

Commit

Permalink
Multiple Selection Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tizisdeepan committed Oct 16, 2018
1 parent c4367a9 commit f2853bf
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 59 deletions.
15 changes: 4 additions & 11 deletions app/src/main/java/com/events/calendarsample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) -> {
Expand Down Expand Up @@ -92,7 +100,6 @@ object EventsCalendarUtil {
}
} else datesInSelectedRange.clear()
}
Log.e("RANGE SIZE", datesInSelectedRange.size.toString())
}

val disabledDates: ArrayList<Calendar> = ArrayList()
Expand Down
62 changes: 38 additions & 24 deletions eventscalendar/src/main/java/com/events/calendar/views/DateText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -302,6 +315,7 @@ class DateText : View {
return super.onTouchEvent(event)
}


private fun isPointerInsideArea(event: MotionEvent): Boolean {
touchDown = false
val locationOnScreen = IntArray(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class EventsCalendar : ViewPager, MonthView.Callback {

fun getDatesFromSelectedRange(): ArrayList<Calendar> {
val dates: ArrayList<Calendar> = ArrayList()
dates.addAll(EventsCalendarUtil.datesInSelectedRange.values)
return dates
}

Expand Down Expand Up @@ -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 -> {
}
}
}

Expand Down

0 comments on commit f2853bf

Please sign in to comment.