Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
ObuchiYuki committed Feb 3, 2022
1 parent 1983a90 commit 78b4ee5
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 124 deletions.
8 changes: 8 additions & 0 deletions CoreUtil/CoreUtil.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
B680A8A427A8DA78007CB707 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = B680A8A327A8DA78007CB707 /* Collections */; };
B6AC27A327AA6F5C000FD713 /* Reachability+Publisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6AC27A127AA6F5B000FD713 /* Reachability+Publisher.swift */; };
B6AC27A427AA6F5C000FD713 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6AC27A227AA6F5C000FD713 /* Reachability.swift */; };
B6B5727A27AC223A0069DBA7 /* RestorableState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6B5727927AC223A0069DBA7 /* RestorableState.swift */; };
B6B5727D27AC22480069DBA7 /* RestorableData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6B5727C27AC22480069DBA7 /* RestorableData.swift */; };
B6D1AF3D27A60A210022FED2 /* ExceptionHanlder.m in Sources */ = {isa = PBXBuildFile; fileRef = B6D1AF3A27A60A210022FED2 /* ExceptionHanlder.m */; };
B6D1AF3E27A60A210022FED2 /* ExceptionHanlder.h in Headers */ = {isa = PBXBuildFile; fileRef = B6D1AF3B27A60A210022FED2 /* ExceptionHanlder.h */; settings = {ATTRIBUTES = (Public, ); }; };
B6D1AF3F27A60A210022FED2 /* ExceptionHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D1AF3C27A60A210022FED2 /* ExceptionHandler.swift */; };
Expand Down Expand Up @@ -96,6 +98,8 @@
B680A89B27A8DA16007CB707 /* Action.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Action.swift; sourceTree = "<group>"; };
B6AC27A127AA6F5B000FD713 /* Reachability+Publisher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Reachability+Publisher.swift"; sourceTree = "<group>"; };
B6AC27A227AA6F5C000FD713 /* Reachability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
B6B5727927AC223A0069DBA7 /* RestorableState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestorableState.swift; sourceTree = "<group>"; };
B6B5727C27AC22480069DBA7 /* RestorableData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestorableData.swift; sourceTree = "<group>"; };
B6D1AF3A27A60A210022FED2 /* ExceptionHanlder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExceptionHanlder.m; sourceTree = "<group>"; };
B6D1AF3B27A60A210022FED2 /* ExceptionHanlder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExceptionHanlder.h; sourceTree = "<group>"; };
B6D1AF3C27A60A210022FED2 /* ExceptionHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExceptionHandler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -221,6 +225,8 @@
B64B1ED827A4F73700AC2601 /* Observable.swift */,
B64B1EDB27A4F75600AC2601 /* PipeOperator.swift */,
B64B1ED227A4F71F00AC2601 /* NS+OnAwake.swift */,
B6B5727927AC223A0069DBA7 /* RestorableState.swift */,
B6B5727C27AC22480069DBA7 /* RestorableData.swift */,
);
path = Class;
sourceTree = "<group>";
Expand Down Expand Up @@ -350,6 +356,7 @@
B6AC27A327AA6F5C000FD713 /* Reachability+Publisher.swift in Sources */,
B64B1F2E27A4F83500AC2601 /* Ex+NSControl.swift in Sources */,
B64B1F1627A4F80800AC2601 /* Ex+CGSize.swift in Sources */,
B6B5727A27AC223A0069DBA7 /* RestorableState.swift in Sources */,
B64B1ED927A4F73700AC2601 /* Observable.swift in Sources */,
B64B201127A50E5200AC2601 /* NSColorView.swift in Sources */,
B64B1EE327A4F79100AC2601 /* NSEvent+HotKey.swift in Sources */,
Expand Down Expand Up @@ -377,6 +384,7 @@
B6D1AF3F27A60A210022FED2 /* ExceptionHandler.swift in Sources */,
B64B1ED327A4F71F00AC2601 /* NS+OnAwake.swift in Sources */,
B6AC27A427AA6F5C000FD713 /* Reachability.swift in Sources */,
B6B5727D27AC22480069DBA7 /* RestorableData.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
70 changes: 0 additions & 70 deletions CoreUtil/CoreUtil/Class/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,73 +35,3 @@ public struct Observable<Value> {
self.projectedValue = Publisher(value)
}
}

@propertyWrapper
public struct RestorableState<Value: RawRepresentable> {

public struct Publisher: Combine.Publisher {
public typealias Output = Value
public typealias Failure = Never

let subject: CurrentValueSubject<Value, Never>

init(_ value: Value) { self.subject = CurrentValueSubject(value) }

public func receive<S: Subscriber>(subscriber: S) where S.Failure == Self.Failure, S.Input == Self.Output {
self.subject.receive(subscriber: subscriber)
}
}

public let projectedValue: Publisher
public let key: String

public var wrappedValue: Value {
get { projectedValue.subject.value }
set {
projectedValue.subject.send(newValue)
UserDefaults.standard.set(newValue.rawValue, forKey: key)
}
}
public init(wrappedValue initialValue: Value, _ key: String) {
let wrappedValue: Value

if let rawValue = UserDefaults.standard.object(forKey: key) as? Value.RawValue, let value = Value(rawValue: rawValue) {
wrappedValue = value
} else {
wrappedValue = initialValue
}

self.projectedValue = Publisher(wrappedValue)
self.key = key
}
}

extension String: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}

extension Bool: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}

extension Optional: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}

extension Int: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}

extension CGFloat: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}

extension Double: RawRepresentable {
public var rawValue: Self { self }
public init(rawValue: Self) { self = rawValue }
}
91 changes: 91 additions & 0 deletions CoreUtil/CoreUtil/Class/RestorableData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// RestorableData.swift
// CoreUtil
//
// Created by yuki on 2022/02/03.
//

import Cocoa

private let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(Bundle.main.bundleIdentifier ?? "com.noname.app")

private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
private let restorableDataURL = temporaryDirectoryURL.appendingPathComponent("RestorableData") => {
try? FileManager.default.createDirectory(at: $0, withIntermediateDirectories: true, attributes: nil)
}

@propertyWrapper
public struct RestorableData<Value: Codable> {
public struct Publisher: Combine.Publisher {
public typealias Output = Value
public typealias Failure = Never

let subject: CurrentValueSubject<Value, Never>

init(_ value: Value) { self.subject = CurrentValueSubject(value) }

public func receive<S: Subscriber>(subscriber: S) where S.Failure == Self.Failure, S.Input == Self.Output {
self.subject.receive(subscriber: subscriber)
}
}

public let projectedValue: Publisher
public let key: String
public let fileURL: URL

public var wrappedValue: Value {
get { projectedValue.subject.value }
set {
projectedValue.subject.send(newValue)
do { try encoder.encode(newValue).write(to: fileURL) } catch {}
}
}
public init(wrappedValue initialValue: Value, _ key: String) {
self.key = key
self.fileURL = restorableDataURL.appendingPathComponent(key + ".json")

let wrappedValue: Value
do {
wrappedValue = try decoder.decode(Value.self, from: Data(contentsOf: fileURL))
} catch {
wrappedValue = initialValue
}

self.projectedValue = Publisher(wrappedValue)
}
}

final public class NSImageContainer: Codable {
static let dataDirectoryURL = temporaryDirectoryURL.appendingPathComponent("NSImageContainer") => {
try? FileManager.default.createDirectory(at: $0, withIntermediateDirectories: true, attributes: nil)
}

public let image: NSImage
public let id: String
public init(_ image: NSImage) {
self.image = image
self.id = UUID().uuidString
}

public static func wrap(_ image: NSImage) -> NSImageContainer { NSImageContainer(image) }

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(id)
let fileURL = NSImageContainer.dataDirectoryURL.appendingPathComponent(id)
if !FileManager.default.fileExists(atPath: fileURL.path) {
try image.tiffRepresentation?.write(to: fileURL)
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let id = try container.decode(String.self)
let fileURL = NSImageContainer.dataDirectoryURL.appendingPathComponent(id)
guard let image = NSImage(contentsOf: fileURL) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "No image"))
}
self.image = image
self.id = id
}
}
8 changes: 4 additions & 4 deletions DevToys/DevToys.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
B64B202627A52DB400AC2601 /* ToolType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64B202527A52DB400AC2601 /* ToolType.swift */; };
B64B209827A532DF00AC2601 /* AppWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64B209727A532DF00AC2601 /* AppWindowController.swift */; };
B65DB78027AC0EB400146A3C /* RegexTesterView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = B65DB77F27AC0EB400146A3C /* RegexTesterView+.swift */; };
B66850EE27A64D3200A3FE01 /* ToolPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = B66850ED27A64D3200A3FE01 /* ToolPage.swift */; };
B66850EE27A64D3200A3FE01 /* Page.swift in Sources */ = {isa = PBXBuildFile; fileRef = B66850ED27A64D3200A3FE01 /* Page.swift */; };
B66850F227A65FC200A3FE01 /* SwiftJSONFormatter in Frameworks */ = {isa = PBXBuildFile; productRef = B66850F127A65FC200A3FE01 /* SwiftJSONFormatter */; };
B672CF6527AA763100391A5D /* NetworkSpeedTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B672CF6427AA763100391A5D /* NetworkSpeedTest.swift */; };
B672CF9727AA7E2C00391A5D /* APITestView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = B672CF9627AA7E2C00391A5D /* APITestView+.swift */; };
Expand Down Expand Up @@ -130,7 +130,7 @@
B64B202527A52DB400AC2601 /* ToolType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolType.swift; sourceTree = "<group>"; };
B64B209727A532DF00AC2601 /* AppWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppWindowController.swift; sourceTree = "<group>"; };
B65DB77F27AC0EB400146A3C /* RegexTesterView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RegexTesterView+.swift"; sourceTree = "<group>"; };
B66850ED27A64D3200A3FE01 /* ToolPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolPage.swift; sourceTree = "<group>"; };
B66850ED27A64D3200A3FE01 /* Page.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Page.swift; sourceTree = "<group>"; };
B672CF6427AA763100391A5D /* NetworkSpeedTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkSpeedTest.swift; sourceTree = "<group>"; };
B672CF9627AA7E2C00391A5D /* APITestView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APITestView+.swift"; sourceTree = "<group>"; };
B672CF9F27AAB8DD00391A5D /* FileDrop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileDrop.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -379,7 +379,7 @@
B6D1AEF927A557280022FED2 /* PopupButton.swift */,
B6D1AEFC27A55ED50022FED2 /* CodeTextView.swift */,
B672CF9F27AAB8DD00391A5D /* FileDrop.swift */,
B66850ED27A64D3200A3FE01 /* ToolPage.swift */,
B66850ED27A64D3200A3FE01 /* Page.swift */,
B608541F27A67E4D003BF243 /* TextField.swift */,
B680A83227A8BF7D007CB707 /* TextViewSection.swift */,
B680A86727A8D4D1007CB707 /* TextFieldSection.swift */,
Expand Down Expand Up @@ -519,7 +519,7 @@
B64B1F7727A4FF8B00AC2601 /* ToolMenuView+.swift in Sources */,
B64B202327A52A2D00AC2601 /* R.swift in Sources */,
B64B201D27A5219400AC2601 /* SidebarSearchView+.swift in Sources */,
B66850EE27A64D3200A3FE01 /* ToolPage.swift in Sources */,
B66850EE27A64D3200A3FE01 /* Page.swift in Sources */,
B6D1AEF427A54C440022FED2 /* Section.swift in Sources */,
B64B1F7227A4FDC800AC2601 /* AppModel.swift in Sources */,
B6D1AEFA27A557280022FED2 /* PopupButton.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Coder/Base64Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private enum SourceType: String, TextItem {
var title: String { rawValue }
}

final private class Base64DecoderView: ToolPage {
final private class Base64DecoderView: Page {
let sourceTypePicker = EnumPopupButton<SourceType>()

let fileDrop = FileDrop()
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Coder/HTMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class HTMLDecoderViewController: ToolPageViewController {
}
}

final private class HTMLDecoderView: ToolPage {
final private class HTMLDecoderView: Page {

let encodeTextSection = CodeViewSection(title: "Encoded", options: [.all], language: .xml)
let decodeTextSection = TextViewSection(title: "Decoded", options: [.all])
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Coder/JWTDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class JWTDecoderViewController: ToolPageViewController {
}
}

final private class JWTDecoderView: ToolPage {
final private class JWTDecoderView: Page {

let tokenTextSection = TextViewSection(title: "JWT Token", options: .defaultInput)
let headerCodeSection = CodeViewSection(title: "Header", options: .defaultOutput, language: .javascript)
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Coder/URLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class URLDecoderViewController: ToolPageViewController {
}
}

final private class URLDecoderView: ToolPage {
final private class URLDecoderView: Page {
let encodeTextSection = TextViewSection(title: "Encoded", options: [.all])
let decodeTextSection = TextViewSection(title: "Decoded", options: [.all])

Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Convert/JSONYamlConverter+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private enum FormatStyle: String, TextItem {
var title: String { rawValue }
}

final private class JSONYamlConverterView: ToolPage {
final private class JSONYamlConverterView: Page {

let formatStylePicker = EnumPopupButton<FormatStyle>()

Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Convert/NumberBaseConverter+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class NumberBaseConverterViewController: ToolPageViewController {
}
}

final private class NumberBaseConverterView: ToolPage {
final private class NumberBaseConverterView: Page {
let formatSwitch = NSSwitch()

let decimalSection = TextFieldSection(title: "Decimal")
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Format/JSONFormatter+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum JSONSpacingType: String, TextItem {
var title: String { rawValue }
}

final private class JSONFormatterView: ToolPage {
final private class JSONFormatterView: Page {

let indentControl = EnumPopupButton<JSONSpacingType>()
let inputSection = CodeViewSection(title: "Input", options: .defaultInput, language: .javascript)
Expand Down
19 changes: 12 additions & 7 deletions DevToys/DevToys/Body/Generator/HashGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ final class HashGeneratorViewController: ToolPageViewController {

override func viewDidLoad() {
self.$isUppercase.sink{[unowned self] in self.cell.formatSwitch.isOn = $0 }.store(in: &objectBag)
self.$input.sink{[unowned self] in self.cell.inputSection.string = $0 }.store(in: &objectBag)
self.$input.sink{[unowned self] in self.cell.textInputSection.string = $0 }.store(in: &objectBag)
self.$md5.sink{[unowned self] in self.cell.md5Section.string = $0 }.store(in: &objectBag)
self.$sha1.sink{[unowned self] in self.cell.sha1Section.string = $0 }.store(in: &objectBag)
self.$sha256.sink{[unowned self] in self.cell.sha256Section.string = $0 }.store(in: &objectBag)
self.$sha512.sink{[unowned self] in self.cell.sha512Section.string = $0 }.store(in: &objectBag)

self.cell.inputSection.stringPublisher
.sink{[unowned self] in self.input = $0; updateHash() }.store(in: &objectBag)
// self.cell.inputSection.stringPublisher
// .sink{[unowned self] in self.input = $0; updateHash() }.store(in: &objectBag)
self.cell.formatSwitch.isOnPublisher
.sink{[unowned self] in self.isUppercase = $0; updateHash() }.store(in: &objectBag)
}
Expand All @@ -42,10 +42,14 @@ final class HashGeneratorViewController: ToolPageViewController {
}
}

final class HashGeneratorView: ToolPage {
final class HashGeneratorView: Page {
let formatSwitch = NSSwitch()

let inputSection = TextViewSection(title: "Input", options: .all)
let fileDrop = FileDrop()
lazy var fileInputSection = Section(title: "Input", items: [fileDrop])
let textInputSection = TextViewSection(title: "Input", options: .all)
let inputSection = NSPlaceholderView()

let md5Section = TextFieldSection(title: "MD5", isEditable: false)
let sha1Section = TextFieldSection(title: "SHA1", isEditable: false)
let sha256Section = TextFieldSection(title: "SHA256", isEditable: false)
Expand All @@ -60,8 +64,9 @@ final class HashGeneratorView: ToolPage {
self.addSection(configurationSection)

self.addSection(inputSection)
self.inputSection.textView.snp.remakeConstraints{ make in
make.height.equalTo(100)
self.inputSection.contentView = textInputSection
self.inputSection.snp.remakeConstraints{ make in
make.height.equalTo(180)
}

self.addSection(md5Section)
Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Generator/LoremIpsumGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ enum LoremIpsumGenerateType: String, TextItem {
var title: String { rawValue }
}

final private class LoremIpsumGeneratorView: ToolPage {
final private class LoremIpsumGeneratorView: Page {
let typePicker = EnumPopupButton<LoremIpsumGenerateType>()
let lengthField = NumberField()

Expand Down
2 changes: 1 addition & 1 deletion DevToys/DevToys/Body/Generator/UUIDGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class UUIDGeneratorViewController: ToolPageViewController {
}
}

final private class UUIDGeneratorView: ToolPage {
final private class UUIDGeneratorView: Page {
let hyphensSwitch = NSSwitch()
let uppercaseSwitch = NSSwitch()
let generateCount = NumberField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class ImageOptimaizerViewController: ToolPageViewController {
}


final private class ImageOptimaizerView: ToolPage {
final private class ImageOptimaizerView: Page {
private let listView = NSTableView.list()

let urlPublisher = PassthroughSubject<[URL], Never>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ enum ImageOptimizer {
if ext == "png" { return PngOptimizer.optimize(url, optimizeLevel: optimizeLevel) }
if ext == "jpg" || ext == "jpeg" { return JpegOptimizer.optimize(url, optimizeLevel: optimizeLevel) }

assertionFailure("\(ext) is not supported.")
return nil
}
}
Expand Down
Loading

0 comments on commit 78b4ee5

Please sign in to comment.