-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
snooze parameters being stored in core data
- Loading branch information
1 parent
b1fc7c3
commit 03381e6
Showing
10 changed files
with
345 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
import CoreData | ||
import os | ||
|
||
class SnoozeParametersAccessor { | ||
|
||
// MARK: - Properties | ||
|
||
/// CoreDataManager to use | ||
private let coreDataManager:CoreDataManager | ||
|
||
/// for logging | ||
private var log = OSLog(subsystem: ConstantsLog.subSystem, category: ConstantsLog.categoryApplicationDataSnoozeParameter) | ||
|
||
// MARK: - initializer | ||
|
||
init(coreDataManager:CoreDataManager) { | ||
self.coreDataManager = coreDataManager | ||
} | ||
|
||
// MARK: Public functions | ||
|
||
/// - gets all SnoozeParameters instances from coredata | ||
/// - if this the first call to this function (ie no SnoozeParameters stored yet in coredata), then they will be created for every AlertKind | ||
/// - sorts them by AlertKind.rawvalue (from low to high), ie from 0 to (verylow) to 8 (fastrise) | ||
func getSnoozeParameters() -> [SnoozeParameters] { | ||
|
||
// create fetchRequest to get SnoozeParameters's as SnoozeParameters classes | ||
let snoozeParametersFetchRequest: NSFetchRequest<SnoozeParameters> = SnoozeParameters.fetchRequest() | ||
|
||
// sort by alertkind from low to high | ||
snoozeParametersFetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(SnoozeParameters.alertKind), ascending: true)] | ||
|
||
// fetch the SnoozeParameterss | ||
var snoozeParameterArray = [SnoozeParameters]() | ||
coreDataManager.mainManagedObjectContext.performAndWait { | ||
do { | ||
// Execute Fetch Request | ||
snoozeParameterArray = try snoozeParametersFetchRequest.execute() | ||
} catch { | ||
let fetchError = error as NSError | ||
trace("in getSnoozeParameterss, Unable to Execute SnoozeParameterss Fetch Request : %{public}@", log: self.log, category: ConstantsLog.categoryApplicationDataSnoozeParameter, type: .error, fetchError.localizedDescription) | ||
} | ||
} | ||
|
||
// snoozeParameters are ordered by alertKind so goes from 0 to highest value | ||
// but maybe some (or all) are missing | ||
// if some are missing, then it's either because it's the first time this app runs | ||
// or it's because new alertKind's have been added, in which case it's at the end of the range they are added | ||
for index in snoozeParameterArray.count..<AlertKind.allCases.count { | ||
|
||
if let alertKind = AlertKind(rawValue: index) { | ||
|
||
snoozeParameterArray.append(SnoozeParameters(alertKind: alertKind, snoozePeriodInMinutes: 0, snoozeTimeStamp: nil, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)) | ||
|
||
} | ||
|
||
} | ||
|
||
return snoozeParameterArray | ||
|
||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
xdrip/Core Data/classes/SnoozeParameters+CoreDataClass.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import CoreData | ||
|
||
/// maps to SnoozeParameters | ||
public class SnoozeParameters: NSManagedObject { | ||
|
||
init( | ||
alertKind:AlertKind, | ||
snoozePeriodInMinutes: Int16, | ||
snoozeTimeStamp: Date?, | ||
nsManagedObjectContext:NSManagedObjectContext | ||
) { | ||
let entity = NSEntityDescription.entity(forEntityName: "SnoozeParameters", in: nsManagedObjectContext)! | ||
super.init(entity: entity, insertInto: nsManagedObjectContext) | ||
|
||
self.snoozePeriodInMinutes = snoozePeriodInMinutes | ||
self.alertKind = Int16(alertKind.rawValue) | ||
self.snoozeTimeStamp = snoozeTimeStamp | ||
|
||
} | ||
|
||
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { | ||
super.init(entity: entity, insertInto: context) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
xdrip/Core Data/classes/SnoozeParameters+CoreDataProperties.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// SnoozeParameters+CoreDataProperties.swift | ||
// xdrip | ||
// | ||
// Created by Johan Degraeve on 14/04/2019. | ||
// Copyright © 2019 Johan Degraeve. All rights reserved. | ||
// | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
|
||
extension SnoozeParameters { | ||
|
||
@nonobjc public class func fetchRequest() -> NSFetchRequest<SnoozeParameters> { | ||
return NSFetchRequest<SnoozeParameters>(entityName: "SnoozeParameters") | ||
} | ||
|
||
/// the alertKind for which these snooze parameters are applicable | ||
@NSManaged public var alertKind: Int16 | ||
|
||
/// this is snooze period chosen by user, nil value is not snoozed | ||
@NSManaged public var snoozePeriodInMinutes:Int16 | ||
|
||
/// when was the alert snoozed, nil is not snoozed | ||
@NSManaged public var snoozeTimeStamp:Date? | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.