Sources/OrgstarMobile/UnsavedHeldFiles.swift
23 lines · 1019 bytes
2 symbols in this file
1import Foundation
2
3/// The files held for their passphrase when the app went to the background. iOS may end the
4/// app there without asking, and their edits, kept only in memory, are lost: the next launch
5/// says so.
6enum UnsavedHeldFiles {
7 static let key = "heldFilesInBackground"
8
9 static func record(_ paths: [String]) {
10 if paths.isEmpty {
11 UserDefaults.standard.removeObject(forKey: key)
12 } else {
13 UserDefaults.standard.set(paths.map { ($0 as NSString).lastPathComponent }, forKey: key)
14 }
15 }
16
17 /// The message for files whose edits were lost when the app last ended; read once, at launch.
18 static let atLaunch: String? = {
19 guard let names = UserDefaults.standard.stringArray(forKey: key), !names.isEmpty else { return nil }
20 UserDefaults.standard.removeObject(forKey: key)
21 return String(localized: "Edits to \(names.joined(separator: ", ")) weren't saved: they were waiting for a passphrase when orgstar was closed.")
22 }()
23}