Skip to content

Conversation

@IvanUshakov
Copy link

Details

Make initializer of DayComponentRange with Date range public

Related Issue

programmatically update de selection range

Motivation and Context

In the application that I am currently developing there is a need for a calendar with a selection of date ranges. In this case, I need to pass the date range already selected by the user to the calendar itself, and he can edit it. In your example you use DayComponentsRange to represent date ranges, this is very convenient and works for me. But I can't initialize it with Date objects. So I'll just make initializer public

How Has This Been Tested

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@bryankeller
Copy link
Contributor

bryankeller commented Jan 28, 2024

Hi @IvanUshakov, there's no need to create this type yourself - its initializer is intentionally private.

What exactly is your use case? There are no parts of the public API of HorizonCalendar that require you to pass in these types, so I'd like to better understand what you're trying to do.

My recommendation is to convert DayComponents and DayRangeComponents to Foundation Dates, using the correct Foundation Calendar instance.

@IvanUshakov
Copy link
Author

In the example there is a class DayRangeSelectionTracker which shows well how you can select a range of dates. SwiftUIScreenDemo and DayRangeSelectionTracker uses DayComponentsRange for this. I need to pass the current selected dates by the user to a class similar to SwiftUIScreenDemo, but I cannot do because the initializer is declared as internal. Of course I can use system types for this, but it's not as easy as with DayComponentsRange. And if the user has to use system types, then perhaps the example should also use system types. Because in a large number of cases the user will edit the selected dates, and not just select them from scratch.

Сode is not correct, but it shows the idea:

struct AppCalendarView: View {
    @State private var selectedDayRange: DayComponentsRange?
    @Binding private var selectedRange: ClosedRange<Date>?

    init(selectedRange: Binding<ClosedRange<Date>?>) {
        _selectedRange = selectedRange
        if let selectedRangeBinding = selectedRangeBinding?.wrappedValue {
            self.selectedDayRange = .init(containing: selectedRangeBinding, in: calendar)
        }
        ...
    }
    ...
}

@IvanUshakov
Copy link
Author

@bryankeller have you any updates on this?

@varyamereon
Copy link

@bryankeller Was there an issue with this pull request? Seems it's been waiting to merge for a while and this would be very useful for me!!

@keepfocused
Copy link

keepfocused commented Oct 17, 2025

Hi @IvanUshakov, there's no need to create this type yourself - its initializer is intentionally private.

What exactly is your use case? There are no parts of the public API of HorizonCalendar that require you to pass in these types, so I'd like to better understand what you're trying to do.

My recommendation is to convert DayComponents and DayRangeComponents to Foundation Dates, using the correct Foundation Calendar instance.

My use case for this:
I need to restore state of app if user did selected dates then goes away from calendar screen or even closes the app and then comes back to calendar view.
So I need to be able to open horizon calendar with already selected range of dates.

For this I need to be able to reinitialise DayComponentsRange and parts of this MonthComponents and DayComponents and set them back to calendar.
Screenshot 2025-10-17 at 14 14 34

Or worst solution I need to be able to save to UserDefaults this DayComponentsRange struct but for this is need to conform encodable protocol and this is also impossible due to isInGregorianCalendar' is inaccessible due to 'internal' protection level

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

Successfully merging this pull request may close these issues.

4 participants