Code Monkey home page Code Monkey logo

horizoncalendar's Introduction

HorizonCalendar

A declarative and performant calendar UI component that supports use cases ranging from simple date pickers all the way up to fully-featured calendar apps.

Swift Package Manager compatible Carthage compatible Version License Platform Swift Swift Package Manager compatible

Introduction

HorizonCalendar is a declarative and performant calendar UI component for iOS. It provides many customization points to support a diverse range of designs and use cases, and is used used to implement every calendar and date picker in the Airbnb iOS app.

Features:

  • SwiftUI and UIKit support
  • Vertical and horizontal month layouts
  • Paging for horizontal month layout
  • Right-to-left layout support
  • Declarative API that encourages unidirectional data flow for updating the content of the calendar
  • Supports displaying large (virtually-infinite) date ranges
  • Animated content updates
  • Customizable default views for days, month headers, and days of the week, and a month grid background
  • Specify custom views for individual days, month headers, and days of the week
  • Specify custom views to highlight date ranges
  • Specify custom views to overlay parts of the calendar, enabling features like tooltips
  • Specify custom views for month background decorations (colors, grids, etc.)
  • Specify custom views for day background decorations (colors, patterns, etc.)
  • A day selection handler to monitor when a day is tapped
  • A multi-day selection handler to monitor when multiple days are selected via a drag gesture
  • Customizable layout metrics
  • Pin the days-of-the-week row to the top
  • Show partial boundary months (exactly 2020-03-14 to 2020-04-20, for example)
  • Scroll to arbitrary dates and months, with or without animation
  • Robust accessibility support
  • Inset the content without affecting the scrollable region using layout margins
  • Separator below the days-of-the-week row
  • Supports all calendars from Foundation.Calendar (Gregorian, Japanese, Hebrew, etc.)
Search Stays Availability Calendar Wish List Experience Reservation Experience Host Calendar Management
Search Stay Availability Calendar Wish List Experience Reservation Experience Host Calendar Management

Table of Contents

Example App

An example app is available to showcase and enable you to test some of HorizonCalendar's features. It can be found in ./Example/HorizonCalendarExample.xcworkspace.

Note: Make sure to use the .xcworkspace file, and not the .xcodeproj file, as the latter does not have access to HorizonCalendar.framework.

Demos

The example app has several demo view controllers to try, with both vertical and horizontal layout variations:

Demo Picker

Single Day Selection

Vertical Horizontal
Single Day Selection Vertical Single Day Selection Horizontal

Day Range Selection

Vertical Horizontal
Day Range Selection Vertical Day Range Selection Horizontal

Selected Day Tooltip

Vertical Horizontal
Selected Day Tooltip Vertical Selected Day Tooltip Horizontal

Scroll to Day with Animation

Vertical Horizontal
Scroll to Day with Animation Vertical Scroll to Day with Animation Horizontal

Integration Tutorial

Requirements

  • Deployment target iOS 11.0+
  • Swift 5+
  • Xcode 10.2+

Installation

Swift Package Manager

To install HorizonCalendar using Swift Package Manager, add .package(name: "HorizonCalendar", url: "https://github.com/airbnb/HorizonCalendar.git", from: "1.0.0")," to your Package.swift, then follow the integration tutorial here.

Carthage

To install HorizonCalendar using Carthage, add github "airbnb/HorizonCalendar" to your Cartfile, then follow the integration tutorial here.

CocoaPods

To install HorizonCalendar using CocoaPods, add pod 'HorizonCalendar' to your Podfile, then follow the integration tutorial here.

Creating a calendar

Once you've installed HorizonCalendar into your project, getting a basic calendar working is just a few steps.

Basic Setup

Importing HorizonCalendar

At the top of the file where you'd like to use HorizonCalendar, import HorizonCalendar:

import HorizonCalendar 

Instantiating the view

SwiftUI

CalendarViewRepresentable is the SwiftUI view type that represents the calendar. Like other SwiftUI views, all customization is done through initializer parameters and modifiers. To create a basic calendar, you initialize a CalendarViewRepresentable with some initial data:

let calendar = Calendar.current

let startDate = calendar.date(from: DateComponents(year: 2020, month: 01, day: 01))!
let endDate = calendar.date(from: DateComponents(year: 2021, month: 12, day: 31))!

CalendarViewRepresentable(
  calendar: calendar,
  visibleDateRange: startDate...endDate,
  monthsLayout: .vertical(options: VerticalMonthsLayoutOptions()),
  dataDependency: nil)
UIKit

CalendarView is the UIView subclass that renders the calendar. All visual aspects of CalendarView are controlled through a single type - CalendarViewContent. To create a basic CalendarView, you initialize one with an initial CalendarViewContent:

let calendarView = CalendarView(initialContent: makeContent())
private func makeContent() -> CalendarViewContent {
  let calendar = Calendar.current

  let startDate = calendar.date(from: DateComponents(year: 2020, month: 01, day: 01))!
  let endDate = calendar.date(from: DateComponents(year: 2021, month: 12, day: 31))!

  return CalendarViewContent(
    calendar: calendar,
    visibleDateRange: startDate...endDate,
    monthsLayout: .vertical(options: VerticalMonthsLayoutOptions()))
}

At a minimum, CalendarViewContent must be initialized with a Calendar, a visible date range, and a months layout (either vertical or horizontal). The visible date range will be interpreted as a range of days using the Calendar instance passed in for the calendar parameter.

For this example, we're using a Gregorian calendar, a date range of 2020-01-01 to 2021-12-31, and a vertical months layout.

Make sure to add calendarView as a subview, then give it a valid frame either using Auto Layout or by manually setting its frame property. If you're using Auto Layout, note that CalendarView does not have an intrinsic content size.

view.addSubview(calendarView)

calendarView.translatesAutoresizingMaskIntoConstraints = false

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.layoutMarginsGuide.bottomAnchor),
])

At a minimum, you need to provide a Calendar, a visible date range, and a months layout (either vertical or horizontal). The visible date range will be interpreted as a range of days using the Calendar instance passed in for the calendar parameter.

For this example, we're using a Gregorian calendar, a date range of 2020-01-01 to 2021-12-31, and a vertical months layout.

Adding the view

Next, we'll add the calendar to the view hierarchy.

SwiftUI

Add your calendar to the view hierarchy like any other SwiftUI view. Since the calendar doesn't have an intrinsic content size, you'll need to use the frame modifier to tell SwiftUI that it should consume all vertical and horizontal space. Optionally, use the layoutMargins modifier to apply internal padding, and the normal SwiftUI padding modifier to apply some external padding from the parent's edges.

var body: some View {
  CalendarViewRepresentable(...)
    .layoutMargins(.init(top: 8, leading: 8, bottom: 8, trailing: 8))
    .padding(.horizontal, 16)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
}
UIKit

Add your calendar as a subview, then give it a valid frame either using Auto Layout or by manually setting its frame property. If you're using Auto Layout, note that CalendarView does not have an intrinsic content size.

view.addSubview(calendarView)

calendarView.translatesAutoresizingMaskIntoConstraints = false

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.layoutMarginsGuide.bottomAnchor),
])

At this point, building and running your app should result in something that looks like this:

Basic Calendar Screenshot

Customization

Providing a custom view for each day

HorizonCalendar comes with default views for month headers, day of week items, and day items. You can also provide custom views for each of these item types, enabling you to display whatever custom content makes sense for your app.

Let's start by customizing the view used for each day:

SwiftUI

Since all visual aspects of CalendarViewRepresentable are configured through modifiers, we'll use the days modifier to provide a custom view with a rounded border for each day in the calendar:

CalendarViewRepresentable(...)

  .days { day in
    Text("\(day.day)")
      .font(.system(size: 18))
      .foregroundColor(Color(UIColor.label))
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .overlay {
        RoundedRectangle(cornerRadius: 12)
          .stroke(Color(UIColor.systemBlue), lineWidth: 1)
      }
  }

To use a UIKit view, wrap it using UIViewRepresentable and return it from the same function.

Note

View-provider closures are invoked lazily as parts of the calendar come into view. If you read any view state in any of your view-provider closures, make sure you capture it explicitly using a capture list. If you don't, SwiftUI will fail to identify that state as a dependency of your view unless it was read during the initial body evaluation of your view. This will lead to missed updates when your state changes.

UIKit

Since all visual aspects of CalendarView are configured through CalendarViewContent, we'll expand on our makeContent function. Let's start by providing a custom view for each day in the calendar:

private func makeContent() -> CalendarViewContent {
  return CalendarViewContent(...)
    .dayItemProvider { day in
      // Return a `CalendarItemModel` representing the view for each day
    }
}

The dayItemProvider(_:) function on CalendarViewContent returns a new CalendarViewContent instance with the custom day item model provider configured. This function takes a single parameter - a provider closure that returns a CalendarItemModel for a given DayComponents.

CalendarItemModel is a type that abstracts away the creation and configuration of a view displayed in the calendar. It's generic over a ViewRepresentable type, which can be any type conforming to CalendarItemViewRepresentable. You can think of CalendarItemViewRepresentable as a blueprint for creating and updating instances of a particular type of view to be displayed in the calendar. For example, if we want to use a UILabel for our custom day view with a rounded border, we'll need to create a type that knows how to create and update that label. Here's a simple example:

import HorizonCalendar

struct DayLabel: CalendarItemViewRepresentable {

  /// Properties that are set once when we initialize the view.
  struct InvariantViewProperties: Hashable {
    let font: UIFont
    let textColor: UIColor
    let borderColor: 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.isUserInteractionEnabled = true
    label.layer.borderWidth = 1
    label.layer.borderColor = invariantViewProperties.borderColor.cgColor
    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)"
  }

}

CalendarItemViewRepresentable requires us to implement a static makeView function, which should create and return a view given a set of invariant view properties. We want our label to have a configurable font and text color, so we've made those configurable via the InvariantViewProperties type. In our makeView function, we use those invariant view properties to create and configure an instance of our label.

CalendarItemViewRepresentable also requires us to implement a static setContent function, which should update all data-dependent properties (like the day text) on the provided view.

Now that we have a type conforming to CalendarItemViewRepresentable, we can use it to create a CalendarItemModel to return from the day item model provider:

return CalendarViewContent(...)

  .dayItemProvider { day in
    DayLabel.calendarItemModel(
      invariantViewProperties: .init(
        font: .systemFont(ofSize: 18), 
        textColor: .label,
        borderColor: .systemBlue),
      content: .init(day: day))
  }

Using a SwiftUI view is even easier - simply initialize your SwiftUI view and call .calendarItemModel on it. There's no need to create a custom type conforming to CalendarItemViewRepresentable like we had to do with the UIKit example above, or have separate concepts for invariant and variant (content) view properties.

return CalendarViewContent(...)

  .dayItemProvider { day in
    Text("\(day.day)")
      .font(.system(size: 18))
      .foregroundColor(Color(UIColor.label))
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .overlay {
        RoundedRectangle(cornerRadius: 12)
          .stroke(Color(UIColor.systemBlue), lineWidth: 1)
      }
      .calendarItemModel
  }

Similar item-provider functions are available to customize the views used for month headers, day-of-the-week items, and more.

If you build and run your app, it should now look like this:

Custom Day Views Screenshot

Adjusting layout metrics

We can improve the layout of our current calendar by adding some additional spacing between individual days and months:

SwiftUI
CalendarViewRepresentable(...)
  .days { ... }

  .interMonthSpacing(24)
  .verticalDayMargin(8)
  .horizontalDayMargin(8)
UIKit
return CalendarViewContent(...)
  .dayItemProvider { ... }

  .interMonthSpacing(24)
  .verticalDayMargin(8)
  .horizontalDayMargin(8)

Just like when we configured a custom day view via the day provider, changes to layout metrics are also done through CalendarViewContent. interMonthSpacing(_:), verticalDayMargin(_:), and horizontalDayMargin(_:) each return a mutated CalendarViewContent with the corresponding layout metric value updated, enabling you to chain function calls together to produce a final content instance.

After building and running your app, you should see a much less cramped layout:

Custom Layout Metrics Screenshot

Adding a day range indicator

Day range indicators are useful for calendars that need to highlight not just individual days, but ranges of days. To do this, we can create a custom view that represents the entire highlighted region, and then provide that view to the calendar for day ranges that we care about.

First, we need to create our custom day range indicator view. This view is responsible for drawing the entire highlighted region for a particular day range, which can potentially span multiple weeks, months, or even years. We'll use UIKit and Core Graphics to implement this, but it can easily be done in SwiftUI as well:

import UIKit

final class DayRangeIndicatorView: UIView {

  private let indicatorColor: UIColor

  init(indicatorColor: UIColor) {
    self.indicatorColor = indicatorColor
    super.init(frame: .zero)
    backgroundColor = .clear
  }

  required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

  var framesOfDaysToHighlight = [CGRect]() {
    didSet {
      guard framesOfDaysToHighlight != oldValue else { return }
      setNeedsDisplay()
    }
  }

  override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    context?.setFillColor(indicatorColor.cgColor)

    // Get frames of day rows in the range
    var dayRowFrames = [CGRect]()
    var currentDayRowMinY: CGFloat?
    for dayFrame in framesOfDaysToHighlight {
      if dayFrame.minY != currentDayRowMinY {
        currentDayRowMinY = dayFrame.minY
        dayRowFrames.append(dayFrame)
      } else {
        let lastIndex = dayRowFrames.count - 1
        dayRowFrames[lastIndex] = dayRowFrames[lastIndex].union(dayFrame)
      }
    }

    // Draw rounded rectangles for each day row
    for dayRowFrame in dayRowFrames {
      let roundedRectanglePath = UIBezierPath(roundedRect: dayRowFrame, cornerRadius: 12)
      context?.addPath(roundedRectanglePath.cgPath)
      context?.fillPath()
    }
  }

}

Next, we need to create a ClosedRange<Date> that represents the day range for which we'd like to display our day range indicator view. The Dates in our range will be interpreted as DayComponentss using the Calendar instance that we used when initially setting up our calendar.

let lowerDate = calendar.date(from: DateComponents(year: 2020, month: 01, day: 20))!
let upperDate = calendar.date(from: DateComponents(year: 2020, month: 02, day: 07))!
let dateRangeToHighlight = lowerDate...upperDate
SwiftUI

Next, we'll use the dayRanges modifier on our CalendarViewRepresentable:

CalendarViewRepresentable(...)
  ...
  
  .dayRanges(for: [dateRangeToHighlight]) { dayRangeLayoutContext in 
    DayRangeIndicatorViewRepresentable(
      framesOfDaysToHighlight: dayRangeLayoutContext.daysAndFrames.map { $0.frame })
  }

For each day range derived from the Set<ClosedRange<Date>> passed into this modifier, our day range provider closure will be invoked with a context instance that contains all of the information needed for us to create a view to be used to highlight a particular day range. Since DayRangeIndicatorView is a UIView, we need to bridge it to SwiftUI using UIViewRepresentable:

struct DayRangeIndicatorViewRepresentable: UIViewRepresentable {

  let framesOfDaysToHighlight: [CGRect]

  func makeUIView(context: Context) -> DayRangeIndicatorView {
    DayRangeIndicatorView(indicatorColor: UIColor.systemBlue.withAlphaComponent(0.15))
  }

  func updateUIView(_ uiView: DayRangeIndicatorView, context: Context) {
    uiView.framesOfDaysToHighlight = framesOfDaysToHighlight
  }

}

Note

When wrapping a UIKit view in a UIViewRepresentable, there is no equivalent concept of invariant view properties; all customizable properties must be updated in updateUIView to prevent view-reuse issues.

UIKit

Next, we need to invoke the dayRangeItemProvider(for:_:) on our CalendarViewContent:

  return CalendarViewContent(...)
    ...
    
    .dayRangeItemProvider(for: [dateRangeToHighlight]) { dayRangeLayoutContext in 
      // Return a `CalendarItemModel` representing the view that highlights the entire day range
    }

For each day range derived from the Set<ClosedRange<Date>> passed into this function, our day range item model provider closure will be invoked with a context instance that contains all of the information needed for us to render a view to be used to highlight a particular day range. Here is an example implementation of such a view:

Next, we need a type that conforms to CalendarItemViewRepresentable that knows how to create and update instances of DayRangeIndicatorView. To make things easy, we can just make our view conform to this protocol:

import HorizonCalendar

extension DayRangeIndicatorView: CalendarItemViewRepresentable {

  struct InvariantViewProperties: Hashable {
    let indicatorColor: UIColor
  }

  struct Content: Equatable {
    let framesOfDaysToHighlight: [CGRect]
  }

  static func makeView(
    withInvariantViewProperties invariantViewProperties: InvariantViewProperties)
    -> DayRangeIndicatorView
  {
    DayRangeIndicatorView(indicatorColor: invariantViewProperties.indicatorColor)
  }

  static func setContent(_ content: Content, on view: DayRangeIndicatorView) {
    view.framesOfDaysToHighlight = content.framesOfDaysToHighlight
  }

}

Last, we need to return a CalendarItemModel representing our DayRangeIndicatorView from the day range item model provider closure:

  return CalendarViewContent(...)
    ...
    
    .dayRangeItemProvider(for: [dateRangeToHighlight]) { dayRangeLayoutContext in
      DayRangeIndicatorView.calendarItemModel(
        invariantViewProperties: .init(indicatorColor: UIColor.blue.withAlphaComponent(0.15)),
        content: .init(framesOfDaysToHighlight: dayRangeLayoutContext.daysAndFrames.map { $0.frame }))
    }

If you build and run the app, you should see a day range indicator view that highlights 2020-01-20 to 2020-02-07:

Day Range Indicator Screenshot

Adding grid lines

HorizonCalendar provides an API to add a decorative background behind each month. By using the included MonthGridBackgroundView, we can easily add grid lines to each of the months in the calendar:

SwiftUI
CalendarViewRepresentable(...)

  .monthBackgrounds { monthLayoutContext in
    MonthGridBackgroundViewRepresentable(
      framesOfDays: monthLayoutContext.daysAndFrames.map { $0.frame })
  }

Since MonthGridBackgroundView is a UIView, we need to bridge it to SwiftUI using UIViewRepresentable:

struct MonthGridBackgroundViewRepresentable: UIViewRepresentable {

  let framesOfDays: [CGRect]

  func makeUIView(context: Context) -> MonthGridBackgroundView {
    MonthGridBackgroundView(
      invariantViewProperties: .init(horizontalDayMargin: 8, verticalDayMargin: 8))
  }

  func updateUIView(_ uiView: MonthGridBackgroundView, context: Context) {
    uiView.framesOfDays = framesOfDays
  }

}

Note

When wrapping a UIKit view in a UIViewRepresentable, there is no equivalent concept of invariant view properties; all customizable properties must be updated in updateUIView to prevent view-reuse issues.

UIKit
return CalendarViewContent(...)
  
  .monthBackgroundItemProvider { monthLayoutContext in
    MonthGridBackgroundView.calendarItemModel(
      invariantViewProperties: .init(horizontalDayMargin: 8, verticalDayMargin: 8),
      content: .init(framesOfDays: monthLayoutContext.daysAndFrames.map { $0.frame }))
  }

The month background provider works similarly to the overlay provider and day range provider; for each month in the calendar, the provider closure will be invoked with a layout context. This layout context contains information about the size and positions of elements in the month. Using this information, you can draw grid lines, borders, backgrounds, and more.

Grid Background Screenshot

Responding to day selection

If you're building a date picker, you'll most likely need to respond to the user tapping on days in the calendar.

SwiftUI

In SwiftUI, responding to day selection is easy.

First, define a state property for the current selected date:

@State var selectedDate: Date?

Then, update the selected date using the onDaySelection modifier:

CalendarViewRepresentable(...)
  ...
  
  .onDaySelection { day in
    selectedDate = calendar.date(from: day.components)
  }

Last, return a different view in your day provider closure:

CalendarViewRepresentable(...)
  ...
  
  .days { [selectedDate] day in
    let date = calendar.date(from: day.components)
    let borderColor: UIColor = date == selectedDate ? .systemRed : .systemBlue
    
    Text("\(day.day)")
      .font(.system(size: 18))
      .foregroundColor(Color(UIColor.label))
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .overlay {
        RoundedRectangle(cornerRadius: 12)
          .stroke(Color(borderColor), lineWidth: 1)
      }
  }

Note

View-provider closures are invoked lazily as parts of the calendar come into view. If you read any view state in any of your view-provider closures, make sure you capture it explicitly using a capture list. If you don't, SwiftUI will fail to identify that state as a dependency of your view unless it was read during the initial body evaluation of your view. This will lead to missed updates when your state changes.

UIKit

In UIKit, provide a day selection handler closure by setting CalendarView's daySelectionHandler:

calendarView.daySelectionHandler = { [weak self] day in
  self?.selectedDate = calendar.date(from: day.components)
}
private var selectedDate: Date?

The day selection handler closure is invoked whenever a day in the calendar is selected. You're provided with a DayComponents instance for the day that was selected. If we want to highlight the selected day once its been tapped, we'll need to create a new CalendarViewContent with a day calendar item model that looks different for the selected day:

let selectedDay = self.selectedDay

return CalendarViewContent(...)
  ...

  .dayItemProvider { [selectedDate] day in
    let date = calendar.date(from: day.components)
    let borderColor: UIColor = date == selectedDate ? .systemRed : .systemBlue

    return DayLabel.calendarItemModel(
      invariantViewProperties: .init(
        font: .systemFont(ofSize: 18),
        textColor: .label,
        borderColor: borderColor),
      content: .init(day: day))
  }

Last, we'll change our day selection handler so that it not only stores the selected date, but also sets an updated content instance on calendarView:

calendarView.daySelectionHandler = { [weak self] day in
  guard let self else { return }

  selectedDate = calendar.date(from: day.components)

  let newContent = makeContent()
  calendarView.setContent(newContent)
}

After building and running the app, tapping days should cause them to turn blue:

Day Selection Screenshot

Technical Details

If you'd like to learn about how HorizonCalendar was implemented, check out the Technical Details document. It provides an overview of HorizonCalendar's architecture, along with information about why it's not implemented using UICollectionView.

Contributions

HorizonCalendar welcomes fixes, improvements, and feature additions. If you'd like to contribute, open a pull request with a detailed description of your changes.

As a rule of thumb, if you're proposing an API-breaking change or a change to existing functionality, consider proposing it by opening an issue, rather than a pull request; we'll use the issue as a public forum for discussing whether the proposal makes sense or not. See CONTRIBUTING for more details.

Authors

Bryan Keller

Maintainers

Bryan Keller

Bryn Bodayle

If you or your company has found HorizonCalendar to be useful, let us know!

License

HorizonCalendar is released under the Apache License 2.0. See LICENSE for details.

horizoncalendar's People

Contributors

andrewwilliams avatar bryankeller avatar calda avatar fdiaz avatar hxml16 avatar jun7680 avatar maxdesiatov avatar nosyjoe avatar passt0r avatar rafaelks avatar tadija avatar xingsuo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

horizoncalendar's Issues

Scrolling bug when calendar view extends outside of safe area

I have the calendar view extending to top and bottom edges of the screen:

NSLayoutConstraint.activate([
    calendarView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
    calendarView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
    calendarView.topAnchor.constraint(equalTo: view.topAnchor),
    calendarView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])

When I scroll to the end it glitches:

calendarView.scroll(
            toDayContaining: Date(),
            scrollPosition: .centered,
            animated: true)
scroll-glitch.mov

If I constrain the top and bottom to the layoutMarginsGuide I don't have this issue. I'd prefer to extend to the edges of the screen so that I get the content under the navigation bar and home indicator visual effect.

Incompatible with prefersLargeTitles

When added as the first subview (or only subview) to a view within a navigation controller that has the prefersLargeTitles property set to true there is an issue where the large title will show the large title but if you scroll down and back to the top the large title never comes back which is the usual behaviour.

This is reproducible using the demo app by adding the below to the viewDidLoad of the DemoPickerViewController:

navigationController?.navigationBar.prefersLargeTitles = true

Make sure it also works with tilted screens

If one turns on the other screen orientations for the device and run the example, this calendar is not rendering properly anymore. Make sure that those device orientations are supported.

Day of Week background

Is there a way to change the background color of the days of week section when those are pinned to the top of the calendar?

Bildschirmfoto 2021-01-11 um 10 07 47

Selection handler gets called multiple times?

Hello,

Thanks for making this awesome calendar open source.

I noticed that the selection handler gets called multiple times in a row once a date is selected and It's creating issues on our end because we are calling a delegate function. Is this normal behavior?

Partial visibility doesn't work correctly for < 2 months

If I select visibility range in partially visible mode like

let startDate = calendar.date(from: DateComponents(year: 2021, month: 02, day: 5))!
let endDate = calendar.date(from: DateComponents(year: 2021, month: 03, day: 15))!

the scrolling function stops working at all and part of the calendar just stucks behind bottom of the screen. The problem is reproducible in example project.

Here is a screenshot

Снимок экрана 2021-02-26 в 17 00 31

Request: Disable User Interaction For Scrolling

Hello everyone,

This calendar is still great, really loving the ease of use that comes with it! One request that I have for a specific use case is to disable user-interaction for scrolling. In this scenario, the only way to scroll the calendar would be via button press. Would it be possible to implement the option to disable this scrolling behavior? I should add that my calendar is oriented in the horizontal position.

Thanks!

Ability to configure scrolling behaviour

Hey,

first of all, thank you for open-sourcing this neat library!

Secondly, I wanted to ask you: would you consider adding some kind of ability to configure scrolling behaviour (perhaps to MonthsLayout enum via LayoutOptions)? Because I wanted to implement horizontal calendar with snapping like behaviour (where you have single month shown on the screen and then using swipe gesture or buttons move to next/previous month) and the only way how I managed to achieve that was by going into view hierarchy and disabling scroll on NoContentInsetAdjustmentScrollView which feels kinda hacky as it would stop working if at some point you decide to move scrollview somewhere else.

Also, I was curious: does HorizonCalendar support RTL layout? Because in order to achieve proper scrolling on RTL I had to change transform of CalendarView

Thanks

Check In and Check Out custom time for highlighting days

Hi Airbnb Team,

This is my second time asking question.

  1. Is it possible specify time ie. hours for check in and out highlighting as in screenshot.

Screen Shot 2021-01-29 at 10 58 04 AM

  1. Can I make bigger in size for check in guest images? Similar in screenshot! Overall is it even achievable as design in airbnb calendar?

Thank you for your time!

Multiple dates in a month are blanked out.

While leaving default code, certain dates in each month are blank. When selected they are show but otherwise they are blank... this issue does not occur to other unselected dates.

withMonthHeaderItemModelProvider working?

Am I doing something wrong?

Minor change to SingleDaySelectionDemoViewController as follows:

...
            .withHorizontalDayMargin(8)
            
            .withMonthHeaderItemModelProvider { [weak self] month in
                let textColor: UIColor
                if #available(iOS 13.0, *) {
                    textColor = .label
                } else {
                    textColor = .black
                }

                let monthAccessibilityText: String?
                monthAccessibilityText = nil
                
                return CalendarItemModel<DayView>(
                    invariantViewProperties: .init(textColor: textColor, isSelectedStyle: false),
                    viewModel: .init(dayText: "Month Header Here", dayAccessibilityText: monthAccessibilityText))
            }
            
            .withDayItemModelProvider { [weak self] day in
...

And result - no custom header ...

Screen Shot 2020-11-18 at 6 12 20 pm

Programmatically set dates selection

Hi, first of all, thank you for this great library.

Here is my question, I'm wondering how to programmatically set a selection on the calendar. I have a range of dates that I would like to convert to a DayRange. The initializers of DayRange, Day and Month are all internals and so not accessible outside of the library. Is there a reason for this ? If so how should I do ?

Thank you in advance, and again nice job.

[feat] Pagination for Horizontal Calendars

Hey Contributors, just wanted to say thanks!

I have actually already switched over to using HorizonCalendar in one of our applications that required a vertical calendar implementation and I must say, it is one of the easiest declarative calendar options available,. so much so that we're starting to move over another application over to it.

However, we require a horizontal calendar, with pagination.
I went and attempted to set isPagingEnabled to the underlying UIScrollView, but that definitely lead to some bizarre behavior (specifically around the width I believe), and the pagination isn't true.

Is there any hint at adding an official way to set pagination for horizontal month layouts?

Calendar not being refreshed when new events are fetched from the sever

First of all, thanks for this amazing library.

I've noticed the following issue when using the library, the data is not being refreshed properly after fetching data from the server. I am attaching screenshots at the bottom.

example:

  var events: [String: Any] = [:]

  var presenter: CalendarPresenterDelegate!

  override func viewDidLoad() {
      super.viewDidLoad()
      /// some networking logic
      presenter.fetchEvents()
  }

  override func makeContext() -> CalendarViewContent {

        let startDate = Date()
        let endDate = calendar.date(from: DateComponents(year: 2021, month: 12, day: 31))!

        return CalendarViewContent(calendar: calendar,
                                   visibleDateRange: startDate...endDate,
                                   monthsLayout: .horizontal(monthWidth: min(view.frame.height, view.frame.width) - 40))
            .withDayItemProvider { day -> CalendarItem<EventLabel, Day> in
                let date = self.calendar.date(from: day.components)!
                let isSelectedDay = self.selectedDates.contains(date)
                return CalendarItem<EventLabel, Day>(
                    viewModel: day,
                    styleID: isSelectedDay ? "SelectedDayLabelStyle" : "DayLabelStyle",
                    buildView: {
                        return EventLabel()
                    },
                    updateViewModel: self.setupEvent)
            }
            .withInterMonthSpacing(16)
            .withVerticalDayMargin(8)
            .withHorizontalDayMargin(8)
    }

    func setEvents(events: [String: Any]) {
        self.events = events
        let newContent = makeContent()
        calendarView.setContent(newContent)
    }

    override func setupEvent(label: EventLabel, day: Day) {

        label.text = "\(day.day)"
        // clear the events
        label.hasNoEvents()

        if isSelectedDay {
            label.backgroundColor = Colors.colorAccent
        } else {
            label.backgroundColor = isToday ? Colors.colorPrimary : .clear
        }

        guard let object = getEvents(date: date) else {
            return
        }

        if (object["event1"] as? Bool) == true {
            label.hasEvent1()
            label.textColor = .lightGray
            return
        }

        if (object["event2"] as? [String])?.isEmpty == false {
            label.hasEvent2()
        }
    }

This is what we get after setEvents being called, however the result we need is in the second image:
image 1
Screen Shot 2020-07-14 at 7 11 14 PM
image 2
Screen Shot 2020-07-14 at 7 11 00 PM

PS: if we scroll to September and back the UI refreshes and the UI updates properly. it seems as if the
calendarView.setContent(newContent) is not setting the content properly.

Question: Center Horizontal Calendar

Hey Guys,

Don't know if this belongs here, but I am having troubling finding a way to center the horizontal calendar, as opposed to aligning it to the left, as the default implementation suggests. In the same vein, I am also looking for the correct way to center the Month Title (August 2020) in the center of the screen, rather than aligning it to the left as well. Basically just trying to show only one month at a time with equal padding to the trailing and leading edge of the calendar, as well as center the Month Title. My apologies if these are already features and I am just missing them. I have attached two screenshots to demonstrate what I mean in regards to the alignment issue, with one being what I currently have and one being my desired outcome.

Edit: I did end up figuring out the centered header by working off the default implementation, but needed to basically copy to my source code the extended function "firstDate" since it was internally protected. Is there an easier way around this?

Thanks

Screen Shot 2020-08-08 at 5 04 35 PM

Screen Shot 2020-08-08 at 5 04 22 PM

Interactable MonthHeaderItem

Hi,

First of all thanks for the great work on this library it's beyond awesome.
Currently I'm trying to add two buttons to my header view but looks like whatever is added to the header isn't intractable, as such I'm unable to capture button taps.

Is this a limitation from the library or is there something to make it intractable ?

Thanks a bunch !

Support to iOS 10

Hi, would be possible to lower the iOS requirement to 10? I tried to do some changes in a fork but they are a bit massive because of the use of the iOS 11's layoutMargins.

withMonthHeaderItemModelProvider Full Month

Hello. I have the following code:

import SwiftUI
import HorizonCalendar

struct HorizonView: UIViewRepresentable {
    func makeUIView(context: Context) -> CalendarView {
        let calendarView = CalendarView(initialContent: makeContent())
        return calendarView
    }
    
    func updateUIView(_ uiView: CalendarView, context: UIViewRepresentableContext<HorizonView>) {}
    
    private func makeContent() -> CalendarViewContent {
        let calendar = Calendar.current
        let startDate = calendar.date(from: DateComponents(year: 2020, month: 01, day: 01))!
        let endDate = calendar.date(from: DateComponents(year: 2021, month: 12, day: 31))!
        return CalendarViewContent(
            calendar: calendar,
            visibleDateRange: startDate...endDate,
            monthsLayout: .vertical(options: VerticalMonthsLayoutOptions()))
        
            .withMonthHeaderItemModelProvider { month in
                    let textColor: UIColor
                    if #available(iOS 13.0, *) {
                      textColor = .label
                    } else {
                      textColor = .black
                    }

                    return CalendarItemModel<MonthLabel>(
                        invariantViewProperties: .init(font: UIFont.systemFont(ofSize: 12.0),
                                                       textColor: textColor,
                                                       backgroundColor: .clear),
                      viewModel: .init(month: month))
                 }
    }
}

struct MonthLabel: CalendarItemViewRepresentable {

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

  /// Properties that will vary depending on the particular date being displayed.
  struct ViewModel: Equatable {
    let month: Month
  }

  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

    return label
  }

  static func setViewModel(_ viewModel: ViewModel, on view: UILabel) {
    view.text = "\(viewModel.month)"
  }

}

For some reason my headerItem is being displayed as "2020-01" instead of "January 2020." Any help would be appreciated.

Simulator Screen Shot - iPhone 12 Pro - 2020-12-21 at 13 48 56

Block Out Dates on DayRangeSelection

Thanks for the super swifty code. Can you walk through an example on how to safely block out dates so they are not able to be selected for DayRange Selection?

Unwanted extra trailing spacing/insets

I'm having weird issue where calendar is insetted by 10ish pixels on right side by default. As you can see in picture below, highlighted view is calendarView's scrollView, and on right side after each row of items there is small spacing.

Only spacing related thing i'm changing is .withInterMonthSpacing(16). I have custom view for DayView but same issue happens if I just use default withDayItemModelProvider.

Also, CalendarView is added as subview and it has 4 constraints to superview (leading, trailing, top and bottom).
White spacing seen on sides is from superview's leading/trailing constraints so it shouldn't affect calendar. I've also tried removing that space but same issue appears.

Init with selected day

I'd like to have a CalendarView and a UIDatePicker on the same screen, and be able to initialize the CalendarView with a selected day already provided. Without the user having to select a date.

When trying to create a new Day object I get the error: "'Day' initializer is inaccessible due to 'internal' protection level". So, I tried adding a public init() {} to the Day object with some initial values, but it still is giving the error. The same error happens with the Month object.

I would just make a public init on the Day and Month objects and it would fix my problem, but it's still giving the error.

Fixed Day Header

Hello! Love the library so far! Have a quick question.

Is it possible to have a fixed day header? Not sure what they are exactly called but here's a picture of it.
Screen Shot 2020-07-12 at 7 19 01 PM

I haven't noticed there to be any item providers for them; or maybe I missed them??😅

Thanks a bunch and keep up the good work guys :)

Weekdays header and range bounds color

Hi Team,
Thanks for the great work.
I am trying to implement this calendar into my app but I am unable to change background color of weeks. Is it possible to change?
When I have multiple range selected can I keep beginning bound and ending bound color different?

Here is the screenshot.
Thank you!

IMG_0921

DayRangeIndicatorView not smooth scrolling

Hi Airbnb Team,
I faced with DayRangeIndicatorView problem. When user selects huge date range, for example multiple years, the scroll starts working not smooth. Along with this, there is a jump in RAM consumption. In some moment of scrolling area with selected date range a ton of messages appear in console and selection view disappears. I have the same problem with your demo project.

CoreAnimation: failed to allocate 38375488 bytes
CoreAnimation: failed to allocate 38375488 bytes
CoreAnimation: failed to allocate 38375488 bytes
CoreAnimation: failed to allocate 38375488 bytes

Below the screen recording

rpreplay-final1612440563_2viUKw1o.mp4

Horizontal Calendar Leading and Trailing sizes are different

Hi Airbnb Team,

Thank you for applying pagination to the calendar.

For some reason my calendar leading and trailing spaces are different. I want to use full screen width for horizontal calendar.

See the attached image.

IMG_0649

Thank you for the answer!

Week View

Hello,

Are there any plans to implement a week view in the near future?

DayView Custom Height

Would it be possible to have vertical calendar have custom height for day cells?

Currently, FrameProvider has equal width and height:

let numberOfDaysPerWeek = CGFloat(7)
let availableWidth = insetWidth - (content.horizontalDayMargin * (numberOfDaysPerWeek - 1))
let points = availableWidth / numberOfDaysPerWeek
daySize = CGSize(width: points, height: points)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.