ソースを参照

refactor: change SaveController to not be static

Sam Jaffe 1 週間 前
コミット
8d71b7e8b4

+ 7 - 3
Todos/Controller/SaveController.swift

@@ -8,15 +8,19 @@
 import Foundation
 
 final class SaveController {
-  static func filename(date: String) -> URL {
+  private func filename(date: String) -> URL {
     URL.documentsDirectory.appending(path: "Weekly Tracker \(date).yml")
   }
 
-  static func save(_ items: [Project], toUrl: URL) {
+  func save(_ items: [Project], onDate: String) {
+    save(items, toUrl: filename(date: onDate))
+  }
+
+  func save(_ items: [Project], toUrl: URL) {
     save(data: Data(items.map({ $0.yaml() }).joined().utf8), toUrl: toUrl)
   }
 
-  static func save(data: Data, toUrl: URL) {
+  func save(data: Data, toUrl: URL) {
     do {
       try data.write(to: toUrl, options: [.atomic, .completeFileProtection])
       let input = try String(contentsOf: toUrl, encoding: .utf8)

+ 1 - 1
Todos/View/Menu/AutosaveMenu.swift

@@ -50,7 +50,7 @@ struct AutosaveMenu: View {
   }
 
   func saveAndCleanup(_ date: Date) {
-    SaveController.save(items, toUrl: SaveController.filename(date: ymd(date)))
+    SaveController().save(items, onDate: ymd(date))
 
     for item in items {
       item.tasks.removeAll(where: { $0.status == .complete })

+ 1 - 1
Todos/View/Menu/ExportMenu.swift

@@ -31,7 +31,7 @@ struct ExportMenu: View {
         else {
           return
         }
-        SaveController.save(data: data, toUrl: url)
+        SaveController().save(data: data, toUrl: url)
       case .failure(let error):
         print(error.localizedDescription)
       }

+ 1 - 1
Todos/View/Menu/SaveAsMenu.swift

@@ -24,7 +24,7 @@ struct SaveAsMenu: View {
                   contentType: .yaml) { result in
       switch result {
       case .success(let url):
-        SaveController.save(items, toUrl: url)
+        SaveController().save(items, toUrl: url)
       case .failure(let error):
         print(error.localizedDescription)
       }

+ 1 - 2
Todos/View/Menu/SaveSnapshotMenu.swift

@@ -15,8 +15,7 @@ struct SaveSnapshotMenu: View {
 
   var body: some View {
     Button("Save Snapshot", systemImage: "square.and.arrow.down") {
-      let snapshot = Date().formatted(.iso8601)
-      SaveController.save(items, toUrl: SaveController.filename(date: snapshot))
+      SaveController().save(items, onDate: Date().formatted(.iso8601))
     }
     .keyboardShortcut("S", modifiers: .command)
   }