add in cloudkit toggle

This commit is contained in:
Trey t
2022-01-26 12:08:40 -06:00
parent 6c96602bff
commit 8c58b37be5
10 changed files with 88 additions and 38 deletions

View File

@@ -8,6 +8,10 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
1C0DAB45279DB0FB003B1F21 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1C0DAB47279DB0FB003B1F21 /* Localizable.strings */; }; 1C0DAB45279DB0FB003B1F21 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1C0DAB47279DB0FB003B1F21 /* Localizable.strings */; };
1C10E24E27A1AB110047948B /* UserDefaultsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C5F4977279C945E0092F1B4 /* UserDefaultsStore.swift */; };
1C10E24F27A1AB1D0047948B /* OnboardingData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C5F4975279C84090092F1B4 /* OnboardingData.swift */; };
1C10E25027A1AB220047948B /* OnboardingDay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CA03774279A294800D26164 /* OnboardingDay.swift */; };
1C10E25127A1AB320047948B /* OnboardingTitle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CA03776279A295600D26164 /* OnboardingTitle.swift */; };
1C2618FA2795E41D00FDC148 /* Charts in Frameworks */ = {isa = PBXBuildFile; productRef = 1C2618F92795E41D00FDC148 /* Charts */; }; 1C2618FA2795E41D00FDC148 /* Charts in Frameworks */ = {isa = PBXBuildFile; productRef = 1C2618F92795E41D00FDC148 /* Charts */; };
1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */; }; 1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */; };
1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C26190227960CE500FDC148 /* ChartDataBuildable.swift */; }; 1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C26190227960CE500FDC148 /* ChartDataBuildable.swift */; };
@@ -649,9 +653,13 @@
1CD90B5F278C7EAD001C4FEA /* Random.swift in Sources */, 1CD90B5F278C7EAD001C4FEA /* Random.swift in Sources */,
1CD90B68278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */, 1CD90B68278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */,
1CD90B71278C80CA001C4FEA /* Feels.xcdatamodeld in Sources */, 1CD90B71278C80CA001C4FEA /* Feels.xcdatamodeld in Sources */,
1C10E25127A1AB320047948B /* OnboardingTitle.swift in Sources */,
1C683FCB2792281400745862 /* Stats.swift in Sources */, 1C683FCB2792281400745862 /* Stats.swift in Sources */,
1C10E25027A1AB220047948B /* OnboardingDay.swift in Sources */,
1C10E24F27A1AB1D0047948B /* OnboardingData.swift in Sources */,
1CD90B52278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */, 1CD90B52278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */,
1CD90B4D278C7E7A001C4FEA /* FeelsWidget.swift in Sources */, 1CD90B4D278C7E7A001C4FEA /* FeelsWidget.swift in Sources */,
1C10E24E27A1AB110047948B /* UserDefaultsStore.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -27,7 +27,7 @@ struct FeelsApp: App {
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext) .environment(\.managedObjectContext, persistenceController.viewContext)
}.onChange(of: scenePhase) { phase in }.onChange(of: scenePhase) { phase in
if phase == .background { if phase == .background {
BGTask.scheduleBackgroundProcessing() BGTask.scheduleBackgroundProcessing()

View File

@@ -13,6 +13,10 @@ class ContentModeViewModel: ObservableObject {
@Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding() @Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding()
init() { init() {
PersistenceController.shared.switchContainerListeners.append {
self.getGroupedData()
}
updateData() updateData()
} }

View File

@@ -11,6 +11,7 @@ class UserDefaultsStore {
enum Keys: String { enum Keys: String {
case savedOnboardingData case savedOnboardingData
case needsOnboarding case needsOnboarding
case useCloudKit
} }
static func getOnboarding() -> OnboardingData { static func getOnboarding() -> OnboardingData {

View File

@@ -6,13 +6,10 @@
// //
import CoreData import CoreData
import WidgetKit import SwiftUI
import Foundation
import UIKit
class PersistenceController { class PersistenceController {
private var dataUpdateCall: (() -> Void)? @AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue) private var useCloudKit = false
private let callDelay = 10
static let shared = PersistenceController.persistenceController static let shared = PersistenceController.persistenceController
@@ -24,6 +21,8 @@ class PersistenceController {
return PersistenceController.shared.container.viewContext return PersistenceController.shared.container.viewContext
} }
public var switchContainerListeners = [(() -> Void)]()
public func getEntry(byDate date: Date) -> MoodEntry? { public func getEntry(byDate date: Date) -> MoodEntry? {
let predicate = NSPredicate(format: "forDate == %@", let predicate = NSPredicate(format: "forDate == %@",
date as NSDate) date as NSDate)
@@ -180,40 +179,49 @@ class PersistenceController {
return entries return entries
} }
let container: NSPersistentCloudKitContainer lazy var container: NSPersistentContainer = {
setupContainer()
}()
init(inMemory: Bool = false) { func switchContainer() {
container = NSPersistentCloudKitContainer(name: "Feels") try? viewContext.save()
if inMemory { container = setupContainer()
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") for item in switchContainerListeners {
item()
}
}
private func setupContainer() -> NSPersistentContainer {
if useCloudKit {
container = NSPersistentCloudKitContainer(name: "Feels")
} else {
container = NSPersistentContainer(name: "Feels")
} }
guard let description = container.persistentStoreDescriptions.first else {
fatalError("Could not retrieve a persistent store description.")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.automaticallyMergesChangesFromParent = true
for description in container.persistentStoreDescriptions { for description in container.persistentStoreDescriptions {
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
} }
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
} }
init(inMemory: Bool = false) {
container = setupContainer()
}
public func splitIntoYearMonth() -> [Int: [Int: [MoodEntry]]] { public func splitIntoYearMonth() -> [Int: [Int: [MoodEntry]]] {
let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0), let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0),

View File

@@ -75,19 +75,19 @@ struct AddMoodHeaderView_Previews: PreviewProvider {
Group { Group {
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
}).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) }).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) }).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
}).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) }).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) }).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
} }
} }
} }

View File

@@ -69,13 +69,13 @@ struct BGView: View, Equatable {
struct BGView_Previews: PreviewProvider { struct BGView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
BGView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) BGView().environment(\.managedObjectContext, PersistenceController.shared.viewContext)
.onAppear(perform: { .onAppear(perform: {
PersistenceController.shared.populateMemory() PersistenceController.shared.populateMemory()
}) })
BGView() BGView()
.preferredColorScheme(.dark) .preferredColorScheme(.dark)
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) .environment(\.managedObjectContext, PersistenceController.shared.viewContext)
} }
} }

View File

@@ -212,6 +212,7 @@ struct ContentView: View {
VStack{ VStack{
settingsButtonView settingsButtonView
.padding(.top, 50)
headerView headerView
@@ -231,13 +232,13 @@ private let itemFormatter: DateFormatter = {
struct ContentView_Previews: PreviewProvider { struct ContentView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) ContentView().environment(\.managedObjectContext, PersistenceController.shared.viewContext)
.onAppear(perform: { .onAppear(perform: {
PersistenceController.shared.populateMemory() PersistenceController.shared.populateMemory()
}) })
ContentView() ContentView()
.preferredColorScheme(.dark) .preferredColorScheme(.dark)
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext) .environment(\.managedObjectContext, PersistenceController.shared.viewContext)
} }
} }

View File

@@ -20,6 +20,8 @@ struct SettingsView: View {
@State private var showWhyBGMode = false @State private var showWhyBGMode = false
@ObservedObject var syncMonitor = SyncMonitor.shared @ObservedObject var syncMonitor = SyncMonitor.shared
@AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue) private var useCloudKit = false
var body: some View { var body: some View {
ZStack { ZStack {
Color(UIColor.secondarySystemBackground) Color(UIColor.secondarySystemBackground)
@@ -27,13 +29,18 @@ struct SettingsView: View {
VStack { VStack {
closeButtonView closeButtonView
.padding() .padding()
cloudKitEnable
addTestDataCell addTestDataCell
clearDB clearDB
changeIcon changeIcon
showOnboardingButton showOnboardingButton
whyBackgroundMode whyBackgroundMode
specialThanksCell specialThanksCell
cloudKitStatus
if useCloudKit {
cloudKitStatus
}
Spacer() Spacer()
} }
.padding() .padding()
@@ -192,6 +199,25 @@ struct SettingsView: View {
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous)) .clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
} }
private var cloudKitEnable: some View {
ZStack {
Color(UIColor.systemBackground)
VStack {
Toggle(String(localized: "settings_use_cloudkit_title"),
isOn: $useCloudKit)
.onChange(of: useCloudKit) { value in
print(value)
PersistenceController.shared.switchContainer()
}
.padding()
Text(String(localized: "settings_use_cloudkit_body"))
}
.padding(.bottom)
}
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
}
private var cloudKitStatus: some View { private var cloudKitStatus: some View {
ZStack { ZStack {
Color(UIColor.systemBackground) Color(UIColor.systemBackground)

View File

@@ -50,6 +50,8 @@
"settings_view_why_bg_mode_body" = "We use it to update the database for any missing day. For example: if you rate Monday and Wednesday you'll see that Tuesday will show on the view as missing, and by tapping that row you can then rate the day that is missing. We need a time to search for and add missing days, we do this nightly and while your device is not being used. It's a quick process and doesn't impact battery life."; "settings_view_why_bg_mode_body" = "We use it to update the database for any missing day. For example: if you rate Monday and Wednesday you'll see that Tuesday will show on the view as missing, and by tapping that row you can then rate the day that is missing. We need a time to search for and add missing days, we do this nightly and while your device is not being used. It's a quick process and doesn't impact battery life.";
"settings_view_change_icon" = "Change Icon"; "settings_view_change_icon" = "Change Icon";
"settings_view_show_onboarding" = "Show onboarding"; "settings_view_show_onboarding" = "Show onboarding";
"settings_use_cloudkit_title" = "Use CloudKit";
"settings_use_cloudkit_body" = "CloudKit fucking sucks";
"mood_value_great" = "Great"; "mood_value_great" = "Great";
"mood_value_good" = "Good"; "mood_value_good" = "Good";