Skip to content

Commit

Permalink
Adopt personal formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bradhowes committed Jan 1, 2024
1 parent 7e32b10 commit bd7d35e
Show file tree
Hide file tree
Showing 27 changed files with 1,990 additions and 1,994 deletions.
128 changes: 64 additions & 64 deletions MotionCollector/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Brad Howes. All rights reserved.
// Copyright © 2022, 2024 Brad Howes. All rights reserved.

import os
import UIKit
Expand All @@ -7,75 +7,75 @@ import CoreData
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {

lazy var log = Logging.logger("app")

private let cloudUploader = CloudUploader.shared // Do this to force creation of instance ASAP

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after
// application:didFinishLaunchingWithOptions. Use this method to release any resources that were specific to
// the discarded scenes, as they will not return.
}

func applicationWillResignActive(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillResignActive")
movingToBackground()
}

func applicationDidEnterBackground(_ application: UIApplication) {
os_log(.info, log: log, "applicationDidEnterBackground")
movingToBackground()
}

func applicationWillEnterForeground(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillEnterForeground")
CloudUploader.shared.startUploads()
}

func applicationDidBecomeActive(_ application: UIApplication) {
os_log(.info, log: log, "applicationDidBecomeActive")
}

func applicationWillTerminate(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillTerminate")
movingToBackground()
}
lazy var log = Logging.logger("app")

private let cloudUploader = CloudUploader.shared // Do this to force creation of instance ASAP

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after
// application:didFinishLaunchingWithOptions. Use this method to release any resources that were specific to
// the discarded scenes, as they will not return.
}

func applicationWillResignActive(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillResignActive")
movingToBackground()
}

func applicationDidEnterBackground(_ application: UIApplication) {
os_log(.info, log: log, "applicationDidEnterBackground")
movingToBackground()
}

func applicationWillEnterForeground(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillEnterForeground")
CloudUploader.shared.startUploads()
}

func applicationDidBecomeActive(_ application: UIApplication) {
os_log(.info, log: log, "applicationDidBecomeActive")
}

func applicationWillTerminate(_ application: UIApplication) {
os_log(.info, log: log, "applicationWillTerminate")
movingToBackground()
}
}

extension AppDelegate {

/// Controls uploading to iCloud. Note that although it is a settable value, the value returned will depend on
/// whether the device can access iCloud. If it cannot, this will always return `false`
public var uploadsEnabled: Bool {
get { return CloudUploader.shared.enabled && FileManager.default.hasCloudDirectory }
set { CloudUploader.shared.enabled = newValue }
}

private func movingToBackground() {
NotificationCenter.default.post(name: stopRecordingRequest, object: nil)
RecordingInfoManagedContext.shared.save()
CloudUploader.shared.stopUploads()
}
/// Controls uploading to iCloud. Note that although it is a settable value, the value returned will depend on
/// whether the device can access iCloud. If it cannot, this will always return `false`
public var uploadsEnabled: Bool {
get { return CloudUploader.shared.enabled && FileManager.default.hasCloudDirectory }
set { CloudUploader.shared.enabled = newValue }
}

private func movingToBackground() {
NotificationCenter.default.post(name: stopRecordingRequest, object: nil)
RecordingInfoManagedContext.shared.save()
CloudUploader.shared.stopUploads()
}
}

extension UIApplication {

/// Short-hand for accessing the our delegate
static var appDelegate: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
/// Short-hand for accessing the our delegate
static var appDelegate: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
}
64 changes: 32 additions & 32 deletions MotionCollector/Core Data/ContextDidSaveNotification.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2019 Brad Howes. All rights reserved.
// Copyright © 2019, 2024 Brad Howes. All rights reserved.

import CoreData
import Foundation
Expand All @@ -9,50 +9,50 @@ import Foundation
NOTE: as Apple's documentation states:
You can only use the managed objects in this notification on the same thread on which it was posted.
You can only use the managed objects in this notification on the same thread on which it was posted.
*/
public struct ContextDidSaveNotification<T> where T: NSManagedObject {

private let notification: Notification
private let notification: Notification

/// Obtain an interator over the objects that have been inserted.
public var insertedObjects: AnyIterator<T> { iterator(forKey: NSInsertedObjectsKey) }
/// Obtain an interator over the objects that have been inserted.
public var insertedObjects: AnyIterator<T> { iterator(forKey: NSInsertedObjectsKey) }

/// Obtain an interator over the objects that have been updated.
public var updatedObjects: AnyIterator<T> { iterator(forKey: NSUpdatedObjectsKey) }
/// Obtain an interator over the objects that have been updated.
public var updatedObjects: AnyIterator<T> { iterator(forKey: NSUpdatedObjectsKey) }

/// Obtain an interator over the objects that have been deleted.
public var deletedObjects: AnyIterator<T> { iterator(forKey: NSDeletedObjectsKey) }
/// Obtain an interator over the objects that have been deleted.
public var deletedObjects: AnyIterator<T> { iterator(forKey: NSDeletedObjectsKey) }

/// Get the managed object context from the notification payload.
public var managedObjectContext: NSManagedObjectContext { notification.object as! NSManagedObjectContext }
/// Get the managed object context from the notification payload.
public var managedObjectContext: NSManagedObjectContext { notification.object as! NSManagedObjectContext }

/**
Create wrapper for the given notification. The notification's name must be .NSManagedObjectContextDidSave.
/**
Create wrapper for the given notification. The notification's name must be .NSManagedObjectContextDidSave.
- parameter notification: the object to wrap
*/
public init(notification: Notification) {
guard notification.name == .NSManagedObjectContextDidSave else { fatalError("incorrect notification") }
self.notification = notification
}
- parameter notification: the object to wrap
*/
public init(notification: Notification) {
guard notification.name == .NSManagedObjectContextDidSave else { fatalError("incorrect notification") }
self.notification = notification
}

/// Get an interator to a collection of managed objects from the notification payload.
private func iterator(forKey key: String) -> AnyIterator<T> {
guard let collection = notification.userInfo?[key] as? NSSet else { return AnyIterator { nil } }
var innerIterator = collection.makeIterator()
return AnyIterator { return innerIterator.next() as? T }
}
/// Get an interator to a collection of managed objects from the notification payload.
private func iterator(forKey key: String) -> AnyIterator<T> {
guard let collection = notification.userInfo?[key] as? NSSet else { return AnyIterator { nil } }
var innerIterator = collection.makeIterator()
return AnyIterator { return innerIterator.next() as? T }
}
}

extension ContextDidSaveNotification: CustomDebugStringConvertible {

public var debugDescription: String {
([notification.name.rawValue, managedObjectContext.description] +
[("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)]
.map { name, collection in
name + ": {" + collection.map { $0.objectID.description }.joined(separator: ", ") + "}"
}).joined(separator: " ")
}
public var debugDescription: String {
([notification.name.rawValue, managedObjectContext.description] +
[("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)]
.map { name, collection in
name + ": {" + collection.map { $0.objectID.description }.joined(separator: ", ") + "}"
}).joined(separator: " ")
}
}
44 changes: 22 additions & 22 deletions MotionCollector/Core Data/CoreDataStack.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// CoreDataStack.swift
// Copyright © 2019 Brad Howes. All rights reserved.
// Copyright © 2019, 2024 Brad Howes. All rights reserved.

import CoreData

Expand All @@ -8,32 +8,32 @@ import CoreData
locate the CoreData model definition file in the right bundle.
*/
public final class CoreDataStack<T: NSPersistentContainer> {
public typealias AvailableNotification = CachedValueTypedNotification<NSManagedObjectContext>
public typealias AvailableNotification = CachedValueTypedNotification<NSManagedObjectContext>

/// Notification that will be emitted when the persistent container is available to use.
public let availableNotification: AvailableNotification
/// Notification that will be emitted when the persistent container is available to use.
public let availableNotification: AvailableNotification

/// The context associated with all managed objects from the persistent container
public var managedObjectContext: NSManagedObjectContext? { return availableNotification.cachedValue }
/// The context associated with all managed objects from the persistent container
public var managedObjectContext: NSManagedObjectContext? { return availableNotification.cachedValue }

private let persistentContainer: T
private let persistentContainer: T

/**
Construct a new Core Data stack that will provide values from a given persistent container
/**
Construct a new Core Data stack that will provide values from a given persistent container
- parameter container: the container to provide
*/
public required init(container: T) {
availableNotification = AvailableNotification(name: container.name + "ManagedObjectContext")
persistentContainer = container
create()
}
- parameter container: the container to provide
*/
public required init(container: T) {
availableNotification = AvailableNotification(name: container.name + "ManagedObjectContext")
persistentContainer = container
create()
}

private func create() {
persistentContainer.loadPersistentStores { [weak self] _, _ in
guard let self = self else { return }
let viewContext = self.persistentContainer.viewContext
self.availableNotification.post(value: viewContext)
}
private func create() {
persistentContainer.loadPersistentStores { [weak self] _, _ in
guard let self = self else { return }
let viewContext = self.persistentContainer.viewContext
self.availableNotification.post(value: viewContext)
}
}
}
Loading

0 comments on commit bd7d35e

Please sign in to comment.