HorizonCalendar icon indicating copy to clipboard operation
HorizonCalendar copied to clipboard

Make DayComponentRange initializer public to support pre-selected date range in the calendar.

Open IvanUshakov opened this issue 1 year ago • 4 comments

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)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist

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

IvanUshakov avatar Jan 27 '24 18:01 IvanUshakov

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.

bryankeller avatar Jan 28 '24 23:01 bryankeller

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 avatar Jan 28 '24 23:01 IvanUshakov

@bryankeller have you any updates on this?

IvanUshakov avatar Feb 08 '24 19:02 IvanUshakov

@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!!

varyamereon avatar Aug 21 '24 17:08 varyamereon