Skip to content

Commit

Permalink
cleaned up source files
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jul 14, 2020
1 parent 3ea4c73 commit 7dd9c82
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 363 deletions.
47 changes: 4 additions & 43 deletions Speculid.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
B347128B24B668DE001CCCFD /* ClassicView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B347128A24B668DE001CCCFD /* ClassicView.swift */; };
B34B6BBE24BDEF2B00F4EE6B /* CairoSVG.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = B37B472124B93CB900AE2021 /* CairoSVG.xcframework */; };
B34B6BBF24BDEF2B00F4EE6B /* CairoSVG.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B37B472124B93CB900AE2021 /* CairoSVG.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
B34B6BC224BE354D00F4EE6B /* BookmarkURLCollectionObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = B34B6BC124BE354D00F4EE6B /* BookmarkURLCollectionObject.swift */; };
B3796B7C2432C50100E5F5F9 /* speculid in CopyFiles */ = {isa = PBXBuildFile; fileRef = B3DB4824243166FC00B1DA4F /* speculid */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
B3B6A4CF24B15292009C6E32 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B364624224B14DBB0089382E /* ContentView.swift */; };
B3B6A4D024B15292009C6E32 /* doc_appDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = B364624024B14DBB0089382E /* doc_appDocument.swift */; };
B3B6A4D124B15292009C6E32 /* doc_appApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = B364623E24B14DBB0089382E /* doc_appApp.swift */; };
B3DB48122431642100B1DA4F /* Document.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = B3DB48102431642100B1DA4F /* Document.xcdatamodeld */; };
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 */; };
Expand Down Expand Up @@ -69,21 +69,16 @@
/* Begin PBXFileReference section */
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>"; };
B364623E24B14DBB0089382E /* doc_appApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = doc_appApp.swift; sourceTree = "<group>"; };
B364624024B14DBB0089382E /* doc_appDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = doc_appDocument.swift; sourceTree = "<group>"; };
B364624224B14DBB0089382E /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ContentView.swift; path = "../doc-app/ContentView.swift"; sourceTree = "<group>"; };
B364624424B14DBD0089382E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
B364624724B14DBD0089382E /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
B364624924B14DBD0089382E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B364624A24B14DBD0089382E /* doc_app.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = doc_app.entitlements; sourceTree = "<group>"; };
B3796B792432B7FF00E5F5F9 /* Command.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Command.entitlements; sourceTree = "<group>"; };
B37A52372435014400FEA6E9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B37B472124B93CB900AE2021 /* CairoSVG.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = CairoSVG.xcframework; path = packages/SpeculidKit/packages/CairoSVG/build/CairoSVG.xcframework; sourceTree = "<group>"; };
B39F286B243817F700A69B85 /* SpeculidKit */ = {isa = PBXFileReference; lastKnownFileType = folder; path = SpeculidKit; sourceTree = "<group>"; };
B3DB48072431642100B1DA4F /* Speculid.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Speculid.app; sourceTree = BUILT_PRODUCTS_DIR; };
B3DB480C2431642100B1DA4F /* Document.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Document.swift; sourceTree = "<group>"; };
B3DB480E2431642100B1DA4F /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
B3DB48112431642100B1DA4F /* Document.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Document.xcdatamodel; sourceTree = "<group>"; };
B3DB48132431642600B1DA4F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
B3DB48162431642600B1DA4F /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
B3DB48192431642600B1DA4F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -121,25 +116,6 @@
name = Frameworks;
sourceTree = "<group>";
};
B364623D24B14DBB0089382E /* doc-app */ = {
isa = PBXGroup;
children = (
B364624424B14DBD0089382E /* Assets.xcassets */,
B364624924B14DBD0089382E /* Info.plist */,
B364624A24B14DBD0089382E /* doc_app.entitlements */,
B364624624B14DBD0089382E /* Preview Content */,
);
path = "doc-app";
sourceTree = "<group>";
};
B364624624B14DBD0089382E /* Preview Content */ = {
isa = PBXGroup;
children = (
B364624724B14DBD0089382E /* Preview Assets.xcassets */,
);
path = "Preview Content";
sourceTree = "<group>";
};
B37A5236243500C900FEA6E9 /* command */ = {
isa = PBXGroup;
children = (
Expand All @@ -164,7 +140,6 @@
B39F13FE243817AB00A69B85 /* packages */,
B37A5236243500C900FEA6E9 /* command */,
B3DB48092431642100B1DA4F /* Speculid */,
B364623D24B14DBB0089382E /* doc-app */,
B3DB48082431642100B1DA4F /* Products */,
B303554824381F1200A8EAB9 /* Frameworks */,
);
Expand All @@ -185,16 +160,15 @@
B364623E24B14DBB0089382E /* doc_appApp.swift */,
B364624024B14DBB0089382E /* doc_appDocument.swift */,
B364624224B14DBB0089382E /* ContentView.swift */,
B3DB480C2431642100B1DA4F /* Document.swift */,
B3DB480E2431642100B1DA4F /* ContentView.swift */,
B3DB48132431642600B1DA4F /* Assets.xcassets */,
B3DB48182431642600B1DA4F /* Main.storyboard */,
B3DB481B2431642600B1DA4F /* Info.plist */,
B3DB481C2431642600B1DA4F /* Speculid.entitlements */,
B3DB48102431642100B1DA4F /* Document.xcdatamodeld */,
B3DB48152431642600B1DA4F /* Preview Content */,
B347128824B64968001CCCFD /* ClassicDocument.swift */,
B347128A24B668DE001CCCFD /* ClassicView.swift */,
B34B6BC124BE354D00F4EE6B /* BookmarkURLCollectionObject.swift */,
);
path = Speculid;
sourceTree = "<group>";
Expand Down Expand Up @@ -304,7 +278,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B3DB48122431642100B1DA4F /* Document.xcdatamodeld in Sources */,
B34B6BC224BE354D00F4EE6B /* BookmarkURLCollectionObject.swift in Sources */,
B347128B24B668DE001CCCFD /* ClassicView.swift in Sources */,
B347128924B64968001CCCFD /* ClassicDocument.swift in Sources */,
B3B6A4D124B15292009C6E32 /* doc_appApp.swift in Sources */,
Expand Down Expand Up @@ -781,19 +755,6 @@
productName = SpeculidKit;
};
/* End XCSwiftPackageProductDependency section */

/* Begin XCVersionGroup section */
B3DB48102431642100B1DA4F /* Document.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
B3DB48112431642100B1DA4F /* Document.xcdatamodel */,
);
currentVersion = B3DB48112431642100B1DA4F /* Document.xcdatamodel */;
path = Document.xcdatamodeld;
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
/* End XCVersionGroup section */
};
rootObject = B3DB47FD243162DA00B1DA4F /* Project object */;
}
79 changes: 79 additions & 0 deletions Speculid/BookmarkURLCollectionObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// BookmarkURLCollectionObject.swift
// Speculid
//
// Created by Leo Dion on 7/14/20.
//

import Foundation
public class BookmarkURLCollectionObject : ObservableObject {

//@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]()])
return combined
}()

@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
}
var bookmarkMap = Self.shared.bookmarks ?? [String : Data]()
bookmarkMap[url.path] = newData
Self.shared.bookmarks = bookmarkMap

}

func reset () {
Self.shared.bookmarks = [String : Data]()
}

func isAvailable(basedOn baseURL: URL?, relativePath: String ...) -> Bool {
guard let baseURL = baseURL else {
return false
}

var url = baseURL.deletingLastPathComponent()
for path in relativePath {
url.appendPathComponent(path)
}

return self.bookmarks[url] != nil
}

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
}


// if isStale {
// DispatchQueue.global().async {
// saveBookmark(url)
// }
//
// }
debugPrint("Adding \(isStale ? "stale" : "fresh") bookmark of \(path) for \(url)")
return (URL(fileURLWithPath: path), url)
}

init () {
let bookmarkPublisher = Self.shared.publisher(for: \.bookmarks).compactMap{$0}
bookmarkPublisher.map{
Dictionary(uniqueKeysWithValues:
$0.compactMap(Self.transformPath))
}.receive(on: DispatchQueue.main).assign(to: self.$bookmarks)


}
}
24 changes: 1 addition & 23 deletions Speculid/ClassicDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ClassicDocument: FileDocument {
fileWrapper = FileWrapper(regularFileWithContents: data)
}

func build (fromURL url: URL, onExport: (([URL], @escaping ((Result<[URL : URL], Error>) -> Void)) -> Void)) {
func build (fromURL url: URL) {

let management = FileManagement()

Expand Down Expand Up @@ -101,28 +101,6 @@ struct ClassicDocument: FileDocument {
}

processImages(fromURL: url, management: management, document: document, sandboxMap: urlMap)
// let sandboxMap : [(AssetSpecificationProtocol, String, URL)]
// do {
// sandboxMap = try urlMap.map{ (asset, fileName, url) in
// (asset, fileName, try management.bookmarkURL(fromURL: url))
// }
// } catch {
// onExport(urlMap.map{$0.2}) { (urlsResult) in
// guard case let .success(urls) = urlsResult else {
// return
// }
// let sandboxMap = urlMap.map{ (asset, fileName, url) in
// (asset, fileName, (urls[url]) ?? (try? management.bookmarkURL(fromURL: url)) ?? url)
// }
// processImages(fromURL: url, management: management, document: document, sandboxMap: sandboxMap)
// }
// return
// }
//
//
// processImages(fromURL: url, management: management, document: document, sandboxMap: sandboxMap)

// builder.build(document: document)
}

func processImages (fromURL url: URL, management: FileManagement, document : SpeculidDocument, sandboxMap: [(AssetSpecificationProtocol, String, URL)]) {
Expand Down
110 changes: 4 additions & 106 deletions Speculid/ClassicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@
import SwiftUI
import SpeculidKit

@propertyWrapper struct UserDefaultsBacked<Value> {
let key: String
let defaultValue: Value
var storage: UserDefaults = .standard

var wrappedValue: Value {
get { UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue }
nonmutating set { UserDefaults.standard.set(newValue, forKey: key) }
}
var projectedValue: Binding<Value> {
Binding<Value>( get: { self.wrappedValue }, set: { newValue in self.wrappedValue = newValue } )
}
}

extension UserDefaults {

@objc
Expand All @@ -35,77 +21,7 @@ extension UserDefaults {
}
}

public class BookmarkURLCollectionObject : ObservableObject {

//@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]()])
return combined
}()

@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
}
var bookmarkMap = Self.shared.bookmarks ?? [String : Data]()
bookmarkMap[url.path] = newData
Self.shared.bookmarks = bookmarkMap

}

func reset () {
Self.shared.bookmarks = [String : Data]()
}

func isAvailable(basedOn baseURL: URL?, relativePath: String ...) -> Bool {
guard let baseURL = baseURL else {
return false
}

var url = baseURL.deletingLastPathComponent()
for path in relativePath {
url.appendPathComponent(path)
}

return self.bookmarks[url] != nil
}

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
}


// if isStale {
// DispatchQueue.global().async {
// saveBookmark(url)
// }
//
// }
debugPrint("Adding \(isStale ? "stale" : "fresh") bookmark of \(path) for \(url)")
return (URL(fileURLWithPath: path), url)
}

init () {
let bookmarkPublisher = Self.shared.publisher(for: \.bookmarks).compactMap{$0}
bookmarkPublisher.map{
Dictionary(uniqueKeysWithValues:
$0.compactMap(Self.transformPath))
}.receive(on: DispatchQueue.main).assign(to: self.$bookmarks)


}
}


struct ClassicView: View {
Expand All @@ -118,6 +34,8 @@ struct ClassicView: View {

var canBuild : Bool {
return url != nil &&

self.bookmarkCollection.isAvailable(basedOn: self.url, relativePath: self.document.document.assetDirectoryRelativePath) &&
self.bookmarkCollection.isAvailable(basedOn: self.url, relativePath: self.document.document.assetDirectoryRelativePath, "Contents.json") &&
self.bookmarkCollection.isAvailable(basedOn: self.url, relativePath: self.document.document.sourceImageRelativePath)
}
Expand Down Expand Up @@ -170,7 +88,7 @@ struct ClassicView: View {
}
}, label: {
HStack{
Image(systemName: self.bookmarkCollection.isAvailable(basedOn: self.url, relativePath: self.document.document.assetDirectoryRelativePath, "Contents.json") ? "lock.open.fill" : "lock.fill")
Image(systemName: self.bookmarkCollection.isAvailable(basedOn: self.url, relativePath: self.document.document.assetDirectoryRelativePath) ? "lock.open.fill" : "lock.fill")
Image(systemName: "photo.fill")
Text(self.document.document.assetDirectoryRelativePath)
}
Expand Down Expand Up @@ -208,27 +126,7 @@ struct ClassicView: View {
Button("Build") {
if let url = self.url {

self.document.build(fromURL: url, onExport: {
urls, callback in
exportFiles(moving: urls) { (result) in
guard let result = result else {
callback(.failure(NSError()))
return
}
let flatResult = result.flatMap { (sourceUrls) in
Result{
try sourceUrls.map{
url throws -> (URL,URL) in
try fileManagement.saveBookmark(url)
let resultURL = try fileManagement.bookmarkURL(fromURL: url)
return (url, resultURL)
}
}
}
let dictionaryResult = flatResult.map(Dictionary.init(uniqueKeysWithValues:))
callback(dictionaryResult)
}
})
self.document.build(fromURL: url)
}
}.disabled(!self.canBuild)

Expand Down
Loading

0 comments on commit 7dd9c82

Please sign in to comment.