Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirow committed May 12, 2019
1 parent 1fcca11 commit bc18e31
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 106 deletions.
File renamed without changes.
23 changes: 23 additions & 0 deletions Asset/AssetScale.swift
@@ -0,0 +1,23 @@
//
// AssetScale.swift
// Iconizer
// https://github.com/raphaelhanneken/iconizer
//

import Foundation

// swiftlint:disable identifier_name
enum AssetScale: String {
case x1 = "1x"
case x2 = "2x"
case x3 = "3x"

var value: Int {
switch self {
case .x1: return 1
case .x2: return 2
case .x3: return 3
}
}
}
// swiftlint:enable identifier_name
55 changes: 55 additions & 0 deletions Asset/AssetSize.swift
@@ -0,0 +1,55 @@
//
// AssetSize.swift
// Iconizer
// https://github.com/raphaelhanneken/iconizer
//

import Foundation

struct AssetSize {
let string: String
let width: Float
let height: Float

/** Parse string like 50x45
* or 50.5x25.5
* ! 50.5.5x60.5.6 will not work
*/
init(size: String) throws {
let regex = "^([\\d\\.]+)x([\\d\\.]+)$"
if let expression = try? NSRegularExpression(pattern: regex) {
let range = NSRange(size.startIndex..<size.endIndex, in: size)

var widthResult: Float?
var heightResult: Float?

expression.enumerateMatches(in: size, options: [], range: range) { (match, _, stop) in
defer { stop.pointee = true }
guard let match = match else { return }

if match.numberOfRanges == 3,
let wRange = Range(match.range(at: 1), in: size),
let hRange = Range(match.range(at: 2), in: size) {

widthResult = Float(size[wRange])
heightResult = Float(size[hRange])
}
}

if let width = widthResult, let height = heightResult {
self.string = size
self.width = width
self.height = height
return
}
}

throw AssetCatalogError.invalidFormat(format: .size)
}

func multiply(_ scale: AssetScale) -> NSSize {
let width = Int(self.width * Float(scale.value))
let height = Int(self.height * Float(scale.value))
return NSSize(width: width, height: height)
}
}
Expand Up @@ -13,6 +13,7 @@ enum ImageOrientation: String {
case landscape
case none

//used to generate .json filename
var suffix: String {
switch self {
case .portrait: return "_Portrait"
Expand Down
File renamed without changes.
40 changes: 28 additions & 12 deletions Iconizer.xcodeproj/project.pbxproj
Expand Up @@ -8,11 +8,16 @@

/* Begin PBXBuildFile section */
61547115E26963B8850B6766 /* AppIcon_iMessage.json in Resources */ = {isa = PBXBuildFile; fileRef = 61547C662152E0FD90129D23 /* AppIcon_iMessage.json */; };
61547374EBB2AD6A8638F823 /* ImageOrientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6154785D298698C27CBBF9DA /* ImageOrientation.swift */; };
615474479E1DB785FB330534 /* ImageSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61547EBDFC6B8AAD2ABA45D0 /* ImageSet.swift */; };
615474EBBA949D837E57B37B /* AssetSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61547F235EFD2AB677299655 /* AssetSize.swift */; };
6154752D9E7B4680E54B8B94 /* AppIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61547EC2357DBABDED490912 /* AppIcon.swift */; };
615476129210A99C68C1529B /* AspectMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615472CF3B9100EEC7A67F35 /* AspectMode.swift */; };
615478BFF33EE77B113D9A2E /* iMessageIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6154728EE4ABE06BF6C6F01D /* iMessageIcon.swift */; };
615479408052AD88EF3F1375 /* Platform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615470E5E6246DA215022584 /* Platform.swift */; };
61547989812FC26FB3917DEF /* LaunchImage_iPhone_Portrait.json in Resources */ = {isa = PBXBuildFile; fileRef = 615472EBBE8EE9453E90D31B /* LaunchImage_iPhone_Portrait.json */; };
61547B1BAB5AA958132ADCE7 /* Asset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61547491B396897B2A4F1E2D /* Asset.swift */; };
61547B9CD09513803896420B /* AssetScale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61547BD61AE7FFB120E33CFC /* AssetScale.swift */; };
61547F22829F35935EAA38C1 /* LaunchImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6154771EF6D8D4122C664E15 /* LaunchImage.swift */; };
8E2F99421C01F13600C2E2D8 /* AppIconViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E2F993E1C01F13600C2E2D8 /* AppIconViewController.swift */; };
8E2F99431C01F13600C2E2D8 /* ImageSetViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E2F993F1C01F13600C2E2D8 /* ImageSetViewController.swift */; };
Expand Down Expand Up @@ -46,10 +51,7 @@
C57ED8C0205EB1350048B3D1 /* LaunchImage_iPhone_Landscape.json in Resources */ = {isa = PBXBuildFile; fileRef = C57ED8BC205EB1340048B3D1 /* LaunchImage_iPhone_Landscape.json */; };
C57ED8C1205EB1350048B3D1 /* LaunchImage_iPad_Portrait.json in Resources */ = {isa = PBXBuildFile; fileRef = C57ED8BD205EB1340048B3D1 /* LaunchImage_iPad_Portrait.json */; };
C57ED8C4205EBAF70048B3D1 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C57ED8C3205EBAF70048B3D1 /* Images.xcassets */; };
C58CD0A72220145600D286C0 /* Platform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58CD0A62220145600D286C0 /* Platform.swift */; };
C58CD0A92220ABF800D286C0 /* AspectMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58CD0A82220ABF800D286C0 /* AspectMode.swift */; };
C58CD0AB2220ACD700D286C0 /* ViewControllerTag.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58CD0AA2220ACD700D286C0 /* ViewControllerTag.swift */; };
C58CD0AD2220AD0500D286C0 /* ImageOrientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = C58CD0AC2220AD0500D286C0 /* ImageOrientation.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -66,13 +68,18 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
615470E5E6246DA215022584 /* Platform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Platform.swift; sourceTree = "<group>"; };
6154728EE4ABE06BF6C6F01D /* iMessageIcon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iMessageIcon.swift; sourceTree = "<group>"; };
615472CF3B9100EEC7A67F35 /* AspectMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AspectMode.swift; sourceTree = "<group>"; };
615472EBBE8EE9453E90D31B /* LaunchImage_iPhone_Portrait.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = LaunchImage_iPhone_Portrait.json; path = Resources/LaunchImage_iPhone_Portrait.json; sourceTree = "<group>"; };
61547491B396897B2A4F1E2D /* Asset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Asset.swift; path = Helper/Asset.swift; sourceTree = "<group>"; };
6154771EF6D8D4122C664E15 /* LaunchImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LaunchImage.swift; path = Models/LaunchImage.swift; sourceTree = "<group>"; };
6154785D298698C27CBBF9DA /* ImageOrientation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageOrientation.swift; sourceTree = "<group>"; };
61547BD61AE7FFB120E33CFC /* AssetScale.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetScale.swift; sourceTree = "<group>"; };
61547C662152E0FD90129D23 /* AppIcon_iMessage.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = AppIcon_iMessage.json; path = Resources/AppIcon_iMessage.json; sourceTree = "<group>"; };
61547EBDFC6B8AAD2ABA45D0 /* ImageSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImageSet.swift; path = Models/ImageSet.swift; sourceTree = "<group>"; };
61547EC2357DBABDED490912 /* AppIcon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppIcon.swift; path = Models/AppIcon.swift; sourceTree = "<group>"; };
61547F235EFD2AB677299655 /* AssetSize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetSize.swift; sourceTree = "<group>"; };
8E2F993E1C01F13600C2E2D8 /* AppIconViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppIconViewController.swift; path = Controller/AppIconViewController.swift; sourceTree = "<group>"; };
8E2F993F1C01F13600C2E2D8 /* ImageSetViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImageSetViewController.swift; path = Controller/ImageSetViewController.swift; sourceTree = "<group>"; };
8E2F99401C01F13600C2E2D8 /* LaunchImageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LaunchImageViewController.swift; path = Controller/LaunchImageViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -106,10 +113,7 @@
C57ED8BC205EB1340048B3D1 /* LaunchImage_iPhone_Landscape.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = LaunchImage_iPhone_Landscape.json; path = Resources/LaunchImage_iPhone_Landscape.json; sourceTree = "<group>"; };
C57ED8BD205EB1340048B3D1 /* LaunchImage_iPad_Portrait.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = LaunchImage_iPad_Portrait.json; path = Resources/LaunchImage_iPad_Portrait.json; sourceTree = "<group>"; };
C57ED8C3205EBAF70048B3D1 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = Resources/Images.xcassets; sourceTree = "<group>"; };
C58CD0A62220145600D286C0 /* Platform.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Platform.swift; path = Iconizer/Helper/Platform.swift; sourceTree = "<group>"; };
C58CD0A82220ABF800D286C0 /* AspectMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AspectMode.swift; path = Iconizer/Helper/AspectMode.swift; sourceTree = "<group>"; };
C58CD0AA2220ACD700D286C0 /* ViewControllerTag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ViewControllerTag.swift; path = Iconizer/Helper/ViewControllerTag.swift; sourceTree = "<group>"; };
C58CD0AC2220AD0500D286C0 /* ImageOrientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ImageOrientation.swift; path = Iconizer/Helper/ImageOrientation.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -136,6 +140,18 @@
name = Asset;
sourceTree = "<group>";
};
615478E5B6D62E43DD5EEF11 /* Asset */ = {
isa = PBXGroup;
children = (
6154785D298698C27CBBF9DA /* ImageOrientation.swift */,
615472CF3B9100EEC7A67F35 /* AspectMode.swift */,
615470E5E6246DA215022584 /* Platform.swift */,
61547BD61AE7FFB120E33CFC /* AssetScale.swift */,
61547F235EFD2AB677299655 /* AssetSize.swift */,
);
path = Asset;
sourceTree = "<group>";
};
8E3F8F5C1B23488F0096E9EC = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -232,10 +248,8 @@
8E5C00D31E0AA3260094FD0C /* DragDropImageView.swift */,
8E2F995D1C01F17E00C2E2D8 /* NSImageExtensions.swift */,
8E2F995F1C01F17E00C2E2D8 /* StringExtensions.swift */,
C58CD0A62220145600D286C0 /* Platform.swift */,
C58CD0A82220ABF800D286C0 /* AspectMode.swift */,
C58CD0AA2220ACD700D286C0 /* ViewControllerTag.swift */,
C58CD0AC2220AD0500D286C0 /* ImageOrientation.swift */,
615478E5B6D62E43DD5EEF11 /* Asset */,
);
name = Helper;
path = ..;
Expand Down Expand Up @@ -364,19 +378,16 @@
buildActionMask = 2147483647;
files = (
8E2F99601C01F17E00C2E2D8 /* Constants.swift in Sources */,
C58CD0A72220145600D286C0 /* Platform.swift in Sources */,
C554C3E91F83786700747031 /* IconizerViewControllerProtocol.swift in Sources */,
8E5C00D41E0AA3260094FD0C /* DragDropImageView.swift in Sources */,
8E2F99431C01F13600C2E2D8 /* ImageSetViewController.swift in Sources */,
8E2F99451C01F13600C2E2D8 /* MainWindowController.swift in Sources */,
C58CD0AD2220AD0500D286C0 /* ImageOrientation.swift in Sources */,
8E2F99441C01F13600C2E2D8 /* LaunchImageViewController.swift in Sources */,
8E3F8F6B1B23488F0096E9EC /* AppDelegate.swift in Sources */,
8E2F99621C01F17E00C2E2D8 /* NSImageExtensions.swift in Sources */,
8E2F994F1C01F14E00C2E2D8 /* PreferenceManager.swift in Sources */,
8E2F994C1C01F14E00C2E2D8 /* AssetCatalog.swift in Sources */,
8E2F99421C01F13600C2E2D8 /* AppIconViewController.swift in Sources */,
C58CD0A92220ABF800D286C0 /* AspectMode.swift in Sources */,
8E2F99641C01F17E00C2E2D8 /* StringExtensions.swift in Sources */,
8E2F99611C01F17E00C2E2D8 /* ErrorTypes.swift in Sources */,
C58CD0AB2220ACD700D286C0 /* ViewControllerTag.swift in Sources */,
Expand All @@ -385,6 +396,11 @@
615474479E1DB785FB330534 /* ImageSet.swift in Sources */,
61547B1BAB5AA958132ADCE7 /* Asset.swift in Sources */,
615478BFF33EE77B113D9A2E /* iMessageIcon.swift in Sources */,
61547374EBB2AD6A8638F823 /* ImageOrientation.swift in Sources */,
615476129210A99C68C1529B /* AspectMode.swift in Sources */,
615479408052AD88EF3F1375 /* Platform.swift in Sources */,
61547B9CD09513803896420B /* AssetScale.swift in Sources */,
615474EBBA949D837E57B37B /* AssetSize.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions Iconizer/Controller/AppIconViewController.swift
Expand Up @@ -97,10 +97,10 @@ class AppIconViewController: NSViewController, IconizerViewControllerProtocol {
try appPlatforms.forEach { platform in
if platform == .iMessage {
let iMessageCatalog = AssetCatalog<MessagesIcon>()
try iMessageCatalog.addPlatform(.iMessage)
try iMessageCatalog.add(.iMessage)
try iMessageCatalog.save([.none: image], named: name, toURL: url)
} else {
try catalog.addPlatform(platform)
try catalog.add(platform)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Iconizer/Controller/ImageSetViewController.swift
Expand Up @@ -26,7 +26,7 @@ class ImageSetViewController: NSViewController, IconizerViewControllerProtocol {
}

let catalog = AssetCatalog<ImageSet>()
try catalog.addPlatform(.undefined)
try catalog.add(.undefined)
try catalog.save([.none: image], named: name, toURL: url)
}

Expand Down
4 changes: 2 additions & 2 deletions Iconizer/Controller/LaunchImageViewController.swift
Expand Up @@ -88,11 +88,11 @@ class LaunchImageViewController: NSViewController, IconizerViewControllerProtoco

try enabledPlatforms.forEach { platform in
if images[.landscape] != nil {
try catalog.addPlatform(platform, orientation: .landscape)
try catalog.add(platform, orientation: .landscape)
}

if images[.portrait] != nil {
try catalog.addPlatform(platform, orientation: .portrait)
try catalog.add(platform, orientation: .portrait)
}
}

Expand Down
72 changes: 4 additions & 68 deletions Iconizer/Helper/Asset.swift
Expand Up @@ -7,15 +7,14 @@
import Cocoa

protocol Asset {
static func resourceName(forPlatform platform: String) -> String
static var resourcePrefix: String { get }
static func directory(named: String) -> String

func save(_ image: [ImageOrientation: NSImage], aspect: AspectMode?, to url: URL) throws
}

extension Asset {
static func resourceURL(forPlatform platform: String) throws -> URL {
let name = resourceName(forPlatform: platform)
static func resourceURL(name: String) throws -> URL {

guard let path = Bundle.main.path(forResource: name, ofType: "json") else {
throw AssetCatalogError.missingPlatformJSON
Expand All @@ -26,75 +25,12 @@ extension Asset {
}

extension Asset where Self: Decodable {
static func images(forPlatform platform: Platform, orientation: ImageOrientation? = nil) throws -> [Self] {
static func images(forPlatform platform: Platform, orientation: ImageOrientation = .none) throws -> [Self] {
//.json file
let url = try resourceURL(forPlatform: platform.rawValue + (orientation?.suffix ?? ""))
let url = try resourceURL(name: resourcePrefix + platform.name(forOrientation: orientation))
let data = try Data(contentsOf: url)
//parse json
let decoder = JSONDecoder()
return try decoder.decode([Self].self, from: data)
}
}

// swiftlint:disable identifier_name
enum AssetScale: String {
case x1 = "1x"
case x2 = "2x"
case x3 = "3x"

var value: Int {
switch self {
case .x1: return 1
case .x2: return 2
case .x3: return 3
}
}
}
// swiftlint:enable identifier_name

//(width)x(height)
//must be for scale x1
//ex.: 85.5x85.5
struct AssetSize {
let name: String
let width: Float
let height: Float

init(size: String) throws {
let regex = "^([\\d\\.]+)x([\\d\\.]+)$"
if let expression = try? NSRegularExpression(pattern: regex) {
let range = NSRange(size.startIndex..<size.endIndex, in: size)

var widthResult: Float?
var heightResult: Float?

expression.enumerateMatches(in: size, options: [], range: range) { (match, _, stop) in
defer { stop.pointee = true }
guard let match = match else { return }

if match.numberOfRanges == 3,
let wRange = Range(match.range(at: 1), in: size),
let hRange = Range(match.range(at: 2), in: size) {

widthResult = Float(size[wRange])
heightResult = Float(size[hRange])
}
}

if let width = widthResult, let height = heightResult {
self.name = size
self.width = width
self.height = height
return
}
}

throw AssetCatalogError.invalidFormat(format: .size)
}

func multiply(_ scale: AssetScale) -> NSSize {
let width = Int(self.width * Float(scale.value))
let height = Int(self.height * Float(scale.value))
return NSSize(width: width, height: height)
}
}
6 changes: 2 additions & 4 deletions Iconizer/Models/AppIcon.swift
Expand Up @@ -79,7 +79,7 @@ class AppIcon: Codable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: WriteKeys.self)
try container.encode(idiom, forKey: .idiom)
try container.encode(size.name, forKey: .size)
try container.encode(size.string, forKey: .size)
try container.encode(scale.rawValue, forKey: .scale)
try container.encode(filename, forKey: .filename)
try container.encodeIfPresent(role, forKey: .role)
Expand All @@ -89,9 +89,7 @@ class AppIcon: Codable {
}

extension AppIcon: Asset {
static func resourceName(forPlatform platform: String) -> String {
return "AppIcon_" + platform
}
static let resourcePrefix = "AppIcon_"

static func directory(named: String) -> String {
return "\(directory)/\(named).\(self.extension)"
Expand Down

0 comments on commit bc18e31

Please sign in to comment.