Skip to content

Commit

Permalink
re-add YMEventStandardView
Browse files Browse the repository at this point in the history
  • Loading branch information
matsune committed Jan 13, 2019
1 parent 96781c1 commit 7272a70
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 119 deletions.
24 changes: 16 additions & 8 deletions YMCalendar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
objects = {

/* Begin PBXBuildFile section */
13273C3621EAE47B00DE8D7E /* ReusableObjectQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13273C3421EAE47B00DE8D7E /* ReusableObjectQueue.swift */; };
13273C3721EAE47B00DE8D7E /* ReusableObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13273C3521EAE47B00DE8D7E /* ReusableObject.swift */; };
13273C3A21EAE48F00DE8D7E /* YMEventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13273C3821EAE48F00DE8D7E /* YMEventView.swift */; };
13273C3B21EAE48F00DE8D7E /* YMEventStandardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13273C3921EAE48F00DE8D7E /* YMEventStandardView.swift */; };
1334000B1EB339DE007FE17E /* IndexableDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133400091EB339DE007FE17E /* IndexableDictionary.swift */; };
1334000C1EB339DE007FE17E /* IndexableDictionary+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1334000A1EB339DE007FE17E /* IndexableDictionary+Description.swift */; };
1345C7161EBD87790076FF31 /* YMDayLabelAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1345C7151EBD87790076FF31 /* YMDayLabelAlignment.swift */; };
135586BB20613ABB00897CE8 /* MonthRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586BA20613ABA00897CE8 /* MonthRange.swift */; };
135586BD20613AD000897CE8 /* MonthDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586BC20613AD000897CE8 /* MonthDate.swift */; };
135586BF2061710F00897CE8 /* Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586BE2061710F00897CE8 /* Style.swift */; };
135586C12061713700897CE8 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586C02061713700897CE8 /* UIView.swift */; };
135586C42062174900897CE8 /* ExtensionCompatible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586C32062174900897CE8 /* ExtensionCompatible.swift */; };
135586C72062179A00897CE8 /* UICollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586C62062179A00897CE8 /* UICollectionView.swift */; };
135586C9206217F100897CE8 /* UICollectionReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135586C8206217F100897CE8 /* UICollectionReusableView.swift */; };
Expand Down Expand Up @@ -51,13 +53,15 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
13273C3421EAE47B00DE8D7E /* ReusableObjectQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusableObjectQueue.swift; sourceTree = "<group>"; };
13273C3521EAE47B00DE8D7E /* ReusableObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusableObject.swift; sourceTree = "<group>"; };
13273C3821EAE48F00DE8D7E /* YMEventView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YMEventView.swift; sourceTree = "<group>"; };
13273C3921EAE48F00DE8D7E /* YMEventStandardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YMEventStandardView.swift; sourceTree = "<group>"; };
133400091EB339DE007FE17E /* IndexableDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexableDictionary.swift; sourceTree = "<group>"; };
1334000A1EB339DE007FE17E /* IndexableDictionary+Description.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "IndexableDictionary+Description.swift"; sourceTree = "<group>"; };
1345C7151EBD87790076FF31 /* YMDayLabelAlignment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YMDayLabelAlignment.swift; sourceTree = "<group>"; };
135586BA20613ABA00897CE8 /* MonthRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonthRange.swift; sourceTree = "<group>"; };
135586BC20613AD000897CE8 /* MonthDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonthDate.swift; sourceTree = "<group>"; };
135586BE2061710F00897CE8 /* Style.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Style.swift; sourceTree = "<group>"; };
135586C02061713700897CE8 /* UIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = "<group>"; };
135586C32062174900897CE8 /* ExtensionCompatible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionCompatible.swift; sourceTree = "<group>"; };
135586C62062179A00897CE8 /* UICollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UICollectionView.swift; sourceTree = "<group>"; };
135586C8206217F100897CE8 /* UICollectionReusableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UICollectionReusableView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -172,7 +176,6 @@
135586C52062178C00897CE8 /* UIKit */ = {
isa = PBXGroup;
children = (
135586C02061713700897CE8 /* UIView.swift */,
135586C62062179A00897CE8 /* UICollectionView.swift */,
135586C8206217F100897CE8 /* UICollectionReusableView.swift */,
);
Expand Down Expand Up @@ -225,7 +228,6 @@
135586BA20613ABA00897CE8 /* MonthRange.swift */,
135586BC20613AD000897CE8 /* MonthDate.swift */,
13A518F01E72EA3100F539ED /* DateRange.swift */,
135586BE2061710F00897CE8 /* Style.swift */,
133400081EB339CF007FE17E /* IndexableDictionry */,
);
path = Utils;
Expand Down Expand Up @@ -279,7 +281,11 @@
13A5192E1E72EF6900F539ED /* EventView */ = {
isa = PBXGroup;
children = (
13273C3521EAE47B00DE8D7E /* ReusableObject.swift */,
13273C3421EAE47B00DE8D7E /* ReusableObjectQueue.swift */,
13A518F21E72EA3100F539ED /* YMEventsRowView.swift */,
13273C3921EAE48F00DE8D7E /* YMEventStandardView.swift */,
13273C3821EAE48F00DE8D7E /* YMEventView.swift */,
);
path = EventView;
sourceTree = "<group>";
Expand Down Expand Up @@ -451,10 +457,12 @@
buildActionMask = 2147483647;
files = (
13FCCEE21E90DD460070B038 /* YMCalendarEKViewController.swift in Sources */,
13273C3721EAE47B00DE8D7E /* ReusableObject.swift in Sources */,
135586C42062174900897CE8 /* ExtensionCompatible.swift in Sources */,
13A519101E72EA3100F539ED /* YMCalendarLayoutDataSource.swift in Sources */,
135586CC20621C4E00897CE8 /* Calendar.swift in Sources */,
13A519121E72EA3100F539ED /* YMCalendarView.swift in Sources */,
13273C3621EAE47B00DE8D7E /* ReusableObjectQueue.swift in Sources */,
13AAAAF41EBC1CFD002C8AB5 /* YMSelectAnimation.swift in Sources */,
135586BB20613ABB00897CE8 /* MonthRange.swift in Sources */,
13A519151E72EA3100F539ED /* YMCalendarDelegate.swift in Sources */,
Expand All @@ -467,15 +475,15 @@
13A519191E72EA3100F539ED /* YMScrollDirection.swift in Sources */,
13B222661E85F0FB00BFF183 /* YMCalendarWeekBarView.swift in Sources */,
13A5190F1E72EA3100F539ED /* YMCalendarLayout.swift in Sources */,
13273C3A21EAE48F00DE8D7E /* YMEventView.swift in Sources */,
135586C9206217F100897CE8 /* UICollectionReusableView.swift in Sources */,
13A519141E72EA3100F539ED /* YMCalendarDataSource.swift in Sources */,
13A519071E72EA3100F539ED /* DateRange.swift in Sources */,
13A519311E72F0B100F539ED /* YMEventsRowViewDelegate.swift in Sources */,
1334000C1EB339DE007FE17E /* IndexableDictionary+Description.swift in Sources */,
13A5190E1E72EA3100F539ED /* YMMonthDayCollectionCell.swift in Sources */,
135586BF2061710F00897CE8 /* Style.swift in Sources */,
135586C72062179A00897CE8 /* UICollectionView.swift in Sources */,
135586C12061713700897CE8 /* UIView.swift in Sources */,
13273C3B21EAE48F00DE8D7E /* YMEventStandardView.swift in Sources */,
1334000B1EB339DE007FE17E /* IndexableDictionary.swift in Sources */,
1345C7161EBD87790076FF31 /* YMDayLabelAlignment.swift in Sources */,
13FCCEEB1E9135B30070B038 /* YMCalendarViewController.swift in Sources */,
Expand Down
25 changes: 0 additions & 25 deletions YMCalendar/Extension/UIKit/UIView.swift

This file was deleted.

20 changes: 20 additions & 0 deletions YMCalendar/Month/Cell/EventView/ReusableObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ReusableObject.swift
// YMCalendar
//
// Created by Yuma Matsune on 2017/03/07.
// Copyright © 2017年 Yuma Matsune. All rights reserved.
//

import Foundation

/// ReusableObject will be reused for many times, such as UICollectionViewCell.
/// This object can be registared and dequeued.
public protocol ReusableObject: class {

init()

var reuseIdentifier: String { get set }

func prepareForReuse()
}
56 changes: 56 additions & 0 deletions YMCalendar/Month/Cell/EventView/ReusableObjectQueue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ReusableObjectQueue.swift
// YMCalendar
//
// Created by Yuma Matsune on 2017/04/28.
// Copyright © 2017年 Yuma Matsune. All rights reserved.
//

import Foundation

final internal class ReusableObjectQueue {
typealias T = ReusableObject

var reusableObjects: [String : T] = [:]

var objectClasses: [String : T.Type] = [:]

var totalCreated = 0

var count: Int {
return reusableObjects.count
}

func registerClass(_ objectClass: T.Type?, forObjectWithReuseIdentifier identifier: String) {
if let objClass = objectClass {
objectClasses[identifier] = objClass
} else {
objectClasses.removeValue(forKey: identifier)
reusableObjects.removeValue(forKey: identifier)
}
}

func enqueueReusableObject(_ object: T) {
reusableObjects[object.reuseIdentifier] = object
}

func dequeueReusableObjectWithIdentifier(_ identifier: String) -> T? {
if let object = reusableObjects[identifier] {
reusableObjects.removeValue(forKey: identifier)
object.prepareForReuse()
return object
} else {
guard let anyClass = objectClasses[identifier] else {
fatalError("\(identifier) is not registered.")
}
let object = anyClass.init()
totalCreated += 1
object.reuseIdentifier = identifier
return object
}
}

func removeAll() {
reusableObjects.removeAll()
}
}
56 changes: 56 additions & 0 deletions YMCalendar/Month/Cell/EventView/YMEventStandardView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// YMEventStandardView.swift
// YMCalendar
//
// Created by Yuma Matsune on 2017/03/09.
// Copyright © 2017年 Yuma Matsune. All rights reserved.
//

import Foundation
import UIKit

public class YMEventStandardView: YMEventView {

private let kSpace: CGFloat = 2

public var title: String = ""

public var textColor: UIColor = .white

public var font: UIFont = .systemFont(ofSize: 12.0)

public var attrString = NSMutableAttributedString()

public var baselineOffset: Float = 0.0

override public func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}

override public func prepareForReuse() {
super.prepareForReuse()
setNeedsDisplay()
}

private func redrawStringInRect(_ rect: CGRect) {
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byClipping

let attributedString = NSMutableAttributedString(string: title,
attributes: [
NSAttributedString.Key.font : font,
NSAttributedString.Key.paragraphStyle : style,
NSAttributedString.Key.foregroundColor : textColor,
NSAttributedString.Key.baselineOffset : baselineOffset])

attrString = attributedString
}

override public func draw(_ rect: CGRect) {
let drawRect = rect.insetBy(dx: kSpace, dy: 0)
redrawStringInRect(drawRect)

attrString.draw(with: drawRect, options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin], context: nil)
}
}
37 changes: 37 additions & 0 deletions YMCalendar/Month/Cell/EventView/YMEventView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// YMEventView.swift
// YMCalendar
//
// Created by Yuma Matsune on 2017/03/06.
// Copyright © 2017年 Yuma Matsune. All rights reserved.
//

import Foundation
import UIKit

open class YMEventView: UIView, ReusableObject {

public var reuseIdentifier: String = ""

public var selected: Bool = false

public var visibleHeight: CGFloat = 0

override public init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

private func commonInit() {
clipsToBounds = true
}

public func prepareForReuse() {
selected = false
}
}
33 changes: 13 additions & 20 deletions YMCalendar/Month/Cell/EventView/YMEventsRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class YMEventsRowView: UIScrollView {

let cellSpacing: CGFloat = 2.0

var eventViews: [IndexPath: UIView] = [:]
var eventViews: [IndexPath: YMEventView] = [:]

var maxVisibleLines: Int?

Expand Down Expand Up @@ -94,27 +94,21 @@ final class YMEventsRowView: UIScrollView {
contentSize = CGSize(width: bounds.width, height: (cellSpacing + itemHeight) * CGFloat(lineCount))
}

private let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))

private func createEventView(range: NSRange, line: Int, indexPath: IndexPath) {
let view = UIView()
view.frame = rectForCell(range: range, line: line)
if let style = eventsRowDelegate?.eventsRowView(self, styleForEventViewAt: indexPath) {
view.apply(style)
if let cell = eventsRowDelegate?.eventsRowView(self, cellForEventAtIndexPath: indexPath) {
cell.frame = rectForCell(range: range, line: line)
cell.removeGestureRecognizer(tapGesture)
cell.addGestureRecognizer(tapGesture)
cell.setNeedsDisplay()
eventViews[indexPath] = cell
addSubview(cell)
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
view.addGestureRecognizer(tapGesture)
view.setNeedsDisplay()
eventViews[indexPath] = view
addSubview(view)
}

func viewsInRect(_ rect: CGRect) -> [UIView] {
var rows: [UIView] = []
eventViews.forEach { _, cell in
if cell.frame.intersects(rect) {
rows.append(cell)
}
}
return rows
func viewsInRect(_ rect: CGRect) -> [YMEventView] {
return eventViews.filter { $0.value.frame.intersects(rect) }.map { $0.value }
}

func indexPathForCellAtPoint(_ point: CGPoint) -> IndexPath? {
Expand All @@ -126,13 +120,12 @@ final class YMEventsRowView: UIScrollView {
return nil
}

func eventView(at indexPath: IndexPath) -> UIView? {
func eventView(at indexPath: IndexPath) -> YMEventView? {
return eventViews[indexPath]
}

private func rectForCell(range: NSRange, line: Int) -> CGRect {
let colStart = range.location - daysRange.location

let x = dayWidth * CGFloat(colStart)
let y = CGFloat(line) * (itemHeight + cellSpacing)
let w = dayWidth * CGFloat(range.length)
Expand Down
2 changes: 1 addition & 1 deletion YMCalendar/Month/DataSource/YMCalendarDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import Foundation
public protocol YMCalendarDataSource: class {
func calendarView(_ view: YMCalendarView, numberOfEventsAtDate date: Date) -> Int
func calendarView(_ view: YMCalendarView, dateRangeForEventAtIndex index: Int, date: Date) -> DateRange?
func calendarView(_ view: YMCalendarView, styleForEventViewAt index: Int, date: Date) -> Style<UIView>
func calendarView(_ view: YMCalendarView, eventViewForEventAtIndex index: Int, date: Date) -> YMEventView
}
2 changes: 1 addition & 1 deletion YMCalendar/Month/Delegate/YMEventsRowViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import UIKit
protocol YMEventsRowViewDelegate: UIScrollViewDelegate {
func eventsRowView(_ view: YMEventsRowView, numberOfEventsAt day: Int) -> Int
func eventsRowView(_ view: YMEventsRowView, rangeForEventAtIndexPath indexPath: IndexPath) -> NSRange
func eventsRowView(_ view: YMEventsRowView, styleForEventViewAt indexPath: IndexPath) -> Style<UIView>
func eventsRowView(_ view: YMEventsRowView, didSelectCellAtIndexPath indexPath: IndexPath)
func eventsRowView(_ view: YMEventsRowView, cellForEventAtIndexPath indexPath: IndexPath) -> YMEventView
}
Loading

0 comments on commit 7272a70

Please sign in to comment.