KVKCalendar

repository·master·Indexed 21 days ago

https://github.com/kvyatkovskys/kvkcalendar

A highly customizable calendar library for iOS and macOS featuring five modules: day, week, month, year, and event list. It supports time zone switching, dark mode, and custom event/cell views. The library can be integrated via CocoaPods or Swift Package Manager and is compatible with UIKit and SwiftUI.

Tokens
2.3K
Snippets
5
Records
5
Agent score
24%

What's inside KVKCalendar

  1. Install KVKCalendar via CocoaPods or Swift Package Manager

    master

    KVKCalendar can be integrated into your project using either CocoaPods or Swift Package Manager.

    CocoaPods

    Add the following line to your Podfile:

    Swift Package Manager

    1. In Xcode, navigate to FileSwift PackagesAdd Package Dependency...
    2. Paste the repository URL: https://github.com/kvyatkovskys/KVKCalendar
    3. Select Version (Up to Next Major) for the dependency rule.
    4. Click Finish.
    pod 'KVKCalendar'
  2. Customize Event Views and Date Cells

    master

    KVKCalendar allows deep customization of its UI components through the CalendarDataSource protocol.

    Custom Event Views

    To provide a custom view for specific events, subclass EventViewGeneral and implement the willDisplayEventView(_:frame:date:) method in your data source.

    Custom Date Cells

    To customize date cells (for Day, Week, Month, or Year views) or list cells, implement the dequeueCell<T>(parameter:type:view:indexPath:) method in your data source. This method allows you to dequeue and configure custom cells from UICollectionView or UITableView.

    // Custom Event View
    class CustomViewEvent: EventViewGeneral {
        override init(style: Style, event: Event, frame: CGRect) {
            super.init(style: style, event: event, frame: frame)
        }
    }
    
    // In your CalendarDataSource implementation
    func willDisplayEventView(_ event: Event, frame: CGRect, date: Date?) -> EventViewGeneral? {
        if event.ID == targetID {
            return CustomViewEvent(style: style, event: event, frame: frame)
        }
        return nil
    }
    
    // Custom Cell Dequeuing
    func dequeueCell<T>(parameter: CellParameter, type: CalendarType, view: T, indexPath: IndexPath) -> KVKCalendarCellProtocol? where T: UIScrollView {
        switch type {
        case .year:
            return (view as? UICollectionView)?.dequeueCell(indexPath: indexPath) { (cell: CustomYearCell) in
                // configure cell
            }
        case .day, .week, .month:
            return (view as? UICollectionView)?.dequeueCell(indexPath: indexPath) { (cell: CustomDayCell) in
                // configure cell
            }
        case .list:
            return (view as? UITableView)?.dequeueCell { (cell: CustomListCell) in
                // configure cell
            }
        }
    }
  3. Implement KVKCalendar in UIKit

    master

    To use KVKCalendar in a UIKit application, follow these steps:

    1. Import the module: import KVKCalendar.
    2. Initialize a CalendarView.
    3. Implement CalendarDataSource: This protocol requires you to provide the events for the calendar. You must implement eventsForCalendar(systemEvents:) to return an array of [Event].
    4. Implement CalendarDelegate: Use this to handle user interactions and control calendar behavior.
    5. Add to view hierarchy: Add the CalendarView instance to your view controller and set up constraints.

    Note: If you want to include iOS system calendar events, set the systemCalendars property in your Style object to include the desired calendar names.

    import KVKCalendar
    
    final class KVKCalendarVC: UIViewController, CalendarDataSource, CalendarDelegate {
        var events = [Event]()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let calendar = CalendarView()
            calendar.dataSource = self
            calendar.delegate = self
            view.addSubview(calendar)
            
            // Setup constraints...
            
            // Load and reload data
            createEvents { [weak self] (events) in
                self?.events = events
                calendar.reloadData()
            }
        }
    
        func eventsForCalendar(systemEvents: [EKEvent]) -> [Event] {
            let mappedEvents = systemEvents.compactMap { Event(event: $0) }
            return events + mappedEvents
        }
    }
  4. Use KVKCalendar in SwiftUI

    master

    Since KVKCalendar is a UIKit-based library, you must wrap it using UIViewControllerRepresentable to use it within a SwiftUI view hierarchy.

    1. Create a struct that conforms to UIViewControllerRepresentable.
    2. Implement makeUIViewController to return your configured UIViewController containing the CalendarView.
    3. Implement updateUIViewController (can be empty if no updates are needed).
    4. Use the wrapper struct in your SwiftUI body.
    import SwiftUI
    
    private struct KVKCalendarWrapper: UIViewControllerRepresentable {
        func makeUIViewController(context: Context) -> some UIViewController {
            return KVKCalendarVC() // Your custom UIViewController
        }
        
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
    }
    
    struct CalendarContentView: View {
        var body: some View {
            NavigationStack {
                KVKCalendarWrapper()
            }
        }
    }
  5. Configure Calendar with Style

    master

    The Style struct is used to customize the appearance and behavior of the calendar. You can pass a Style object to the CalendarView initializer.

    Key properties include:

    • event: EventStyle for event appearance.
    • timeline: TimelineStyle for timeline appearance.
    • week: WeekStyle for week view appearance.
    • allDay: AllDayStyle for all-day event appearance.
    • headerScroll: HeaderScrollStyle.
    • month: MonthStyle.
    • year: YearStyle.
    • list: ListViewStyle.
    • locale: Locale (defaults to .current).
    • calendar: Calendar (defaults to .current).
    • timezone: TimeZone (defaults to .current).
    • defaultType: CalendarType?.
    • timeHourSystem: TimeHourSystem (e.g., .twentyFourHour).
    • startWeekDay: StartDayType (e.g., .monday).
    • followInSystemTheme: Bool (enables dark/light mode following).
    • systemCalendars: Set<String> (names of iOS system calendars to display).
    public struct Style {
        public var event = EventStyle()
        public var timeline = TimelineStyle()
        public var week = WeekStyle()
        public var allDay = AllDayStyle()
        public var headerScroll = HeaderScrollStyle()
        public var month = MonthStyle()
        public var year = YearStyle()
        public var list = ListViewStyle()
        public var locale = Locale.current
        public var calendar = Calendar.current
        public var timezone = TimeZone.current
        public var defaultType: CalendarType?
        public var timeHourSystem: TimeHourSystem = .twentyFourHour
        public var startWeekDay: StartDayType = .monday
        public var followInSystemTheme: Bool = false 
        public var systemCalendars: Set<String> = []
    }