Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
replace TimelineStyle with CNTimelineCellStyle
replace LineStyle to CNTimelineCellStyle.LineStyle
replace BubbleStyle to CNTimelineCellStyle.BubbleStyle
add DocC support for document generation
  • Loading branch information
chrisnyw committed Apr 7, 2022
1 parent 69a227b commit 5a22654
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 115 deletions.
58 changes: 23 additions & 35 deletions Example/CNTimelineCellDemo/TimelineTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,37 @@ import CNTimelineCell

class TimelineTableViewController: UITableViewController {

var timelineStyle: TimelineStyle = TimelineStyle()
let dataItem: [TimelineItem] = [TimelineItem(title: "Good morning", content: "Today is sunny day!", image: UIImage(named: "light")),
TimelineItem(title: "08:00 am", content: "Tom wakes up", image: UIImage(named: "star"), leftType: .start),
TimelineItem(title: "08:05 am", content: "Breakfast time", leftType: .spot),
TimelineItem(title: "08:30 am", content: "Mary wakes up", image: UIImage(named: "star"), leftType: .line, rightType: .start),
TimelineItem(title: "09:00 am", content: "School time", image: UIImage(named: "school"), leftType: .spot, rightType: .spot),
TimelineItem(title: "09:00 am - 12:00pm", content: "Many lessons\nMath\nEnglish\netc...", leftType: .line, rightType: .line),
TimelineItem(title: "12:05 pm", content: "Lunch together", image: UIImage(named: "restaurant"), leftType: .spot, rightType: .spot),
TimelineItem(title: "01:00 pm", content: "Outdoor activities, play games in sports court.", image: UIImage(named: "sports_basketball"), leftType: .spot, rightType: .spot),
var timelineStyle: CNTimelineCellStyle = CNTimelineCellStyle()
let dataItem: [CNTimelineCellItem] = [CNTimelineCellItem(title: "Good morning", content: "Today is sunny day!", image: UIImage(named: "light")),
CNTimelineCellItem(title: "08:00 am", content: "Tom wakes up", image: UIImage(named: "star"), leftType: .start),
CNTimelineCellItem(title: "08:05 am", content: "Breakfast time", leftType: .spot),
CNTimelineCellItem(title: "08:30 am", content: "Mary wakes up", image: UIImage(named: "star"), leftType: .line, rightType: .start),
CNTimelineCellItem(title: "09:00 am", content: "School time", image: UIImage(named: "school"), leftType: .spot, rightType: .spot),
CNTimelineCellItem(title: "09:00 am - 12:00pm", content: "Many lessons\nMath\nEnglish\netc...", leftType: .line, rightType: .line),
CNTimelineCellItem(title: "12:05 pm", content: "Lunch together", image: UIImage(named: "restaurant"), leftType: .spot, rightType: .spot),
CNTimelineCellItem(title: "01:00 pm", content: "Outdoor activities, play games in sports court.", image: UIImage(named: "sports_basketball"), leftType: .spot, rightType: .spot),

TimelineItem(title: "until 03:00 pm", content: "Afternoon lesson", leftType: .line, rightType: .line),
TimelineItem(title: "03:00 pm", content: "Free time", leftType: .spot, rightType: .spot),
TimelineItem(title: "03:30 pm", content: "Art lesson", leftType: .line, rightType: .spot),
TimelineItem(title: "03:15 pm", content: "Take a nap", leftType: .end, rightType: .line),
TimelineItem(title: "04:00 pm", content: "Play in playground", leftType: .none, rightType: .spot),
TimelineItem(title: "04:15 pm", content: "Wake up from nap", leftType: .start, rightType: .line),
TimelineItem(title: "05:00 pm", content: "Play TV games", leftType: .spot, rightType: .line),
TimelineItem(title: "07:00 pm", content: "Dinner", image: UIImage(named: "restaurant"), leftType: .line, rightType: .spot),
TimelineItem(title: "08:00 pm", content: "Dinner", image: UIImage(named: "restaurant"), leftType: .spot, rightType: .line),
TimelineItem(title: "10:00 pm", content: "Goto sleep", image: UIImage(named: "bed"), leftType: .line, rightType: .end),
TimelineItem(title: "10:30 pm", content: "Goto sleep", image: UIImage(named: "single_bed"), leftType: .end, rightType: .none),
TimelineItem(title: "Awesome", content: "That's all for the day", image: UIImage(named: "auto_awesome")),]
CNTimelineCellItem(title: "until 03:00 pm", content: "Afternoon lesson", leftType: .line, rightType: .line),
CNTimelineCellItem(title: "03:00 pm", content: "Free time", leftType: .spot, rightType: .spot),
CNTimelineCellItem(title: "03:30 pm", content: "Art lesson", leftType: .line, rightType: .spot),
CNTimelineCellItem(title: "03:15 pm", content: "Take a nap", leftType: .end, rightType: .line),
CNTimelineCellItem(title: "04:00 pm", content: "Play in playground", leftType: .none, rightType: .spot),
CNTimelineCellItem(title: "04:15 pm", content: "Wake up from nap", leftType: .start, rightType: .line),
CNTimelineCellItem(title: "05:00 pm", content: "Play TV games", leftType: .spot, rightType: .line),
CNTimelineCellItem(title: "07:00 pm", content: "Dinner", image: UIImage(named: "restaurant"), leftType: .line, rightType: .spot),
CNTimelineCellItem(title: "08:00 pm", content: "Dinner", image: UIImage(named: "restaurant"), leftType: .spot, rightType: .line),
CNTimelineCellItem(title: "10:00 pm", content: "Goto sleep", image: UIImage(named: "bed"), leftType: .line, rightType: .end),
CNTimelineCellItem(title: "10:30 pm", content: "Goto sleep", image: UIImage(named: "single_bed"), leftType: .end, rightType: .none),
CNTimelineCellItem(title: "Awesome", content: "That's all for the day", image: UIImage(named: "auto_awesome")),]

override func viewDidLoad() {
super.viewDidLoad()

let timelineTableViewCellNib = UINib(nibName: CNTimelineCell.identifier, bundle: CNTimelineCell.bundle)
self.tableView.register(timelineTableViewCellNib, forCellReuseIdentifier: CNTimelineCell.identifier)

let timelineRight = LineStyle(lineColor: .red)
timelineStyle = TimelineStyle(rightLineStyle: timelineRight)

let container = UIView()
let rect = UIView()

container.addSubview(rect)
container.frame = CGRect(x: 0, y: 0, width: 500, height: 500)
rect.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
print(rect.frame)
print(rect.bounds)
rect.transform = CGAffineTransform(scaleX: 2, y: 2)
print(rect.frame)
print(rect.bounds)
let timelineRight = CNTimelineCellStyle.LineStyle(lineColor: .red)
timelineStyle = CNTimelineCellStyle(rightLineStyle: timelineRight)
}

// MARK: - Table view data source
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Sample screenshot:

---
### Requirements
- iOS 13+
- iOS 11+
- Swift 5.3+
---
### Installation
Expand All @@ -22,7 +22,7 @@ Once you have your Swift package set up, adding CNTimelineCell as a dependency i

```swift
dependencies: [
.package(url: "https://github.com/chrisnyw/CNTimelineCell", from: "0.2")
.package(url: "https://github.com/chrisnyw/CNTimelineCell", from: "0.3")
]
```
---
Expand Down
12 changes: 7 additions & 5 deletions Sources/CNTimelineCell/CNTimelineCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import UIKit

/// A UITableViewCell library for generating TimelineCell
open class CNTimelineCell: UITableViewCell {
public static let identifier = String(describing: CNTimelineCell.self)
public static let bundle = Bundle.module
Expand All @@ -33,17 +34,18 @@ open class CNTimelineCell: UITableViewCell {
icon.image = nil
}

public func setCell(item: TimelineItem, style: TimelineStyle? = nil) {
/// helper function to setup the CNTimelineCell item and style
public func setCell(item: CNTimelineCellItem, style: CNTimelineCellStyle? = nil) {
setCellWithItem(item)
setCellStyle(style ?? TimelineStyle.default)
setCellStyle(style ?? CNTimelineCellStyle.default)
}

// private funcations
private func configureCell() {
setCellStyle(TimelineStyle.default)
setCellStyle(CNTimelineCellStyle.default)
}

private func setCellWithItem(_ item: TimelineItem) {
private func setCellWithItem(_ item: CNTimelineCellItem) {
timelineLeft.timelineType = item.leftType
timelineLeft.isHidden = item.leftType == .none
timelineRight.timelineType = item.rightType
Expand All @@ -65,7 +67,7 @@ open class CNTimelineCell: UITableViewCell {
icon.isHidden = item.image == nil
}

private func setCellStyle(_ timelineStyle: TimelineStyle) {
private func setCellStyle(_ timelineStyle: CNTimelineCellStyle) {
if let myConstraint = timelineLeft.constraintWith(identifier: "timelineLeftWidth"){
myConstraint.constant = timelineStyle.leftLineStyle.spotDiameter
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TimelineItem.swift
// CNTimelineCellItem.swift
// CNTimelineCell
//
// Created by Ying Wai Ng on 31/3/2022.
Expand All @@ -8,11 +8,22 @@
import Foundation
import UIKit

public struct TimelineItem {
/// Used item to setup the cell
public struct CNTimelineCellItem {

/// title text displayesin dialog upper positon
var title: String

/// message content text displays in dialog lower positon
var content: String? = nil

/// icon image display in top-left corner insdie dialog
var image: UIImage? = nil

/// left ``TimelineType`` setup
var leftType: TimelineType = .none

/// right ``TimelineType`` setup
var rightType: TimelineType = .none

public init(title: String,
Expand Down
106 changes: 106 additions & 0 deletions Sources/CNTimelineCell/Model/CNTimelineCellStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// CNTimelineCellStyle.swift
// CNTimelineCell
//
// Created by Ying Wai Ng on 31/3/2022.
//

import Foundation
import UIKit

/// A style configuration for CNTimelineCell
///
/// Used to setting the left and right ``CNTimelineCellStyle/LineStyle``,
/// ``CNTimelineCellStyle/BubbleStyle-swift.struct`` and color
/// of message separator in message dialog
///
///
public struct CNTimelineCellStyle {

/// left line style
public var leftLineStyle: LineStyle = LineStyle()

/// right line style
public var rightLineStyle: LineStyle = LineStyle()

/// bubble style
public var bubbleStyle: BubbleStyle = BubbleStyle()

/// setting color of message separator
public var messageSeparator: UIColor = .lightGray

static let `default` = CNTimelineCellStyle()

public init(leftLineStyle: LineStyle = LineStyle(),
rightLineStyle: LineStyle = LineStyle(),
bubbleStyle: BubbleStyle = BubbleStyle(),
messageSeparator: UIColor = .lightGray) {
self.leftLineStyle = leftLineStyle
self.rightLineStyle = rightLineStyle
self.bubbleStyle = bubbleStyle
self.messageSeparator = messageSeparator
}
}

public extension CNTimelineCellStyle {

/// Line style can be configured by LineStyle struct.
struct LineStyle {

/// width of the central line
public var lineWidth: CGFloat = 6

/// diameter of circle spot
public var spotDiameter: CGFloat = 20

/// vertical offset of spot
public var spotOffsetY: CGFloat = 29

/// color of line
public var lineColor: UIColor = .init(_colorLiteralRed: 0/255, green: 144/255, blue: 182/255, alpha: 1)

/// color of central circle spot
public var spotColor: UIColor = .white

public init(lineWidth: CGFloat = 6,
spotDiameter: CGFloat = 20,
spotOffsetY: CGFloat = 29,
lineColor: UIColor = .init(_colorLiteralRed: 0/255, green: 144/255, blue: 182/255, alpha: 1),
spotColor: UIColor = .white) {
self.lineWidth = lineWidth
self.spotDiameter = spotDiameter
self.spotOffsetY = spotOffsetY
self.lineColor = lineColor
self.spotColor = spotColor
}
}
}

public extension CNTimelineCellStyle {

/// Set the dialog style using BubbleStyle struct
struct BubbleStyle {

/// vertical offset of arrow
public var arrowOffsetY: CGFloat = 30

/// width of border
public var borderWidth: CGFloat = 2

/// color of border
public var borderColor: UIColor = .init(red: 0/255, green: 147/255, blue: 51/255, alpha: 1)

/// button background color
public var backgroundColor: UIColor = .clear

public init(arrowOffsetY: CGFloat = 30,
borderWidth: CGFloat = 2,
borderColor: UIColor = .init(red: 0/255, green: 147/255, blue: 51/255, alpha: 1),
backgroundColor: UIColor = .clear) {
self.arrowOffsetY = arrowOffsetY
self.borderWidth = borderWidth
self.borderColor = borderColor
self.backgroundColor = backgroundColor
}
}
}
3 changes: 2 additions & 1 deletion Sources/CNTimelineCell/TimelineBubbleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import UIKit

/// A bubble dialog view drew by UIBezierPath
@IBDesignable
open class TimelineBubbleView: UIView {
let arrowDepth = 8.0
Expand Down Expand Up @@ -71,7 +72,7 @@ open class TimelineBubbleView: UIView {
}
}

open var bubbleStyle: BubbleStyle = BubbleStyle() {
open var bubbleStyle: CNTimelineCellStyle.BubbleStyle = .init() {
didSet {
arrowOffsetY = bubbleStyle.arrowOffsetY
borderWidth = bubbleStyle.borderWidth
Expand Down
3 changes: 2 additions & 1 deletion Sources/CNTimelineCell/TimelineStraightLineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import UIKit

/// A timeline view drew by UIBezierPath
@IBDesignable
open class TimelineStraightLineView: UIView {
override init(frame: CGRect) {
Expand Down Expand Up @@ -61,7 +62,7 @@ open class TimelineStraightLineView: UIView {
}
}

open var lineStyle: LineStyle = LineStyle() {
open var lineStyle: CNTimelineCellStyle.LineStyle = .init() {
didSet {
spotOffsetY = lineStyle.spotOffsetY
lineColor = lineStyle.lineColor
Expand Down
65 changes: 0 additions & 65 deletions Sources/CNTimelineCell/TimelineStyle.swift

This file was deleted.

Loading

0 comments on commit 5a22654

Please sign in to comment.