Skip to content

Commit

Permalink
Add Export/Import configuration #13
Browse files Browse the repository at this point in the history
  • Loading branch information
aahung committed Oct 26, 2018
1 parent 826022e commit a691f8c
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Unshaky.xcodeproj/project.pbxproj
Expand Up @@ -11,6 +11,7 @@
5B3168EE20DB9CF300185DE5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5B3168ED20DB9CF300185DE5 /* Assets.xcassets */; };
5B3168F120DB9CF300185DE5 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5B3168EF20DB9CF300185DE5 /* MainMenu.xib */; };
5B3168FC20DB9ED300185DE5 /* ShakyPressPreventer.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B3168FB20DB9ED300185DE5 /* ShakyPressPreventer.m */; };
5B50712521839D5400A65B81 /* ExportImportViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B50712421839D5400A65B81 /* ExportImportViewController.swift */; };
5BCA730720F5AF4A00A1210E /* Preference.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5BCA730620F5AF4A00A1210E /* Preference.storyboard */; };
5BCA730920F5B43300A1210E /* PreferenceViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BCA730820F5B43300A1210E /* PreferenceViewController.swift */; };
8A8865A9A724FA97AF9FE132 /* Pods_Unshaky.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3210D714B742F537687136CC /* Pods_Unshaky.framework */; };
Expand All @@ -29,6 +30,7 @@
5B3168FA20DB9ED300185DE5 /* ShakyPressPreventer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShakyPressPreventer.h; sourceTree = "<group>"; };
5B3168FB20DB9ED300185DE5 /* ShakyPressPreventer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ShakyPressPreventer.m; sourceTree = "<group>"; };
5B31691220DBC43400185DE5 /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
5B50712421839D5400A65B81 /* ExportImportViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportImportViewController.swift; sourceTree = "<group>"; };
5BCA730620F5AF4A00A1210E /* Preference.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Preference.storyboard; sourceTree = "<group>"; };
5BCA730820F5B43300A1210E /* PreferenceViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceViewController.swift; sourceTree = "<group>"; };
FE12C965EC0C37A2C87000C1 /* Pods-Unshaky.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Unshaky.release.xcconfig"; path = "Target Support Files/Pods-Unshaky/Pods-Unshaky.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,6 +78,7 @@
5B3168FA20DB9ED300185DE5 /* ShakyPressPreventer.h */,
5B3168FB20DB9ED300185DE5 /* ShakyPressPreventer.m */,
5BCA730820F5B43300A1210E /* PreferenceViewController.swift */,
5B50712421839D5400A65B81 /* ExportImportViewController.swift */,
5B3168F920DB9ED300185DE5 /* Unshaky-Bridging-Header.h */,
);
path = Unshaky;
Expand Down Expand Up @@ -245,6 +248,7 @@
files = (
5BCA730920F5B43300A1210E /* PreferenceViewController.swift in Sources */,
5B3168FC20DB9ED300185DE5 /* ShakyPressPreventer.m in Sources */,
5B50712521839D5400A65B81 /* ExportImportViewController.swift in Sources */,
5B3168EC20DB9CF100185DE5 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
88 changes: 88 additions & 0 deletions Unshaky/ExportImportViewController.swift
@@ -0,0 +1,88 @@
//
// ExportImportViewController.swift
// Unshaky
//
// Created by Xinhong LIU on 10/26/18.
// Copyright © 2018 Nested Error. All rights reserved.
//

import Cocoa

class ExportImportViewController: NSViewController {

enum Mode {
case Export
case Import
}

struct Configuration: Codable {
let delays: [Int]
}

@IBOutlet weak var importButtons: NSStackView!
@IBOutlet weak var exportButtons: NSStackView!
@IBOutlet var textView: NSTextView!
weak var preferenceViewController: PreferenceViewController?

var mode: Mode!

override func viewDidLoad() {
super.viewDidLoad()

switch mode {
case .Export?:
importButtons.isHidden = true
loadPreference()
case .Import?:
exportButtons.isHidden = true
textView.string = "Paste the configuration here"
textView.selectAll(textView)
case .none:
break
}
}

func loadPreference() {
let delays = UserDefaults.standard.array(forKey: "delays") as! [Int]
let config = Configuration(delays: delays)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try! encoder.encode(config)
let jsonString = String(data: jsonData, encoding: .utf8)
textView.string = jsonString!
}

@IBAction func copyConfig(_ sender: Any) {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(textView.string, forType: .string)
self.dismiss(self)
}

@IBAction func pasteConfig(_ sender: Any) {
guard let jsonString = NSPasteboard.general.string(forType: .string) else {
return
}
textView.string = jsonString
}

@IBAction func applyConfig(_ sender: Any) {
guard let config = try? JSONDecoder().decode(Configuration.self, from: textView.string.data(using: .utf8)!) else {
let alert = NSAlert()
alert.messageText = "Invalid inputs"
alert.runModal()
return
}

// validate length
guard config.delays.count == 128 else {
let alert = NSAlert()
alert.messageText = "Invalid inputs, length does not match"
alert.runModal()
return
}

UserDefaults.standard.set(config.delays, forKey: "delays")
preferenceViewController?.preferenceChanged(sender: self)
self.dismiss(self)
}
}

0 comments on commit a691f8c

Please sign in to comment.