add a view to list worlds
This commit is contained in:
1 parent
9c1bfe2024
commit
38a202109d
7 files changed
+108
-13
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
|
import SwiftUI
|
||||||
|
|
||||||
struct MainTabView: View {
|
struct MainTabView: View {
|
||||||
|
@Environment(AppRouter.self) private var router
|
||||||
|
|
||||||
var body : some View {
|
var body : some View {
|
||||||
TabView {
|
@Bindable var router = router
|
||||||
|
|
||||||
|
TabView(selection: $router.selectedTab) {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
HomeView()
|
HomeView()
|
||||||
}.tabItem {
|
|
||||||
Label("Accueil", systemImage: "house")
|
|
||||||
}
|
}
|
||||||
|
.tabItem {Label("Accueil", systemImage: "house")}
|
||||||
|
.tag(AppTab.home)
|
||||||
|
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
WorldListView()
|
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,10 +6,18 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
struct CreateWorldView: View {
|
struct CreateWorldView: View {
|
||||||
@State private var worldName = ""
|
@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 {
|
var body: some View {
|
||||||
Form {
|
Form {
|
||||||
@@ -18,14 +26,23 @@ struct CreateWorldView: View {
|
|||||||
|
|
||||||
TextField(
|
TextField(
|
||||||
"Description",
|
"Description",
|
||||||
text: $worldDescription,
|
text: $worldOverview,
|
||||||
axis: .vertical
|
axis: .vertical
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
Button("Créer le monde") {
|
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)
|
.disabled(worldName.isEmpty)
|
||||||
.padding(.horizontal, 24)
|
.padding(.horizontal, 24)
|
||||||
@@ -39,9 +56,10 @@ struct CreateWorldView: View {
|
|||||||
}
|
}
|
||||||
.navigationTitle("Créer un monde")
|
.navigationTitle("Créer un monde")
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarTitleDisplayMode(.inline)
|
||||||
|
.alert("Erreur", isPresented: $showSaveError) {
|
||||||
|
Button("OK") { }
|
||||||
|
} message: {
|
||||||
|
Text(saveErrorMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createWorld() {
|
|
||||||
print("Création du monde : \(worldName)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ struct HomeView : View {
|
|||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationLink("Créer un monde"){
|
NavigationLink("Créer un monde"){
|
||||||
CreateWorldView()
|
CreateWorldView()
|
||||||
}.buttonStyle(.glass)
|
}
|
||||||
|
.buttonStyle(.glass)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,9 +6,30 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
struct WorldListView: View {
|
struct WorldListView: View {
|
||||||
|
@Environment(\.modelContext) private var context
|
||||||
|
@Query(sort: \World.createdAt, order: .reverse) private var worlds: [World]
|
||||||
|
|
||||||
var body: some View {
|
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 SwiftUI
|
||||||
|
import SwiftData
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct WorldBuilderApp: App {
|
struct WorldBuilderApp: App {
|
||||||
|
@State private var router = AppRouter()
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
|
.environment(router)
|
||||||
}
|
}
|
||||||
|
.modelContainer(for: World.self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user