add in cloudkit toggle
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
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 */; };
|
||||
1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */; };
|
||||
1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C26190227960CE500FDC148 /* ChartDataBuildable.swift */; };
|
||||
@@ -649,9 +653,13 @@
|
||||
1CD90B5F278C7EAD001C4FEA /* Random.swift in Sources */,
|
||||
1CD90B68278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */,
|
||||
1CD90B71278C80CA001C4FEA /* Feels.xcdatamodeld in Sources */,
|
||||
1C10E25127A1AB320047948B /* OnboardingTitle.swift in Sources */,
|
||||
1C683FCB2792281400745862 /* Stats.swift in Sources */,
|
||||
1C10E25027A1AB220047948B /* OnboardingDay.swift in Sources */,
|
||||
1C10E24F27A1AB1D0047948B /* OnboardingData.swift in Sources */,
|
||||
1CD90B52278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */,
|
||||
1CD90B4D278C7E7A001C4FEA /* FeelsWidget.swift in Sources */,
|
||||
1C10E24E27A1AB110047948B /* UserDefaultsStore.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
@@ -27,7 +27,7 @@ struct FeelsApp: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
||||
.environment(\.managedObjectContext, persistenceController.viewContext)
|
||||
}.onChange(of: scenePhase) { phase in
|
||||
if phase == .background {
|
||||
BGTask.scheduleBackgroundProcessing()
|
||||
|
||||
@@ -13,6 +13,10 @@ class ContentModeViewModel: ObservableObject {
|
||||
@Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding()
|
||||
|
||||
init() {
|
||||
PersistenceController.shared.switchContainerListeners.append {
|
||||
self.getGroupedData()
|
||||
|
||||
}
|
||||
updateData()
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class UserDefaultsStore {
|
||||
enum Keys: String {
|
||||
case savedOnboardingData
|
||||
case needsOnboarding
|
||||
case useCloudKit
|
||||
}
|
||||
|
||||
static func getOnboarding() -> OnboardingData {
|
||||
|
||||
@@ -6,13 +6,10 @@
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import WidgetKit
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
class PersistenceController {
|
||||
private var dataUpdateCall: (() -> Void)?
|
||||
private let callDelay = 10
|
||||
@AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue) private var useCloudKit = false
|
||||
|
||||
static let shared = PersistenceController.persistenceController
|
||||
|
||||
@@ -24,6 +21,8 @@ class PersistenceController {
|
||||
return PersistenceController.shared.container.viewContext
|
||||
}
|
||||
|
||||
public var switchContainerListeners = [(() -> Void)]()
|
||||
|
||||
public func getEntry(byDate date: Date) -> MoodEntry? {
|
||||
let predicate = NSPredicate(format: "forDate == %@",
|
||||
date as NSDate)
|
||||
@@ -180,40 +179,49 @@ class PersistenceController {
|
||||
return entries
|
||||
}
|
||||
|
||||
let container: NSPersistentCloudKitContainer
|
||||
lazy var container: NSPersistentContainer = {
|
||||
setupContainer()
|
||||
}()
|
||||
|
||||
init(inMemory: Bool = false) {
|
||||
container = NSPersistentCloudKitContainer(name: "Feels")
|
||||
if inMemory {
|
||||
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
|
||||
func switchContainer() {
|
||||
try? viewContext.save()
|
||||
container = setupContainer()
|
||||
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
|
||||
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)")
|
||||
}
|
||||
})
|
||||
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
|
||||
container.viewContext.automaticallyMergesChangesFromParent = true
|
||||
|
||||
for description in container.persistentStoreDescriptions {
|
||||
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
|
||||
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]]] {
|
||||
let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0),
|
||||
|
||||
@@ -75,19 +75,19 @@ struct AddMoodHeaderView_Previews: PreviewProvider {
|
||||
Group {
|
||||
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
|
||||
|
||||
}).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
}).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
|
||||
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
|
||||
|
||||
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
|
||||
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
|
||||
|
||||
}).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
}).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
|
||||
AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
|
||||
|
||||
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
}).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,13 +69,13 @@ struct BGView: View, Equatable {
|
||||
|
||||
struct BGView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
BGView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
BGView().environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
.onAppear(perform: {
|
||||
PersistenceController.shared.populateMemory()
|
||||
})
|
||||
|
||||
BGView()
|
||||
.preferredColorScheme(.dark)
|
||||
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
.environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,6 +212,7 @@ struct ContentView: View {
|
||||
|
||||
VStack{
|
||||
settingsButtonView
|
||||
.padding(.top, 50)
|
||||
|
||||
headerView
|
||||
|
||||
@@ -231,13 +232,13 @@ private let itemFormatter: DateFormatter = {
|
||||
|
||||
struct ContentView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
ContentView().environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
.onAppear(perform: {
|
||||
PersistenceController.shared.populateMemory()
|
||||
})
|
||||
|
||||
ContentView()
|
||||
.preferredColorScheme(.dark)
|
||||
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
||||
.environment(\.managedObjectContext, PersistenceController.shared.viewContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ struct SettingsView: View {
|
||||
@State private var showWhyBGMode = false
|
||||
@ObservedObject var syncMonitor = SyncMonitor.shared
|
||||
|
||||
@AppStorage(UserDefaultsStore.Keys.useCloudKit.rawValue) private var useCloudKit = false
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(UIColor.secondarySystemBackground)
|
||||
@@ -27,13 +29,18 @@ struct SettingsView: View {
|
||||
VStack {
|
||||
closeButtonView
|
||||
.padding()
|
||||
cloudKitEnable
|
||||
addTestDataCell
|
||||
clearDB
|
||||
changeIcon
|
||||
showOnboardingButton
|
||||
whyBackgroundMode
|
||||
specialThanksCell
|
||||
cloudKitStatus
|
||||
|
||||
if useCloudKit {
|
||||
cloudKitStatus
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.padding()
|
||||
@@ -192,6 +199,25 @@ struct SettingsView: View {
|
||||
.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 {
|
||||
ZStack {
|
||||
Color(UIColor.systemBackground)
|
||||
|
||||
@@ -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_change_icon" = "Change Icon";
|
||||
"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_good" = "Good";
|
||||
|
||||
Reference in New Issue
Block a user