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

daySelectionHandler doesn't work with custom .dayItemProvider #301

Open
kennyDang opened this issue Mar 7, 2024 · 0 comments
Open

daySelectionHandler doesn't work with custom .dayItemProvider #301

kennyDang opened this issue Mar 7, 2024 · 0 comments

Comments

@kennyDang
Copy link

kennyDang commented Mar 7, 2024

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)"
  }

}


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

1 participant