forked from appcoda/JavaScriptCoreDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevicesViewController.swift
More file actions
168 lines (119 loc) · 5.11 KB
/
Copy pathDevicesViewController.swift
File metadata and controls
168 lines (119 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// DevicesViewController.swift
// JSCore
//
// Created by Gabriel Theodoropoulos on 13/02/17.
// Copyright © 2017 Appcoda. All rights reserved.
//
import UIKit
import JavaScriptCore
class DevicesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblDeviceList: UITableView!
var jsContext: JSContext!
var deviceInfo: [DeviceInfo]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tblDeviceList.delegate = self
tblDeviceList.dataSource = self
tblDeviceList.tableFooterView = UIView(frame: CGRect.zero)
tblDeviceList.register(UINib(nibName: "DeviceCell", bundle: nil), forCellReuseIdentifier: "idDeviceCell")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
initializeJS()
parseDeviceData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// MARK: IBAction Methods
@IBAction func dismiss(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
// MARK: Custom Methods
func initializeJS() {
self.jsContext = JSContext()
// Add an exception handler.
self.jsContext.exceptionHandler = { context, exception in
if let exc = exception {
print("JS Exception:", exc.toString())
}
}
// Load the PapaParse library.
if let papaParsePath = Bundle.main.path(forResource: "papaparse.min", ofType: "js") {
do {
let papaParseContents = try String(contentsOfFile: papaParsePath)
self.jsContext.evaluateScript(papaParseContents)
}
catch {
print(error.localizedDescription)
}
}
// Load the Javascript source code from the jssource.js file.
if let jsSourcePath = Bundle.main.path(forResource: "jssource", ofType: "js") {
do {
let jsSourceContents = try String(contentsOfFile: jsSourcePath)
self.jsContext.evaluateScript(jsSourceContents)
}
catch {
print(error.localizedDescription)
}
}
// Set the DeviceInfo class to the JSContext.
self.jsContext.setObject(DeviceInfo.self, forKeyedSubscript: "DeviceInfo" as (NSCopying & NSObjectProtocol)!)
}
func parseDeviceData() {
if let path = Bundle.main.path(forResource: "iPhone_List", ofType: "csv") {
do {
let contents = try String(contentsOfFile: path)
if let functionParseiPhoneList = self.jsContext.objectForKeyedSubscript("parseiPhoneList") {
if let parsedDeviceData = functionParseiPhoneList.call(withArguments: [contents]).toArray() as? [DeviceInfo] {
self.deviceInfo = parsedDeviceData
self.tblDeviceList.reloadData()
}
}
}
catch {
print(error.localizedDescription)
}
}
}
// MARK: UITableViewDelegate & UITableViewDataSource Methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.deviceInfo != nil) ? self.deviceInfo.count : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idDeviceCell") as! DeviceCell
let currentDevice = self.deviceInfo[indexPath.row]
cell.textLabel?.text = currentDevice.model
cell.detailTextLabel?.text = currentDevice.concatOS()
(URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: URL(string: currentDevice.imageURL)!, completionHandler: { (imageData, response, error) in
if let data = imageData {
DispatchQueue.main.async {
cell.imageView?.image = UIImage(data: data)
cell.layoutSubviews()
}
}
}).resume()
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60.0
}
}