SaveController.swift 756 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // SaveController.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 3/1/26.
  6. //
  7. import Foundation
  8. final class SaveController {
  9. private func filename(date: String) -> URL {
  10. URL.documentsDirectory.appending(path: "Weekly Tracker \(date).yml")
  11. }
  12. func save(_ items: [Project], onDate: String) {
  13. save(items, toUrl: filename(date: onDate))
  14. }
  15. func save(_ items: [Project], toUrl: URL) {
  16. save(data: Data(items.map({ $0.yaml() }).joined().utf8), toUrl: toUrl)
  17. }
  18. func save(data: Data, toUrl: URL) {
  19. do {
  20. try data.write(to: toUrl, options: [.atomic, .completeFileProtection])
  21. let input = try String(contentsOf: toUrl, encoding: .utf8)
  22. print(input)
  23. } catch {
  24. print(error.localizedDescription)
  25. }
  26. }
  27. }