Skip to content

Instantly share code, notes, and snippets.

@foxicode
Last active August 24, 2023 20:52
Show Gist options
  • Save foxicode/ef3917a8740b1df6f4df1af73813798d to your computer and use it in GitHub Desktop.
Save foxicode/ef3917a8740b1df6f4df1af73813798d to your computer and use it in GitHub Desktop.
SimpleToDoApp main app file
//
// SimpleToDoApp.swift
// SimpleToDo
//
// Copyright © 2022 Paul Hudson.
// Licensed under MIT license.
//
// https://github.com/twostraws/simple-swiftui
// See LICENSE for license information
//
import SwiftUI
/// The main struct for our app, responsible for creating the view model, setting up our
/// basic user interface, and also triggering a save when the app moves to the background.
@main
struct SimpleToDoApp: App {
/// The main shared instance of our view model, created here once and sent into `ContentView`.
@StateObject private var model = ViewModel()
/// Used to detect when the app moves all scenes to the background, so we can trigger a save.
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
NavigationView {
ContentView(model: model)
SelectSomethingView()
}
}
.onChange(of: scenePhase) { phase in
// When all our scenes have moved to the background, we force a save
// immediately in case the user is about to terminate the app.
if phase == .background {
model.save()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment