Skip to content

Commit 1c3627b

Browse files
committed
Updated UI to function with new code.
1 parent 4d5354e commit 1c3627b

File tree

8 files changed

+138
-137
lines changed

8 files changed

+138
-137
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ cache:
55
osx_image: xcode8.0
66
xcode_workspace: SwiftyJSONAccelerator.xcworkspace
77
xcode_scheme: SwiftyJSONAccelerator
8-
xcode_sdk: macosx10.12
8+
xcode_sdk: macosx10.11
99
podfile: Podfile
1010
install:
1111
- bundle install
1212
- gem install cocoapods
1313
- ./install_swiftlint.sh
1414
before_script:
1515
- rm -rf Pods
16+
- rm -rf Podlock
1617
- bundle install
1718
- bundle exec pod install
1819
script:
19-
- xcodebuild -workspace SwiftyJSONAccelerator.xcworkspace -scheme "SwiftyJSONAccelerator" test | tee xcodebuild.log | xcpretty -s
20+
- xcodebuild -workspace SwiftyJSONAccelerator.xcworkspace -schee "SwiftyJSONAccelerator" test | tee xcodebuild.log | xcpretty -s
2021
- swiftlint
2122
after_success:
2223
- slather

SwiftyJSONAccelerator/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum VariableType: String {
2727
case Bool = "Bool"
2828
case Array = "[]"
2929
case Object = "{OBJ}"
30-
case Null = "AnyObject"
30+
case Null = "Any"
3131
}
3232

3333
/**

SwiftyJSONAccelerator/Library-Extensions/ObjectMapperModelFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ struct ObjectMapperModelFile: ModelFile, DefaultModelFileComponent {
6767
generateCommonComponentsFor(property)
6868

6969
case .EmptyArray:
70-
component.declarations.append(genVariableDeclaration(property.name, "AnyObject", true))
70+
component.declarations.append(genVariableDeclaration(property.name, "Any", true))
7171
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
72-
component.decoders.append(genDecoder(property.name, "AnyObject", property.constantName, true))
73-
component.encoders.append(genEncoder(property.name, "AnyObject", property.constantName))
72+
component.decoders.append(genDecoder(property.name, "Any", property.constantName, true))
73+
component.encoders.append(genEncoder(property.name, "Any", property.constantName))
7474
generateCommonComponentsFor(property)
7575
case .NullType: break
7676
// Currently we do not deal with null values.

SwiftyJSONAccelerator/Library-Extensions/SwiftyJSONModelFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ struct SwiftyJSONModelFile: ModelFile, DefaultModelFileComponent {
7474
case .EmptyArray:
7575
component.stringConstants.append(genStringConstant(property.constantName, property.key))
7676
component.initialisers.append(genInitializerForPrimitiveArray(property.name, "object", property.constantName))
77-
component.declarations.append(genVariableDeclaration(property.name, "AnyObject", true))
77+
component.declarations.append(genVariableDeclaration(property.name, "Any", true))
7878
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
79-
component.decoders.append(genDecoder(property.name, "AnyObject", property.constantName, true))
80-
component.encoders.append(genEncoder(property.name, "AnyObject", property.constantName))
79+
component.decoders.append(genDecoder(property.name, "Any", property.constantName, true))
80+
component.encoders.append(genEncoder(property.name, "Any", property.constantName))
8181
case .NullType:
8282
// Currently we do not deal with null values.
8383
break

SwiftyJSONAccelerator/Storyboard/Base.lproj/Main.storyboard

Lines changed: 67 additions & 81 deletions
Large diffs are not rendered by default.

SwiftyJSONAccelerator/UI/SJEditorViewController.swift

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
import Cocoa
10+
import SwiftyJSON
11+
1012
fileprivate func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
1113
switch (lhs, rhs) {
1214
case let (l?, r?):
@@ -39,11 +41,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
3941
@IBOutlet var prefixClassTextField: NSTextField!
4042
@IBOutlet var companyNameTextField: NSTextField!
4143
@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!
4748

4849
// MARK: View methods
4950
override func loadView() {
@@ -89,7 +90,9 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
8990
handleError(error)
9091
textView!.lnv_textDidChange(Notification.init(name: NSNotification.Name.NSTextDidChange, object: nil))
9192
return false
92-
}
93+
} else {
94+
genericJSONError()
95+
}
9396
return false
9497
}
9598

@@ -101,13 +104,13 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
101104
// The base class field is blank, cannot proceed without it.
102105
// Possibly can have a default value in the future.
103106
if baseClassTextField?.stringValue.characters.count <= 0 {
104-
let alert: NSAlert = NSAlert()
107+
let alert = NSAlert()
105108
alert.messageText = "Enter a base class name to continue."
106109
alert.runModal()
107110
return
108111
}
109112

110-
let filePath: String? = openFile()
113+
let filePath = openFile()
111114

112115
// No file path was selected, go back!
113116
if filePath == nil {
@@ -119,27 +122,28 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
119122
// Checks for validity of the content, else can cause crashes.
120123
if object != nil {
121124

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)
143147
} else {
144148
let alert: NSAlert = NSAlert()
145149
alert.messageText = "Unable to save the file check the content."
@@ -148,23 +152,21 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
148152
}
149153

150154
@IBAction func recalcEnabledBoxes(_ sender: AnyObject) {
155+
self.enableNSCodingSupportCheckbox.isEnabled = (modelTypeSelectorSegment.selectedSegment == 1)
156+
}
151157

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-
}
160158

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)
165168
}
166-
}
167-
169+
168170
// MARK: Internal Methods
169171

170172
/**
@@ -224,9 +226,18 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
224226
} else {
225227
invalidJSONError(message)
226228
}
229+
} else {
230+
genericJSONError()
227231
}
228232
}
229233

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+
230241
/// MARK: Resetting and showing error messages
231242

232243
/**
@@ -258,7 +269,10 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
258269

259270
// MARK: TextView Delegate
260271
func textDidChange(_ notification: Notification) {
261-
validateAndFormat(false)
272+
let isValid = validateAndFormat(false)
273+
if isValid {
274+
resetErrorImage()
275+
}
262276
}
263277

264278
// MARK: Internal Methods

SwiftyJSONAcceleratorTests/ModelGeneratorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class ModelGeneratorTests: XCTestCase {
216216
"public var valueSix: ACValueSix?",
217217
"public var valueOne: String?",
218218
"public var valueThree: Bool = false",
219-
"public var valueEight: [AnyObject]?"
219+
"public var valueEight: [Any]?"
220220
]
221221
for declaration in declarations {
222222
expect(baseModelFile!.component.declarations.contains(declaration)).to(equal(true))
@@ -260,7 +260,7 @@ class ModelGeneratorTests: XCTestCase {
260260
"self.valueSix = aDecoder.decodeObject(forKey: kACBaseClassValueSixKey) as? ACValueSix",
261261
"self.valueOne = aDecoder.decodeObject(forKey: kACBaseClassValueOneKey) as? String",
262262
"self.valueThree = aDecoder.decodeBool(forKey: kACBaseClassValueThreeKey)",
263-
"self.valueEight = aDecoder.decodeObject(forKey: kACBaseClassValueEightKey) as? [AnyObject]"
263+
"self.valueEight = aDecoder.decodeObject(forKey: kACBaseClassValueEightKey) as? [Any]"
264264
]
265265
for decoder in decoders {
266266
expect(baseModelFile!.component.decoders.contains(decoder)).to(equal(true))

SwiftyJSONAcceleratorTests/Support Files/TestJSONFile.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
}
2020
}
2121
}
22-
}
22+
}dd

0 commit comments

Comments
 (0)