Skip to content

Commit

Permalink
Optimize application interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
vvkeep committed May 13, 2022
1 parent 0629b14 commit a9666cc
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 106 deletions.
34 changes: 31 additions & 3 deletions JSONConverter/Classes/Common/Macros/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,38 @@ enum LangType: Int {
return "golang"
}
}

var title: String {
switch self {
case .Swift:
return "Swift"
case .HandyJSON:
return "Swift - HandyJSON"
case .SwiftyJSON:
return "Swift - SwiftyJSON"
case .KakaJSON:
return "Swift - KakaJSON"
case .ObjectMapper:
return "Swift - ObjectMapper"
case .Codable:
return "Swift - Codable"
case .ObjC:
return "ObjectiveC"
case .YYModel:
return "ObjectiveC - YYModel"
case .MJExtension:
return "ObjectiveC - MJExtension"
case .Flutter:
return "Flutter - json_serializable"
case .Java:
return "Java"
case .Golang:
return "Golang"
}
}

// the arrary index is enum LangType value
static func languages() -> [String] {
return ["Swift", "HandyJSON", "KakaJSON", "SwiftyJSON", "ObjectMapper", "Codable", "ObjC", "YYModel", "MJExtension", "Flutter", "Java", "Golang"]
static func allValues() -> [LangType] {
return [.Swift, .HandyJSON, .KakaJSON, .SwiftyJSON, .ObjectMapper, .Codable, .ObjC, .YYModel, .MJExtension, .Flutter, .Java, .Golang]
}

var onlyCompatibleClass: Bool {
Expand Down
110 changes: 57 additions & 53 deletions JSONConverter/Classes/Controller/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Cocoa
import Highlightr

class MainViewController: NSViewController {
@IBOutlet weak var languageBox: NSComboBox!
@IBOutlet weak var structureBox: NSComboBox!
@IBOutlet weak var themeBox: NSComboBox!
@IBOutlet weak var languagesPopup: NSPopUpButton!
@IBOutlet weak var structurePopup: NSPopUpButton!
@IBOutlet weak var themePopup: NSPopUpButton!

@IBOutlet weak var JSONScrollViewWidthCons: NSLayoutConstraint!
@IBOutlet weak var classScrollViewHeightCons: NSLayoutConstraint!
Expand Down Expand Up @@ -57,22 +57,22 @@ class MainViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupCacheConfig()
loadCacheConfig()
checkVersion()
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotiAction), name: NSNotification.Name.ApplicationWillTerminateNoti, object: nil)
updateFileConfigAndViews()
updateConfigUI()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminateNotiAction),
name: NSNotification.Name.ApplicationWillTerminateNoti, object: nil)
}

private func checkVersion() {
UpgradeUtils.newestVersion { (version) in
guard let tagName = version?.tag_name,
let bundleVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
let newVersion = Int(tagName.replacingOccurrences(of: ".", with: "")),
let currentVeriosn = Int(bundleVersion.replacingOccurrences(of: ".", with: "")) else {
let bundleVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
return
}

if newVersion > currentVeriosn {
if tagName.compare(bundleVersion) == .orderedDescending {
let upgradeVc = UpgradeViewController()
upgradeVc.versionInfo = version
upgradeVc.currentVer = bundleVersion
Expand All @@ -84,20 +84,14 @@ class MainViewController: NSViewController {
private func setupUI() {
saveBtn.title = "parameter_save_title".localized

languageBox.addItems(withObjectValues: LangType.languages())
languageBox.delegate = self
languageBox.isEditable = false
languageBox.isSelectable = false
languagesPopup.removeAllItems()
languagesPopup.addItems(withTitles: LangType.allValues().map({ $0.title }))

structureBox.addItems(withObjectValues: StructType.strusts())
structureBox.delegate = self
structureBox.isEditable = false
structureBox.isSelectable = false
structurePopup.removeAllItems()
structurePopup.addItems(withTitles: StructType.strusts())

themeBox.addItems(withObjectValues: highlightr.availableThemes())
themeBox.delegate = self
themeBox.isEditable = false
themeBox.isSelectable = false
themePopup.removeAllItems()
themePopup.addItems(withTitles: highlightr.availableThemes())

classTextView.isEditable = false
classTextView.setUpLineNumberView()
Expand Down Expand Up @@ -132,12 +126,12 @@ class MainViewController: NSViewController {
showJSONOperateResult(false, content: nil)
}

private func setupCacheConfig() {
let configFile = FileCacheManager.shared.configFile()
languageBox.selectItem(at: configFile.langStruct.langType.rawValue)
structureBox.selectItem(at: configFile.langStruct.structType.rawValue)
if let themeIndex = highlightr.availableThemes().firstIndex(where: { configFile.theme == $0 }) {
themeBox.selectItem(at: themeIndex)
private func loadCacheConfig() {
let config = FileCacheManager.shared.configFile()
languagesPopup.selectItem(at: config.langStruct.langType.rawValue)
structurePopup.selectItem(at: config.langStruct.structType.rawValue)
if let themeIndex = highlightr.availableThemes().firstIndex(where: { config.theme == $0 }) {
themePopup.selectItem(at: themeIndex)
let theme = highlightr.availableThemes()[themeIndex]
upateTextThemeUI(theme)
}
Expand Down Expand Up @@ -197,7 +191,7 @@ class MainViewController: NSViewController {

func generateClasses() {
guard let JSONTextViewString = JSONTextView.textStorage?
.string.components(separatedBy: CharacterSet.whitespacesAndNewlines).joined(separator: ""),
.string.components(separatedBy: CharacterSet.whitespacesAndNewlines).joined(separator: ""),
JSONTextViewString.count > 0 else {
setupClassTextViewContent("")
setupClassImpTextViewContent("")
Expand All @@ -210,8 +204,8 @@ class MainViewController: NSViewController {
let JSONObject = try? JSONSerialization.jsonObject(with: JSONTextViewData, options: [.mutableContainers, .mutableLeaves]),
let JSONData = try? JSONSerialization.data(withJSONObject: JSONObject, options: [.sortedKeys, .prettyPrinted]),
let JSONString = String(data: JSONData, encoding: .utf8) {
let configFile = FileCacheManager.shared.configFile()
let fileString = JSONProcesser.shared.buildWithJSONObject(JSONObject, file: configFile)
let file = FileCacheManager.shared.configFile()
let fileString = JSONProcesser.shared.buildWithJSONObject(JSONObject, file: file)
let endTime1 = CFAbsoluteTimeGetCurrent()
let offsetTime1 = Int((endTime1 - startTime) * 1000)

Expand Down Expand Up @@ -268,10 +262,10 @@ class MainViewController: NSViewController {
JSONStorage.highlightr.theme.codeFont = NSFont(name: "Menlo", size: 14)
}

private func updateFileConfigAndViews() {
let configFile = FileCacheManager.shared.configFile()
guard let langType = LangType(rawValue: languageBox.indexOfSelectedItem),
let structType = StructType(rawValue: structureBox.indexOfSelectedItem)
private func updateConfigUI() {
let config = FileCacheManager.shared.configFile()
guard let langType = LangType(rawValue: languagesPopup.indexOfSelectedItem),
let structType = StructType(rawValue: structurePopup.indexOfSelectedItem)
else {
assert(false, "lang or struct type error")
return
Expand All @@ -293,11 +287,11 @@ class MainViewController: NSViewController {
}

let transStruct = LangStruct(langType: langType, structType: structType)
configFile.langStruct = transStruct
config.langStruct = transStruct

let theme = highlightr.availableThemes()[themeBox.indexOfSelectedItem]
configFile.theme = theme
FileCacheManager.shared.updateConfigWithFile(configFile)
let theme = highlightr.availableThemes()[themePopup.indexOfSelectedItem]
config.theme = theme
FileCacheManager.shared.updateConfigWithFile(config)
generateClasses()
}

Expand All @@ -321,22 +315,32 @@ extension MainViewController {
}
}

extension MainViewController: NSComboBoxDelegate {
func comboBoxWillDismiss(_ notification: Notification) {
let comBox = notification.object as! NSComboBox
if comBox == languageBox || comBox == structureBox {
let langType = LangType(rawValue: languageBox.indexOfSelectedItem)!
if langType.onlyCompatibleClass {
structureBox.selectItem(at: 1)
} else if langType.onlyCompatibleStruct {
structureBox.selectItem(at: 0)
}
} else if comBox == themeBox {
let theme = highlightr.availableThemes()[themeBox.indexOfSelectedItem]
upateTextThemeUI(theme)
extension MainViewController {
@IBAction func languageSelectedChanged(_ sender: NSPopUpButton) {
let langType = LangType(rawValue: sender.indexOfSelectedItem)!
if langType.onlyCompatibleClass {
structurePopup.selectItem(at: 1)
} else if langType.onlyCompatibleStruct {
structurePopup.selectItem(at: 0)
}

updateFileConfigAndViews()
updateConfigUI()
}

@IBAction func structureSelectedChanged(_ sender: NSPopUpButton) {
let langType = LangType(rawValue: languagesPopup.indexOfSelectedItem)!
if langType.onlyCompatibleClass {
structurePopup.selectItem(at: 1)
} else if langType.onlyCompatibleStruct {
structurePopup.selectItem(at: 0)
}

updateConfigUI()
}

@IBAction func themeSelctedChanged(_ sender: NSPopUpButton) {
let theme = highlightr.availableThemes()[sender.indexOfSelectedItem]
upateTextThemeUI(theme)
}
}

Expand Down
Loading

0 comments on commit a9666cc

Please sign in to comment.