@@ -62,7 +62,105 @@ class ViewController: NSViewController {
62
62
loadUserLastInputContent ( )
63
63
}
64
64
65
+ /// GET request URL
66
+ /// example url:
67
+ ///
68
+ /// 今日热榜(微博):https://v1.alapi.cn/api/tophub/get?type=weibo
69
+
70
+ @IBAction func requestURLBtnClicked( _ sender: NSButton ) {
71
+
72
+ var urlString = urlTF. stringValue
73
+ if urlString. isBlank { return }
74
+ urlString = urlString. urlEncoding ( )
75
+ print ( " encode URL = \( urlTF. stringValue) " )
76
+
77
+ UserDefaults . standard. setValue ( urlString, forKey: LastInputURLCacheKey)
78
+
79
+ let session = URLSession . shared
80
+ let task = session. dataTask ( with: URL ( string: urlString) !) { [ weak self] ( data, response, error) in
81
+
82
+ guard let data = data, error == nil else { return }
83
+
84
+ do {
85
+ let jsonObj = try JSONSerialization . jsonObject ( with: data, options: . mutableContainers)
86
+ if JSONSerialization . isValidJSONObject ( jsonObj) {
87
+ let formatJsonData = try JSONSerialization . data ( withJSONObject: jsonObj, options: . prettyPrinted)
88
+ if let jsonString = String ( data: formatJsonData, encoding: String . Encoding. utf8) {
89
+ self ? . configJsonTextView ( text: jsonString, textView: self !. jsonTextView, color: NSColor . blue)
90
+ }
91
+ }
92
+ } catch let error {
93
+ print ( " error = \( error) " )
94
+ }
95
+ }
96
+ task. resume ( )
97
+ }
98
+
99
+ /// config ui on main queue.
100
+
101
+ func configJsonTextView( text: String , textView: NSTextView , color: NSColor ) {
102
+ let attrString = NSAttributedString ( string: text)
103
+ DispatchQueue . main. async {
104
+ textView. textStorage? . setAttributedString ( attrString)
105
+ textView. textStorage? . font = NSFont . systemFont ( ofSize: 15 )
106
+ textView. textStorage? . foregroundColor = color
107
+ }
108
+ }
109
+
110
+
111
+ /// start generate code....
112
+
113
+ @IBAction func startMakeCode( _ sender: NSButton ) {
114
+
115
+ let jsonString = jsonTextView. textStorage? . string
116
+
117
+ guard let jsonObj = jsonString? . _toJsonObj ( ) else {
118
+ showAlertInfoWith ( " warn: input valid json string! " , . warning)
119
+ return
120
+ }
121
+
122
+ guard JSONSerialization . isValidJSONObject ( jsonObj) else {
123
+ showAlertInfoWith ( " warn: is not a valid JSON !!! " , . warning)
124
+ return
125
+ }
126
+
127
+ saveUserInputContent ( )
128
+
129
+ do {
130
+ let formatJsonData = try JSONSerialization . data ( withJSONObject: jsonObj, options: . prettyPrinted)
131
+ if let jsonString = String ( data: formatJsonData, encoding: String . Encoding. utf8) {
132
+ configJsonTextView ( text: jsonString, textView: jsonTextView, color: NSColor . blue)
133
+ }
134
+ } catch let error {
135
+ print ( " error = \( error) " )
136
+ }
137
+
138
+ if builder. config. codeType == . OC {
139
+ builder. build_OC_code ( with: jsonObj) { [ weak self] ( hString, mString) in
140
+ print ( " hString = \( hString) " )
141
+ print ( " mString = \( mString) " )
142
+
143
+ self ? . configJsonTextView ( text: hString as String , textView: self !. hTextView, color: NSColor . red)
144
+ self ? . configJsonTextView ( text: mString as String , textView: self !. mTextView, color: NSColor . red)
145
+ }
146
+ }
147
+ }
148
+
149
+
150
+ @IBAction func chooseOutputFilePath( _ sender: NSButton ) {
151
+
152
+ }
153
+
154
+
155
+ func showAlertInfoWith( _ info: String , _ style: NSAlert . Style ) {
156
+ let alert = NSAlert ( )
157
+ alert. messageText = info
158
+ alert. alertStyle = style
159
+ alert. beginSheetModal ( for: self . view. window!, completionHandler: nil )
160
+ }
161
+
65
162
/// load cache
163
+
66
164
func loadUserLastInputContent( ) {
67
165
68
166
if let lastUrl = UserDefaults . standard. string ( forKey: LastInputURLCacheKey) {
@@ -80,7 +178,7 @@ class ViewController: NSViewController {
80
178
if let authorName = UserDefaults . standard. string ( forKey: AuthorNameCacheKey) {
81
179
authorNameTF. stringValue = authorName
82
180
}
83
- if let outFilePath = UserDefaults . standard. string ( forKey: LastInputURLCacheKey ) {
181
+ if let outFilePath = UserDefaults . standard. string ( forKey: GenerateFilePathCacheKey ) {
84
182
outputFilePath = outFilePath
85
183
}
86
184
@@ -93,10 +191,41 @@ class ViewController: NSViewController {
93
191
generateFileBtn. state = UserDefaults . standard. bool ( forKey: SupportJSONModelTypeCacheKey) ? . on : . off
94
192
}
95
193
96
- /// save cache
194
+ /// MARK: save cache
97
195
func saveUserInputContent( ) {
196
+
98
197
let superClassName = superClassNameTF. stringValue. isBlank ? " NSObject " : superClassNameTF. stringValue
99
198
UserDefaults . standard. setValue ( superClassName, forKey: SuperClassNameCacheKey)
199
+ builder. config. superClassName = superClassName
200
+
201
+ let modelNamePrefix = modelNamePrefixTF. stringValue. isBlank ? " NS " : modelNamePrefixTF. stringValue
202
+ UserDefaults . standard. setValue ( modelNamePrefix, forKey: ModelNamePrefixCacheKey)
203
+ builder. config. modelNamePrefix = modelNamePrefix
204
+
205
+ let rootModelName = rootModelNameTF. stringValue. isBlank ? " NSRootModel " : rootModelNameTF. stringValue
206
+ UserDefaults . standard. setValue ( rootModelName, forKey: RootModelNameCacheKey)
207
+ builder. config. rootModelName = rootModelName
208
+
209
+ let authorName = authorNameTF. stringValue. isBlank ? " SKGenerateModelTool " : authorNameTF. stringValue
210
+ UserDefaults . standard. setValue ( authorName, forKey: AuthorNameCacheKey)
211
+ builder. config. authorName = authorName
212
+
213
+ builder. config. codeType = SKCodeBuilderCodeType ( rawValue: codeTypeBtn. indexOfSelectedItem + 1 ) !
214
+ UserDefaults . standard. set ( codeTypeBtn. indexOfSelectedItem + 1 , forKey: BuildCodeTypeCacheKey)
215
+
216
+ builder. config. jsonType = SKCodeBuilderJSONModelType ( rawValue: jsonTypeBtn. indexOfSelectedItem) !
217
+ UserDefaults . standard. set ( jsonTypeBtn. indexOfSelectedItem, forKey: SupportJSONModelTypeCacheKey)
218
+
219
+ if builder. config. superClassName. compare ( " NSObject " ) == . orderedSame {
220
+ if builder. config. jsonType == . HandyJSON {
221
+ builder. config. superClassName = " HandyJSON "
222
+ } else if builder. config. jsonType == . YYModel {
223
+ builder. config. superClassName = " YYModel "
224
+ }
225
+ }
226
+
227
+ UserDefaults . standard. setValue ( outputFilePath, forKey: GenerateFilePathCacheKey)
228
+ UserDefaults . standard. set ( generateFileBtn. state == . on , forKey: ShouldGenerateFileCacheKey)
100
229
}
101
230
102
231
0 commit comments