HorizonCalendar icon indicating copy to clipboard operation
HorizonCalendar copied to clipboard

daySelectionHandler doesn't work with custom .dayItemProvider

Open kennyDang opened this issue 11 months ago • 0 comments

Following the example in the README, I can't seem to get the selected day when using a custom dayItemProvider view.

Here is the full code:

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let calendarView = CalendarView(initialContent: makeContent())
        calendarView.backgroundColor = .black
        calendarView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(calendarView)

        NSLayoutConstraint.activate([
          calendarView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
          calendarView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
          calendarView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
          calendarView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -(view.bounds.height * 0.3)),
        ])
        
        calendarView.daySelectionHandler = { [weak self] day in
            print("day \(day)")
        }
    }


    private func makeContent() -> CalendarViewContent {
        let calendar = Calendar.current
        let date = Date()
        let components = calendar.dateComponents([.year, .month, .day], from: date)
        let startDate = calendar.date(from: components)!
        
        return CalendarViewContent(
            calendar: calendar,
            visibleDateRange: startDate...startDate,
            monthsLayout:
                .vertical(options: VerticalMonthsLayoutOptions()))
                .dayItemProvider { day in
                    var invariantViewProperties = ExampleView.InvariantViewProperties(
                        font: UIFont.systemFont(ofSize: 18),
                        textColor: .darkGray,
                        backgroundColor: .clear)
                      
                      return ExampleView.calendarItemModel(
                        invariantViewProperties: invariantViewProperties,
                        content: .init(day: day))
                }
    }
}
struct ExampleView: CalendarItemViewRepresentable {

  /// Properties that are set once when we initialize the view.
  struct InvariantViewProperties: Hashable {
    let font: UIFont
    let textColor: UIColor
    let backgroundColor: UIColor
  }

  /// Properties that will vary depending on the particular date being displayed.
  struct Content: Equatable {
    let day: DayComponents
  }

  static func makeView(
    withInvariantViewProperties invariantViewProperties: InvariantViewProperties)
    -> UILabel
  {
    let label = UILabel()

    label.backgroundColor = invariantViewProperties.backgroundColor
    label.font = invariantViewProperties.font
    label.textColor = invariantViewProperties.textColor

    label.textAlignment = .center
    label.clipsToBounds = true
    label.layer.cornerRadius = 12
    
    return label
  }

  static func setContent(_ content: Content, on view: UILabel) {
    view.text = "\(content.day.day)"
  }

}


kennyDang avatar Mar 07 '24 05:03 kennyDang