Skip to content

Commit

Permalink
small memory optimizations in LayoutProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-gallagher committed Jul 21, 2019
1 parent ccb0e60 commit 7fbdefe
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 58 deletions.
102 changes: 56 additions & 46 deletions Sources/MultiProgressView/LayoutProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,77 @@ protocol LayoutProvidable {

struct LayoutProvider: LayoutProvidable {

static var trackFrame: (MultiProgressView) -> CGRect = { progressView in
switch progressView.lineCap {
case .butt:
return CGRect(x: 0,
y: progressView.trackInset,
width: progressView.frame.width,
height: progressView.frame.height - 2 * progressView.trackInset)
case .round, .square:
return CGRect(x: progressView.trackInset,
y: progressView.trackInset,
width: progressView.frame.width - 2 * progressView.trackInset,
height: progressView.frame.height - 2 * progressView.trackInset)
static var trackFrame: (MultiProgressView) -> CGRect {
return { progressView in
switch progressView.lineCap {
case .butt:
return CGRect(x: 0,
y: progressView.trackInset,
width: progressView.frame.width,
height: progressView.frame.height - 2 * progressView.trackInset)
case .round, .square:
return CGRect(x: progressView.trackInset,
y: progressView.trackInset,
width: progressView.frame.width - 2 * progressView.trackInset,
height: progressView.frame.height - 2 * progressView.trackInset)
}
}

}

static var trackImageViewFrame: (MultiProgressView) -> CGRect = { progressView in
return progressView.track.bounds
static var trackImageViewFrame: (MultiProgressView) -> CGRect {
return { progressView in
return progressView.track.bounds
}
}


static var cornerRadius: (MultiProgressView) -> CGFloat = { progressView in
switch progressView.lineCap {
case .round:
return progressView.cornerRadius == 0 ?
progressView.bounds.height / 2 : progressView.cornerRadius
case .butt, .square:
return 0
static var cornerRadius: (MultiProgressView) -> CGFloat {
return { progressView in
switch progressView.lineCap {
case .round:
return progressView.cornerRadius == 0 ?
progressView.bounds.height / 2 : progressView.cornerRadius
case .butt, .square:
return 0
}
}
}

static var trackCornerRadius: (MultiProgressView) -> CGFloat = { progressView in
let cornerRadiusFactor = progressView.cornerRadius / progressView.bounds.height
let trackHeight = progressView.track.bounds.height

switch progressView.lineCap {
case .round:
return progressView.cornerRadius == 0 ?
trackHeight / 2 : cornerRadiusFactor * trackHeight
case .butt, .square:
return 0
static var trackCornerRadius: (MultiProgressView) -> CGFloat {
return { progressView in
let cornerRadiusFactor = progressView.cornerRadius / progressView.bounds.height
let trackHeight = progressView.track.bounds.height

switch progressView.lineCap {
case .round:
return progressView.cornerRadius == 0 ?
trackHeight / 2 : cornerRadiusFactor * trackHeight
case .butt, .square:
return 0
}
}
}

static var sectionFrame: (MultiProgressView, Int) -> CGRect = { progressView, section in
let trackBounds = progressView.track.bounds
let width = trackBounds.width * CGFloat(progressView.progress(forSection: section))
let size = CGSize(width: width, height: trackBounds.height)

var origin: CGPoint = trackBounds.origin
for index in 0..<progressView.progressViewSections.count {
if index < section {
origin.x += progressView.progressViewSections[index].frame.width
static var sectionFrame: (MultiProgressView, Int) -> CGRect {
return { progressView, section in
let trackBounds = progressView.track.bounds
let width = trackBounds.width * CGFloat(progressView.progress(forSection: section))
let size = CGSize(width: width, height: trackBounds.height)

var origin: CGPoint = trackBounds.origin
for index in 0..<progressView.progressViewSections.count {
if index < section {
origin.x += progressView.progressViewSections[index].frame.width
}
}

return CGRect(origin: origin, size: size)
}

return CGRect(origin: origin, size: size)
}

static var sectionImageViewFrame: (ProgressViewSection) -> CGRect = { section in
return section.bounds
static var sectionImageViewFrame: (ProgressViewSection) -> CGRect {
return { section in
return section.bounds
}
}

static func anchorToSuperview(_ view: UIView,
Expand Down
38 changes: 26 additions & 12 deletions Tests/MultiProgressViewTests/Mocks/MockLayoutProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,45 @@
struct MockLayoutProvider: LayoutProvidable {

static var testTrackFrame: CGRect = .zero
static var trackFrame: (MultiProgressView) -> CGRect = { _ in
return testTrackFrame
static var trackFrame: (MultiProgressView) -> CGRect {
return { _ in
return testTrackFrame
}
}

static var testTrackImageViewFrame: CGRect = .zero
static var trackImageViewFrame: (MultiProgressView) -> CGRect = { _ in
return testTrackImageViewFrame
static var trackImageViewFrame: (MultiProgressView) -> CGRect {
return { _ in
return testTrackImageViewFrame
}
}

static var testSectionImageViewFrame: CGRect = .zero
static var sectionImageViewFrame: (ProgressViewSection) -> CGRect = { _ in
return testSectionImageViewFrame
static var sectionImageViewFrame: (ProgressViewSection) -> CGRect {
return { _ in
return testSectionImageViewFrame
}
}

static var testSectionFrame: CGRect = .zero
static var sectionFrame: (MultiProgressView, Int) -> CGRect = { _, _ in
return testSectionFrame
static var sectionFrame: (MultiProgressView, Int) -> CGRect {
return { _, _ in
return testSectionFrame
}
}

static var testCornerRadius: CGFloat = 0.0
static var cornerRadius: (MultiProgressView) -> CGFloat = { _ in
return testCornerRadius
static var cornerRadius: (MultiProgressView) -> CGFloat {
return { _ in
return testCornerRadius
}
}

static var testTrackCornerRadius: CGFloat = 0.0
static var trackCornerRadius: (MultiProgressView) -> CGFloat = { _ in
return testTrackCornerRadius
static var trackCornerRadius: (MultiProgressView) -> CGFloat {
return { _ in
return testTrackCornerRadius
}
}

static var testAnchorConstraints: [NSLayoutConstraint] = []
Expand All @@ -52,6 +64,8 @@ struct MockLayoutProvider: LayoutProvidable {
return testAnchorConstraints
}

// MARK: - Test Helpers

static func reset() {
testTrackFrame = .zero
testTrackImageViewFrame = .zero
Expand Down

0 comments on commit 7fbdefe

Please sign in to comment.