Skip to content

Commit

Permalink
Add iOS illustration scales to the config file (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoFearJoe authored Feb 19, 2021
1 parent 003bfcd commit efe4dac
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

# Other

.DS_Store
6 changes: 4 additions & 2 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ figma:
common:
# [optional]
colors:
# [optional] RegExp pattern for color name validation before exporting
# [optional] RegExp pattern for color name validation before exporting
nameValidateRegexp: '^[a-zA-Z_]+$' # RegExp pattern for: background, background_primary, widget_primary_background
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'color_$1'
Expand All @@ -34,7 +34,7 @@ common:
icons:
# [optional] Name of the Figma's frame where icons components are located
figmaFrameName: Icons
# [optional] RegExp pattern for icon name validation before exporting
# [optional] RegExp pattern for icon name validation before exporting
nameValidateRegexp: '^(ic)_(\d\d)_([a-z0-9_]+)$' # RegExp pattern for: ic_24_icon_name, ic_24_icon
# [optional] RegExp pattern for replacing. Supports only $n
nameReplaceRegexp: 'icon_$2_$1'
Expand Down Expand Up @@ -99,6 +99,8 @@ ios:
assetsFolder: Illustrations
# Image name style: camelCase or snake_case
nameStyle: camelCase
# [optional] An array of asset scales that should be downloaded. The valid values are 1, 2, 3. The deafault value is [1, 2, 3].
scales: [1, 2, 3]
# [optional] Absolute or relative path to swift file where to export images (SwiftUI’s Image) for accessing from the code (e.g. Image.illZeroNoInternet)
swiftUIImageSwift: "./Source/Image+extension_illustrations.swift"
# [optional] Absolute or relative path to swift file where to generate extension for UIImage for accessing illustrations from the code (e.g. UIImage.illZeroNoInternet)
Expand Down
29 changes: 15 additions & 14 deletions Sources/FigmaExport/Input/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,76 @@ struct Params: Decodable {
let darkFileId: String?
let timeout: TimeInterval?
}

struct Common: Decodable {
struct Colors: Decodable {
let nameValidateRegexp: String?
let nameReplaceRegexp: String?
let useSingleFile: Bool?
let darkModeSuffix: String?
}

struct Icons: Decodable {
let nameValidateRegexp: String?
let figmaFrameName: String?
let nameReplaceRegexp: String?
}

struct Images: Decodable {
let nameValidateRegexp: String?
let figmaFrameName: String?
let nameReplaceRegexp: String?
}

let colors: Colors?
let icons: Icons?
let images: Images?
}

enum VectorFormat: String, Decodable {
case pdf
case svg
}

struct iOS: Decodable {

struct Colors: Decodable {
let useColorAssets: Bool
let assetsFolder: String?
let nameStyle: NameStyle

let colorSwift: URL?
let swiftuiColorSwift: URL?
}

struct Icons: Decodable {
let format: VectorFormat
let assetsFolder: String
let preservesVectorRepresentation: [String]?
let nameStyle: NameStyle

let imageSwift: URL?
let swiftUIImageSwift: URL?

let renderMode: XcodeRenderMode?
}

struct Images: Decodable {
let assetsFolder: String
let nameStyle: NameStyle

let scales: [Double]?

let imageSwift: URL?
let swiftUIImageSwift: URL?
}

struct Typography: Decodable {
let fontSwift: URL?
let swiftUIFontSwift: URL?
let generateLabels: Bool
let labelsDirectory: URL?
}

let xcodeprojPath: String
let target: String
let xcassetsPath: URL
Expand Down
30 changes: 20 additions & 10 deletions Sources/FigmaExport/Loaders/ImagesLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ final class ImagesLoader {
let figmaClient: FigmaClient
let params: Params
let platform: Platform

private var iconsFrameName: String {
params.common?.icons?.figmaFrameName ?? "Icons"
}

private var imagesFrameName: String {
params.common?.images?.figmaFrameName ?? "Illustrations"
}

init(figmaClient: FigmaClient, params: Params, platform: Platform) {
self.figmaClient = figmaClient
self.params = params
Expand Down Expand Up @@ -67,7 +67,7 @@ final class ImagesLoader {
frameName: imagesFrameName,
params: SVGParams(),
filter: filter)

let darkPacks = try params.figma.darkFileId.map {
try _loadImages(
fileId: $0,
Expand All @@ -88,14 +88,14 @@ final class ImagesLoader {
($0.description == platform.rawValue || $0.description == nil || $0.description == "") &&
$0.description?.contains("none") == false
}

if let filter = filter {
let assetsFilter = AssetsFilter(filter: filter)
components = components.filter { component -> Bool in
assetsFilter.match(name: component.name)
}
}

return Dictionary(uniqueKeysWithValues: components.map { ($0.nodeId, $0) })
}

Expand All @@ -106,11 +106,11 @@ final class ImagesLoader {
filter: String? = nil
) throws -> [ImagePack] {
let imagesDict = try fetchImageComponents(fileId: fileId, frameName: frameName, filter: filter)

guard !imagesDict.isEmpty else {
throw FigmaExportError.componentsNotFound
}

let imagesIds: [NodeId] = imagesDict.keys.map { $0 }
let imageIdToImagePath = try loadImages(fileId: fileId, nodeIds: imagesIds, params: params)

Expand All @@ -133,13 +133,13 @@ final class ImagesLoader {

private func loadPNGImages(fileId: String, frameName: String, filter: String? = nil, platform: Platform) throws -> [ImagePack] {
let imagesDict = try fetchImageComponents(fileId: fileId, frameName: frameName, filter: filter)

guard !imagesDict.isEmpty else {
throw FigmaExportError.componentsNotFound
}

let imagesIds: [NodeId] = imagesDict.keys.map { $0 }
let scales = platform == .android ? [1, 2, 3, 1.5, 4.0] : [1, 2, 3]
let scales = getScales(platform: platform)

var images: [Double: [NodeId: ImagePath]] = [:]
for scale in scales {
Expand All @@ -165,6 +165,16 @@ final class ImagesLoader {
return imagePacks
}

private func getScales(platform: Platform) -> [Double] {
if platform == .android {
return [1, 2, 3, 1.5, 4.0]
} else {
let validScales: [Double] = [1, 2, 3]
let customScales = params.ios?.images.scales?.filter { validScales.contains($0) } ?? []
return customScales.isEmpty ? validScales : customScales
}
}

// MARK: - Figma

private func loadComponents(fileId: String) throws -> [Component] {
Expand Down

0 comments on commit efe4dac

Please sign in to comment.