mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-26 22:07:12 +00:00
Reimplemented reachability test and mode display in tracking view
This commit is contained in:
1 parent
980dd6455b
commit
ee349e13df
8 files changed
+89
-78
No files matched your search
@@ -15,23 +15,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
var window: UIWindow?
|
||||
|
||||
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
|
||||
|
||||
// Set up connection logging for debug builds
|
||||
#if DEBUG
|
||||
//AFNetworkActivityLogger.sharedLogger().startLogging()
|
||||
//AFNetworkActivityLogger.sharedLogger().level = AFHTTPRequestLoggerLevel.AFLoggerLevelDebug
|
||||
#endif
|
||||
|
||||
// Initialize core data, migrate database if needed, or delete if migration needed but not possible
|
||||
CoreDataManager.sharedManager
|
||||
|
||||
// Start timer in case GPS fixes need to be sent
|
||||
// SendGPSFixController.sharedManager.timer()
|
||||
|
||||
// Setup
|
||||
self.setupNavigationBarApperance()
|
||||
self.setupPageControlApperance()
|
||||
|
||||
setupAFNetworking()
|
||||
setupNavigationBarApperance()
|
||||
setupPageControlApperance()
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -66,6 +57,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
// MARK: - Setups
|
||||
|
||||
private func setupAFNetworking() {
|
||||
AFNetworkActivityIndicatorManager.sharedManager().enabled = true
|
||||
AFNetworkReachabilityManager.sharedManager().startMonitoring()
|
||||
|
||||
// Set up connection logging for debug builds
|
||||
#if DEBUG
|
||||
//AFNetworkActivityLogger.sharedLogger().startLogging()
|
||||
//AFNetworkActivityLogger.sharedLogger().level = AFHTTPRequestLoggerLevel.AFLoggerLevelDebug
|
||||
#endif
|
||||
}
|
||||
|
||||
private func setupNavigationBarApperance() {
|
||||
let tintColor = UIColor(hex: 0x009de0)
|
||||
let font = UIFont(name: "OpenSans-Bold", size: CGFloat(17.0)) ?? UIFont.systemFontOfSize(17.0)
|
||||
|
||||
@@ -10,6 +10,29 @@ import UIKit
|
||||
|
||||
class GPSFixController: NSObject {
|
||||
|
||||
struct NotificationType {
|
||||
static let ModeChanged = "RegattaController.ModeChanged"
|
||||
}
|
||||
|
||||
struct UserInfo {
|
||||
static let Mode = "Mode"
|
||||
}
|
||||
|
||||
enum Mode: String {
|
||||
case BatterySaving
|
||||
case Error
|
||||
case Online
|
||||
case Offline
|
||||
var description: String {
|
||||
switch self {
|
||||
case .BatterySaving: return NSLocalizedString("Battery Saving", comment: "")
|
||||
case .Error: return NSLocalizedString("Error", comment: "")
|
||||
case .Offline: return NSLocalizedString("Offline", comment: "")
|
||||
case .Online: return NSLocalizedString("Online", comment: "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let regatta: Regatta
|
||||
|
||||
init(regatta: Regatta) {
|
||||
@@ -43,6 +66,7 @@ class GPSFixController: NSObject {
|
||||
private func sendGPSFixesSuccess(gpsFixes: Set<GPSFix>, completion: () -> Void) {
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
self.log("Sending \(gpsFixes.count) GPS fixes was successful")
|
||||
self.postModeChangedNotification(BatteryManager.sharedManager.batterySaving ? .BatterySaving : .Online)
|
||||
CoreDataManager.sharedManager.deleteObjects(gpsFixes)
|
||||
CoreDataManager.sharedManager.saveContext()
|
||||
self.log("\(gpsFixes.count) GPS fixes deleted")
|
||||
@@ -53,10 +77,19 @@ class GPSFixController: NSObject {
|
||||
private func sendGPSFixesFailure(error: AnyObject, completion: () -> Void) {
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
self.log("Sending GPS fixes failed for reason: \(error)")
|
||||
self.postModeChangedNotification(AFNetworkReachabilityManager.sharedManager().reachable ? .Error : .Offline)
|
||||
completion()
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: - Notifications
|
||||
|
||||
private func postModeChangedNotification(mode: Mode) {
|
||||
let userInfo = [UserInfo.Mode: mode.rawValue]
|
||||
let notification = NSNotification(name: NotificationType.ModeChanged, object: self, userInfo: userInfo)
|
||||
NSNotificationQueue.defaultQueue().enqueueNotification(notification, postingStyle: NSPostingStyle.PostASAP)
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
lazy var requestManager: RequestManager = {
|
||||
|
||||
@@ -14,8 +14,8 @@ class ModeTrackingTableViewCell: UITableViewCell {
|
||||
|
||||
override func awakeFromNib() {
|
||||
super.awakeFromNib()
|
||||
self.setupModeLabel(RegattaController.Mode.Offline)
|
||||
self.subscribeForNotifications()
|
||||
setupModeLabel(GPSFixController.Mode.Offline)
|
||||
subscribeForNotifications()
|
||||
}
|
||||
|
||||
deinit {
|
||||
@@ -24,7 +24,7 @@ class ModeTrackingTableViewCell: UITableViewCell {
|
||||
|
||||
// MARK: - Setups
|
||||
|
||||
private func setupModeLabel(mode: RegattaController.Mode) {
|
||||
private func setupModeLabel(mode: GPSFixController.Mode) {
|
||||
modeLabel.text = mode.description
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class ModeTrackingTableViewCell: UITableViewCell {
|
||||
private func subscribeForNotifications() {
|
||||
NSNotificationCenter.defaultCenter().addObserver(self,
|
||||
selector:#selector(regattaControllerModeChanged),
|
||||
name:RegattaController.NotificationType.ModeChanged,
|
||||
name:GPSFixController.NotificationType.ModeChanged,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
@@ -44,7 +44,8 @@ class ModeTrackingTableViewCell: UITableViewCell {
|
||||
|
||||
func regattaControllerModeChanged(notification: NSNotification) {
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
guard let mode = notification.userInfo?[RegattaController.UserInfo.Mode] as? RegattaController.Mode else { return }
|
||||
guard let rawValue = notification.userInfo?[GPSFixController.UserInfo.Mode] as? String else { return }
|
||||
guard let mode = GPSFixController.Mode(rawValue: rawValue) else { return }
|
||||
self.setupModeLabel(mode)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,7 +11,13 @@ import UIKit
|
||||
class Preferences: NSObject {
|
||||
|
||||
struct NotificationType {
|
||||
static let NewCheckInURLChanged = "NewCheckInURLStringChanged"
|
||||
static let BatterySavingChanged = "BatterySavingChanged"
|
||||
static let NewCheckInURLChanged = "NewCheckInURLChanged"
|
||||
}
|
||||
|
||||
struct UserInfo {
|
||||
static let BatterySaving = "BatterySaving"
|
||||
static let CheckInURL = "CheckInURL"
|
||||
}
|
||||
|
||||
struct PreferenceKey {
|
||||
@@ -34,7 +40,9 @@ class Preferences: NSObject {
|
||||
preferences.synchronize()
|
||||
|
||||
// Send notification
|
||||
let notification = NSNotification(name: NotificationType.NewCheckInURLChanged, object: self)
|
||||
var userInfo = [String: AnyObject]()
|
||||
userInfo[UserInfo.CheckInURL] = value
|
||||
let notification = NSNotification(name: NotificationType.NewCheckInURLChanged, object: self, userInfo: userInfo)
|
||||
NSNotificationQueue.defaultQueue().enqueueNotification(notification, postingStyle: NSPostingStyle.PostASAP)
|
||||
}
|
||||
}
|
||||
@@ -69,6 +77,11 @@ class Preferences: NSObject {
|
||||
set(value) {
|
||||
preferences.setBool(value, forKey: PreferenceKey.BatterySaving)
|
||||
preferences.synchronize()
|
||||
|
||||
// Send notification
|
||||
let userInfo = [UserInfo.BatterySaving: value]
|
||||
let notification = NSNotification(name: NotificationType.BatterySavingChanged, object: self, userInfo: userInfo)
|
||||
NSNotificationQueue.defaultQueue().enqueueNotification(notification, postingStyle: NSPostingStyle.PostASAP)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,38 +8,8 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
@objc protocol RegattaControllerDelegate {
|
||||
|
||||
// optional func regattaControllerDidStartTracking(sender: RegattaController)
|
||||
// optional func regattaControllerDidStopTracking(sender: RegattaController)
|
||||
|
||||
}
|
||||
|
||||
class RegattaController: NSObject {
|
||||
|
||||
struct NotificationType {
|
||||
static let ModeChanged = "RegattaController.ModeChanged"
|
||||
}
|
||||
|
||||
struct UserInfo {
|
||||
static let Mode = "Mode"
|
||||
}
|
||||
|
||||
enum Mode: String {
|
||||
case BatterySaving = "BatterySaving"
|
||||
case Error = "Error"
|
||||
case Online = "Online"
|
||||
case Offline = "Offline"
|
||||
var description: String {
|
||||
switch self {
|
||||
case .BatterySaving: return NSLocalizedString("Battery Saving", comment: "")
|
||||
case .Error: return NSLocalizedString("Error", comment: "")
|
||||
case .Offline: return NSLocalizedString("Offline", comment: "")
|
||||
case .Online: return NSLocalizedString("Online", comment: "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct SendingInterval {
|
||||
static let Normal: NSTimeInterval = 3
|
||||
static let BatterySaving: NSTimeInterval = 30
|
||||
@@ -47,7 +17,6 @@ class RegattaController: NSObject {
|
||||
|
||||
let regatta: Regatta
|
||||
|
||||
var delegate: RegattaControllerDelegate?
|
||||
var sendingBackgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
|
||||
var sendingDate: NSDate = NSDate()
|
||||
|
||||
@@ -59,6 +28,10 @@ class RegattaController: NSObject {
|
||||
subscribeForNotifications()
|
||||
}
|
||||
|
||||
deinit {
|
||||
unsubscribeFromNotifications()
|
||||
}
|
||||
|
||||
// MARK: - Notifications
|
||||
|
||||
private func subscribeForNotifications() {
|
||||
@@ -67,6 +40,11 @@ class RegattaController: NSObject {
|
||||
name:LocationManager.NotificationType.Updated,
|
||||
object: nil
|
||||
)
|
||||
NSNotificationCenter.defaultCenter().addObserver(self,
|
||||
selector:#selector(batterySavingChanged(_:)),
|
||||
name:Preferences.NotificationType.BatterySavingChanged,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
|
||||
private func unsubscribeFromNotifications() {
|
||||
@@ -85,6 +63,15 @@ class RegattaController: NSObject {
|
||||
})
|
||||
}
|
||||
|
||||
func batterySavingChanged(notification: NSNotification) {
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
guard let batterySaving = notification.userInfo?[Preferences.UserInfo.BatterySaving] as? Bool else { return }
|
||||
if !batterySaving {
|
||||
self.sendingDate = NSDate() // Send next GPS fixes soon as possible
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: - Background Task
|
||||
|
||||
private func beginGPSFixSendingInBackgroundTask() {
|
||||
@@ -103,7 +90,7 @@ class RegattaController: NSObject {
|
||||
|
||||
private func endGPSFixSendingInBackgroundTask() {
|
||||
UIApplication.sharedApplication().endBackgroundTask(sendingBackgroundTask)
|
||||
sendingBackgroundTask = UIBackgroundTaskInvalid;
|
||||
sendingBackgroundTask = UIBackgroundTaskInvalid
|
||||
}
|
||||
|
||||
// MARK: - Update
|
||||
|
||||
@@ -358,25 +358,3 @@ extension RegattaViewController: UIImagePickerControllerDelegate {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - RegattaControllerDelegate
|
||||
|
||||
extension RegattaViewController: RegattaControllerDelegate {
|
||||
|
||||
// func showRegattaAlert(sender: RegattaController, alertController: UIAlertController) {
|
||||
// presentViewController(alertController, animated: true, completion: nil)
|
||||
// }
|
||||
|
||||
// func regattaControllerDidUpdate(sender: RegattaController) {
|
||||
// // FIXME: Update UI
|
||||
// }
|
||||
|
||||
func regattaControllerDidStartTracking(sender: RegattaController) {
|
||||
|
||||
}
|
||||
|
||||
func regattaControllerDidStopTracking(sender: RegattaController) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -213,7 +213,7 @@ class RequestManager: NSObject {
|
||||
result(imageURL: nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func getTeamImageURLFailed(error: AnyObject, result: (imageURL: String?) -> Void) {
|
||||
result(imageURL: nil) // No image but that's ok
|
||||
}
|
||||
@@ -298,10 +298,6 @@ class RequestManager: NSObject {
|
||||
dataTask.resume();
|
||||
}
|
||||
|
||||
// MARK: - Connection
|
||||
|
||||
var suspended: Bool { get { return manager.operationQueue.suspended } }
|
||||
|
||||
// MARK: - Helper
|
||||
|
||||
private func millisSince1970() -> NSNumber {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#import <TSMarkdownParser/TSMarkdownParser.h>
|
||||
#import <SVProgressHUD/SVProgressHUD.h>
|
||||
#import "UIImageView+AFNetworking.h"
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
|
||||
#if DEBUG
|
||||
#import "AFNetworkActivityLogger.h"
|
||||
|
||||
Reference in new issue
Block a user