Skip to content

Commit

Permalink
Merge pull request #136 from brightdigit/feature/ui-cleanup
Browse files Browse the repository at this point in the history
Feature/ui cleanup
  • Loading branch information
leogdion authored Jul 17, 2020
2 parents 207e6b3 + 4a37bff commit 2e01aef
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 134 deletions.
16 changes: 16 additions & 0 deletions Speculid.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
B303554A24381F1200A8EAB9 /* SpeculidKit in Frameworks */ = {isa = PBXBuildFile; productRef = B303554924381F1200A8EAB9 /* SpeculidKit */; };
B3326D4324C1CFF000629F70 /* Tooltip.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3326D4224C1CFF000629F70 /* Tooltip.swift */; };
B347128924B64968001CCCFD /* ClassicDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = B347128824B64968001CCCFD /* ClassicDocument.swift */; };
B347128B24B668DE001CCCFD /* ClassicView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B347128A24B668DE001CCCFD /* ClassicView.swift */; };
B34B6BBE24BDEF2B00F4EE6B /* CairoSVG.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = B37B472124B93CB900AE2021 /* CairoSVG.xcframework */; };
Expand All @@ -18,6 +19,9 @@
B3DB48142431642600B1DA4F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B3DB48132431642600B1DA4F /* Assets.xcassets */; };
B3DB48172431642600B1DA4F /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B3DB48162431642600B1DA4F /* Preview Assets.xcassets */; };
B3DB481A2431642600B1DA4F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B3DB48182431642600B1DA4F /* Main.storyboard */; };
B3E1707C24C1DBF700784D20 /* LabeledOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E1707B24C1DBF700784D20 /* LabeledOption.swift */; };
B3E1707E24C1DC1100784D20 /* ResizeOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E1707D24C1DC1100784D20 /* ResizeOption.swift */; };
B3E1708024C1DC6C00784D20 /* ClassicObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E1707F24C1DC6C00784D20 /* ClassicObject.swift */; };
B3E8D6B22434D71000B34464 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3DB4826243166FC00B1DA4F /* main.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -65,6 +69,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
B3326D4224C1CFF000629F70 /* Tooltip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tooltip.swift; sourceTree = "<group>"; };
B347128824B64968001CCCFD /* ClassicDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassicDocument.swift; sourceTree = "<group>"; };
B347128A24B668DE001CCCFD /* ClassicView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassicView.swift; sourceTree = "<group>"; };
B34B6BC124BE354D00F4EE6B /* BookmarkURLCollectionObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarkURLCollectionObject.swift; sourceTree = "<group>"; };
Expand All @@ -82,6 +87,9 @@
B3DB481C2431642600B1DA4F /* Speculid.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Speculid.entitlements; sourceTree = "<group>"; };
B3DB4824243166FC00B1DA4F /* speculid */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = speculid; sourceTree = BUILT_PRODUCTS_DIR; };
B3DB4826243166FC00B1DA4F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
B3E1707B24C1DBF700784D20 /* LabeledOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabeledOption.swift; sourceTree = "<group>"; };
B3E1707D24C1DC1100784D20 /* ResizeOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResizeOption.swift; sourceTree = "<group>"; };
B3E1707F24C1DC6C00784D20 /* ClassicObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassicObject.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -163,6 +171,10 @@
B347128824B64968001CCCFD /* ClassicDocument.swift */,
B347128A24B668DE001CCCFD /* ClassicView.swift */,
B34B6BC124BE354D00F4EE6B /* BookmarkURLCollectionObject.swift */,
B3326D4224C1CFF000629F70 /* Tooltip.swift */,
B3E1707B24C1DBF700784D20 /* LabeledOption.swift */,
B3E1707D24C1DC1100784D20 /* ResizeOption.swift */,
B3E1707F24C1DC6C00784D20 /* ClassicObject.swift */,
);
path = Speculid;
sourceTree = "<group>";
Expand Down Expand Up @@ -272,10 +284,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B3E1707E24C1DC1100784D20 /* ResizeOption.swift in Sources */,
B34B6BC224BE354D00F4EE6B /* BookmarkURLCollectionObject.swift in Sources */,
B347128B24B668DE001CCCFD /* ClassicView.swift in Sources */,
B3E1707C24C1DBF700784D20 /* LabeledOption.swift in Sources */,
B347128924B64968001CCCFD /* ClassicDocument.swift in Sources */,
B3326D4324C1CFF000629F70 /* Tooltip.swift in Sources */,
B3B6A4D124B15292009C6E32 /* doc_appApp.swift in Sources */,
B3E1708024C1DC6C00784D20 /* ClassicObject.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 23 additions & 7 deletions Speculid/BookmarkURLCollectionObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import SpeculidKit

extension UserDefaults {

Expand All @@ -19,11 +20,8 @@ extension UserDefaults {
}
}
}
public class BookmarkURLCollectionObject : ObservableObject {
public class BookmarkURLCollectionObject : ObservableObject, Sandbox {

//@AppStorage("bookmarks", store: UserDefaults(suiteName: "MLT7M394S7.group.com.brightdigit.Speculid"))
// @UserDefaultsBacked(key: "bookmarks", defaultValue: [String : Data](), storage: UserDefaults.shared)

static let shared: UserDefaults = {
let combined = UserDefaults(suiteName: "MLT7M394S7.group.com.brightdigit.Speculid")!
combined.register(defaults: ["bookmarks" : [String : Data]()])
Expand All @@ -33,7 +31,6 @@ public class BookmarkURLCollectionObject : ObservableObject {
@Published var bookmarks = [URL : URL]()

static func saveBookmark(_ url: URL) {
debugPrint("saving bookmark for \(url)")
guard let newData = try? url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil) else {
return
}
Expand All @@ -43,6 +40,10 @@ public class BookmarkURLCollectionObject : ObservableObject {

}

public func saveBookmark(_ url: URL) {
Self.saveBookmark(url)
}

func reset () {
Self.shared.bookmarks = [String : Data]()
}
Expand All @@ -60,10 +61,25 @@ public class BookmarkURLCollectionObject : ObservableObject {
return self.bookmarks[url] != nil
}

static func transformPath(_ path: String, withBookmarkData bookmarkData: Data) -> (URL, URL)? {
public func bookmarkURL(fromURL url: URL) throws -> URL {

var isStale : Bool = false

let fromURLResult : Result<URL, Error>
let fromURLCurrentResult = Self.shared.bookmarks?[url.path].map{
data in
Result{
try URL(resolvingBookmarkData: data, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
}
}
if isStale {
saveBookmark(url)
}
fromURLResult = fromURLCurrentResult ?? .failure(NoBookmarkAvailableError(url : url))
return try fromURLResult.get()
}

static func transformPath(_ path: String, withBookmarkData bookmarkData: Data) -> (URL, URL)? {
var isStale : Bool = false

guard let url = try? URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale) else {
return nil
Expand Down
36 changes: 8 additions & 28 deletions Speculid/ClassicDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,15 @@ struct SpeculidMutableSpecificationsFile : SpeculidSpecificationsFileProtocol {
var assetDirectoryRelativePath: String
var sourceImageRelativePath: String
var geometry: Geometry?
var backgroundColor: Color
var background: NSColor?
var removeAlpha: Bool

var background: NSColor? {
guard backgroundColor != .clear else {
return nil
}

return NSColor(self.backgroundColor)
}

init (source: SpeculidSpecificationsFileProtocol) {
self.assetDirectoryRelativePath = source.assetDirectoryRelativePath
self.sourceImageRelativePath = source.sourceImageRelativePath
self.geometry = source.geometry
self.backgroundColor = source.background.map( Color.init ) ?? .clear
self.background = source.background
self.removeAlpha = source.removeAlpha
}
}
Expand Down Expand Up @@ -69,22 +62,18 @@ struct ClassicDocument: FileDocument {
fileWrapper = FileWrapper(regularFileWithContents: data)
}

func build (fromURL url: URL) {

let management = FileManagement()

func build (fromURL url: URL, inSandbox sandbox: Sandbox) {

let urls = ["assetDirectory" : url.deletingLastPathComponent().appendingPathComponent(document.assetDirectoryRelativePath),
"sourceImage" : url.deletingLastPathComponent().appendingPathComponent(document.sourceImageRelativePath)]






//
let document : SpeculidDocument
do {
document = try SpeculidDocument(sandboxedFromFile: self.document, withURL: url, decoder: JSONDecoder(), withManager: management)
document = try SpeculidDocument(sandboxedFromFile: self.document, withURL: url, decoder: JSONDecoder(), withManager: sandbox)
} catch {
debugPrint(error)
debugPrint(error.localizedDescription)
Expand All @@ -100,14 +89,13 @@ struct ClassicDocument: FileDocument {
return (asset, fileName, document.destinationURL(forFileName: fileName))
}

processImages(fromURL: url, management: management, document: document, sandboxMap: urlMap)
processImages(fromURL: url, management: sandbox, document: document, sandboxMap: urlMap)
}

func processImages (fromURL url: URL, management: FileManagement, document : SpeculidDocument, sandboxMap: [(AssetSpecificationProtocol, String, URL)]) {
func processImages (fromURL url: URL, management: Sandbox, document : SpeculidDocument, sandboxMap: [(AssetSpecificationProtocol, String, URL)]) {

let assetDirectoryURL = url.deletingLastPathComponent().appendingPathComponent(self.document.assetDirectoryRelativePath)
debugPrint(self.document.assetDirectoryRelativePath)
debugPrint(assetDirectoryURL)

let imageSpecificationBuilder = SpeculidImageSpecificationBuilder()
let imageSpecifications: [ImageSpecification]
do {
Expand All @@ -128,14 +116,6 @@ struct ClassicDocument: FileDocument {
return
}
let destinationURL = try? management.bookmarkURL( fromURL: assetDirectoryURL )
if let testURL = destinationURL?.appendingPathComponent("test.text") {
do {
try "test".write(to: testURL, atomically: true, encoding: .utf8)
} catch {
debugPrint(error)
}
debugPrint("test written")
}

destinationURL?.startAccessingSecurityScopedResource()

Expand Down
82 changes: 82 additions & 0 deletions Speculid/ClassicObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// ClassicObject.swift
// Speculid
//
// Created by Leo Dion on 7/17/20.
//

import Foundation
import SwiftUI
import Combine
import SpeculidKit

class ClassicObject : ObservableObject {
let url : URL?
@Published var geometryValue : Float = 0.0
@Published var assetDirectoryRelativePath: String = ""
@Published var sourceImageRelativePath: String = ""
@Published var removeAlpha : Bool = false
@Published var backgroundColor: Color = .white
@Published var resizeOption = ResizeOption.none.rawValue
@Published var addBackground: Bool = false

var document: ClassicDocument

var cancellables = [AnyCancellable]()
var assetDirectoryURL : URL? {
self.url?.appendingPathComponent(self.assetDirectoryRelativePath)
}

var sourceImageURL : URL? {
self.url?.appendingPathComponent(self.sourceImageRelativePath)
}

var isAppIcon : Bool {
self.assetDirectoryRelativePath.lowercased().hasSuffix(".appiconset")
}

var isImageSet : Bool {
self.assetDirectoryRelativePath.lowercased().hasSuffix(".imageset")
}

init (url: URL?, document: ClassicDocument) {
self.url = url
self.document = document

self.assetDirectoryRelativePath = self.document.document.assetDirectoryRelativePath
self.sourceImageRelativePath = self.document.document.sourceImageRelativePath
self.addBackground = self.document.document.background != nil
self.backgroundColor = self.document.document.background.map(Color.init) ?? .clear
self.geometryValue = self.document.document.geometry?.value ?? 0.0
self.resizeOption = ResizeOption(geometryType: self.document.document.geometry?.dimension).rawValue
self.removeAlpha = self.document.document.removeAlpha

let nscolorPub = self.$addBackground.combineLatest(self.$backgroundColor) {
$0 ? NSColor($1) : nil
}


let geoPub = self.$resizeOption.map(ResizeOption.init(rawValue:)).combineLatest(self.$geometryValue) { (option, value) -> Geometry? in
switch (option) {
case .some(.width):
return Geometry(value: value, dimension: .width)
case .some(.height):
return Geometry(value: value, dimension: .height)
default:
return nil
}
}

self.assign($assetDirectoryRelativePath, documentProperty: \.assetDirectoryRelativePath)
self.assign($sourceImageRelativePath, documentProperty: \.sourceImageRelativePath)
self.assign($removeAlpha, documentProperty: \.removeAlpha)
self.assign(nscolorPub, documentProperty: \.background)
self.assign(geoPub, documentProperty: \.geometry)
}

func assign<PublisherType : Publisher, ValueType>(_ publisher: PublisherType, documentProperty: WritableKeyPath<SpeculidMutableSpecificationsFile, ValueType>) where PublisherType.Output == ValueType, PublisherType.Failure == Never {
publisher.sink { (value) in
self.document.document[keyPath: documentProperty] = value
}.store(in: &self.cancellables)
}
}
Loading

0 comments on commit 2e01aef

Please sign in to comment.