-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from jacuzzicoding/feature/towns
Feature/towns
- Loading branch information
Showing
6 changed files
with
213 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// DataManager.swift | ||
import Foundation | ||
import SwiftData | ||
import Combine | ||
import SwiftUI | ||
|
||
class DataManager: ObservableObject { | ||
// Access to the Model Context | ||
private var modelContext: ModelContext | ||
|
||
// Published properties to notify views of changes | ||
@Published var currentTown: Town? | ||
|
||
// Initialize DataManager and fetch the current town | ||
init(modelContext: ModelContext) { | ||
self.modelContext = modelContext | ||
fetchCurrentTown() | ||
} | ||
|
||
/// Fetches the current town from the persistent store. | ||
/// If no town exists, it creates a default one. | ||
func fetchCurrentTown() { | ||
// Create a basic fetch descriptor for Town | ||
let descriptor = FetchDescriptor<Town>() | ||
|
||
do { | ||
let towns = try modelContext.fetch(descriptor) | ||
if let town = towns.first { | ||
DispatchQueue.main.async { | ||
self.currentTown = town | ||
} | ||
} else { | ||
// No town exists; create a default one | ||
let defaultTown = Town(name: "My Town") | ||
modelContext.insert(defaultTown) | ||
try modelContext.save() | ||
DispatchQueue.main.async { | ||
self.currentTown = defaultTown | ||
} | ||
} | ||
} catch { | ||
print("Error fetching or creating Town: \(error)") | ||
} | ||
} | ||
|
||
|
||
/// Updates the town's name. | ||
/// - Parameter newName: The new name for the town. | ||
func updateTownName(_ newName: String) { | ||
guard let town = currentTown else { return } | ||
town.name = newName | ||
|
||
do { | ||
try modelContext.save() | ||
} catch { | ||
print("Error updating town name: \(error)") | ||
} | ||
} | ||
|
||
/// Adds a new town. (Optional: If you plan to support multiple towns in the future) | ||
/// - Parameter town: The `Town` object to add. | ||
func addTown(_ town: Town) { | ||
modelContext.insert(town) | ||
|
||
do { | ||
try modelContext.save() | ||
currentTown = town | ||
} catch { | ||
print("Error adding new town: \(error)") | ||
} | ||
} | ||
|
||
/// Deletes the current town. (Optional: If you plan to support multiple towns) | ||
func deleteCurrentTown() { | ||
guard let town = currentTown else { return } | ||
modelContext.delete(town) | ||
|
||
do { | ||
try modelContext.save() | ||
fetchCurrentTown() | ||
} catch { | ||
print("Error deleting town: \(error)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Town.swift | ||
import SwiftData | ||
|
||
@Model | ||
class Town { | ||
var name: String | ||
|
||
init(name: String) { | ||
self.name = name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import SwiftUI | ||
import SwiftData | ||
|
||
struct EditTownView: View { | ||
@Binding var isPresented: Bool | ||
@Binding var townName: String | ||
@EnvironmentObject var dataManager: DataManager | ||
|
||
var body: some View { | ||
#if os(macOS) | ||
editTownContent | ||
.frame(width: 300, height: 150) | ||
#else | ||
NavigationStack { | ||
editTownContent | ||
.navigationTitle("Edit Town") | ||
.toolbar { | ||
ToolbarItem(placement: .cancellationAction) { | ||
Button("Cancel") { | ||
isPresented = false | ||
} | ||
} | ||
ToolbarItem(placement: .confirmationAction) { | ||
Button("Save") { | ||
dataManager.updateTownName(townName) | ||
isPresented = false | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
private var editTownContent: some View { | ||
Form { | ||
Section(header: Text("Town Details")) { | ||
TextField("Town Name", text: $townName) | ||
} | ||
#if os(macOS) | ||
HStack { | ||
Button("Cancel") { | ||
isPresented = false | ||
} | ||
Spacer() | ||
Button("Save") { | ||
dataManager.updateTownName(townName) | ||
isPresented = false | ||
} | ||
} | ||
.padding() | ||
#endif | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
EditTownView( | ||
isPresented: .constant(true), | ||
townName: .constant("Test Town") | ||
) | ||
.environmentObject(DataManager(modelContext: try! ModelContext(ModelContainer(for: Town.self)))) | ||
} |