Skip to content

Commit 5b688ab

Browse files
author
CodeOcen
committed
feat: 优化
1 parent beabc94 commit 5b688ab

File tree

6 files changed

+80
-114
lines changed

6 files changed

+80
-114
lines changed

SwiftJSONModeler For Xcode.xcodeproj/xcuserdata/yibin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -180,37 +180,5 @@
180180
landmarkType = "7">
181181
</BreakpointContent>
182182
</BreakpointProxy>
183-
<BreakpointProxy
184-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
185-
<BreakpointContent
186-
uuid = "FBB3965E-FAF3-47AF-A2E1-88A0A8B61F70"
187-
shouldBeEnabled = "Yes"
188-
ignoreCount = "0"
189-
continueAfterRunningActions = "No"
190-
filePath = "SwiftJSONModeler For Xcode/Controller/ConfigViewController.swift"
191-
startingColumnNumber = "9223372036854775807"
192-
endingColumnNumber = "9223372036854775807"
193-
startingLineNumber = "110"
194-
endingLineNumber = "110"
195-
landmarkName = "saveButtonTap(_:)"
196-
landmarkType = "7">
197-
</BreakpointContent>
198-
</BreakpointProxy>
199-
<BreakpointProxy
200-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
201-
<BreakpointContent
202-
uuid = "85E9C064-2E1F-4E79-9C30-B8A576B2887F"
203-
shouldBeEnabled = "Yes"
204-
ignoreCount = "0"
205-
continueAfterRunningActions = "No"
206-
filePath = "SwiftJSONModeler For Xcode/Config/ConfigCenter.swift"
207-
startingColumnNumber = "9223372036854775807"
208-
endingColumnNumber = "9223372036854775807"
209-
startingLineNumber = "18"
210-
endingLineNumber = "18"
211-
landmarkName = "config"
212-
landmarkType = "24">
213-
</BreakpointContent>
214-
</BreakpointProxy>
215183
</Breakpoints>
216184
</Bucket>

SwiftJSONModeler For Xcode/Base.lproj/Main.storyboard

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="5gI-5U-AMq">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="5gI-5U-AMq">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097.2"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17701"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<scenes>
@@ -744,7 +744,7 @@
744744
</textFieldCell>
745745
</textField>
746746
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="vSj-38-HG4">
747-
<rect key="frame" x="674" y="33" width="92" height="51"/>
747+
<rect key="frame" x="673" y="33" width="94" height="52"/>
748748
<constraints>
749749
<constraint firstAttribute="width" constant="80" id="IgU-pX-0Ab"/>
750750
<constraint firstAttribute="height" constant="40" id="L1E-c2-3gR"/>
@@ -824,7 +824,7 @@
824824
</textFieldCell>
825825
</textField>
826826
<button verticalHuggingPriority="750" tag="12" translatesAutoresizingMaskIntoConstraints="NO" id="EHm-R1-Ay3">
827-
<rect key="frame" x="670" y="111" width="22" height="18"/>
827+
<rect key="frame" x="674" y="111" width="18" height="18"/>
828828
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="hL8-OC-8uq">
829829
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
830830
<font key="font" metaFont="system"/>
@@ -834,8 +834,8 @@
834834
</connections>
835835
</button>
836836
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="CXz-h2-IF1">
837-
<rect key="frame" x="38" y="66" width="101" height="16"/>
838-
<textFieldCell key="cell" lineBreakMode="clipping" title="是否为显示可选:" id="T0b-vs-rcz">
837+
<rect key="frame" x="38" y="66" width="169" height="16"/>
838+
<textFieldCell key="cell" lineBreakMode="clipping" title="是否为隐式可选(如Any!):" id="T0b-vs-rcz">
839839
<font key="font" usesAppearanceFont="YES"/>
840840
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
841841
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -850,7 +850,7 @@
850850
</textFieldCell>
851851
</textField>
852852
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Cn0-Pn-vUD">
853-
<rect key="frame" x="670" y="19" width="22" height="18"/>
853+
<rect key="frame" x="674" y="19" width="18" height="18"/>
854854
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="5wq-U8-PPe">
855855
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
856856
<font key="font" metaFont="system"/>
@@ -860,7 +860,7 @@
860860
</connections>
861861
</button>
862862
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4Ca-td-HhY">
863-
<rect key="frame" x="670" y="65" width="22" height="18"/>
863+
<rect key="frame" x="674" y="65" width="18" height="18"/>
864864
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="TSZ-Cw-Dq7">
865865
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
866866
<font key="font" metaFont="system"/>
@@ -909,7 +909,7 @@
909909
</textFieldCell>
910910
</textField>
911911
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="pKM-dx-KjW">
912-
<rect key="frame" x="670" y="127" width="22" height="18"/>
912+
<rect key="frame" x="674" y="127" width="18" height="18"/>
913913
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" inset="2" id="KPb-bY-Bpq">
914914
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
915915
<font key="font" metaFont="system"/>
@@ -965,7 +965,7 @@
965965
</textFieldCell>
966966
</textField>
967967
<comboBox verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="e6x-Y0-QC6">
968-
<rect key="frame" x="149" y="45" width="405" height="36"/>
968+
<rect key="frame" x="149" y="45" width="413" height="36"/>
969969
<constraints>
970970
<constraint firstAttribute="height" constant="30" id="FO2-EN-wZh"/>
971971
</constraints>
@@ -976,10 +976,10 @@
976976
</comboBoxCell>
977977
</comboBox>
978978
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Mih-Hn-oxm">
979-
<rect key="frame" x="575" y="47" width="131" height="32"/>
979+
<rect key="frame" x="582" y="47" width="125" height="32"/>
980980
<buttonCell key="cell" type="push" title="配置多个Token" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="voO-Fi-BVZ">
981981
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
982-
<font key="font" size="13" name=".PingFangSC-Regular"/>
982+
<font key="font" metaFont="system"/>
983983
</buttonCell>
984984
<connections>
985985
<segue destination="bxC-4J-HfZ" kind="modal" id="LJ4-XP-kB0"/>
@@ -1046,10 +1046,10 @@
10461046
</textFieldCell>
10471047
</textField>
10481048
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tHc-az-ITJ">
1049-
<rect key="frame" x="24" y="43" width="94" height="32"/>
1049+
<rect key="frame" x="23" y="43" width="88" height="32"/>
10501050
<buttonCell key="cell" type="push" title="使用指南" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="0tb-VI-0xc">
10511051
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
1052-
<font key="font" size="13" name=".PingFangSC-Regular"/>
1052+
<font key="font" metaFont="system"/>
10531053
</buttonCell>
10541054
<connections>
10551055
<action selector="guideButtonTap:" target="5gI-5U-AMq" id="ut3-46-pbJ"/>
@@ -1160,7 +1160,7 @@
11601160
<rect key="frame" x="0.0" y="359" width="698" height="60"/>
11611161
<subviews>
11621162
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="l7l-RJ-2Of">
1163-
<rect key="frame" x="616" y="8" width="73" height="41"/>
1163+
<rect key="frame" x="615" y="8" width="75" height="42"/>
11641164
<constraints>
11651165
<constraint firstAttribute="width" constant="61" id="9Hb-xt-ogA"/>
11661166
<constraint firstAttribute="height" constant="30" id="yuK-or-Tn0"/>

SwiftJSONModeler For Xcode/Config/ConfigCenter.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ let configPath = (NSHomeDirectory() as NSString).appendingPathComponent("Config
1313

1414
class ConfigCenter {
1515
static let `default` = ConfigCenter()
16-
var config: ConfigModel {
17-
didSet { // 类赋值调用, 结构体直接改变属性也会调用
18-
Self.writeConfig(config)
19-
}
20-
}
16+
var config: ConfigModel
2117

2218
init() {
2319
config = Self.readConfig()
2420
}
21+
@discardableResult
22+
func save() -> Bool {
23+
return Self.writeConfig(config)
24+
}
2525

2626

2727
}
@@ -34,11 +34,15 @@ private extension ConfigCenter {
3434
return writeDefaultConfig()
3535
}
3636

37-
let decoder = CleanJSONDecoder()
38-
guard let model = try? decoder.decode(ConfigModel.self, from: data) else {
37+
let decoder = PropertyListDecoder()//CleanJSONDecoder()
38+
do {
39+
let model = try decoder.decode(ConfigModel.self, from: data)
40+
return model
41+
} catch let error {
42+
print("读取 plist 转 model 失败")
43+
print(error)
3944
return writeDefaultConfig()
4045
}
41-
return model
4246
}
4347

4448
static func writeDefaultConfig() -> ConfigModel {
@@ -48,11 +52,17 @@ private extension ConfigCenter {
4852
}
4953
@discardableResult
5054
static func writeConfig(_ config: ConfigModel) -> Bool {
51-
let propertyListEncoder = PropertyListEncoder()
52-
propertyListEncoder.outputFormat = .xml
53-
54-
let plistData = try? propertyListEncoder.encode(config)
55-
let isSuccess = FileManager.default.createFile(atPath: configPath, contents: plistData, attributes: nil)
55+
// let propertyListEncoder = PropertyListEncoder()
56+
// propertyListEncoder.outputFormat = .xml
57+
// let plistData = try? propertyListEncoder.encode(config)
58+
guard let data = try? config.toJSON() else {
59+
return false
60+
}
61+
guard let dic = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary else {
62+
return false
63+
}
64+
let isSuccess = dic.write(toFile: configPath, atomically: true)
65+
//let isSuccess = FileManager.default.createFile(atPath: configPath, contents: data, attributes: nil)
5666
return isSuccess
5767
}
5868
}

SwiftJSONModeler For Xcode/Config/Model/ConfigModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
struct ConfigModel: Codable {
11+
class ConfigModel: Codable {
1212
var appVersion: String = ""
1313
var conform: String = ""
1414
var module: String = ""
@@ -26,7 +26,7 @@ struct ConfigModel: Codable {
2626
var yapiToken = ""
2727
var yapiHost = ""
2828
/// 备注
29-
var remark: String?
29+
var remark: String = ""
3030
/// 配置的多个 token
3131
var yapiTokenList: [YApiTokenModel] = []
3232
}
@@ -52,7 +52,7 @@ extension ConfigModel {
5252

5353
}
5454

55-
struct YApiTokenModel: Codable {
55+
class YApiTokenModel: Codable {
5656
/// 项目名称
5757
var name: String = ""
5858
/// 项目 token

SwiftJSONModeler For Xcode/Controller/ConfigViewController.swift

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import Cocoa
1010

1111
class ConfigViewController: NSViewController {
12-
13-
private let config = Config()
12+
private var configCenter = ConfigCenter.default
13+
private var config = ConfigCenter.default.config
1414
private var tokens: [Token] = []
1515
private var token: String = ""
1616

@@ -54,9 +54,9 @@ class ConfigViewController: NSViewController {
5454
setupBox()
5555
remarkTextField.stringValue = config.remark
5656

57-
isAllowOptionalBtn.state = config.isNotOptional ? .off : .on
58-
isShowOptionalBtn.state = config.isImplicitlyOptional ? .off : .on
59-
isArrayEmptyBtn.state = config.arrayIsDefaultNotEmpty ? .off : .on
57+
isAllowOptionalBtn.state = config.isOptional ? .on : .off
58+
isShowOptionalBtn.state = config.isImplicitlyOptional ? .on : .off
59+
isArrayEmptyBtn.state = config.isArrayDefaultEmpty ? .on : .off
6060
isShowYApiMockBtn.state = config.isShowYApiMock ? .on : .off
6161
}
6262

@@ -106,35 +106,37 @@ class ConfigViewController: NSViewController {
106106
}
107107

108108
@IBAction func saveButtonTap(_ sender: NSButton) {
109-
let config = ConfigCenter.default
110-
var c = config.config
111-
c.appVersion = "2.1.0"
112-
config.config = c
113-
// config.conform = confromTextField.stringValue
114-
// config.module = moduleTextField.stringValue
115-
// config.prefix = prefixTextField.stringValue
116-
// config.subffix = subffixTextField.stringValue
117-
// config.yapiPath = pathTextField.stringValue
118-
// config.yapiToken = tokenForSave()
119-
// config.yapiHost = yapiHostTextField.stringValue
120-
// config.remark = remarkTextField.stringValue
121-
// view.window?.close()
109+
// let panel = OpenSavePanel()
110+
// panel.importFile { (error) in
111+
// print(error)
112+
// }
113+
config.conform = confromTextField.stringValue
114+
config.module = moduleTextField.stringValue
115+
config.prefix = prefixTextField.stringValue
116+
config.subffix = subffixTextField.stringValue
117+
config.yapiPath = pathTextField.stringValue
118+
config.yapiToken = tokenForSave()
119+
config.yapiHost = yapiHostTextField.stringValue
120+
config.remark = remarkTextField.stringValue
121+
ConfigCenter.default.save()
122+
view.window?.close()
122123
}
123124

124125
@IBAction func typeOptionalBtnTap(_ sender: NSButton) {
125-
config.isNotOptional = !(sender.state == .on)
126+
config.isOptional = (sender.state == .on)
127+
configCenter.save()
126128
}
127129
/// 是否为显示可选
128130
@IBAction func isShowOptional(_ sender: NSButton) {
129-
config.isImplicitlyOptional = !(sender.state == .on)
131+
config.isImplicitlyOptional = (sender.state == .on)
132+
configCenter.save()
130133
}
131134
@IBAction func isEmptyArrayBtnTap(_ sender: NSButton) {
132-
config.arrayIsDefaultNotEmpty = !(sender.state == .on)
135+
config.isArrayDefaultEmpty = (sender.state == .on)
136+
configCenter.save()
133137
}
134138
@IBAction func isShowYApiMockBtnTap(_ sender: NSButton) {
135139
config.isShowYApiMock = sender.state == .on
140+
configCenter.save()
136141
}
137-
138-
139-
140142
}

SwiftJSONModeler For Xcode/Utils/OpenSavePanel.swift

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// 文件导入和导出
99

1010
import AppKit
11+
import CleanJSON
1112

1213
struct OpenSavePanel {
1314
/// 导入文件
@@ -63,35 +64,20 @@ struct OpenSavePanel {
6364
}
6465
/// 导入保存
6566
private func read(url: URL) -> Bool {
66-
// guard let url = url else {
67-
// return
68-
// }
69-
// let plistFile = NSDictionary.init(contentsOf: url)
70-
// print(plistFile)
71-
// let data = try? Data(contentsOf: url) // 可能 datameiyou
72-
// let decoder = PropertyListDecoder()
73-
// do {
74-
// let config = try decoder.decode(ConfigModel.self, from: data!)
75-
// } catch let error {
76-
// print(error)
77-
// }
78-
67+
let configCenter = ConfigCenter.default
68+
guard let dic = NSDictionary(contentsOfFile:url.path), JSONSerialization.isValidJSONObject(dic) else {
69+
return false
70+
}
71+
guard let dicData = try? JSONSerialization.data(withJSONObject: dic, options: .fragmentsAllowed) else {
72+
return false
73+
}
7974

80-
// let configModel = ConfigModel()
81-
// let propertyListEncoder = PropertyListEncoder()
82-
// propertyListEncoder.outputFormat = .xml
83-
//
84-
// let plistData = try? propertyListEncoder.encode(configModel)
85-
86-
let fileManager = FileManager.default
87-
let configData = fileManager.contents(atPath: url.path)
88-
let isSuccess = fileManager.createFile(atPath: configPath, contents: configData, attributes: nil) // 全覆盖写入
89-
if isSuccess {
90-
print("保存成功:\(configPath)")
91-
} else {
92-
print("保存失败")
75+
let decoder = CleanJSONDecoder()
76+
guard let model = try? decoder.decode(ConfigModel.self, from: dicData) else {
77+
return false
9378
}
94-
return isSuccess
79+
configCenter.config = model
80+
return configCenter.save()
9581
}
9682

9783
/// 导出保存文件

0 commit comments

Comments
 (0)