RaffiKian / RKCalendar

SwiftUI Simple Calendar / Date Picker for iOS
MIT License
535 stars 71 forks source link

Updates to RKCalendar #4

Open workingDog opened 5 years ago

workingDog commented 5 years ago

RaffiKian, great project.

I've made some modifications to your code to include xCode 11 beta 6, multi-dates selection, disabled dates option, etc... If you are interested in these I'd be happy to do another pull request. If not let me know.

https://github.com/workingDog/RKCalendar

RaffiKian commented 5 years ago

Thank you for your contribution, amazing additions. I will contact you by email.

amuresia commented 5 years ago

Hi @workingDog,

Thanks for working on this and sharing it with us! The disabled options functionality that you've added is something that I am particularly interested in integrating in my app! However I noticed a small problem when the RKCalendar is not part of the ContentsView which in my Stage Delegate is the root view.

Unfortunately the disabled dates don't work if I try to create a new TestView with the RKCalendar, and then include the TestView in ContentsView

import SwiftUI

struct TestView : View {

    @State var deselectedIsPresented = false

    var rkManager4 = RKManager(calendar: Calendar.current, minimumDate: Date(), maximumDate: Date().addingTimeInterval(60*60*24*365), mode: 1)

    var body: some View {
        VStack (spacing: 25) {

            Button(action: { self.deselectedIsPresented.toggle() }) {
                Text("Example 4 - Disabled Dates Setting").foregroundColor(.blue)
            }
            .sheet(isPresented: self.$deselectedIsPresented, content: {
                RKViewController(isPresented: self.$deselectedIsPresented, rkManager: self.rkManager4)})
            datesView(dates: self.rkManager4.disabledDates)

        }
        .onAppear(perform: startUp)
        .navigationViewStyle(StackNavigationViewStyle())
    }

    func datesView(dates: [Date]) -> some View {
        ScrollView (.horizontal) {
            HStack {
                ForEach(dates, id: \.self) { date in
                    Text(self.getTextFromDate(date: date))
                }
            }
        }.padding(.horizontal, 15)
    }

    func startUp() {        
        // example of pre-setting disabled dates
        let testOffDates = [
            Date().addingTimeInterval(60*60*24*4),
            Date().addingTimeInterval(60*60*24*5),
            Date().addingTimeInterval(60*60*24*7)]
        rkManager4.disabledDates.append(contentsOf: testOffDates)
    }

    func getTextFromDate(date: Date!) -> String {
        let formatter = DateFormatter()
        formatter.locale = .current
        formatter.dateFormat = "EEEE, MMMM d, yyyy"
        return date == nil ? "" : formatter.string(from: date)
    }

}
import SwiftUI
import Combine

struct ContentView: View {
    var body: some View {
        VStack {
                TestView()
        }
    }
}

Any thought on how to fix or work around this issue? Thank you very much in advance!

workingDog commented 5 years ago

Hi Raffi,

I’ve cut and pasted your code as is, and all is working fine for me.

I can see the disabled dates, gray out and un-selectable.

I’ll continue to investigate.

Ringo

On Oct 20, 2019, at 7:10, Andrei Muresianu notifications@github.com wrote:

Hi @workingDog https://github.com/workingDog,

Thanks for working on this and sharing it with us! The disabled options functionality that you've added is something that I am particularly interested in integrating in my app! However I noticed a small problem when the RKCalendar is not part of the ContentsView which in my Stage Delegate is the root view.

Unfortunately the disabled dates don't work if I try to create a new TestView with the RKCalendar, and then include the TestView in ContentsView

import SwiftUI

struct TestView : View {

@State var deselectedIsPresented = false

var rkManager4 = RKManager(calendar: Calendar.current, minimumDate: Date(), maximumDate: Date().addingTimeInterval(60*60*24*365), mode: 1)

var body: some View {
    VStack (spacing: 25) {

        Button(action: { self.deselectedIsPresented.toggle() }) {
            Text("Example 4 - Disabled Dates Setting").foregroundColor(.blue)
        }
        .sheet(isPresented: self.$deselectedIsPresented, content: {
            RKViewController(isPresented: self.$deselectedIsPresented, rkManager: self.rkManager4)})
        datesView(dates: self.rkManager4.disabledDates)

    }
    .onAppear(perform: startUp)
    .navigationViewStyle(StackNavigationViewStyle())
}

func datesView(dates: [Date]) -> some View {
    ScrollView (.horizontal) {
        HStack {
            ForEach(dates, id: \.self) { date in
                Text(self.getTextFromDate(date: date))
            }
        }
    }.padding(.horizontal, 15)
}

func startUp() {        
    // example of pre-setting disabled dates
    let testOffDates = [
        Date().addingTimeInterval(60*60*24*4),
        Date().addingTimeInterval(60*60*24*5),
        Date().addingTimeInterval(60*60*24*7)]
    rkManager4.disabledDates.append(contentsOf: testOffDates)
}

func getTextFromDate(date: Date!) -> String {
    let formatter = DateFormatter()
    formatter.locale = .current
    formatter.dateFormat = "EEEE, MMMM d, yyyy"
    return date == nil ? "" : formatter.string(from: date)
}

} import SwiftUI import Combine

struct ContentView: View { var body: some View { VStack { TestView() } } } — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RaffiKian/RKCalendar/issues/4?email_source=notifications&email_token=AAZ6GDF77HQA7HM33TBYOJTQPOAVPA5CNFSM4IPL67ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBX5U4A#issuecomment-544201328, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ6GDAHQANHAQFBWPPZ4F3QPOAVPANCNFSM4IPL67ZA.

amuresia commented 5 years ago

Thank you! I should have mentioned that I'm running it on Xcode Beta 7 Version 11.2 beta (11B41). On what version is it working for you? I can downgrade and test that if you'd like.

workingDog commented 5 years ago

I’m running on Xcode 11.2 beta 2.

But I believe it also works with Xcode 11 GM.

On Oct 20, 2019, at 8:15, Andrei Muresianu notifications@github.com wrote:

Thank you! I should have mentioned that I'm running it on Xcode Beta 7 Version 11.2 beta (11B41). On what version is it working for you? I can downgrade and test that if you'd like.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RaffiKian/RKCalendar/issues/4?email_source=notifications&email_token=AAZ6GDGYXN46PYWV3LTJPELQPOIKFA5CNFSM4IPL67ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBX6RHI#issuecomment-544204957, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ6GDD62BR7OW66NLUAJEDQPOIKFANCNFSM4IPL67ZA.

workingDog commented 5 years ago

I’m also now on macOS 10.15 Catalina

On Oct 20, 2019, at 8:15, Andrei Muresianu notifications@github.com wrote:

Thank you! I should have mentioned that I'm running it on Xcode Beta 7 Version 11.2 beta (11B41). On what version is it working for you? I can downgrade and test that if you'd like.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RaffiKian/RKCalendar/issues/4?email_source=notifications&email_token=AAZ6GDGYXN46PYWV3LTJPELQPOIKFA5CNFSM4IPL67ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBX6RHI#issuecomment-544204957, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ6GDD62BR7OW66NLUAJEDQPOIKFANCNFSM4IPL67ZA.

FireNeuron commented 4 years ago

Hi Raffi,

Thanks for creating such a simple to use calendar. I am new to SwiftUI. I have encountered a small problem when implementing your calendar: if I set the calendar to show two years of dates, i.e. maximumDate: Date().addingTimeInterval(606024730). Xcode gives me a warning in the console whenever I tap on a date: ForEach<Range, Int, RKMonth> count (25) != its initial count (19). ForEach(_:content:) should only be used for constant* data. Instead conform data to Identifiable or use ForEach(_:id:content:) and provide an explicit id!

Could you help with this? Many thanks

workingDog commented 4 years ago

@FireNeuron,

I could not replicate your problem. Could you track down where exactly this warning comes from. Maybe the warning has the name of the class or struct. Could you share that.

FireNeuron commented 4 years ago

Thanks for your quick reply. I figured it out. Turns out List() doesn't load all of its content in once (like LazyVStack). So, the ForEach() in the List of RKViewController.swift only populated 19 rows (months) initially in my case rather than 25 (for two years). The smaller the device screen, the less rows the List() would load.

I changed the List() to: ScrollView (.vertical) { VStack{} } The problem went away.

Many thanks any way!

workingDog commented 4 years ago

I'm glad you worked it out. You maybe be interested in my fork of RKCalendar, with a number of additional features at:

https://github.com/workingDog/RKCalendar

I'd be interested to see if you get the same issue with it.