ydnabApp.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // ydnabApp.swift
  3. // ydnab
  4. //
  5. // Created by Andrea Franceschini on 20/09/2020.
  6. //
  7. import UIKit
  8. import SwiftUI
  9. import os.log
  10. class AppDelegate : NSObject, UIApplicationDelegate {
  11. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
  12. let applicationSupportURL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
  13. #if DEBUG
  14. print(applicationSupportURL.path)
  15. #endif
  16. guard !applicationSupportURL.path.isEmpty else {
  17. print("Couldn't find Application Support path... something is wrong here.")
  18. // TODO: Crash
  19. return false
  20. }
  21. let budgetsListURL = URL(fileURLWithPath: "Budgets.json", relativeTo: applicationSupportURL)
  22. if !FileManager.default.fileExists(atPath: budgetsListURL.path) {
  23. // TODO: Run onboarding, create first budget
  24. let budget: [BudgetSection] = [
  25. BudgetSection(name: "Everyday Expenses", categories: [
  26. BudgetCategory(name: "Groceries"),
  27. BudgetCategory(name: "Eating out"),
  28. BudgetCategory(name: "Medical"),
  29. BudgetCategory(name: "Clothing"),
  30. BudgetCategory(name: "Household goods")
  31. ]),
  32. BudgetSection(name: "Travel", categories: [
  33. BudgetCategory(name: "Transport"),
  34. BudgetCategory(name: "Fuel"),
  35. BudgetCategory(name: "Accommodation")
  36. ]),
  37. BudgetSection(name: "Rainy Days", categories: [
  38. BudgetCategory(name: "Emergencies"),
  39. BudgetCategory(name: "Car insurance")
  40. ]),
  41. BudgetSection(name: "Monthly Expenses", categories: [
  42. BudgetCategory(name: "Rent"),
  43. BudgetCategory(name: "Mobile")
  44. ]),
  45. BudgetSection(name: "Savings Goals", categories: [
  46. BudgetCategory(name: "Savings"),
  47. BudgetCategory(name: "Holidays")
  48. ])
  49. ]
  50. let budgetsList: BudgetsList = [
  51. BudgetInfo(name: "Default Budget",
  52. localeIdentifier: "en-us",
  53. sections: budget)
  54. ]
  55. let jsonBudget: Data
  56. do {
  57. let encoder = JSONEncoder()
  58. encoder.outputFormatting = .prettyPrinted
  59. jsonBudget = try encoder.encode(budgetsList)
  60. try jsonBudget.write(to: budgetsListURL)
  61. } catch {
  62. print("Couldn't encode budgets list.")
  63. print("Error: \(error)")
  64. // TODO: Crash?
  65. return false
  66. }
  67. }
  68. return true
  69. }
  70. }
  71. @main
  72. struct ydnabApp: App {
  73. let persistenceController = PersistenceController.shared
  74. @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
  75. @Environment(\.scenePhase) private var scenePhase
  76. @AppStorage("lastLoadedBudgetId") private var lastLoadedBudgetId = ""
  77. var body: some Scene {
  78. WindowGroup {
  79. NavigationView {
  80. BudgetsListView(currentBudgetId: UUID(uuidString: lastLoadedBudgetId))
  81. }
  82. .environment(\.managedObjectContext, persistenceController.container.viewContext)
  83. }
  84. .onChange(of: scenePhase) { phase in
  85. print(phase)
  86. switch phase {
  87. case .background:
  88. let context = persistenceController.container.viewContext
  89. if context.hasChanges {
  90. do {
  91. try context.save()
  92. } catch {
  93. print(error)
  94. }
  95. }
  96. default:
  97. print(phase)
  98. }
  99. }
  100. }
  101. }