Skip to content

Commit

Permalink
Merge branch 'release/v0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
gee1k committed Sep 6, 2019
2 parents 586c339 + dada4b3 commit 2662c77
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 16 deletions.
14 changes: 14 additions & 0 deletions uPic/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@
<outlet property="cancelUploadMenuSeparator" destination="ggN-TB-JZW" id="V4H-J4-ftj"/>
<outlet property="checkUpdateMenuItem" destination="yai-4F-Btp" id="klG-hy-GGO"/>
<outlet property="compressFactorMenuItem" destination="eIU-EA-uQZ" id="uMq-Nm-vXy"/>
<outlet property="exportHostsMenuItem" destination="Yuc-Vp-9Pd" id="JIW-vZ-WbR"/>
<outlet property="helpMenuItem" destination="2Jw-ET-3eH" id="Ubq-wz-L3N"/>
<outlet property="historyMenuItem" destination="EhO-Zw-XXj" id="okQ-0q-kK5"/>
<outlet property="hostMenuItem" destination="d02-bT-1Lv" id="Ah0-J0-Z4M"/>
<outlet property="importHostsMenuItem" destination="HNM-7K-zzn" id="Jmj-DT-7ky"/>
<outlet property="ouputFormatMenuItem" destination="1BX-LF-q51" id="H54-M9-CBq"/>
<outlet property="preferenceMenuItem" destination="VW7-Z2-hXR" id="Mhm-Tb-F4W"/>
<outlet property="quitMenuItem" destination="0JF-m2-1iq" id="qTr-PH-bBg"/>
Expand Down Expand Up @@ -213,6 +215,18 @@
<action selector="guideMenuItemClicked:" target="veO-oa-UPB" id="PRm-Bb-SKG"/>
</connections>
</menuItem>
<menuItem title="导入图床配置" id="HNM-7K-zzn">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="importHostsMenuItemClicked:" target="veO-oa-UPB" id="76m-Ru-6rH"/>
</connections>
</menuItem>
<menuItem title="导出图床配置" id="Yuc-Vp-9Pd">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="exportHostsMenuItemClicked:" target="veO-oa-UPB" id="ZAp-pp-wFi"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down
21 changes: 21 additions & 0 deletions uPic/Extensions/NotificationExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ class NotificationExt:NSObject {
self.post(title: NSLocalizedString("notification.upload.task-not-complete.subtitle", comment: ""),
info: body!)
}


func postImportErrorNotice(_ body: String? = NSLocalizedString("notification.import.error.body", comment: "")) {
self.post(title: NSLocalizedString("notification.import.error.title", comment: ""),
info: body!)
}

func postImportSuccessfulNotice() {
self.post(title: NSLocalizedString("notification.success.title", comment: ""),
info: NSLocalizedString("notification.import.success.body", comment: ""))
}

func postExportErrorNotice(_ body: String? = NSLocalizedString("notification.export.error.body.invalid", comment: "")) {
self.post(title: NSLocalizedString("notification.upload.task-not-complete.subtitle", comment: ""),
info: body!)
}

func postExportSuccessfulNotice() {
self.post(title: NSLocalizedString("notification.success.title", comment: ""),
info: NSLocalizedString("notification.export.success.body", comment: ""))
}
}

@available(OSX 10.14, *)
Expand Down
105 changes: 105 additions & 0 deletions uPic/General/Managers/ConfigManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,108 @@ extension ConfigManager {
}
}

extension ConfigManager {
// import & export config

func importHosts() {
NSApp.activate(ignoringOtherApps: true)
let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.allowedFileTypes = ["json"]

openPanel.begin { (result) -> Void in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
guard let url = openPanel.url,
let data = NSData(contentsOfFile: url.path),
let array = try? JSONSerialization.jsonObject(with: data as Data) as? [String]
else {
NotificationExt.shared.postImportErrorNotice()
return
}
let hostItems = array.map(){ str in
return Host.deserialize(str: str)
}.filter { $0 != nil }
if hostItems.count == 0 {
NotificationExt.shared.postImportErrorNotice()
return
}

// choose import method

let alert = NSAlert()

alert.messageText = NSLocalizedString("alert.import_hosts_title", comment: "")
alert.informativeText = NSLocalizedString("alert.import_hosts_description", comment: "")

alert.addButton(withTitle: NSLocalizedString("alert.import_hosts_merge", comment: "")).refusesFirstResponder = true

alert.addButton(withTitle: NSLocalizedString("alert.import_hosts_overwrite", comment: "")).refusesFirstResponder = true

let modalResult = alert.runModal()

switch modalResult {
case .alertFirstButtonReturn:
// current Items
var currentHostItems = ConfigManager.shared.getHostItems()
for host in hostItems {
let isContains = currentHostItems.contains(where: {item in
return item == host
})
if (!isContains) {
currentHostItems.append(host!)
}
}
ConfigManager.shared.setHostItems(items: currentHostItems)
NotificationExt.shared.postImportSuccessfulNotice()
case .alertSecondButtonReturn:
ConfigManager.shared.setHostItems(items: hostItems as! [Host])
NotificationExt.shared.postImportSuccessfulNotice()
default:
print("Cancel Import")
}
}
}
}

func exportHosts() {
let hostItems = ConfigManager.shared.getHostItems()
if hostItems.count == 0 {
NotificationExt.shared.postExportErrorNotice(NSLocalizedString("notification.export.error.body.no-hosts", comment: ""))
return
}

NSApp.activate(ignoringOtherApps: true)
let savePanel = NSSavePanel()
savePanel.directoryURL = URL(fileURLWithPath: NSHomeDirectory().appendingPathComponent(path: "Documents"))
savePanel.nameFieldStringValue = "uPic_hosts.json"
savePanel.allowsOtherFileTypes = false
savePanel.isExtensionHidden = true
savePanel.canCreateDirectories = true
savePanel.allowedFileTypes = ["json"]

savePanel.begin { (result) -> Void in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {

guard let url = savePanel.url else {
NotificationExt.shared.postImportErrorNotice()
return
}

let hostStrArr = hostItems.map(){ hostItem in
return hostItem.serialize()
}
if (!JSONSerialization.isValidJSONObject(hostStrArr)) {
NotificationExt.shared.postImportErrorNotice()
return
}
let os = OutputStream(toFileAtPath: url.path, append: false)
os?.open()
JSONSerialization.writeJSONObject(hostStrArr, to: os!, options: .prettyPrinted, error: .none)
os?.close()
NotificationExt.shared.postExportSuccessfulNotice()
}
}
}
}
4 changes: 3 additions & 1 deletion uPic/General/Utils/PreferenceKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ extension UserDefaults {
for item in arr {
let str = item as! String
let host = Host.deserialize(str: str)
result.append(host)
if host != nil {
result.append(host!)
}
}
}
return result
Expand Down
10 changes: 5 additions & 5 deletions uPic/Models/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ class Host: Equatable, CustomDebugStringConvertible, Codable {
}

func copy() -> Host {
let newHost = Host.deserialize(str: self.serialize())
let newHost = Host.deserialize(str: self.serialize())!
newHost.id = Date().timeStamp
return newHost
}

static func deserialize(str: String) -> Host {
let data = str.data(using: String.Encoding.utf8)!
let json = try! JSON(data: data)
let type = HostType(rawValue: json["type"].intValue)!
static func deserialize(str: String) -> Host? {
guard let data = str.data(using: String.Encoding.utf8), let json = try? JSON(data: data),let type = HostType(rawValue: json["type"].intValue) else {
return nil
}
let hostData = HostConfig.deserialize(type: type, str: json["data"].string)

let host = Host(type, data: hostData)
Expand Down
2 changes: 1 addition & 1 deletion uPic/PreferencesWindow/Base.lproj/Preferences.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ Gw
<image name="NSAdvanced" width="32" height="32"/>
<image name="NSPreferencesGeneral" width="32" height="32"/>
<image name="NSRemoveTemplate" width="11" height="11"/>
<image name="about" width="256" height="256"/>
<image name="about" width="1024" height="1024"/>
<image name="host" width="64" height="64"/>
</resources>
</document>
22 changes: 19 additions & 3 deletions uPic/PreferencesWindow/HostPreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,18 @@ class HostPreferencesViewController: PreferencesViewController {
}
}


override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.delegate = self

self.refreshButtonStatus()
self.initHostItems()
self.addObserver()
}

override func viewDidDisappear() {
super.viewDidDisappear()
self.removeObserver()

}

@IBAction func addHostButtonClicked(_ sender: NSPopUpButton) {
Expand Down Expand Up @@ -154,6 +153,23 @@ class HostPreferencesViewController: PreferencesViewController {

// MARK: 将正在编辑的输入框执行 endEdit
func blurEditingTextField() {
for view in self.tableView.subviews {
if !(view is NSTableRowView) {
continue
}
for subView in view.subviews {
if !(subView is NSTableCellView) {
continue
}
let sV = subView as! NSTableCellView
if let subTextField = sV.textField, let editor = subTextField.currentEditor() {
subTextField.endEditing(editor)
}

}
}


for view in self.configView.subviews {
for subView in view.subviews {
if !(subView is NSTextField) {
Expand Down Expand Up @@ -291,6 +307,7 @@ class HostPreferencesViewController: PreferencesViewController {
@objc func hostConfigChanged() {
hostConfigChangedDebouncedFunc(false)
}


func addObserver() {
// 设置监听配置变化的节流函数,当0.5秒后没有再次变化就刷新当前状态
Expand Down Expand Up @@ -369,7 +386,6 @@ extension HostPreferencesViewController: NSTextFieldDelegate {
}

extension HostPreferencesViewController: NSWindowDelegate {

func windowShouldClose(_ sender: NSWindow) -> Bool {
if self.hostItemsChanged, let window = self.view.window {
let alert = NSAlert()
Expand Down
5 changes: 5 additions & 0 deletions uPic/PreferencesWindow/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class PreferencesWindowController: NSWindowController {

}
extension PreferencesWindowController: NSWindowDelegate {

func windowWillBeginSheet(_ notification: Notification) {
print("ssss")
}

func windowWillClose(_ notification: Notification) {
// 关闭偏好设置时在去掉 Dock 栏显示应用图标
NSApp.setActivationPolicy(.accessory)
Expand Down
4 changes: 2 additions & 2 deletions uPic/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.11.2</string>
<string>0.12.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
Expand All @@ -32,7 +32,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>20190901</string>
<string>20190906</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
18 changes: 17 additions & 1 deletion uPic/Views/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class StatusMenuController: NSObject, NSMenuDelegate {
@IBOutlet weak var helpMenuItem: NSMenuItem!
@IBOutlet weak var checkUpdateMenuItem: NSMenuItem!
@IBOutlet weak var tutorialMenuItem: NSMenuItem!
@IBOutlet weak var importHostsMenuItem: NSMenuItem!
@IBOutlet weak var exportHostsMenuItem: NSMenuItem!

@IBOutlet weak var sponsorMenuItem: NSMenuItem!
@IBOutlet weak var quitMenuItem: NSMenuItem!

Expand All @@ -47,6 +50,8 @@ class StatusMenuController: NSObject, NSMenuDelegate {
helpMenuItem.title = NSLocalizedString("status-menu.help", comment: "help")
checkUpdateMenuItem.title = NSLocalizedString("status-menu.check-update", comment: "Check update")
tutorialMenuItem.title = NSLocalizedString("status-menu.tutorial", comment: "Tutorial")
importHostsMenuItem.title = NSLocalizedString("status-menu.import-hosts", comment: "")
exportHostsMenuItem.title = NSLocalizedString("status-menu.export-hosts", comment: "")
sponsorMenuItem.title = NSLocalizedString("status-menu.sponsor", comment: "Sponsor")
quitMenuItem.title = NSLocalizedString("status-menu.quit", comment: "Quit")

Expand Down Expand Up @@ -111,7 +116,18 @@ class StatusMenuController: NSObject, NSMenuDelegate {
}
NSWorkspace.shared.open(url)
}


// import hosts from config file
@IBAction func importHostsMenuItemClicked(_ sender: NSMenuItem) {
ConfigManager.shared.importHosts()
}

// export hosts to config file
@IBAction func exportHostsMenuItemClicked(_ sender: NSMenuItem) {
ConfigManager.shared.exportHosts()
}


// support -- paypal
@IBAction func paypalMenuItemClicked(_ sender: Any) {
(NSApplication.shared.delegate as? AppDelegate)?.sponsorByPaypal()
Expand Down
25 changes: 22 additions & 3 deletions uPic/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@
/* 当前上传任务还未完成通知副标题 */
"notification.upload.task-not-complete.subtitle" = "The current upload task is not complete";
/* 上传失败通知标题 */
"notification.upload.error.title" = "Uploaded fail";

"notification.upload.error.title" = "Upload failed";

/* 导入图床配置失败标题 */
"notification.import.error.title" = "Import failed";
/* 导入图床配置失败内容 */
"notification.import.error.body" = "The configuration file is invalid, please check!";
/* 导入图床配置成功内容 */
"notification.import.success.body" = "The configuration has been imported, please check and use!";
/* 导出图床配置失败标题 */
"notification.export.error.title" = "Export failed";
/* 导出图床配置失败内容 */
"notification.export.error.body.no-hosts" = "No exportable hosts!";
/* 导出图床配置失败内容2 */
"notification.export.error.body.invalid" = "configuration export error!";
/* 导出图床配置成功内容 */
"notification.export.success.body" = "The configuration file is exported successfully, Do not modify the file contents!";

/* 文件不存在或已被删除 */
"file-does-not-exist" = "The file does not exist or has been deleted!";
Expand Down Expand Up @@ -69,14 +83,19 @@
/* 菜单栏帮助 */
"status-menu.help" = "Help";
"status-menu.tutorial" = "Tutorial";
"status-menu.import-hosts" = "Import hosts";
"status-menu.export-hosts" = "Export hosts";
"status-menu.sponsor" = "Sponsor";
/* cancel upload */
"status-menu.cancel-upload" = "Cancel upload";

"alert.reset_preferences_title" = "Reset User Preferences?";
"alert.reset_preferences_description" = "⚠️ Note that this will reset all user preferences";
"alert.unsave_description" = "Continuing will lose unsaved data. Do you want to continue?";

"alert.import_hosts_title" = "Import host configuration";
"alert.import_hosts_description" = "⚠️ Please choose import method, merge or overwrite?";
"alert.import_hosts_merge" = "merge";
"alert.import_hosts_overwrite" = "⚠️ overwrite";

/* host -- start */
"host.field.region" = "Region";
Expand Down
Loading

0 comments on commit 2662c77

Please sign in to comment.