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

[WIP] Custom event views support #274

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions CalendarKitDemo/CalendarApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
391B6FA72541611500A49621 /* MyEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 391B6FA62541611500A49621 /* MyEvent.swift */; };
397054E0253D390E00C33B44 /* MyEventView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 397054DF253D390E00C33B44 /* MyEventView.xib */; };
397054E2253D394100C33B44 /* MyEventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 397054E1253D394100C33B44 /* MyEventView.swift */; };
75776AF58133F6BBDDCF1CE7 /* Pods_CalendarApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A223D8E39EB8C7A25F4E823A /* Pods_CalendarApp.framework */; };
791ACF5023FC887D00B2D602 /* TimeChunk+DateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791ACF4F23FC887D00B2D602 /* TimeChunk+DateComponents.swift */; };
7924834D23CB7B58003E3D27 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7924834C23CB7B58003E3D27 /* Assets.xcassets */; };
Expand All @@ -19,6 +22,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
391B6FA62541611500A49621 /* MyEvent.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = MyEvent.swift; sourceTree = "<group>"; };
397054DF253D390E00C33B44 /* MyEventView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MyEventView.xib; sourceTree = "<group>"; };
397054E1253D394100C33B44 /* MyEventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyEventView.swift; sourceTree = "<group>"; };
786F0C3439DCA027A1D04664 /* Pods-CalendarApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CalendarApp.release.xcconfig"; path = "Target Support Files/Pods-CalendarApp/Pods-CalendarApp.release.xcconfig"; sourceTree = "<group>"; };
791ACF4F23FC887D00B2D602 /* TimeChunk+DateComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TimeChunk+DateComponents.swift"; sourceTree = "<group>"; };
7924834023CB7B57003E3D27 /* CalendarApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CalendarApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -46,6 +52,16 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
397054DE253D38F000C33B44 /* CustomEventView */ = {
isa = PBXGroup;
children = (
397054DF253D390E00C33B44 /* MyEventView.xib */,
397054E1253D394100C33B44 /* MyEventView.swift */,
391B6FA62541611500A49621 /* MyEvent.swift */,
);
path = CustomEventView;
sourceTree = "<group>";
};
57CFFDE58D70A1545DA52F6E /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -76,6 +92,7 @@
7924834223CB7B57003E3D27 /* CalendarApp */ = {
isa = PBXGroup;
children = (
397054DE253D38F000C33B44 /* CustomEventView */,
7924836523CB7BFE003E3D27 /* AppDelegate.swift */,
791ACF4F23FC887D00B2D602 /* TimeChunk+DateComponents.swift */,
7924836623CB7BFE003E3D27 /* CustomCalendarExampleController.swift */,
Expand Down Expand Up @@ -159,6 +176,7 @@
buildActionMask = 2147483647;
files = (
7924835023CB7B58003E3D27 /* LaunchScreen.storyboard in Resources */,
397054E0253D390E00C33B44 /* MyEventView.xib in Resources */,
7924834D23CB7B58003E3D27 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -212,6 +230,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
391B6FA72541611500A49621 /* MyEvent.swift in Sources */,
397054E2253D394100C33B44 /* MyEventView.swift in Sources */,
7924836B23CB7BFE003E3D27 /* AppDelegate.swift in Sources */,
7924836E23CB7BFE003E3D27 /* ExampleNotificationController.swift in Sources */,
7924836C23CB7BFE003E3D27 /* CustomCalendarExampleController.swift in Sources */,
Expand Down Expand Up @@ -356,13 +376,14 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = H8S6FURL87;
INFOPLIST_FILE = CalendarApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = github.richardtop.CalendarApp;
PRODUCT_BUNDLE_IDENTIFIER = github.richardtop.CalendarApp1123;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
Expand All @@ -377,13 +398,14 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = H8S6FURL87;
INFOPLIST_FILE = CalendarApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = github.richardtop.CalendarApp;
PRODUCT_BUNDLE_IDENTIFIER = github.richardtop.CalendarApp1123;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
42 changes: 32 additions & 10 deletions CalendarKitDemo/CalendarApp/CustomCalendarExampleController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import UIKit
import CalendarKit
import DateToolsSwift

class CustomCalendarExampleController: DayViewController, DatePickerControllerDelegate {

class CustomCalendarExampleController: DayViewController, DatePickerControllerDelegate, TimelineViewAppearance {
var data = [["Breakfast at Tiffany's",
"New York, 5th avenue"],

Expand Down Expand Up @@ -56,6 +55,7 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe
override func loadView() {
calendar = customCalendar
dayView = DayView(calendar: calendar)
dayView.timelinePagerView.timelineViewAppearance = self
view = dayView
}

Expand All @@ -68,6 +68,7 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe
action: #selector(ExampleController.presentDatePicker))
navigationController?.navigationBar.isTranslucent = false
dayView.autoScrollToFirstEvent = true
dayView.timelinePagerView.timelineViewAppearance = self
reloadData()
}

Expand Down Expand Up @@ -125,10 +126,15 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe

private func generateEventsForDate(_ date: Date) -> [EventDescriptor] {
var workingDate = date.add(TimeChunk.dateComponents(hours: Int(arc4random_uniform(10) + 5)))
var events = [Event]()
var events = [EventDescriptor]()

for i in 0...4 {
let event = Event()
let event: EventDescriptor
if (Bool.random()) {
event = Event()
} else {
event = MyEvent()
}
let duration = Int(arc4random_uniform(160) + 60)
let datePeriod = TimePeriod(beginning: workingDate,
chunk: TimeChunk.dateComponents(minutes: duration))
Expand Down Expand Up @@ -159,7 +165,7 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe

let nextOffset = Int(arc4random_uniform(250) + 40)
workingDate = workingDate.add(TimeChunk.dateComponents(minutes: nextOffset))
event.userInfo = String(i)
// event.userInfo = String(i)
}

print("Events for \(date)")
Expand All @@ -171,6 +177,16 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe
baseColor.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
return UIColor(hue: h, saturation: s * 0.3, brightness: b, alpha: a)
}

// MARK: TimelineViewAppearance

func timelineView(_ timeloneView: TimelineView, viewFor event: EventDescriptor) -> EventView {
if (event is MyEvent) {
return MyEventView()
} else {
return DefaultEventView()
}
}

// MARK: DayViewDelegate

Expand All @@ -184,11 +200,12 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe
}

override func dayViewDidLongPressEventView(_ eventView: EventView) {
guard let descriptor = eventView.descriptor as? Event else {
return
}
let descriptor = eventView.descriptor!
// guard let descriptor = eventView.descriptor as? Event else { // TODO: for what puproses it was here?
// return
// }
endEventEditing()
print("Event has been longPressed: \(descriptor) \(String(describing: descriptor.userInfo))")
print("Event has been longPressed: \(descriptor) \(String(describing: descriptor.text/*.userInfo*/))") // TODO: userInfo not works now
beginEditing(event: descriptor, animated: true)
print(Date())
}
Expand Down Expand Up @@ -223,7 +240,12 @@ class CustomCalendarExampleController: DayViewController, DatePickerControllerDe
private func generateEventNearDate(_ date: Date) -> EventDescriptor {
let duration = Int(arc4random_uniform(160) + 60)
let startDate = date.subtract(TimeChunk.dateComponents(minutes: Int(CGFloat(duration) / 2)))
let event = Event()
var event: EventDescriptor
if (Bool.random()) {
event = Event()
} else {
event = MyEvent()
}
let datePeriod = TimePeriod(beginning: startDate,
chunk: TimeChunk.dateComponents(minutes: duration))
event.startDate = datePeriod.beginning!
Expand Down
73 changes: 73 additions & 0 deletions CalendarKitDemo/CalendarApp/CustomEventView/MyEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// MyEvent.swift
// CalendarApp
//
// Created by RareScrap on 22.10.2020.
// Copyright © 2020 Richard Topchii. All rights reserved.
//

import Foundation
import CalendarKit

class MyEvent: EventDescriptor {
public var myVar = "asdasd"
public var startDate = Date()
public var endDate = Date()
public var isAllDay = false
public var text = ""
public var attributedText: NSAttributedString?
public var color = SystemColors.systemBlue {
didSet {
updateColors()
}
}
public var backgroundColor = SystemColors.systemBlue.withAlphaComponent(0.3)
public var textColor = SystemColors.label
public var font = UIFont.boldSystemFont(ofSize: 12)
public var userInfo: Any?
public weak var editedEvent: EventDescriptor? {
didSet {
updateColors()
}
}

public init() {}

func makeEditable() -> Self {
let cloned = MyEvent()
cloned.startDate = startDate
cloned.endDate = endDate
cloned.isAllDay = isAllDay
cloned.text = text
cloned.attributedText = attributedText
cloned.color = color
cloned.backgroundColor = backgroundColor
cloned.textColor = textColor
cloned.userInfo = userInfo
cloned.editedEvent = self
return cloned as! Self
}

public func commitEditing() {
guard let edited = editedEvent else {return}
edited.startDate = startDate
edited.endDate = endDate
}

private func updateColors() {
(editedEvent != nil) ? applyEditingColors() : applyStandardColors()
}

private func applyStandardColors() {
backgroundColor = color.withAlphaComponent(0.3)
var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
color.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
textColor = UIColor(hue: h, saturation: s, brightness: b * 0.4, alpha: a)
}

private func applyEditingColors() {
backgroundColor = color
textColor = .white
}

}
32 changes: 32 additions & 0 deletions CalendarKitDemo/CalendarApp/CustomEventView/MyEventView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MyEventView.swift
// CalendarApp
//
// Created by RareScrap on 19.10.2020.
// Copyright © 2020 Richard Topchii. All rights reserved.
//

import Foundation
import CalendarKit

class MyEventView: EventView {

override func configure() {
super.configure()

let v = Bundle.main.loadNibNamed("MyEventView", owner: self, options: nil)?.first as! UIView
self.addSubview(v)

v.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = v.topAnchor.constraint(equalTo: self.topAnchor)
let verticalConstraint = v.bottomAnchor.constraint(equalTo: self.bottomAnchor)
let widthConstraint = v.trailingAnchor.constraint(equalTo: self.trailingAnchor)
let heightConstraint = v.leadingAnchor.constraint(equalTo: self.leadingAnchor)
self.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
}

override open func updateWithDescriptor(event: EventDescriptor) {
super.updateWithDescriptor(event: event)
subviews[2].backgroundColor = event.color // TODO: should we do this before super?
}
}
Loading