-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathIOSHistoryViewController.swift
241 lines (109 loc) · 7.54 KB
/
IOSHistoryViewController.swift
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// IOSHistoryViewController.swift
// BatteryStatusShow
//
// Created by slee on 2017/5/31.
// Copyright © 2017年 sicreativelee. All rights reserved.
//
import Cocoa
class IOSHistoryViewController: BatteryViewController {
var history: [HistoryMO]!
override func viewDidLoad() {
super.viewDidLoad()
(view as! BaseChartView).title.stringValue = ""
(view as! BaseChartView).title.sizeToFit()
(view as! BaseChartView).ytitle.stringValue = "capacity"
(view as! BaseChartView).ytitle.sizeToFit()
(view as! BaseChartView).xtitle.stringValue = "months"
(view as! BaseChartView).xtitle.sizeToFit()
(view as! BaseChartView).showbelowcolor = true
(view as! BaseChartView).showmaxmin = true
(view as! BaseChartView).offsety = -10
// Do any additional setup after loading the view.
}
override func updatebatterydesc() {
(view as! BaseChartView).linechart.removeAll()
(view as! BaseChartView).rowstep = iobattery.iosDesignCapacity / 4
(view as! BaseChartView).rowinit = iobattery.iosDesignCapacity / 4
let historyFetch :NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "IOSHistory")
historyFetch.sortDescriptors = [NSSortDescriptor(key: "time", ascending: true)]
historyFetch.predicate = NSPredicate(format:"( serialno = %@ )",iobattery.iosBatterySerialNumber)
do {
history = try datacontroller.managedObjectContext.fetch(historyFetch) as? [HistoryMO]
var maxduration : Int = 12;
let now = Date()
let dateformatter = DateFormatter()
dateformatter.timeZone = NSTimeZone.local
dateformatter.dateStyle = .medium
dateformatter.timeStyle = .medium
if (history.count>0){
var interval = history[0].time?.timeIntervalSince(now)
let months = interval! / (-86400 * 30.4375)
let calender = Calendar.current
if (Int(months) > maxduration){
maxduration = Int(months)
}else{
interval = calender.date(byAdding: .year, value: -1, to: now)?.timeIntervalSince(now)
}
(view as! BaseChartView).columnstep = -maxduration / 4
(view as! BaseChartView).columninit = maxduration / 4 * 3
var nextpointday = 1;
let showcount :Int = months>12 ? 24:(Int(months)+1)*2
if (history.count / showcount > 0){
nextpointday = history.count / showcount + 1
}
var dayfollowing = calender.startOfDay(for: history[0].time! )
var min = history[0].capacity as! Int
var max = history[0].capacity as! Int
var numskip = 0
var totalcap = 0
for item in history {
let capacity = item.capacity as! Int
if (capacity > max){
max = capacity
}
if (capacity < min){
min = capacity
}
totalcap += capacity
if ((dayfollowing.timeIntervalSince(calender.startOfDay(for: item.time!))) > TimeInterval(0) ){
numskip += 1
continue;
}
dayfollowing = dayfollowing.addingTimeInterval(Double(nextpointday) * 86400.0 )
let iteminterval = item.time?.timeIntervalSince(now)
let xvalue = iteminterval! / interval!
totalcap /= (numskip + 1)
(view as! BaseChartView).linechart.append(CGPoint(x:1-xvalue,
y: Double ( totalcap ) / Double(iobattery.iosDesignCapacity)))
// print (String(format: "ios history: %d, %@", totalcap,showdate ))
totalcap = 0
numskip = 0
}
if ((view as! BaseChartView).linechart.count>0){
(view as! BaseChartView).linechart.append(CGPoint(x:1,
y: (view as! BaseChartView).linechart[(view as! BaseChartView).linechart.count-1].y))
}
(view as! BaseChartView).custommin = CGFloat(min)
(view as! BaseChartView).custommax = CGFloat(max)
if (history.count>30){
let reg = DetailViewController.calcatedline(history)
let date = Date().timeIntervalSince1970
let nowcap = DetailViewController.calcatepredict(x: date, slope: reg.slope, intersection: reg.intersect)
let firstcap = DetailViewController.calcatepredict(x: (history[0].time?.timeIntervalSince1970)!, slope: reg.slope, intersection: reg.intersect)
(view as! BaseChartView).reglineBegin = CGPoint(x:1-(history[0].time?.timeIntervalSinceNow)!/interval!,y:firstcap/Double(iobattery.iosDesignCapacity))
(view as! BaseChartView).reglineEnd = CGPoint(x:1,y:nowcap/Double(iobattery.iosDesignCapacity))
}
/*
if ((view as! BaseChartView).minnum != nil && (view as! BaseChartView).maxnum != nil && (view as! BaseChartView).minnum.stringValue == (view as! BaseChartView).maxnum.stringValue){
(view as! BaseChartView).showmaxmin = false;
}
*/
}
}catch {
// fatalError("Failed to fetch IOS history: \(error)")
}
view.layer?.setNeedsDisplay()
view.layer?.displayIfNeeded()
}
}