patchthecode / JTAppleCalendar

The Unofficial Apple iOS Swift Calendar View. Swift calendar Library. iOS calendar Control. 100% Customizable
https://patchthecode.com
MIT License
7.57k stars 810 forks source link

How selecting multiple dates work #6

Closed patchthecode closed 8 years ago

patchthecode commented 8 years ago

Pasted from user:

It's me again.

This new function is working in a weird way. Let me show you. When the app first loads it runs this: calendarView.selectDates(selectedDays, triggerSelectionDelegate: false)

And this works as expected, my dates get isSelected to true. Then, when I tap on a date to select it or deselect it that code is repeated, but instead of selecting every date in my array again it sets their isSelected property to false and they disappear.

tried to play with it a little and I typed:

calendarView.selectDates(selectedDays, triggerSelectionDelegate: false) // makes dates in array selected calendarView.selectDates(selectedDays, triggerSelectionDelegate: false) // this one destroys work of that previous one

That second function really reverts their state to the opposite of their previous state. Why is that happening?

patchthecode commented 8 years ago

Ok let me understand. Tell me which part below is wrong:

  1. I assume that you have multipleSelection enabled.
  2. calendarView.selectDates(selectedDays, triggerSelectionDelegate: false) works as expected on viewDidLoad - It correctly selects the dates.
  3. After step 2, you select an already selected date (a date that was selected in the initial selection). You did this selection by tapping the simulator instead of using code.
  4. After step 3, you say that the the isSelected property goes away, and the cell's visibility dissappears?
patchthecode commented 8 years ago

Ok i think i understand your problem. This is the behavior of multi selection. If multi-select is enabled, and you select a date on the screen. That date will be selected. If you select that same date again, the selection will disappear.

Did you want the date to remain selected after tapping it again?

If you think this is a bad behavior then lets talk about it :)

patchthecode commented 8 years ago

From the screenshot you provided of your calendar, here is what i am guessing. (my guess could be wrong, but here goes)

You have some dates saved, and you want the background of the saved dates so show color yellow right? So you enabled multiselection, and had those dates selected as yellow right?

Well this i not the right way to do it. Selection is for Selection and should mean Selection - i.e. A User selected a date. What you want is not selection. What you want is a new state.

Lets call this state: PreviouslySavedDate

This is the beauty of JTAppleCalendar, you can make up anything you want. The power is in the hands of the developer. The following example is based on the sample code i have here in github. Head over to CellView.swift and write up your own code to handle how a SavedDate state will look.

Your code can be something like this:

If myDate == aDateThatExistsInMySavedDateArray { // In other words, a PreviouslySavedDate
   // Make my PreviouslySavedDateBackView color to be yellow and set its `.Hidden` property to be false. 
   // Dont have a PreviouslySavedDateBackView on your cellView.xib? easy, just create one. 
   // You can create anything you want on cellview.xib. 
}  else {
    // Hide my PreviouslySavedDateBackView by making `.Hidden` property = true
}
antonijap commented 8 years ago

Hey,

But what if I want to deselect/erase previously selected day? If I only change background and not isSelected = true, then I would't be able to tap on it to delete it, wouldn't I?

Basically I want to be in sync every second because when user taps on a date it is saved on Firebase, when taps again on a date that has isSelected = true then it is deselected, erased from Firebase and that new array of dates is downloaded again, previous array is emptied and filled with new fresh values that are displayed on calendar.

patchthecode commented 8 years ago

I see what you are saying. But one thing. This multiple selection behavior is not some thing i made up. This is Apple's behavior of multiple selection. You can see in their documentation here:

screen shot 2016-04-12 at 11 31 57 am

If i change the way it behaves then this will be changing the way it behaves for many users that expect it to behave like this. So maybe you need to architecture your code differently?

So here is a question for you: From what you've said, selection happens when a user taps the cell right? So can you tell me how you want to have deselection happen?

antonijap commented 8 years ago

The thing is that I use Firebase that has a function which trigers code when I make a change in my Firebase. I am making changes as user taps on a date. If I tap on a date which is not previously selected it will upload that date (string value) into Firebase. If I deselect that date it will delete that date from Firebase. Every time these changes are happening one block of code is repeated. In that block of code is .selectDates() function. So when I run my app everything is okay, dates are populated. But when I want to add a date (tapping on it) then .selectDates() runs one more time and deselects all my dates. The same thing happens if I deselect previously selected date. I'm starting to think this is not posible the way I wanted to.

antonijap commented 8 years ago

UPDATE:

I managed to find a workaround for my problem. Now it works. Finally!

patchthecode commented 8 years ago

Awsome. Closing this issue :)

vital-edu commented 7 years ago

The allowsMultipleSelection is not in the documentation anymore. :/

patchthecode commented 7 years ago

@vital-edu i do not understand what you mean?

Its documented on Apple's Site for UICollectionViews. Let me know.

vital-edu commented 7 years ago

I am sorry. I didn't notice that the calendar is a UICollectionViews.

patchthecode commented 7 years ago

Its cool. cheers 🍺