ECWeekView

repository·master·Indexed 42 days ago

https://github.com/evancooper9/swift-week-view

An iOS calendar library for displaying events in a weekly view format. It features infinite horizontal scrolling, asynchronous data loading via the ECWeekViewDataSource protocol, and customizable styling through the ECWeekViewStyler protocol. Supports programmatic and Storyboard initialization, and user interaction handling via the ECWeekViewDelegate.

Tokens
1.3K
Snippets
5
Records
5
Agent score
3%

What's inside ECWeekView

  1. Initialize ECWeekView

    master

    You can initialize ECWeekView either programmatically or via Storyboard.

    Programmatic Initialization

    Create an instance by specifying a frame and the number of visible days.

    ECWeekView Arguments:

    • frame: The frame of the calendar view.
    • visibleDays: The number of days visible on one page (Default: 5).
    • date: (Optional) The initial date to load (Default: today).

    Storyboard Initialization

    1. Add a view to your storyboard.
    2. Set its class to ECWeekView.
    3. Assign the dataSource property programmatically in your ViewController.
    // Programmatic
    let weekView = ECWeekView(frame: frame, visibleDays: 5)
    weekView.dataSource = self
    addSubview(weekView)
    
    // Storyboard
    @IBOutlet weak var weekView: ECWeekView!
    weekView.dataSource = self
  2. Handle user interaction with ECWeekViewDelegate

    master

    To respond to user taps on events or empty spaces, implement the ECWeekViewDelegate protocol and assign it to the delegate property of your ECWeekView instance.

    Delegate Methods:

    • weekViewDidClickOnEvent(_:event:view:): Triggered when a specific calendar event is touched.
    • weekViewDidClickOnFreeTime(_:date:): Triggered when a space without an event is tapped.
    // Fires when a calendar event is touched on
    func weekViewDidClickOnEvent(_ weekView: ECWeekView, event: ECWeekViewEvent, view: UIView)
    
    // Fires when a space without an event is tapped
    func weekViewDidClickOnFreeTime(_ weekView: ECWeekView, date: DateInRegion)
  3. Implement the ECWeekViewDataSource protocol

    master

    To provide data to the calendar, implement the ECWeekViewDataSource protocol. You must implement the weekViewGenerateEvents function, which returns an array of ECWeekViewEvent objects for a given date.

    Asynchronous Loading: If events need to be fetched asynchronously, return any immediate events directly and use the eventCompletion closure to pass the loaded events later. The eventCompletion call will overwrite the previously returned events.

    ECWeekViewEvent Properties:

    • title: The title of the event.
    • subtitle: A subtitle or description.
    • start: The start time of the event.
    • end: The end time of the event.

    Note: Events currently rely on a 24-hour clock.

    func weekViewGenerateEvents(_ weekView: ECWeekView, date: DateInRegion, eventCompletion: @escaping ([ECWeekViewEvent]?) -> Void) -> [ECWeekViewEvent]? {
      let start: DateInRegion = date.dateBySet(hour: 12, min: 0, secs: 0)!
      let end: DateInRegion = date.dateBySet(hour: 13, min: 0, secs: 0)!
      let event: ECWeekViewEvent = ECWeekViewEvent(title: "Lunch", start: start, end: end)
    
      DispatchQueue.global(.background).async {
        // do some async work & create events...
        eventCompletion([event, ...])
      }
    
      return [event]
    }
  4. Apply custom styling with ECWeekViewStyler

    master

    To customize the appearance of events and headers, implement the ECWeekViewStyler protocol and assign it to the styler property. By default, ECWeekView uses its own internal styler.

    Styler Methods:

    • weekViewStylerECEventView(_:eventContainer:event:): Returns a custom UIView to represent an event.
    • weekViewStylerHeaderView(_:with:in:): Returns a custom UIView for the day header (e.g., containing the date information).
    // Creates the view for an event
    func weekViewStylerECEventView(_ weekView: ECWeekView, eventContainer: CGRect, event: ECWeekViewEvent) -> UIView
    
    // Create the header view for the day in the calendar.
    func weekViewStylerHeaderView(_ weekView: ECWeekView, with date: DateInRegion, in cell: UICollectionViewCell) -> UIView