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

tooltips and avoid to select a some dates #227

Open
alexisgarciaa opened this issue Apr 21, 2023 · 2 comments
Open

tooltips and avoid to select a some dates #227

alexisgarciaa opened this issue Apr 21, 2023 · 2 comments

Comments

@alexisgarciaa
Copy link

Hi @bryankeller i am working o a fitness app and , i really love how easy has been to implement the calendar, how ever i am getting some issues now trying to make that some day be unavailable as airbnb app and also make that those day wont select the dates , and also i was able to implement the tooltip option however i would love to implement as airbnb app that show it once is press the date by the way i am using swiftui

@command-tab
Copy link

My experience with making days unavailable has required two steps:

  1. Make the day actually un-selectable by returning from the calendarView.daySelectionHandler in makeUIView when an unavailable day has been tapped:

    func makeUIView(context _: Context) -> CalendarView {
        let calendarView = CalendarView(initialContent: makeContent())
        calendarView.daySelectionHandler = { day in
            // Do nothing if the user attempts to select a day before today
            if dayIsBeforeToday(day: day) {
                return
            }
    
            // Also return here if your day is unavailable for some other reason
    
            // Otherwise, add code here to update the selection like usual
        }
        return calendarView
    }
    
    func dayIsBeforeToday(day: Day) -> Bool {
        if let selectedDate = calendar.date(from: day.components) {
            let startOfToday = calendar.startOfDay(for: Date())
            if selectedDate < startOfToday {
                return true
            }
        }
        return false
    }
  2. Make the day appear un-selectable in makeContent by passing a boolean to the SwiftUI day View:

    private func makeContent() -> CalendarViewContent {
        // ...
    
        return CalendarViewContent(
            calendar: calendar,
            visibleDateRange: visibleDateRange,
            monthsLayout: .vertical(options: VerticalMonthsLayoutOptions())
        )
        .dayItemProvider { day in
            var isSelected = false
            // Do a conditional check here that toggles isSelected if your `day` should appear selected
    
            var isEnabled = true
            // Do a conditional check here that toggles isEnabled if your `day` should appear disabled
    
            return YourCustomSwiftUIDayView(
                dayNumber: day.day,
                isSelected: isSelected,
                isEnabled: isEnabled
            ).calendarItemModel
    }
    struct YourCustomSwiftUIDayView: View {
        let dayNumber: Int
        let isSelected: Bool
        let isEnabled: Bool
    
        var body: some View {
            ZStack(alignment: .center) {
                Text("\(dayNumber)")
                    .foregroundColor(isEnabled ? Color(UIColor.label) : .gray)
                if isSelected {
                    Circle()
                        .strokeBorder(Color.accentColor, lineWidth: 1)
                        .background {
                            Circle()
                                .foregroundColor(Color(UIColor(Color.accentColor).withAlphaComponent(0.15)))
                        }
                        .aspectRatio(1, contentMode: .fill)
                }
            }
        }
    }

I'm still fairly new to Swift and SwiftUI, so take this with a grain of salt :)

@jonathanhyperion
Copy link

Thank you so much that help me a lot by the way have you ever tried to make a quick selection with a button a show a range of date selected?

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

No branches or pull requests

3 participants