Skip to content

Fix Tasks Not Saving #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class CEWorkspaceSettings: ObservableObject {

private(set) var folderURL: URL

private var settingsURL: URL {
folderURL.appending(path: "settings").appending(path: "json")
var settingsURL: URL {
folderURL.appending(path: "settings").appendingPathExtension("json")
}

init(workspaceURL: URL) {
Expand Down Expand Up @@ -54,7 +54,17 @@ final class CEWorkspaceSettings: ObservableObject {
/// Save``CEWorkspaceSettingsManager`` model to `.codeedit/settings.json`
func savePreferences() throws {
// If the user doesn't have any settings to save, don't save them.
guard !settings.isEmpty() else { return }
guard !settings.isEmpty() else {
// Settings is empty, remove the file & directory if it's empty.
if fileManager.fileExists(atPath: settingsURL.path()) {
try fileManager.removeItem(at: settingsURL)

if try fileManager.contentsOfDirectory(atPath: folderURL.path()).isEmpty {
try fileManager.removeItem(at: folderURL)
}
}
return
}

if !fileManager.fileExists(atPath: folderURL.path()) {
try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ struct CETaskFormView: View {
@ObservedObject var task: CETask
@State private var selectedEnvID: UUID?

@StateObject var settingsViewModel = SettingsViewModel()
var body: some View {
Form {
Section {
Expand Down Expand Up @@ -85,7 +84,6 @@ struct CETaskFormView: View {
}
}
.formStyle(.grouped)
.environmentObject(settingsViewModel)
}

func removeSelectedEnv() {
Expand All @@ -100,7 +98,3 @@ struct CETaskFormView: View {
})
}
}

// #Preview {
// CETaskFormView()
// }
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ struct CEWorkspaceSettingsView: View {
@EnvironmentObject var workspaceSettingsManager: CEWorkspaceSettings
@EnvironmentObject var workspace: WorkspaceDocument

@StateObject var settingsViewModel = SettingsViewModel()

@State var selectedTaskID: UUID?
@State var showAddTaskSheet: Bool = false

Expand Down Expand Up @@ -68,7 +66,6 @@ struct CEWorkspaceSettingsView: View {
}
.padding()
}
.environmentObject(settingsViewModel)
.sheet(isPresented: $showAddTaskSheet) {
if let selectedTaskIndex = workspaceSettingsManager.settings.tasks.firstIndex(where: {
$0.id == selectedTaskID
Expand Down
24 changes: 16 additions & 8 deletions CodeEdit/Features/CEWorkspaceSettings/Views/EditCETaskView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ struct EditCETaskView: View {
Divider()
HStack {
Button(role: .destructive) {
workspaceSettingsManager.settings.tasks.removeAll(where: {
$0.id == task.id
})
try? workspaceSettingsManager.savePreferences()
taskManager.deleteTask(taskID: task.id)
self.dismiss()
do {
workspaceSettingsManager.settings.tasks.removeAll(where: {
$0.id == task.id
})
try workspaceSettingsManager.savePreferences()
taskManager.deleteTask(taskID: task.id)
self.dismiss()
} catch {
NSAlert(error: error).runModal()
}
} label: {
Text("Delete")
.foregroundStyle(.red)
Expand All @@ -38,8 +42,12 @@ struct EditCETaskView: View {
Spacer()

Button {
try? workspaceSettingsManager.savePreferences()
self.dismiss()
do {
try workspaceSettingsManager.savePreferences()
self.dismiss()
} catch {
NSAlert(error: error).runModal()
}
} label: {
Text("Done")
.frame(minWidth: 56)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// CEWorkspaceSettingsTests.swift
// CodeEditTests
//
// Created by Khan Winter on 4/21/25.
//

import Foundation
import Testing
@testable import CodeEdit

struct CEWorkspaceSettingsTests {
let settings: CEWorkspaceSettings = CEWorkspaceSettings(workspaceURL: URL(filePath: "/"))

@Test
func settingsURLNoSpace() async throws {
#expect(settings.folderURL.lastPathComponent == ".codeedit")
#expect(settings.settingsURL.lastPathComponent == "settings.json")
}
}