Skip to content

Commit 1e889c8

Browse files
committed
place versions and branches into store
1 parent a11c121 commit 1e889c8

6 files changed

Lines changed: 118 additions & 43 deletions

File tree

mac/Branch.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Branch.swift
3+
// node-box
4+
//
5+
// Created by Learning on 2019/1/24.
6+
// Copyright © 2019 Learning. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
* A Node.js version branch
13+
*/
14+
class Branch {
15+
var name:String
16+
var pattern:String
17+
18+
init(data: Dictionary<String, String>) {
19+
self.name = data["name"] ?? ""
20+
self.pattern = data["pattern"] ?? ""
21+
}
22+
}

mac/DownloadViewController.swift

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import Cocoa
1010

1111
let rootElement: String = "VERSION SERIES"
12-
let items: [String] = ["All", "Node.js 10.x", "Node.js 9.x", "Node.js 8.x", "Node.js 6.x", "Node.js 4.x", "Node.js 0.12.x", "Node.js 0.10.x"]
1312

1413
class DownloadViewController: NSSplitViewController,
1514
NSOutlineViewDelegate,
@@ -25,43 +24,34 @@ class DownloadViewController: NSSplitViewController,
2524
static let DateCell = "DATE_CELL"
2625
}
2726

28-
var versions: Array<Dictionary<String, Any>>?;
29-
var branches: Array<Dictionary<String, Any>>?;
30-
var currentList: Array<Dictionary<String, Any>>?;
27+
var store: Store?;
28+
var currentList: Array<Version>?;
3129

3230
override func viewDidLoad() {
3331
super.viewDidLoad()
3432
sidebarView.expandItem(rootElement)
35-
sidebarView.selectRowIndexes(IndexSet(integer: 1), byExtendingSelection: false)
36-
self.initData()
33+
self.initStore()
3734
}
3835

3936
/**
4037
* Prepare the downloadable version list for the app
4138
*/
42-
func initData() {
43-
let data = VersionManager.getData()
44-
if data != nil {
45-
print("already exists")
46-
versions = data!["versions"]
47-
branches = data!["branches"]
48-
updateList()
49-
sidebarView.reloadData()
50-
tableView.reloadData()
51-
} else {
52-
print("not exists, downloading...")
53-
VersionManager.updateDownloadList {
54-
self.initData()
55-
}
39+
func initStore() {
40+
Store.getStore { (s) -> Void in
41+
self.store = s
42+
self.sidebarView.reloadData()
43+
self.sidebarView.selectRowIndexes(IndexSet(integer: 1), byExtendingSelection: false)
5644
}
5745
}
5846

5947
/**
6048
* Update downloadable version list when sidebar item cliked
6149
*/
6250
func updateList() {
63-
print(sidebarView.selectedRow)
64-
currentList = versions
51+
let branch = self.store?.branches[sidebarView.selectedRow - 1]
52+
let pattern:String = branch?.pattern ?? ""
53+
currentList = self.store?.versions.filter { $0.version.starts(with: pattern) }
54+
tableView.reloadData()
6555
}
6656

6757
/* ---------- Sidebar ---------- */
@@ -70,7 +60,7 @@ class DownloadViewController: NSSplitViewController,
7060
if item == nil {
7161
return 1
7262
}
73-
return branches?.count ?? 0
63+
return self.store?.branches.count ?? 0
7464
}
7565

7666
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
@@ -102,7 +92,7 @@ class DownloadViewController: NSSplitViewController,
10292
// root element
10393
return rootElement
10494
} else {
105-
return (branches?[index] as! Dictionary<String, String>)["name"] ?? ""
95+
return self.store?.branches[index].name ?? ""
10696
}
10797
}
10898

@@ -118,7 +108,6 @@ class DownloadViewController: NSSplitViewController,
118108
}
119109

120110
func outlineViewSelectionDidChange(_ notification: Notification) {
121-
print("outlineViewSelectionDidChange ...")
122111
updateList()
123112
}
124113

@@ -132,16 +121,16 @@ class DownloadViewController: NSSplitViewController,
132121
var identifier: String = "";
133122

134123
// Get an item from list
135-
guard let item:Dictionary<String, Any> = currentList?[row] else {
124+
guard let item:Version = currentList?[row] else {
136125
return nil
137126
}
138127

139128

140129
if tableColumn == tableView.tableColumns[0] {
141-
text = item["version"] as! String
130+
text = item.version
142131
identifier = CellIdentifiers.VersionCell
143132
} else if tableColumn == tableView.tableColumns[1] {
144-
text = item["date"] as! String
133+
text = item.date
145134
identifier = CellIdentifiers.DateCell
146135
}
147136

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,43 @@ import Alamofire
1111

1212
let VERSION_URL = "https://raw.githubusercontent.com/learning/node-box/master/data.json"
1313

14-
class VersionManager {
14+
class Store {
1515
static private var directory: URL? = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?.appendingPathComponent(Bundle.main.bundleIdentifier!, isDirectory: true)
16+
static private var store:Store? = nil;
17+
18+
var branches:Array<Branch>;
19+
var versions:Array<Version>;
20+
21+
init(data: Dictionary<String, Any>) {
22+
self.branches = (data["branches"] as! Array<Dictionary<String, String>>).map { Branch(data: $0) }
23+
self.versions = (data["versions"] as! Array<Dictionary<String, Any>>).map { Version(data: $0) }
24+
}
25+
26+
static public func getStore(onSuccess success: @escaping (Store) -> Void) {
27+
if store != nil {
28+
// Store exists
29+
success(store!)
30+
} else {
31+
// Initialize needed
32+
var data = getData()
33+
if data != nil {
34+
print("file exists.")
35+
store = Store(data: data!)
36+
success(store!)
37+
} else {
38+
print("file dose not exists, downloading...")
39+
updateDownloadList {
40+
data = getData()
41+
if data != nil {
42+
store = Store(data: data!)
43+
success(store!)
44+
} else {
45+
NotificationCenter.default.post(name: Notification.Name("alert"), object: "Download error, please try again.")
46+
}
47+
}
48+
}
49+
}
50+
}
1651

1752
/**
1853
* Get a local file from file system
@@ -68,23 +103,17 @@ class VersionManager {
68103
* - **data["branches"]**: All available version branches
69104
* - **data["versions"]**: All downloadable version list
70105
*/
71-
static func getData () -> Dictionary<String, Array<Dictionary<String, Any>>>? {
106+
static private func getData () -> Dictionary<String, Any>? {
72107
let file: URL? = getFile(name: "data.json")
73108
if file != nil {
74-
do {
75-
let data: Dictionary<String, Array<Dictionary<String, Any>>> =
76-
try JSONSerialization.jsonObject(with: Data(contentsOf: file!), options: []) as! Dictionary<String, Array<Dictionary<String, Any>>>
77-
return data
78-
} catch {
79-
return nil
80-
}
81-
109+
let data = try! JSONSerialization.jsonObject(with: Data(contentsOf: file!), options: [])
110+
return data as? Dictionary<String, Any>
82111
} else {
83112
return nil
84113
}
85114
}
86115

87-
static func updateDownloadList (onSuccess success: @escaping () -> Void) {
116+
static private func updateDownloadList (onSuccess success: @escaping () -> Void) {
88117
Alamofire.request(VERSION_URL).responseJSON { response in
89118
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
90119
// Write json to file

mac/Version.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Version.swift
3+
// node-box
4+
//
5+
// Created by Learning on 2019/1/24.
6+
// Copyright © 2019 Learning. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class Version {
12+
var version:String;
13+
var npmVersion:String;
14+
var v8Version:String;
15+
var nodeModuleVersion:String;
16+
var url:String
17+
var date:String;
18+
19+
init(data: Dictionary<String, Any>) {
20+
self.version = data["version"] as? String ?? ""
21+
self.npmVersion = data["npm"] as? String ?? ""
22+
self.v8Version = data["v8"] as? String ?? ""
23+
self.nodeModuleVersion = data["node-module-version"] as? String ?? ""
24+
self.url = (data["url"] as! Dictionary<String, String>)["darwin"] ?? ""
25+
self.date = data["date"] as? String ?? ""
26+
}
27+
}

node-box.xcodeproj/project.pbxproj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
B06D8EDC212E88F500B65EEE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B06D8EDB212E88F500B65EEE /* Assets.xcassets */; };
1818
B06D8EDF212E88F500B65EEE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B06D8EDD212E88F500B65EEE /* Main.storyboard */; };
1919
B06D8EE8212EB5FB00B65EEE /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B06D8EE7212EB5FB00B65EEE /* WindowController.swift */; };
20-
B06D8EEA212FB87A00B65EEE /* VersionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B06D8EE9212FB87A00B65EEE /* VersionManager.swift */; };
20+
B06D8EEA212FB87A00B65EEE /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = B06D8EE9212FB87A00B65EEE /* Store.swift */; };
21+
B0EAC05921F995E600E48C15 /* Branch.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0EAC05821F995E600E48C15 /* Branch.swift */; };
22+
B0EAC05B21F9A11C00E48C15 /* Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0EAC05A21F9A11C00E48C15 /* Version.swift */; };
2123
/* End PBXBuildFile section */
2224

2325
/* Begin PBXCopyFilesBuildPhase section */
@@ -46,7 +48,9 @@
4648
B06D8EDE212E88F500B65EEE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
4749
B06D8EE0212E88F500B65EEE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4850
B06D8EE7212EB5FB00B65EEE /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
49-
B06D8EE9212FB87A00B65EEE /* VersionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionManager.swift; sourceTree = "<group>"; };
51+
B06D8EE9212FB87A00B65EEE /* Store.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = "<group>"; };
52+
B0EAC05821F995E600E48C15 /* Branch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Branch.swift; sourceTree = "<group>"; };
53+
B0EAC05A21F9A11C00E48C15 /* Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Version.swift; sourceTree = "<group>"; };
5054
B0F19CEF2133ABC0005C7BD2 /* node-box.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = "node-box.entitlements"; sourceTree = "<group>"; };
5155
/* End PBXFileReference section */
5256

@@ -96,7 +100,9 @@
96100
B066C06C2133D1FC005F126B /* DownloadViewController.swift */,
97101
B052CED7213688CA004B5E87 /* VersionListViewController.swift */,
98102
B052CED92136987E004B5E87 /* VersionListView.swift */,
99-
B06D8EE9212FB87A00B65EEE /* VersionManager.swift */,
103+
B06D8EE9212FB87A00B65EEE /* Store.swift */,
104+
B0EAC05821F995E600E48C15 /* Branch.swift */,
105+
B0EAC05A21F9A11C00E48C15 /* Version.swift */,
100106
B06D8EDB212E88F500B65EEE /* Assets.xcassets */,
101107
B06D8EDD212E88F500B65EEE /* Main.storyboard */,
102108
B06D8EE0212E88F500B65EEE /* Info.plist */,
@@ -182,11 +188,13 @@
182188
isa = PBXSourcesBuildPhase;
183189
buildActionMask = 2147483647;
184190
files = (
185-
B06D8EEA212FB87A00B65EEE /* VersionManager.swift in Sources */,
191+
B06D8EEA212FB87A00B65EEE /* Store.swift in Sources */,
192+
B0EAC05B21F9A11C00E48C15 /* Version.swift in Sources */,
186193
B06D8EDA212E88F400B65EEE /* TabViewController.swift in Sources */,
187194
B06D8EE8212EB5FB00B65EEE /* WindowController.swift in Sources */,
188195
B066C06D2133D1FC005F126B /* DownloadViewController.swift in Sources */,
189196
B052CEDA2136987E004B5E87 /* VersionListView.swift in Sources */,
197+
B0EAC05921F995E600E48C15 /* Branch.swift in Sources */,
190198
B06D8ED8212E88F400B65EEE /* AppDelegate.swift in Sources */,
191199
B052CED8213688CA004B5E87 /* VersionListViewController.swift in Sources */,
192200
);
Binary file not shown.

0 commit comments

Comments
 (0)