Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Given an array of saved dates, how to do i select a number of dates on the calendar? #1

Closed
antonijap opened this issue Apr 5, 2016 · 40 comments
Assignees

Comments

@antonijap
Copy link

I have an array of dates (that user previously tapped) and I'm trying display them on a calendar by assigning them isSelected = true, but I can't because Xcode tells me isSelected is a let.

Is there some workaround to this?

@patchthecode
Copy link
Owner

Acknowledged your problem. Will give you a response in some hours.

@patchthecode
Copy link
Owner

@antonijap Ok. As i'm the sole dev on this project, I missed this obvious feature. Thanks for pointing it out to me. I now have the code for this updated and i'm now reviewing it before i commit it. Was there any other part of this project you found to be missing? It's always good to have more eyes.

@antonijap
Copy link
Author

For now, this is the only thing that was missing. If I find anything else I'll let you know. Thanks for being so quick with this issue.

@patchthecode
Copy link
Owner

Description of the fix: -->

func selectDate(date: NSDate) will be changed to func selectDate(date: [NSDate])
In that function, you can provide an array of dates to be selected.

So let's say you have a bunch of dates saved from some time before. You can use like this:

let d1 = someDate...
let d2 = someDate...
let d3 = someDate...

calendarView.selectDate([d1, d2, d3])

This will cause your dates to be selected. If your date is off the screen, it will still be selected. Does this proposed fix sound like it will meet your needs?

@antonijap
Copy link
Author

I think it will be great. Let me show you my code:

func setupCellBeforeDisplay(cellState: CellState, date: NSDate) {
        // Setup Cell text
        dayLabel.text = cellState.text

        // Setup text color
        configureTextColor(cellState)

        // Setup Cell Background color for today
        if c.stringFromDate(date) == todayDate {
            todayView.hidden = false
            todayView.layer.cornerRadius = 10
        } else {
            todayView.hidden = true
        }

        // Setup cell selection status
        configueViewIntoBubbleView(cellState)

        // Configure Visibility
        configureVisibility(cellState)

        // Grab selected dates from database, select it
        for day in demoDays {
            let blah = c.dateFromString(day)

            if date.isEqualToDate(blah!) {
                dayLabel.text = "YES" // Just for testing, delete this
                // make them selectable
            }
        }

    }

Basically, when a user taps on certain dates they will be stored in my database, and then they should be permanently displayed as selected dates on a calendar. User can deselect it and they will be erased from a database. Can you just explain a bit how should I use updated function with my code?

@patchthecode
Copy link
Owner

The function isnt updated yet. I have not pushed the changes yet. I am currently testing it to make sure i didnt break anything :) . But I will be pushing it in some hours.

Now in regards to how you will use it, here are some things.

  1. I see you're using the demo code provided. I see you have the following code inside of the setupCellBeforeDisplay function.
// Grab selected dates from database, select it
        for day in demoDays {
            let blah = c.dateFromString(day)

            if date.isEqualToDate(blah!) {
                dayLabel.text = "YES" // Just for testing, delete this
                // make them selectable
            }
        }

Keep in mind the calendarView is just like a UITableView. Just like in a UITableView, the cellForRowAtIndexPath will be called for every cell, it will be the same for this calendarView. Therefore the setupCellBeforeDisplay function will be called before every cell is displayed. The function is meant to give you some time to setup the dayCell's visual looks before it is displayed. Are you sure you want to put a loop in that function that takes information from a database? Remember, setting up a cell should be fast.

  1. I would set this up in your viewController. After i push the code change, you will be able to use the new function like this
// Grab selected dates from database, select it
        for day in demoDays {
            let blah = c.dateFromString(day)

            if date.isEqualToDate(blah!) {
                dayLabel.text = "YES" // Just for testing, delete this
                // make them selectable
                calendarView.selectDate([blah])
            }
        }

or you can gather them to be selected all at once. Like so:

// Grab selected dates from database, select it
        var datesToBeSelected: [NSDate] = []
        for day in demoDays {
            let blah = c.dateFromString(day)

            if date.isEqualToDate(blah!) {
                dayLabel.text = "YES" // Just for testing, delete this
                // make them selectable
                datesToBeSelected.append(blah)
            }
        }
      calendarView.selectDate(datesToBeSelected)

@antonijap
Copy link
Author

I played with "old" function selectDate(NSDate), I will use it in viewDidLoad and it should work great. I was playing in setupCellBeforeDisplay because selectDate function only accepted one value. I won't use my code, this new function should do the trick.

@patchthecode
Copy link
Owner

Alright then. I will still do the update however. The selecting of the date with the old function will only work if the date is visible on the screen. You will have to scroll to the date first before selecting it. Like so:

calendarView.scrollToDate(someDate)
calendarView.selectDate(someDate)

After the new code is updated however, selecting a date will work even if the date is not visible on screen. Will update code in some some hours. Cheers.

@patchthecode
Copy link
Owner

Code committed. You can check it out now. It is now version 2.0.0 (so you can put that in your Pod file if you are using cocoapods)
I decided to let the function have the same name. If you need multiple dates selected, then simply select them in a loop. I will close this issue now, but if there is still some error with it, then do not hesitate to reopen it. Thanks.

@antonijap
Copy link
Author

screen shot 2016-04-06 at 12 03 09

Shouldn't it be like you said calendarView.selectDate([d1, d2, d3])Now it's not a method.

@patchthecode
Copy link
Owner

Ok. As i've said in the last message. I have decided to leave the function with the same name. Therefore, to use do just like you have done.

self.calendarView.selectDate(date)

If you need to select multiple dates, you can select this like so:

for date in dates {
   self.calendar.selectDate(date)
}

I did not want to break the functionality of method by changing its signature to accept an array or dates.

@patchthecode
Copy link
Owner

Tomorrow, i will also commit a new function called:
func selectDate(date: [NSDate])

This one will behave the way like we have discussed. But for now, use the function in a loop like I showed you above.

@antonijap
Copy link
Author

Oh sorry, I didn't gather that part. Well, untill tomorrow...

@patchthecode
Copy link
Owner

Committed. Do update on version 2.0.1.
Let me know if it works for you.

@patchthecode patchthecode changed the title How to assign isSelected = true to dates? Given an array of saved dates, how to do i select a number of dates on the calendar? Apr 6, 2016
@antonijap
Copy link
Author

Ok, I tested it, it works. Thanks!

@patchthecode
Copy link
Owner

Hey, im seeing this message in my email -> "I don't know if it's something in my code but didSelectDate is not working as expected. When I click nothing happens, if I scroll forward and then get back to month where selected date is - then it shows."

But im not seeing this message here. Are you experiencing difficulty? or was that an old email.

@antonijap
Copy link
Author

I deleted it because I found a way to make everything work. I attached delegate and datasource on my view controller and it didn't work right, then I made extension and now it works.

@patchthecode
Copy link
Owner

I think i know why it wasnt working for you. I suspect you didnt look at the release notes for 2.0.1.
this method was changed from this:
func calendar(calendar: JTAppleCalendarView, didSelectDate date: NSDate, cell: JTAppleDayCellView, cellState: CellState)
to this
func calendar(calendar: JTAppleCalendarView, didSelectDate date: NSDate, cell: JTAppleDayCellView?, cellState: CellState)

Notice there is an added --> ? after the JTAppleDayCellView. This is the only major change made in 2.0.0 that can break functionality.

@antonijap
Copy link
Author

Just to let you know selectDates is not working well with Firebase. I have reading data and displaying it on a calendar in my viewDidLoad. I'm using observeEventType so every change in Firebase triggers reading/displaying data.

So, in my Firebase block of code I have selectDates(array of nsdates) and in didSelectDate I'm uploading selected dates into Firebase which triggers observeEventType which has selectDates(array of nsdates) which triggers didSelectDate... These 2 methods are playing ping-pong and I wonder is there some way not to trigger didSelectDate when using selectDates(array of nsdates) method somewhere in my code?

@antonijap
Copy link
Author

To show you problem in a simple form, this is a code that happens when the app loads:

            ref.childByAppendingPath("dates").queryOrderedByValue().observeSingleEventOfType(.Value, withBlock: { snapshot in
                self.selectedDays = []

                if let dates = snapshot.value as? String {
                    let days = dates.componentsSeparatedByString(", ")
                    for day in days {
                        let date = day.toDate(DateFormat.Custom("YYYY MM dd"))!
                        self.selectedDays.append(date)
                    }
                }

                print("I've just downloaded: \(self.selectedDays)")
                self.calendarView.selectDates(self.selectedDays)

                }, withCancelBlock: { error in
                    print(error.description)
            })

And this is a code in didSelectDate:

    func calendar(calendar: JTAppleCalendarView, didSelectDate date: NSDate, cell: JTAppleDayCellView?, cellState: CellState) {
        (cell as? CellView)?.cellSelectionChanged(cellState)

        print("I just tapped with my finger: \(date.toString(DateFormat.Custom("YYYY MM dd"))!)")

    }

AND when I open my app, without touching anything, console says:

I've just downloaded: [2016-04-10 22:00:00 +0000, 2016-04-11 22:00:00 +0000]
I just tapped with my finger: 2016 04 11
I just tapped with my finger: 2016 04 12

Maybe you intended to connect these two functions but it is impossible to work with multiple dates in a database when selectDates(dates: [NSDate]) fires up didSelectDate which should trigger only when a user taps on it.

@patchthecode
Copy link
Owner

So just to be clear, you want to select a date, but you only want the didSelectDate to be triggered only when a user taps on it, and not when youre just trying to setup the initial data?

@antonijap
Copy link
Author

Yes, if it's possible. Because when I load initial data .selectDates() triggers didSelectDate which has code that uploads to database which again calls .selectDates and then I have a nasty loop.

@patchthecode
Copy link
Owner

As this is a new issue i've created a new Issue here -> #4

Expect a fix in some hours.

@antonijap
Copy link
Author

You're really quick. As you are better programmer than me I can't offer you help with that but if you want super-cool logo for your calendar or something like that - I'm here.

@patchthecode
Copy link
Owner

Ah cool. Well, i'm not much of a designer, so i just whipped one up before creating this app. Since i'm already so attached to the one i created, it will be sad to part with it. But i wont refuse a logo if you create one that looks awesome. But no pressure. It's up to you :)

@tosifkanuga
Copy link

Hello,
I am not getting selected date just like today date is 2017-10-14 but i am getting previous date
currentDate 2017-10-14 10:59:02 +0000 selectedDate 2017-10-12 18:30:00 +0000
I tap on 2017-10-13 but i get 2017-10-12
what should i have to do.

@patchthecode
Copy link
Owner

@tosifkanuga
let me know if this discussion helped -> #252

@tosifkanuga
Copy link

tosifkanuga commented Oct 14, 2017 via email

@patchthecode
Copy link
Owner

awesome :)
cool 🍻

@tosifkanuga
Copy link

tosifkanuga commented Oct 14, 2017 via email

@axita27
Copy link

axita27 commented Jun 28, 2018

hey.. I want select dates when calendar is load ...n I used selectdates() bt last date will be selected ...I used for loop then all dates are selected....but problem is when I scrolled then all selected dates are gone

@patchthecode
Copy link
Owner

@axita27 what code did you use to select the dates?

@axita27
Copy link

axita27 commented Jun 29, 2018

func setupViewsOfCalendar(from visibleDates: DateSegmentInfo){
guard let date = visibleDates.monthDates.first?.date else {return}
Formatter.dateFormat = "MMM yyyy"
self.MonthLbl.text = Formatter.string(from: date)
for i in self.arr{
self.calendarView.selectDates([i])
}
self.ShowDateTime?.text = ""
}

this function is called in didScrollToDateSegmentWith method
after 5-6 time scrolled when one of the date is unselected

@patchthecode
Copy link
Owner

that is not how to select multiple dates.
This is how it is done

 self.calendarView.selectDates(self.arr)

@patchthecode
Copy link
Owner

@axita27 I do not understand what you area asking.

@axita27
Copy link

axita27 commented Jun 29, 2018

ohhh....I just want to select my array dates....n not select other dates...
and when I scrolled multiple time current month to other month then one of my array date unselected automatically

@patchthecode
Copy link
Owner

ohhh....I just want to select my array dates....

calendarView.allowsMultipleSelection = true
 self.calendarView.selectDates(self.arr)

and when I scrolled multiple time current month to other month then one of my array date unselected automatically

Is this something you want? or is this something you do not want?

@axita27
Copy link

axita27 commented Jun 29, 2018

yes I don't want when I scrolled multiple time current month to other month then one of my array date unselected automatically
and also this: not select other dates.

@patchthecode
Copy link
Owner

@axita27 can you join me here? https://gitter.im/patchthecode/JTAppleCalendar

@axita27
Copy link

axita27 commented Jun 29, 2018

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants