-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from artemnovichkov/feature/refactoring
Refactoring helper functions to Zepcode singleton
- Loading branch information
Showing
7 changed files
with
215 additions
and
87 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,90 @@ | ||
const linearGradientTemplate = ( | ||
gradient, | ||
colorStopsString, | ||
colorStopsPositionString | ||
) => ` | ||
let gradientLayer = CAGradientLayer() | ||
gradientLayer.frame = view.bounds | ||
${ | ||
gradient.angle === 90 | ||
? `gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) | ||
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)` | ||
: `` | ||
const linearGradientTemplate = (gradient, colorStopsString) => | ||
`import UIKit | ||
final class LinearGradientLayer: CALayer { | ||
enum Direction { | ||
case vertical | ||
case horizontal | ||
case custom(start: CGPoint, end: CGPoint) | ||
var points: (start: CGPoint, end: CGPoint) { | ||
switch self { | ||
case .vertical: | ||
return (CGPoint(x: 0.5, y: 0.0), CGPoint(x: 0.5, y: 1.0)) | ||
case .horizontal: | ||
return (CGPoint(x: 0.0, y: 0.5), CGPoint(x: 1.0, y: 0.5)) | ||
case let .custom(start, end): | ||
return (start, end) | ||
} | ||
} | ||
} | ||
var direction: Direction = .vertical | ||
var colorSpace = CGColorSpaceCreateDeviceRGB() | ||
var colors: [CGColor]? | ||
var locations: [CGFloat]? | ||
var options: CGGradientDrawingOptions = CGGradientDrawingOptions(rawValue: 0) | ||
required override init() { | ||
super.init() | ||
masksToBounds = true | ||
needsDisplayOnBoundsChange = true | ||
} | ||
required init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
} | ||
required override init(layer: Any) { | ||
super.init(layer: layer) | ||
} | ||
override func draw(in ctx: CGContext) { | ||
ctx.saveGState() | ||
guard let colors = colors, let gradient = CGGradient(colorsSpace: colorSpace, | ||
colors: colors as CFArray, locations: locations) else { return } | ||
let points = direction.points | ||
ctx.drawLinearGradient( | ||
gradient, | ||
start: transform(points.start), | ||
end: transform(points.end), | ||
options: options | ||
) | ||
} | ||
private func transform(_ point: CGPoint) -> CGPoint { | ||
return CGPoint(x: bounds.width * point.x, y: bounds.height * point.y) | ||
} | ||
} | ||
gradientLayer.colors = [${colorStopsString}] | ||
gradientLayer.locations = [${colorStopsPositionString}] | ||
view.layer.insertSublayer(gradientLayer, at: 0)`; | ||
final class GradientView: UIView { | ||
lazy var gradientLayer: LinearGradientLayer = { | ||
let gradientLayer = LinearGradientLayer() | ||
gradientLayer.colors = [${colorStopsString}] | ||
return gradientLayer | ||
}() | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
layer.insertSublayer(gradientLayer, at: 0) | ||
} | ||
required init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
layer.insertSublayer(gradientLayer, at: 0) | ||
} | ||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
gradientLayer.frame = bounds | ||
} | ||
}`; | ||
|
||
export default linearGradientTemplate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import colorExtensionTemplate from './templates/color-extension'; | ||
import customColorTemplate from './templates/custom-color'; | ||
import colorTemplate from './templates/color'; | ||
import linearGradientTemplate from './templates/linear-gradient'; | ||
import radialGradientTemplate from './templates/radial-gradient'; | ||
import fontExtensionTemplate from './templates/font-extension'; | ||
|
||
const zepcode = (() => { | ||
let instance; | ||
|
||
function init(privateContext) { | ||
let me; | ||
if (privateContext !== undefined) { | ||
me = { | ||
options: { | ||
useColorNames: privateContext.getOption('use_color_names'), | ||
useCustomColorInitializer: privateContext.getOption( | ||
'use_custom_color_initializer' | ||
), | ||
}, | ||
project: privateContext.project, | ||
}; | ||
} | ||
|
||
me.cgColorString = color => { | ||
const styleguideColor = me.project.findColorEqual(color); | ||
const cgColorPostfix = '.cgColor'; | ||
|
||
if (me.options.useColorNames && styleguideColor) { | ||
return `UIColor.${styleguideColor.name}${cgColorPostfix}`; | ||
} | ||
if (me.options.useCustomColorInitializer) { | ||
return customColorTemplate(color) + cgColorPostfix; | ||
} | ||
return colorTemplate(color) + cgColorPostfix; | ||
}; | ||
|
||
me.colorStopsString = gradient => { | ||
const { colorStops } = gradient; | ||
return colorStops | ||
.map(colorStop => me.cgColorString(colorStop.color)) | ||
.join(', '); | ||
}; | ||
|
||
me.linearGradientLayer = gradient => | ||
linearGradientTemplate(gradient, me.colorStopsString(gradient)); | ||
|
||
me.radialGradientLayer = gradient => | ||
radialGradientTemplate(me.colorStopsString(gradient)); | ||
|
||
me.commentString = text => { | ||
const { textOption } = me.options; | ||
return `${text} ${textOption !== undefined ? textOption : ''}`; | ||
}; | ||
|
||
me.generateColorExtension = colors => ({ | ||
code: colorExtensionTemplate(colors, me.options), | ||
language: 'swift', | ||
filename: 'UIColor+AppColors.swift', | ||
}); | ||
|
||
me.generateFontExtension = textStyles => { | ||
const uniqueFonts = Array.from( | ||
new Set(textStyles.map(style => style.fontFace)) | ||
).sort(); | ||
|
||
return { | ||
code: fontExtensionTemplate(uniqueFonts), | ||
language: 'swift', | ||
filename: 'UIFont+AppFonts.swift', | ||
}; | ||
}; | ||
|
||
return me; | ||
} | ||
|
||
return context => { | ||
if (!instance) { | ||
instance = init(context); | ||
} | ||
|
||
return instance; | ||
}; | ||
})(); | ||
|
||
export default zepcode; |