At the moment we can made a range selection but, what i what to try is to make only one range selection.
If we select a range, it will remove other麓s selections that we have.
Thanks
Well, you can code the range selection however you wish.
The range selection that I gave in the tutorial is only an example.
I do not have any code in the library does range selection. The person that is doing the selection and the code for the selection you the developer (yourself).
What code are you using to range select?
It has been 4 days with no reply from you.
I will assume that your issue was resolved.
If you are still having problems, then re-open this issue and I will resume where we left off.
I have the same question. Can we open this or should I make my own ticket?
I've been trying so hard to implement this one range selection, I've been doing this inside the didDeselectDate delegate of the calendar. But upon triggering that function, the date is already erased! I believe I won't be able to implement this feature
I have re-opened this one.
I do not understand what you mean by one range selection.
Have you checked out the range selection code in the tutorial yet? It has all the code you need.
If the code does not answer your question, then maybe I do not properly understand what you mean by one range selection
Hi, thanks for re-opening this. I tried out the delegate function that you gave to me:
func calendar(_ calendar: JTAppleCalendarView, shouldSelectDate date: Date, cell: JTAppleDayCellView, cellState: CellState)
However, Xcode doesn't auto complete and so it isn't being triggered. Anyways, about the one range selection, I'll give an example. If you select dates 5,6,7,8,9,10 and you de-select the 8 you'd have two range:
In one range selection, the 9&10 should be dropped. So, we would have only 1 range, which is the first: 5,6,7.
EDIT: I of course have tried running your example, in fact, it helped me a lot, I got the idea on how to setup the calendar using your example project, is that where I can find the range selection code? Oh, you mentioned tutorial, I'll go and find that out.
@glennposadas what version are you using?
The full function name is this ->
func calendar(_ calendar: JTAppleCalendarView, shouldSelectDate date: Date, cell: JTAppleDayCellView, cellState: CellState) -> Bool
I left out the last part.
Concerning the range selection. The code is entirely up to you. The only thing the library provides is the positioning of the range -> left, middle, right, full
Therefore in order to do what you want, when a user selects the 8th, you need to also select the dates after the 8th.
Check out my solution below. This will let you have a single selected range of dates.
func calendar(_ calendar: JTAppleCalendarView, shouldDeselectDate date: Date, cell: JTAppleDayCellView, cellState: CellState) -> Bool {
let selectedDates = calendar.selectedDates
if selectedDates.contains(date) {
// remove dates from the last selected.
if (selectedDates.count > 2 && selectedDates.first != date && selectedDates.last != date) {
let indexOfDate = selectedDates.index(of: date)
let dateBeforeDeselectedDate = selectedDates[indexOfDate!-1]
calendar.deselectAllDates()
calendar.selectDates(
from: selectedDates.first!,
to: dateBeforeDeselectedDate,
triggerSelectionDelegate: true,
keepSelectionIfMultiSelectionAllowed: true)
calendar.reloadData()
}
}
return true
}
Your code is ok.
But I would have coded it this way
func calendar(_ calendar: JTAppleCalendarView, shouldDeselectDate date: Date, cell: JTAppleDayCellView, cellState: CellState) -> Bool {
let selectedDates = calendar.selectedDates
if let indexOfSelectedDate = selectedDates.index(of: date),
indexOfSelectedDate + 1 < selectedDates.count {
let datesToDeselect = Array(selectedDates[indexOfSelectedDate..<calendarView.selectedDates.count])
calendar.selectDates(datesToDeselect)
}
return true
}
But like I said, the code is up to the coder.
The calendar is flexible enough that there is no right or wrong way to do something. You can code it however you want.
Thanks again, @patchthecode . I'll check that later tonight. I shared your calendar to our group, you earned stars from a certain members. ;) Cheers.
Thanks!
Hi all! First of all, thanks for making this awesome library @patchthecode ! I'm having trouble implementing the selection of only one range of dates.. with both of your above mentioned approaches, it fails from time to time.. and I can't figure out what is going wrong.. quite aggravating. : ) Also, the shouldDeselectDate delegate method is never fired..
@Kaisp i missed this issue. Because this one was closed.
lets track it here -> https://github.com/patchthecode/JTAppleCalendar/issues/609
I wrote like what you have in shouldDeselectDate, but how do I update the selection states for those cells?
@joseph-francis im not quite sure the context of what you are asking.
calendarView.reloadData() is the general way to reload the calendar,
but i'm not sure what you are trying to do.
Lets say we deselect some dates in shouldDeselectDate and returned false. How do I update the cells' views to a deselected view?
Your code is ok.
But I would have coded it this wayfunc calendar(_ calendar: JTAppleCalendarView, shouldDeselectDate date: Date, cell: JTAppleDayCellView, cellState: CellState) -> Bool { let selectedDates = calendar.selectedDates if let indexOfSelectedDate = selectedDates.index(of: date), indexOfSelectedDate + 1 < selectedDates.count { let datesToDeselect = Array(selectedDates[indexOfSelectedDate..<calendarView.selectedDates.count]) calendar.selectDates(datesToDeselect) } return true }But like I said, the code is up to the coder.
The calendar is flexible enough that there is no right or wrong way to do something. You can code it however you want.
The issue with this code is, selectDates method will call shouldDeselectDate indefinitely. One solution is
var datesToDeselect: [Date]? func calendar(_ calendar: JTAppleCalendarView, shouldDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) -> Bool { if let selectedDates = datesToDeselect, selectedDates.contains(date) { return true } let selectedDates = calendar.selectedDates if let selectedDateIndex = selectedDates.firstIndex(of: date), selectedDateIndex + 1 < selectedDates.count { datesToDeselect = Array(selectedDates[selectedDateIndex+1..<calendar.selectedDates.count]) calendar.deselect(dates: datesToDeselect!) } return true }
@lalkrishna if you are concerned that it will be called indefinately, then inside the shouldSelect function, you can do a check to see if the selection was made by a user or if it was programatically done.
cellState.selectionType
if it was programatic, then do not allow the cell to be selected again.
yes. thanks for the tip. Please update your code, may help someone in future.
Most helpful comment
Thanks again, @patchthecode . I'll check that later tonight. I shared your calendar to our group, you earned stars from a certain members. ;) Cheers.