Skip to content
This repository was archived by the owner on Jun 23, 2019. It is now read-only.

Delegate method to set date font color for specific indexPath and date #50

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
73 changes: 56 additions & 17 deletions Koyomi/Koyomi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ import UIKit
- Returns: A font for item at the indexPath or nil for default font.
*/
@objc optional func koyomi(_ koyomi: Koyomi, fontForItemAt indexPath: IndexPath, date: Date) -> UIFont?


/**
Returns font color for individual cells.

- Parameter koyomi: The current Koyomi instance.
- Parameter indexPath: The index path of the cell that was selected.
- Parameter date: The date representing current item.

- Returns: A color for item at the indexPath or nil for default font.
*/
@objc optional func koyomi(_ koyomi: Koyomi, fontColorForItemAt indexPath: IndexPath, date: Date) -> UIColor?

}

Expand Down Expand Up @@ -482,22 +494,49 @@ private extension Koyomi {
case (_, false), (.single(style: .background), true), (.multiple(style: .background), true), (.sequence(style: .background), true):
return .standard

//Selected and circle style of single, multiple, sequence mode
case (.single(style: .circle), true), (.multiple(style: .circle), true), (.sequence(style: .circle), true):
return .circle

//Selected and sequence mode, semicircleEdge style
case (.sequence(style: .semicircleEdge), true):
return .semicircleEdge(position: sequencePosition)

case (.single(style: .line), true), (.multiple(style: .line), true):
// Position is always nil.
return .line(position: nil)

case (.sequence(style: .line), true):
return .line(position: sequencePosition)

default: return .standard
// //Selected and circle style of single, multiple, sequence mode
// case (.single(style: .circle), true), (.multiple(style: .circle), true), (.sequence(style: .circle), true):
// return .circle
//
// //Selected and sequence mode, semicircleEdge style
// case (.sequence(style: .semicircleEdge), true):
// return .semicircleEdge(position: sequencePosition)
//
// case (.single(style: .line), true), (.multiple(style: .line), true):
// // Position is always nil.
// return .line(position: nil)
//
// case (.sequence(style: .line), true):
// return .line(position: sequencePosition)
//
default:
let isFirstItemInSection = indexPath.item == 0
let isLastItemInSection = indexPath.item == DateModel.maxCellCount - 1

func isPreviousDateSelected() -> Bool {
if isFirstItemInSection {
return false
} else {
return model.isSelect(with: IndexPath(item: indexPath.item - 1, section: indexPath.section))
}
}
func isNextDateSelected() -> Bool {
if isLastItemInSection {
return false
} else {
return model.isSelect(with: IndexPath(item: indexPath.item + 1, section: indexPath.section))
}
}

if isSelected && isNextDateSelected() && isPreviousDateSelected() == false {
return .semicircleEdge(position: .left)
} else if isSelected && isPreviousDateSelected() == false && isNextDateSelected() == false {
return .circle
} else if isSelected && isPreviousDateSelected() && isNextDateSelected() == false {
return .semicircleEdge(position: .right)
} else {
return .semicircleEdge(position: .middle)
}
}
}()

Expand All @@ -513,7 +552,7 @@ private extension Koyomi {
if isSelected {
return calendarDelegate?.koyomi?(self, selectionTextColorForItemAt: indexPath, date: date) ?? textColor
} else {
return textColor
return calendarDelegate?.koyomi?(self, fontColorForItemAt: indexPath, date: date) ?? textColor
}
}()
cell.contentPosition = postion
Expand Down
31 changes: 21 additions & 10 deletions Koyomi/KoyomiCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,17 @@ final class KoyomiCell: UICollectionViewCell {
leftSemicircleView.mask(with: .left)
rightSemicircleView.mask(with: .none)
} else if case .middle = position {
rightSemicircleView.isHidden = true
leftSemicircleView.isHidden = true
self.backgroundColor = color

leftSemicircleView.frame.size.width = bounds.width / 2

rightSemicircleView.isHidden = false
leftSemicircleView.isHidden = false

self.backgroundColor = backgroundColor


leftSemicircleView.backgroundColor = color
rightSemicircleView.backgroundColor = color

leftSemicircleView.mask(with: .none)
rightSemicircleView.mask(with: .none)
} else if case .right = position {
rightSemicircleView.isHidden = false
leftSemicircleView.isHidden = false
Expand Down Expand Up @@ -192,14 +197,18 @@ private extension KoyomiCell {
}

func setup() {
let diameter = bounds.width * circularViewDiameter
let halfWidth = bounds.width / 2
let halfWidthMinusDiameter = (bounds.width - diameter) / 2

circularView.isHidden = true
addSubview(circularView)

leftSemicircleView.frame = CGRect(x: 0, y: 0, width: bounds.width / 2, height: bounds.height)
leftSemicircleView.frame = CGRect(x: 0, y: (bounds.height - diameter) / 2, width: halfWidth, height: diameter)
leftSemicircleView.isHidden = true
addSubview(leftSemicircleView)

rightSemicircleView.frame = CGRect(x: bounds.width / 2, y: 0, width: bounds.width / 2, height: bounds.height)
rightSemicircleView.frame = CGRect(x: halfWidthMinusDiameter, y: (bounds.height - diameter) / 2, width: halfWidth, height: diameter)
rightSemicircleView.isHidden = true
addSubview(rightSemicircleView)

Expand All @@ -212,11 +221,13 @@ private extension KoyomiCell {
}

func adjustSubViewsFrame() {
let diameter = bounds.width * circularViewDiameter
let halfWidth = bounds.width / 2
contentLabel.sizeToFit()
contentLabel.frame.origin = postion

rightSemicircleView.frame = CGRect(x: bounds.width / 2, y: 0, width: bounds.width / 2, height: bounds.height)
leftSemicircleView.frame = CGRect(x: 0, y: 0, width: bounds.width / 2, height: bounds.height)
rightSemicircleView.frame = CGRect(x: halfWidth, y: (bounds.height - diameter) / 2, width: halfWidth, height: diameter)
leftSemicircleView.frame = CGRect(x: 0, y: (bounds.height - diameter) / 2, width: halfWidth, height: diameter)
}

func configureCircularView() {
Expand Down