add a view to list worlds
This commit is contained in:
1 parent
9c1bfe2024
commit
38a202109d
7 files changed
+110
-15
No files matched your search
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// AppRouter.swift
|
||||
// WorldBuilder
|
||||
//
|
||||
// Created by Giovanni Josserand on 01/09/2026.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
@Observable
|
||||
class AppRouter {
|
||||
var selectedTab: AppTab = .home
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum AppTab: Hashable {
|
||||
case home
|
||||
case worldsList
|
||||
}
|
||||
@@ -7,19 +7,23 @@
|
||||
import SwiftUI
|
||||
|
||||
struct MainTabView: View {
|
||||
@Environment(AppRouter.self) private var router
|
||||
|
||||
var body : some View {
|
||||
TabView {
|
||||
@Bindable var router = router
|
||||
|
||||
TabView(selection: $router.selectedTab) {
|
||||
NavigationStack {
|
||||
HomeView()
|
||||
}.tabItem {
|
||||
Label("Accueil", systemImage: "house")
|
||||
}
|
||||
.tabItem {Label("Accueil", systemImage: "house")}
|
||||
.tag(AppTab.home)
|
||||
|
||||
NavigationStack {
|
||||
WorldListView()
|
||||
}.tabItem {
|
||||
Label("Mes mondes", systemImage: "globe.europe.africa")
|
||||
}
|
||||
.tabItem {Label("Mes mondes", systemImage: "globe.europe.africa")}
|
||||
.tag(AppTab.worldsList)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// World.swift
|
||||
// WorldBuilder
|
||||
//
|
||||
// Created by Giovanni Josserand on 01/09/2026.
|
||||
//
|
||||
|
||||
import SwiftData
|
||||
import Foundation
|
||||
|
||||
@Model
|
||||
class World {
|
||||
var id : UUID
|
||||
var createdAt : Date
|
||||
var updatedAt : Date
|
||||
var name : String
|
||||
var overview : String
|
||||
|
||||
init(name: String, overview: String) {
|
||||
self.id = UUID()
|
||||
self.name = name
|
||||
self.overview = overview
|
||||
self.createdAt = Date()
|
||||
self.updatedAt = Date()
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,19 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct CreateWorldView: View {
|
||||
@State private var worldName = ""
|
||||
@State private var worldDescription = ""
|
||||
|
||||
@State private var worldOverview = ""
|
||||
|
||||
@Environment(\.modelContext) private var context
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(AppRouter.self) private var router
|
||||
|
||||
@State private var showSaveError = false
|
||||
@State private var saveErrorMessage = ""
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("Informations") {
|
||||
@@ -18,14 +26,23 @@ struct CreateWorldView: View {
|
||||
|
||||
TextField(
|
||||
"Description",
|
||||
text: $worldDescription,
|
||||
text: $worldOverview,
|
||||
axis: .vertical
|
||||
)
|
||||
}
|
||||
|
||||
Section {
|
||||
Button("Créer le monde") {
|
||||
createWorld()
|
||||
let world = World(name: worldName, overview: worldOverview)
|
||||
context.insert(world)
|
||||
do {
|
||||
try context.save()
|
||||
router.selectedTab = .worldsList
|
||||
dismiss()
|
||||
}catch {
|
||||
saveErrorMessage = "Impossible de sauvegarder le monde : \(error.localizedDescription)"
|
||||
showSaveError = true
|
||||
}
|
||||
}
|
||||
.disabled(worldName.isEmpty)
|
||||
.padding(.horizontal, 24)
|
||||
@@ -39,9 +56,10 @@ struct CreateWorldView: View {
|
||||
}
|
||||
.navigationTitle("Créer un monde")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
}
|
||||
|
||||
private func createWorld() {
|
||||
print("Création du monde : \(worldName)")
|
||||
.alert("Erreur", isPresented: $showSaveError) {
|
||||
Button("OK") { }
|
||||
} message: {
|
||||
Text(saveErrorMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ struct HomeView : View {
|
||||
var body: some View {
|
||||
NavigationLink("Créer un monde"){
|
||||
CreateWorldView()
|
||||
}.buttonStyle(.glass)
|
||||
}
|
||||
.buttonStyle(.glass)
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,30 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct WorldListView: View {
|
||||
@Environment(\.modelContext) private var context
|
||||
@Query(sort: \World.createdAt, order: .reverse) private var worlds: [World]
|
||||
|
||||
var body: some View {
|
||||
Text("My worlds")
|
||||
Group {
|
||||
if worlds.isEmpty {
|
||||
Text("Aucun monde")
|
||||
NavigationLink("Créer un monde"){
|
||||
CreateWorldView()
|
||||
}
|
||||
.buttonStyle(.glass)
|
||||
}else{
|
||||
List {
|
||||
ForEach(worlds) {world in
|
||||
Text(world.name)
|
||||
Text(world.overview)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Mes mondes")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,17 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
@main
|
||||
struct WorldBuilderApp: App {
|
||||
@State private var router = AppRouter()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environment(router)
|
||||
}
|
||||
.modelContainer(for: World.self)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user