7
7
//
8
8
9
9
import Cocoa
10
+ import SwiftyJSON
11
+
10
12
fileprivate func < < T: Comparable > ( lhs: T ? , rhs: T ? ) -> Bool {
11
13
switch ( lhs, rhs) {
12
14
case let ( l? , r? ) :
@@ -39,11 +41,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
39
41
@IBOutlet var prefixClassTextField : NSTextField !
40
42
@IBOutlet var companyNameTextField : NSTextField !
41
43
@IBOutlet var authorNameTextField : NSTextField !
42
- @IBOutlet var includeSwiftyCheckbox : NSButton !
43
- @IBOutlet var supportNSCodingCheckbox : NSButton !
44
- @IBOutlet var supportSwiftyJSONCheckbox : NSButton !
45
- @IBOutlet var supportObjectMapperCheckbox : NSButton !
46
- @IBOutlet var includeObjectMapperCheckbox : NSButton !
44
+ @IBOutlet var includeHeaderImportCheckbox : NSButton !
45
+ @IBOutlet var enableNSCodingSupportCheckbox : NSButton !
46
+ @IBOutlet var librarySelector : NSPopUpButton !
47
+ @IBOutlet var modelTypeSelectorSegment : NSSegmentedControl !
47
48
48
49
// MARK: View methods
49
50
override func loadView( ) {
@@ -89,7 +90,9 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
89
90
handleError ( error)
90
91
textView!. lnv_textDidChange ( Notification . init ( name: NSNotification . Name. NSTextDidChange, object: nil ) )
91
92
return false
92
- }
93
+ } else {
94
+ genericJSONError ( )
95
+ }
93
96
return false
94
97
}
95
98
@@ -101,13 +104,13 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
101
104
// The base class field is blank, cannot proceed without it.
102
105
// Possibly can have a default value in the future.
103
106
if baseClassTextField? . stringValue. characters. count <= 0 {
104
- let alert : NSAlert = NSAlert ( )
107
+ let alert = NSAlert ( )
105
108
alert. messageText = " Enter a base class name to continue. "
106
109
alert. runModal ( )
107
110
return
108
111
}
109
112
110
- let filePath : String ? = openFile ( )
113
+ let filePath = openFile ( )
111
114
112
115
// No file path was selected, go back!
113
116
if filePath == nil {
@@ -119,27 +122,28 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
119
122
// Checks for validity of the content, else can cause crashes.
120
123
if object != nil {
121
124
122
- // let swiftyState = self.includeSwiftyCheckbox?.state == 1 ? true : false
123
- // let supportSwiftyState = self.supportSwiftyJSONCheckbox?.state == 1 ? true : false
124
- //
125
- // let nscodingState = self.supportNSCodingCheckbox?.state == 1 ? true : false
126
- //
127
- // let objectMapperState = self.includeObjectMapperCheckbox?.state == 1 ? true : false
128
- // let supportObjectMapperState = self.supportObjectMapperCheckbox?.state == 1 ? true : false
129
-
130
- // let generator: ModelGenerator = ModelGenerator.init(baseContent: JSON(object!), baseClassName: baseClassTextField.stringValue, filePath: filePath!)
131
- //
132
- // generator.prefix = prefixClassTextField.stringValue
133
- // generator.authorName = authorNameTextField.stringValue
134
- // generator.companyName = companyNameTextField.stringValue
135
- // generator.type = ModelType.kClassType
136
- // generator.supportSwiftyJSON = supportSwiftyState
137
- // generator.includeSwiftyJSON = swiftyState
138
- // generator.supportObjectMapper = supportObjectMapperState
139
- // generator.includeObjectMapper = objectMapperState
140
- // generator.supportNSCoding = nscodingState
141
- //
142
- // generator.generate()
125
+ let nsCodingState = self . enableNSCodingSupportCheckbox. state == 1 && ( modelTypeSelectorSegment. selectedSegment == 1 )
126
+ let constructType = self . modelTypeSelectorSegment. selectedSegment == 0 ? ConstructType . ClassType : ConstructType . StructType
127
+ let libraryType = self . librarySelector. indexOfSelectedItem == 0 ? JSONMappingLibrary . SwiftyJSON : JSONMappingLibrary . ObjectMapper
128
+ let configuration = ModelGenerationConfiguration . init (
129
+ filePath: filePath!. appending ( " / " ) ,
130
+ baseClassName: baseClassTextField. stringValue,
131
+ authorName: authorNameTextField. stringValue,
132
+ companyName: companyNameTextField. stringValue,
133
+ prefix: prefixClassTextField. stringValue,
134
+ constructType: constructType,
135
+ modelMappingLibrary: libraryType,
136
+ supportNSCoding: nsCodingState)
137
+ let modelGenerator = ModelGenerator . init ( JSON ( object!) , configuration)
138
+ let filesGenerated = modelGenerator. generate ( )
139
+ var successState = true
140
+ for file in filesGenerated {
141
+ let content = FileGenerator . generateFileContentWith ( file, configuration: configuration)
142
+ let name = file. fileName
143
+ let path = configuration. filePath
144
+ successState = FileGenerator . writeToFileWith ( name, content: content, path: path)
145
+ }
146
+ notify ( completionState: successState, fileCount: filesGenerated. count)
143
147
} else {
144
148
let alert : NSAlert = NSAlert ( )
145
149
alert. messageText = " Unable to save the file check the content. "
@@ -148,23 +152,21 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
148
152
}
149
153
150
154
@IBAction func recalcEnabledBoxes( _ sender: AnyObject ) {
155
+ self . enableNSCodingSupportCheckbox. isEnabled = ( modelTypeSelectorSegment. selectedSegment == 1 )
156
+ }
151
157
152
- let supportSwiftyState = self . supportSwiftyJSONCheckbox? . state == 1 ? true : false
153
- let supportObjectMapperState = self . supportObjectMapperCheckbox? . state == 1 ? true : false
154
-
155
- if supportSwiftyState {
156
- self . includeSwiftyCheckbox? . isEnabled = true
157
- } else {
158
- self . includeSwiftyCheckbox? . isEnabled = false
159
- }
160
158
161
- if supportObjectMapperState {
162
- self . includeObjectMapperCheckbox? . isEnabled = true
163
- } else {
164
- self . includeObjectMapperCheckbox? . isEnabled = false
159
+ func notify( completionState: Bool , fileCount: Int ) {
160
+ let notification : NSUserNotification = NSUserNotification ( )
161
+ notification. title = " SwiftyJSONAccelerator "
162
+ if completionState && fileCount > 0 {
163
+ notification. subtitle = " Completed - \( fileCount) Generated. "
164
+ } else {
165
+ notification. subtitle = " No files were generated, cannot model arrays inside arrays. "
166
+ }
167
+ NSUserNotificationCenter . default. deliver ( notification)
165
168
}
166
- }
167
-
169
+
168
170
// MARK: Internal Methods
169
171
170
172
/**
@@ -224,9 +226,18 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
224
226
} else {
225
227
invalidJSONError ( message)
226
228
}
229
+ } else {
230
+ genericJSONError ( )
227
231
}
228
232
}
229
233
234
+ /**
235
+ Shows a generic error about JSON in case the system is not able to figure out what is wrong.
236
+ */
237
+ func genericJSONError( ) {
238
+ invalidJSONError ( " The JSON seems to be invalid! " )
239
+ }
240
+
230
241
/// MARK: Resetting and showing error messages
231
242
232
243
/**
@@ -258,7 +269,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
258
269
259
270
// MARK: TextView Delegate
260
271
func textDidChange( _ notification: Notification ) {
261
- validateAndFormat ( false )
272
+ let isValid = validateAndFormat ( false )
273
+ if isValid {
274
+ resetErrorImage ( )
275
+ }
262
276
}
263
277
264
278
// MARK: Internal Methods
0 commit comments