Skip to content

Commit 414c4f4

Browse files
committed
Detail view wasn't updating correctly.
1 parent a4d6616 commit 414c4f4

9 files changed

+141
-25
lines changed

Nightscouter.xcodeproj/project.pbxproj

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
1771B08B1B75251A006C1F5E /* ColorBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1771B0891B75251A006C1F5E /* ColorBlockView.swift */; };
3131
1771B08C1B75251A006C1F5E /* CompassControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1771B08A1B75251A006C1F5E /* CompassControl.swift */; };
3232
177A22091C249F2200912679 /* WatchSessionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8389AC661BEB044F00B81A18 /* WatchSessionManager.swift */; };
33+
1781AA3D1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */; };
34+
1781AA3E1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */; };
35+
178257531C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */; };
3336
178AACD11BEFE90000B0DEBC /* SiteDetailInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 178AACD01BEFE90000B0DEBC /* SiteDetailInterfaceController.swift */; };
3437
17961F921C5D99BF00617898 /* ComplicationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17961F911C5D99BF00617898 /* ComplicationModel.swift */; };
3538
17961F931C5D99BF00617898 /* ComplicationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17961F911C5D99BF00617898 /* ComplicationModel.swift */; };
@@ -238,6 +241,8 @@
238241
1771B0711B7524D5006C1F5E /* NSURL-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSURL-Extensions.swift"; path = "Extensions/NSURL-Extensions.swift"; sourceTree = "<group>"; };
239242
1771B0891B75251A006C1F5E /* ColorBlockView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ColorBlockView.swift; path = Views/ColorBlockView.swift; sourceTree = "<group>"; };
240243
1771B08A1B75251A006C1F5E /* CompassControl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CompassControl.swift; path = Views/CompassControl.swift; sourceTree = "<group>"; };
244+
1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UpdatableUserInterfaceType.swift; path = "Shared Framework Code/Protocols/UpdatableUserInterfaceType.swift"; sourceTree = SOURCE_ROOT; };
245+
178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UpdatableUserInterfaceType-Extensions.swift"; sourceTree = "<group>"; };
241246
178AACD01BEFE90000B0DEBC /* SiteDetailInterfaceController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SiteDetailInterfaceController.swift; sourceTree = "<group>"; };
242247
17961F911C5D99BF00617898 /* ComplicationModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ComplicationModel.swift; path = Models/ComplicationModel.swift; sourceTree = "<group>"; };
243248
17A8A87A1C8612E200BA6147 /* NSUbiquitousKeyValueStore+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSUbiquitousKeyValueStore+Extensions.swift"; path = "Extensions/NSUbiquitousKeyValueStore+Extensions.swift"; sourceTree = "<group>"; };
@@ -451,6 +456,7 @@
451456
children = (
452457
17B07CD71BF643B1001017CE /* DictionaryRepresentable.swift */,
453458
17C3F2BF1C1E607900C8B0CA /* BundleRepresntable.swift */,
459+
1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */,
454460
);
455461
name = Protocols;
456462
sourceTree = "<group>";
@@ -494,6 +500,7 @@
494500
isa = PBXGroup;
495501
children = (
496502
1771B0711B7524D5006C1F5E /* NSURL-Extensions.swift */,
503+
178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */,
497504
);
498505
name = Extensions;
499506
sourceTree = "<group>";
@@ -1069,6 +1076,7 @@
10691076
isa = PBXSourcesBuildPhase;
10701077
buildActionMask = 2147483647;
10711078
files = (
1079+
1781AA3D1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */,
10721080
17BD1C181BF0EC1D0014EE38 /* Entry.swift in Sources */,
10731081
17BD1C3A1BF0F76A0014EE38 /* UIColor+Extension.swift in Sources */,
10741082
17B2DF991C4097F8005FE2D9 /* Array+Extensions.swift in Sources */,
@@ -1098,6 +1106,7 @@
10981106
17DA132F1BF4DF0000A89DA4 /* MeterBloodGlucose.swift in Sources */,
10991107
17B07CD31BF590B2001017CE /* WatchModel.swift in Sources */,
11001108
17BD1C281BF0ECE40014EE38 /* NSCalendar+Extension.swift in Sources */,
1109+
178257531C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift in Sources */,
11011110
1771B0671B7524AA006C1F5E /* AppTheme.swift in Sources */,
11021111
17BD1C1E1BF0EC1D0014EE38 /* WatchEntry.swift in Sources */,
11031112
17B07CD81BF643B1001017CE /* DictionaryRepresentable.swift in Sources */,
@@ -1121,6 +1130,7 @@
11211130
17961F931C5D99BF00617898 /* ComplicationModel.swift in Sources */,
11221131
17B07CD61BF591C5001017CE /* NAssetWatchOS+Helpers.swift in Sources */,
11231132
17DA132E1BF4DF0000A89DA4 /* Device.swift in Sources */,
1133+
1781AA3E1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */,
11241134
17B07CD91BF643B1001017CE /* DictionaryRepresentable.swift in Sources */,
11251135
17B2DF9B1C409B70005FE2D9 /* Array+Extensions.swift in Sources */,
11261136
17BD1C271BF0ECE40014EE38 /* Double+Extension.swift in Sources */,

Nightscouter/AppDelegate.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, BundleRepresentable {
235235
print("Posting \(NightscoutAPIClientNotification.DataIsStaleUpdateNow) Notification at \(NSDate())")
236236
#endif
237237

238-
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: self))
239-
//AppDataManageriOS.sharedInstance.updateWatch(withAction: .UserInfo)
240-
238+
NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
239+
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: self))
240+
//AppDataManageriOS.sharedInstance.updateWatch(withAction: .UserInfo
241+
}
241242
if (self.timer == nil) {
242243
self.timer = createUpdateTimer()
243244
}

Nightscouter/SiteDetailViewController.swift

+35-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SiteDetailViewController: UIViewController, UIWebViewDelegate {
3131
}
3232
}
3333
}
34-
var nsApi: NightscoutAPIClient?
34+
// var nsApi: NightscoutAPIClient?
3535
var data = [AnyObject]()
3636

3737
// MARK: View Lifecycle
@@ -56,7 +56,7 @@ class SiteDetailViewController: UIViewController, UIWebViewDelegate {
5656
override func didReceiveMemoryWarning() {
5757
super.didReceiveMemoryWarning()
5858
// Dispose of any resources that can be recreated.
59-
nsApi?.task?.cancel()
59+
// nsApi?.task?.cancel()
6060
data.removeAll()
6161
}
6262

@@ -74,6 +74,11 @@ extension SiteDetailViewController{
7474
// print(">>> Entering \(__FUNCTION__) <<<")
7575
// print("\(segue)")
7676
}
77+
78+
override func viewDidDisappear(animated: Bool) {
79+
super.viewDidDisappear(animated)
80+
UIApplication.sharedApplication().idleTimerDisabled = false
81+
}
7782
}
7883

7984
// MARK: WebKit WebView Delegates
@@ -103,7 +108,7 @@ extension SiteDetailViewController {
103108
self.loadWebView()
104109

105110
if let siteOptional = site {
106-
nsApi = NightscoutAPIClient(url:siteOptional.url)
111+
// nsApi = NightscoutAPIClient(url:siteOptional.url)
107112
UIApplication.sharedApplication().idleTimerDisabled = siteOptional.overrideScreenLock
108113
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateSite:", name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: nil)
109114

@@ -122,10 +127,10 @@ extension SiteDetailViewController {
122127
self.siteNameLabel?.text = title
123128
}
124129

125-
func updateData() {
130+
func updateData(forceUpdate force: Bool = false) {
126131
guard let site = self.site else { return }
127132

128-
if (site.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || site.entries == nil || site.configuration == nil) {
133+
if (site.lastConnectedDate?.compare(site.nextRefreshDate) == .OrderedDescending || site.entries == nil || site.configuration == nil || force == true) {
129134

130135
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
131136
self.siteActivityView?.startAnimating()
@@ -142,7 +147,7 @@ extension SiteDetailViewController {
142147
}
143148
}
144149

145-
func updateUI () {
150+
func updateUI() {
146151
defer {
147152
print("setting networkActivityIndicatorVisible: false and stopping animation.")
148153

@@ -218,16 +223,16 @@ extension SiteDetailViewController {
218223
}
219224

220225
func updateScreenOverride(shouldOverride: Bool) {
221-
if let site = self.site {
222-
site.overrideScreenLock = shouldOverride
223-
224-
AppDataManageriOS.sharedInstance.updateSite(site)
225-
UIApplication.sharedApplication().idleTimerDisabled = site.overrideScreenLock
226-
226+
guard let site = self.site else {
227+
return
227228
}
229+
UIApplication.sharedApplication().idleTimerDisabled = shouldOverride
230+
site.overrideScreenLock = shouldOverride
231+
AppDataManageriOS.sharedInstance.updateSite(site)
232+
228233

229234
#if DEBUG
230-
print("{site.overrideScreenLock:\(site?.overrideScreenLock), UIApplication.idleTimerDisabled:\(UIApplication.sharedApplication().idleTimerDisabled)}")
235+
print("{site.overrideScreenLock:\(site.overrideScreenLock), UIApplication.idleTimerDisabled:\(UIApplication.sharedApplication().idleTimerDisabled)}")
231236
#endif
232237
}
233238

@@ -290,5 +295,22 @@ extension SiteDetailViewController {
290295
#endif
291296
}
292297
}
298+
}
299+
300+
extension SiteDetailViewController: UpdatableUserInterfaceType {
301+
override func viewWillAppear(animated: Bool) {
302+
super.viewWillAppear(animated)
303+
startUpdateUITimer()
304+
}
293305

306+
func updateUI(notif: NSTimer) {
307+
308+
print("updating ui for: \(notif)")
309+
self.updateData()
310+
}
311+
312+
override func viewWillDisappear(animated: Bool) {
313+
super.viewWillDisappear(animated)
314+
updateUITimer.invalidate()
315+
}
294316
}

Nightscouter/SiteListTableViewController.swift

+22-2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class SiteListTableViewController: UITableViewController {
296296
let site = sites[indexPath.row]
297297
cell.configureCell(site)
298298
// FIXME:// this prevents a loop, but needs to be fixed and errors need to be reported.
299-
if (site.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
299+
if (site.lastConnectedDate?.compare(site.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
300300
refreshDataFor(site, index: indexPath.row)
301301
}
302302
}
@@ -424,4 +424,24 @@ class SiteListTableViewController: UITableViewController {
424424
}
425425

426426

427-
}
427+
}
428+
429+
430+
extension SiteListTableViewController: UpdatableUserInterfaceType {
431+
override func viewWillAppear(animated: Bool) {
432+
super.viewWillAppear(animated)
433+
startUpdateUITimer()
434+
}
435+
436+
func updateUI(notif: NSTimer) {
437+
print("updating ui for: \(notif)")
438+
self.tableView.reloadData()
439+
}
440+
441+
override func viewDidDisappear(animated: Bool) {
442+
super.viewDidDisappear(animated)
443+
updateUITimer.invalidate()
444+
}
445+
}
446+
447+

NightscouterKit/API/AppDataManageriOS.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ public class AppDataManageriOS: NSObject, BundleRepresentable {
112112

113113
public let iCloudKeyStore = NSUbiquitousKeyValueStore.defaultStore()
114114

115-
public var nextRefreshDate: NSDate {
116-
let date = NSDate().dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime)
117-
print("iOS nextRefreshDate: " + date.description)
118-
return date
119-
}
115+
// public var nextRefreshDate: NSDate {
116+
// let date = NSDate().dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime)
117+
// print("iOS nextRefreshDate: " + date.description)
118+
// return date
119+
// }
120120

121121
// MARK: Save and Load Data
122122
public func saveData() {
@@ -368,7 +368,7 @@ public class AppDataManageriOS: NSObject, BundleRepresentable {
368368
public func updateComplication() {
369369
print("updateComplication")
370370
if let siteToLoad = self.defaultSite() {
371-
if (siteToLoad.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || siteToLoad.configuration == nil) {
371+
if (siteToLoad.lastConnectedDate?.compare(siteToLoad.nextRefreshDate) == .OrderedDescending || siteToLoad.configuration == nil) {
372372
print("START: iOS is updating complication data for \(siteToLoad.url)")
373373
fetchSiteData(siteToLoad, handler: { (returnedSite, error) -> Void in
374374
self.updateSite(returnedSite)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Protocols.swift
3+
// Nightscouter
4+
//
5+
// Created by Peter Ina on 3/10/16.
6+
// Copyright © 2016 Peter Ina. All rights reserved.
7+
//
8+
9+
import UIKit

NightscouterToday/TodayViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class TodayViewController: UITableViewController, NCWidgetProviding {
8787

8888
contentCell.configureCell(site)
8989

90-
if (site.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
90+
if (site.lastConnectedDate?.compare(site.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
9191
refreshDataFor(site, index: indexPath.row)
9292
}
9393

Shared Framework Code/Models/Site.swift

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public class Site: NSObject, NSCoding, DictionaryConvertible {
5858
public private(set) var uuid: NSUUID
5959
public private(set) var lastConnectedDate: NSDate?
6060

61+
public var nextRefreshDate: NSDate {
62+
let date = lastConnectedDate?.dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime) ?? NSDate().dateByAddingTimeInterval(-10)
63+
print("iOS nextRefreshDate: " + date.description)
64+
return date
65+
}
66+
67+
public var updateNow: Bool {
68+
return self.lastConnectedDate?.compare(self.nextRefreshDate) == .OrderedDescending
69+
}
70+
6171
public override var description: String {
6272
return dictionary.description
6373
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// UpdatableUserInterfaceType.swift
3+
// Nightscouter
4+
//
5+
// Created by Peter Ina on 3/10/16.
6+
// Copyright © 2016 Peter Ina. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
#if os(iOS)
13+
import UIKit
14+
public typealias ViewController = UIViewController
15+
#elseif os(watchOS)
16+
import WatchKit
17+
public typealias ViewController = WKInterfaceController
18+
#elseif os(OSX)
19+
import Cocoa
20+
public typealias ViewController = NSViewController
21+
#endif
22+
23+
public protocol UpdatableUserInterfaceType {
24+
func startUpdateUITimer()
25+
var updateInterval: NSTimeInterval { get }
26+
func updateUI(notif: NSTimer)
27+
}
28+
29+
public extension UpdatableUserInterfaceType where Self: ViewController {
30+
31+
var updateUITimer: NSTimer {
32+
return NSTimer.scheduledTimerWithTimeInterval(updateInterval, target: self, selector: Selector("updateUI:"), userInfo: nil, repeats: true)
33+
}
34+
35+
func startUpdateUITimer() {
36+
print(updateUITimer)
37+
}
38+
39+
var updateInterval: NSTimeInterval {
40+
return 60.0
41+
}
42+
}
43+
44+

0 commit comments

Comments
 (0)