Skip to content

Commit

Permalink
Detail view wasn't updating correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
someoneAnyone committed Mar 11, 2016
1 parent a4d6616 commit 414c4f4
Showing 9 changed files with 141 additions and 25 deletions.
10 changes: 10 additions & 0 deletions Nightscouter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@
1771B08B1B75251A006C1F5E /* ColorBlockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1771B0891B75251A006C1F5E /* ColorBlockView.swift */; };
1771B08C1B75251A006C1F5E /* CompassControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1771B08A1B75251A006C1F5E /* CompassControl.swift */; };
177A22091C249F2200912679 /* WatchSessionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8389AC661BEB044F00B81A18 /* WatchSessionManager.swift */; };
1781AA3D1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */; };
1781AA3E1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */; };
178257531C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */; };
178AACD11BEFE90000B0DEBC /* SiteDetailInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 178AACD01BEFE90000B0DEBC /* SiteDetailInterfaceController.swift */; };
17961F921C5D99BF00617898 /* ComplicationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17961F911C5D99BF00617898 /* ComplicationModel.swift */; };
17961F931C5D99BF00617898 /* ComplicationModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17961F911C5D99BF00617898 /* ComplicationModel.swift */; };
@@ -238,6 +241,8 @@
1771B0711B7524D5006C1F5E /* NSURL-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSURL-Extensions.swift"; path = "Extensions/NSURL-Extensions.swift"; sourceTree = "<group>"; };
1771B0891B75251A006C1F5E /* ColorBlockView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ColorBlockView.swift; path = Views/ColorBlockView.swift; sourceTree = "<group>"; };
1771B08A1B75251A006C1F5E /* CompassControl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CompassControl.swift; path = Views/CompassControl.swift; sourceTree = "<group>"; };
1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UpdatableUserInterfaceType.swift; path = "Shared Framework Code/Protocols/UpdatableUserInterfaceType.swift"; sourceTree = SOURCE_ROOT; };
178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UpdatableUserInterfaceType-Extensions.swift"; sourceTree = "<group>"; };
178AACD01BEFE90000B0DEBC /* SiteDetailInterfaceController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SiteDetailInterfaceController.swift; sourceTree = "<group>"; };
17961F911C5D99BF00617898 /* ComplicationModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ComplicationModel.swift; path = Models/ComplicationModel.swift; sourceTree = "<group>"; };
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 @@
children = (
17B07CD71BF643B1001017CE /* DictionaryRepresentable.swift */,
17C3F2BF1C1E607900C8B0CA /* BundleRepresntable.swift */,
1781AA3C1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift */,
);
name = Protocols;
sourceTree = "<group>";
@@ -494,6 +500,7 @@
isa = PBXGroup;
children = (
1771B0711B7524D5006C1F5E /* NSURL-Extensions.swift */,
178257521C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift */,
);
name = Extensions;
sourceTree = "<group>";
@@ -1069,6 +1076,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1781AA3D1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */,
17BD1C181BF0EC1D0014EE38 /* Entry.swift in Sources */,
17BD1C3A1BF0F76A0014EE38 /* UIColor+Extension.swift in Sources */,
17B2DF991C4097F8005FE2D9 /* Array+Extensions.swift in Sources */,
@@ -1098,6 +1106,7 @@
17DA132F1BF4DF0000A89DA4 /* MeterBloodGlucose.swift in Sources */,
17B07CD31BF590B2001017CE /* WatchModel.swift in Sources */,
17BD1C281BF0ECE40014EE38 /* NSCalendar+Extension.swift in Sources */,
178257531C91C5F900E1B3D3 /* UpdatableUserInterfaceType-Extensions.swift in Sources */,
1771B0671B7524AA006C1F5E /* AppTheme.swift in Sources */,
17BD1C1E1BF0EC1D0014EE38 /* WatchEntry.swift in Sources */,
17B07CD81BF643B1001017CE /* DictionaryRepresentable.swift in Sources */,
@@ -1121,6 +1130,7 @@
17961F931C5D99BF00617898 /* ComplicationModel.swift in Sources */,
17B07CD61BF591C5001017CE /* NAssetWatchOS+Helpers.swift in Sources */,
17DA132E1BF4DF0000A89DA4 /* Device.swift in Sources */,
1781AA3E1C91C4F100AE1A52 /* UpdatableUserInterfaceType.swift in Sources */,
17B07CD91BF643B1001017CE /* DictionaryRepresentable.swift in Sources */,
17B2DF9B1C409B70005FE2D9 /* Array+Extensions.swift in Sources */,
17BD1C271BF0ECE40014EE38 /* Double+Extension.swift in Sources */,
7 changes: 4 additions & 3 deletions Nightscouter/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -235,9 +235,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, BundleRepresentable {
print("Posting \(NightscoutAPIClientNotification.DataIsStaleUpdateNow) Notification at \(NSDate())")
#endif

NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: self))
//AppDataManageriOS.sharedInstance.updateWatch(withAction: .UserInfo)

NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: self))
//AppDataManageriOS.sharedInstance.updateWatch(withAction: .UserInfo
}
if (self.timer == nil) {
self.timer = createUpdateTimer()
}
48 changes: 35 additions & 13 deletions Nightscouter/SiteDetailViewController.swift
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ class SiteDetailViewController: UIViewController, UIWebViewDelegate {
}
}
}
var nsApi: NightscoutAPIClient?
// var nsApi: NightscoutAPIClient?
var data = [AnyObject]()

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

@@ -74,6 +74,11 @@ extension SiteDetailViewController{
// print(">>> Entering \(__FUNCTION__) <<<")
// print("\(segue)")
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
UIApplication.sharedApplication().idleTimerDisabled = false
}
}

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

if let siteOptional = site {
nsApi = NightscoutAPIClient(url:siteOptional.url)
// nsApi = NightscoutAPIClient(url:siteOptional.url)
UIApplication.sharedApplication().idleTimerDisabled = siteOptional.overrideScreenLock
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateSite:", name: NightscoutAPIClientNotification.DataIsStaleUpdateNow, object: nil)

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

func updateData() {
func updateData(forceUpdate force: Bool = false) {
guard let site = self.site else { return }

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

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

func updateUI () {
func updateUI() {
defer {
print("setting networkActivityIndicatorVisible: false and stopping animation.")

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

func updateScreenOverride(shouldOverride: Bool) {
if let site = self.site {
site.overrideScreenLock = shouldOverride

AppDataManageriOS.sharedInstance.updateSite(site)
UIApplication.sharedApplication().idleTimerDisabled = site.overrideScreenLock

guard let site = self.site else {
return
}
UIApplication.sharedApplication().idleTimerDisabled = shouldOverride
site.overrideScreenLock = shouldOverride
AppDataManageriOS.sharedInstance.updateSite(site)


#if DEBUG
print("{site.overrideScreenLock:\(site?.overrideScreenLock), UIApplication.idleTimerDisabled:\(UIApplication.sharedApplication().idleTimerDisabled)}")
print("{site.overrideScreenLock:\(site.overrideScreenLock), UIApplication.idleTimerDisabled:\(UIApplication.sharedApplication().idleTimerDisabled)}")
#endif
}

@@ -290,5 +295,22 @@ extension SiteDetailViewController {
#endif
}
}
}

extension SiteDetailViewController: UpdatableUserInterfaceType {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
startUpdateUITimer()
}

func updateUI(notif: NSTimer) {

print("updating ui for: \(notif)")
self.updateData()
}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
updateUITimer.invalidate()
}
}
24 changes: 22 additions & 2 deletions Nightscouter/SiteListTableViewController.swift
Original file line number Diff line number Diff line change
@@ -296,7 +296,7 @@ class SiteListTableViewController: UITableViewController {
let site = sites[indexPath.row]
cell.configureCell(site)
// FIXME:// this prevents a loop, but needs to be fixed and errors need to be reported.
if (site.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
if (site.lastConnectedDate?.compare(site.nextRefreshDate) == .OrderedDescending || lastUpdatedTime == nil || site.configuration == nil) {
refreshDataFor(site, index: indexPath.row)
}
}
@@ -424,4 +424,24 @@ class SiteListTableViewController: UITableViewController {
}


}
}


extension SiteListTableViewController: UpdatableUserInterfaceType {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
startUpdateUITimer()
}

func updateUI(notif: NSTimer) {
print("updating ui for: \(notif)")
self.tableView.reloadData()
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
updateUITimer.invalidate()
}
}


12 changes: 6 additions & 6 deletions NightscouterKit/API/AppDataManageriOS.swift
Original file line number Diff line number Diff line change
@@ -112,11 +112,11 @@ public class AppDataManageriOS: NSObject, BundleRepresentable {

public let iCloudKeyStore = NSUbiquitousKeyValueStore.defaultStore()

public var nextRefreshDate: NSDate {
let date = NSDate().dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime)
print("iOS nextRefreshDate: " + date.description)
return date
}
// public var nextRefreshDate: NSDate {
// let date = NSDate().dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime)
// print("iOS nextRefreshDate: " + date.description)
// return date
// }

// MARK: Save and Load Data
public func saveData() {
@@ -368,7 +368,7 @@ public class AppDataManageriOS: NSObject, BundleRepresentable {
public func updateComplication() {
print("updateComplication")
if let siteToLoad = self.defaultSite() {
if (siteToLoad.lastConnectedDate?.compare(AppDataManageriOS.sharedInstance.nextRefreshDate) == .OrderedDescending || siteToLoad.configuration == nil) {
if (siteToLoad.lastConnectedDate?.compare(siteToLoad.nextRefreshDate) == .OrderedDescending || siteToLoad.configuration == nil) {
print("START: iOS is updating complication data for \(siteToLoad.url)")
fetchSiteData(siteToLoad, handler: { (returnedSite, error) -> Void in
self.updateSite(returnedSite)
9 changes: 9 additions & 0 deletions NightscouterKit/UpdatableUserInterfaceType-Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Protocols.swift
// Nightscouter
//
// Created by Peter Ina on 3/10/16.
// Copyright © 2016 Peter Ina. All rights reserved.
//

import UIKit
2 changes: 1 addition & 1 deletion NightscouterToday/TodayViewController.swift
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ class TodayViewController: UITableViewController, NCWidgetProviding {

contentCell.configureCell(site)

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

10 changes: 10 additions & 0 deletions Shared Framework Code/Models/Site.swift
Original file line number Diff line number Diff line change
@@ -58,6 +58,16 @@ public class Site: NSObject, NSCoding, DictionaryConvertible {
public private(set) var uuid: NSUUID
public private(set) var lastConnectedDate: NSDate?

public var nextRefreshDate: NSDate {
let date = lastConnectedDate?.dateByAddingTimeInterval(Constants.NotableTime.StandardRefreshTime) ?? NSDate().dateByAddingTimeInterval(-10)
print("iOS nextRefreshDate: " + date.description)
return date
}

public var updateNow: Bool {
return self.lastConnectedDate?.compare(self.nextRefreshDate) == .OrderedDescending
}

public override var description: String {
return dictionary.description
}
44 changes: 44 additions & 0 deletions Shared Framework Code/Protocols/UpdatableUserInterfaceType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// UpdatableUserInterfaceType.swift
// Nightscouter
//
// Created by Peter Ina on 3/10/16.
// Copyright © 2016 Peter Ina. All rights reserved.
//

import Foundation


#if os(iOS)
import UIKit
public typealias ViewController = UIViewController
#elseif os(watchOS)
import WatchKit
public typealias ViewController = WKInterfaceController
#elseif os(OSX)
import Cocoa
public typealias ViewController = NSViewController
#endif

public protocol UpdatableUserInterfaceType {
func startUpdateUITimer()
var updateInterval: NSTimeInterval { get }
func updateUI(notif: NSTimer)
}

public extension UpdatableUserInterfaceType where Self: ViewController {

var updateUITimer: NSTimer {
return NSTimer.scheduledTimerWithTimeInterval(updateInterval, target: self, selector: Selector("updateUI:"), userInfo: nil, repeats: true)
}

func startUpdateUITimer() {
print(updateUITimer)
}

var updateInterval: NSTimeInterval {
return 60.0
}
}


0 comments on commit 414c4f4

Please sign in to comment.