Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
npna committed Mar 15, 2023
1 parent 7b97f1f commit 330479e
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 212 deletions.
30 changes: 17 additions & 13 deletions KnotClock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
0D13C08229C13F07006AC521 /* BackupRestore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D13C08129C13F07006AC521 /* BackupRestore.swift */; };
0D13C08229C13F07006AC521 /* Backup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D13C08129C13F07006AC521 /* Backup.swift */; };
0D18A3A929C21D9F00D075D4 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D18A3A829C21D9F00D075D4 /* Notifications.swift */; };
0D18A3AB29C21F1F00D075D4 /* Alerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D18A3AA29C21F1F00D075D4 /* Alerts.swift */; };
0D18A3AD29C2231000D075D4 /* DateHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D18A3AC29C2231000D075D4 /* DateHelper.swift */; };
0D803C0229C0B62100C7F4BE /* ContextualMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D803C0129C0B62100C7F4BE /* ContextualMenu.swift */; };
0D803C0429C0B7BA00C7F4BE /* HiddenCountdowns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D803C0329C0B7BA00C7F4BE /* HiddenCountdowns.swift */; };
0DA02AE529BD1DC200CEBB8F /* ConditionalModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DA02AE429BD1DC200CEBB8F /* ConditionalModifier.swift */; };
Expand Down Expand Up @@ -46,7 +49,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0D13C08129C13F07006AC521 /* BackupRestore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackupRestore.swift; sourceTree = "<group>"; };
0D13C08129C13F07006AC521 /* Backup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Backup.swift; sourceTree = "<group>"; };
0D18A3A829C21D9F00D075D4 /* Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
0D18A3AA29C21F1F00D075D4 /* Alerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alerts.swift; sourceTree = "<group>"; };
0D18A3AC29C2231000D075D4 /* DateHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateHelper.swift; sourceTree = "<group>"; };
0D803C0129C0B62100C7F4BE /* ContextualMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContextualMenu.swift; sourceTree = "<group>"; };
0D803C0329C0B7BA00C7F4BE /* HiddenCountdowns.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HiddenCountdowns.swift; sourceTree = "<group>"; };
0DA02AE429BD1DC200CEBB8F /* ConditionalModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConditionalModifier.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,15 +103,6 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0D13C08029C13EF6006AC521 /* Countdowns */ = {
isa = PBXGroup;
children = (
0DCA4C3E29BBABD400F78368 /* Countdowns.swift */,
0D13C08129C13F07006AC521 /* BackupRestore.swift */,
);
path = Countdowns;
sourceTree = "<group>";
};
0DCA4BFB29BBAB3400F78368 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -237,10 +234,14 @@
0DCA4C3A29BBABD400F78368 /* Classes */ = {
isa = PBXGroup;
children = (
0D13C08029C13EF6006AC521 /* Countdowns */,
0DCA4C3E29BBABD400F78368 /* Countdowns.swift */,
0DCA4C3B29BBABD400F78368 /* DataController.swift */,
0DCA4C3C29BBABD400F78368 /* MacMenubar.swift */,
0DCA4C3D29BBABD400F78368 /* AppDelegate.swift */,
0D18A3A829C21D9F00D075D4 /* Notifications.swift */,
0D18A3AC29C2231000D075D4 /* DateHelper.swift */,
0D18A3AA29C21F1F00D075D4 /* Alerts.swift */,
0D13C08129C13F07006AC521 /* Backup.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -331,6 +332,7 @@
0DCA4C5429BBABD400F78368 /* DataController.swift in Sources */,
0DCA4C5029BBABD400F78368 /* Small.swift in Sources */,
0DCA4C4029BBABD400F78368 /* Preferences.swift in Sources */,
0D18A3AD29C2231000D075D4 /* DateHelper.swift in Sources */,
0DCA4C4429BBABD400F78368 /* CountdownsSettingsView.swift in Sources */,
0DA02AE529BD1DC200CEBB8F /* ConditionalModifier.swift in Sources */,
0D803C0429C0B7BA00C7F4BE /* HiddenCountdowns.swift in Sources */,
Expand All @@ -342,10 +344,12 @@
0DCA4C3F29BBABD400F78368 /* Countdown.swift in Sources */,
0DCA4C4D29BBABD400F78368 /* OverviewDailyCountdownsTableCellWithAction.swift in Sources */,
0DCA4C5629BBABD400F78368 /* AppDelegate.swift in Sources */,
0D18A3AB29C21F1F00D075D4 /* Alerts.swift in Sources */,
0DCA4C4129BBABD400F78368 /* HideDaily.swift in Sources */,
0D13C08229C13F07006AC521 /* BackupRestore.swift in Sources */,
0D13C08229C13F07006AC521 /* Backup.swift in Sources */,
0DCA4C4729BBABD400F78368 /* TimeSliced.swift in Sources */,
0DCA4C4F29BBABD400F78368 /* Tiny.swift in Sources */,
0D18A3A929C21D9F00D075D4 /* Notifications.swift in Sources */,
0DCA4C4B29BBABD400F78368 /* FocusModeView.swift in Sources */,
0D803C0229C0B62100C7F4BE /* ContextualMenu.swift in Sources */,
0DCA4C4529BBABD400F78368 /* IndicationsSettingsView.swift in Sources */,
Expand Down
20 changes: 20 additions & 0 deletions KnotClock/Classes/Alerts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Alerts.swift
// KnotClock
//
// Created by NA on 3/15/23.
//

import Foundation

class Alerts: ObservableObject {
static let shared = Alerts()

@Published var message = ""
@Published var isPresented = false

static func show(_ message: String) {
Alerts.shared.message = message
Alerts.shared.isPresented = true
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
//
// BackupRestore.swift
// Backup.swift
// KnotClock
//
// Created by NA on 3/14/23.
//

import SwiftUI
import CoreData
import UniformTypeIdentifiers

#if os(macOS)
extension Countdowns {
func backup() {
let backupFileName = "\(K.appName) Backup \(getCurrentDate()).sqlite"
class Backup {
#if os(macOS)
func save() {
let backupFileName = "\(K.appName) Backup \(DateHelper().getCurrent()).sqlite"

let backupDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let backupURL = backupDirectory.appendingPathComponent(backupFileName)
Expand All @@ -21,7 +20,7 @@ extension Countdowns {
// Delete older backup with same backupFileName
try? FileManager.default.removeItem(at: backupURL)

if let store = coordinator.persistentStores.last {
if let store = coordinator.persistentStores.first {
// Save new backup
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
do {
Expand All @@ -31,14 +30,12 @@ extension Countdowns {
// Open path in Finder
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: backupDirectory.path())
} catch {
self.alertMessage = "Failed to create/store backup file: \(error)"
self.showAlert = true
Alerts.show("Failed to create/store backup file: \(error)")
}
}
}
}

func restoreBackup() {
func restore() {
guard let backupFileType = UTType.init(filenameExtension: "sqlite") else { return }

let coordinator = DataController.shared.container.persistentStoreCoordinator
Expand All @@ -52,12 +49,11 @@ extension Countdowns {
if panel.runModal() == .OK, let selectedBackupURL = panel.url, let storeURL = stores.first?.url {
do {
try coordinator.replacePersistentStore(at: storeURL, destinationOptions: nil, withPersistentStoreFrom: selectedBackupURL, sourceOptions: nil, ofType: NSSQLiteStoreType)
reset(level: .reloadContainerRefetchResetNotifs)
Countdowns.shared.reset(level: .reloadContainerRefetchResetNotifs)
} catch {
self.alertMessage = "Failed to restore backup file: \(error)"
self.showAlert = true
Alerts.show("Failed to restore backup file: \(error)")
}
}
}
#endif
}
#endif
Loading

0 comments on commit 330479e

Please sign in to comment.