From 8428f2580878f9594335af3b906fd749bde7a7dd Mon Sep 17 00:00:00 2001 From: psychehose Date: Sat, 10 Jun 2023 13:33:37 +0900 Subject: [PATCH] [#2] ADD: MonsterList Item, Mock --- RunningRPG.xcodeproj/project.pbxproj | 12 +++ RunningRPG/Model/Monster.swift | 2 + .../Tabbar/MonsterList/MonsterListView.swift | 97 +++++++++++-------- .../Subviews/MonsterListItem.swift | 30 ++++++ 4 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 RunningRPG/Tabbar/MonsterList/Subviews/MonsterListItem.swift diff --git a/RunningRPG.xcodeproj/project.pbxproj b/RunningRPG.xcodeproj/project.pbxproj index 867c351..7327349 100644 --- a/RunningRPG.xcodeproj/project.pbxproj +++ b/RunningRPG.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 770A002B2A34236700160BD6 /* Equiment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770A002A2A34236700160BD6 /* Equiment.swift */; }; 770A002D2A34240200160BD6 /* Monster.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770A002C2A34240200160BD6 /* Monster.swift */; }; 7DF63E742A342F3A0051985C /* HomeBottomView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DF63E732A342F3A0051985C /* HomeBottomView.swift */; }; + 770A00302A34303100160BD6 /* MonsterListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770A002F2A34303100160BD6 /* MonsterListItem.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -104,6 +105,7 @@ 770A002A2A34236700160BD6 /* Equiment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Equiment.swift; sourceTree = ""; }; 770A002C2A34240200160BD6 /* Monster.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Monster.swift; sourceTree = ""; }; 7DF63E732A342F3A0051985C /* HomeBottomView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeBottomView.swift; sourceTree = ""; }; + 770A002F2A34303100160BD6 /* MonsterListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterListItem.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -234,6 +236,7 @@ 2A9A06D62A342BD400EC8141 /* MonsterList */ = { isa = PBXGroup; children = ( + 770A002E2A34301B00160BD6 /* Subviews */, 2A9A06CE2A3426B800EC8141 /* MonsterListView.swift */, ); path = MonsterList; @@ -285,6 +288,14 @@ path = Model; sourceTree = ""; }; + 770A002E2A34301B00160BD6 /* Subviews */ = { + isa = PBXGroup; + children = ( + 770A002F2A34303100160BD6 /* MonsterListItem.swift */, + ); + path = Subviews; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -452,6 +463,7 @@ 2A9A06A82A3414B700EC8141 /* Item.swift in Sources */, 2A9A06DD2A3440A600EC8141 /* CoreMotionService.swift in Sources */, 2A9A06CD2A34268E00EC8141 /* ShopView.swift in Sources */, + 770A00302A34303100160BD6 /* MonsterListItem.swift in Sources */, 2A9A069F2A3414B500EC8141 /* RunningRPGApp.swift in Sources */, 770A00292A34221900160BD6 /* Character.swift in Sources */, 770A002D2A34240200160BD6 /* Monster.swift in Sources */, diff --git a/RunningRPG/Model/Monster.swift b/RunningRPG/Model/Monster.swift index 8c4ad50..e744f26 100644 --- a/RunningRPG/Model/Monster.swift +++ b/RunningRPG/Model/Monster.swift @@ -12,6 +12,8 @@ struct Monster: Hashable, Codable, Identifiable { let hp: Double let currentHp: Double let avatar: String + let level: Int + let isOpen: Bool var id: String { name diff --git a/RunningRPG/Tabbar/MonsterList/MonsterListView.swift b/RunningRPG/Tabbar/MonsterList/MonsterListView.swift index ce1c99c..3168be6 100644 --- a/RunningRPG/Tabbar/MonsterList/MonsterListView.swift +++ b/RunningRPG/Tabbar/MonsterList/MonsterListView.swift @@ -9,52 +9,67 @@ import SwiftUI import SwiftData struct MonsterListView: View { - @Environment(\.modelContext) private var modelContext - @Query private var items: [Item] - - var body: some View { - NavigationView { - List { - ForEach(items) { item in - NavigationLink { - Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") - } label: { - Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) - } - } - .onDelete(perform: deleteItems) - } - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - EditButton() - } - ToolbarItem { - Button(action: addItem) { - Label("Add Item", systemImage: "plus") - } - } - } - Text("Select an item") - } - } +// @Environment(\.modelContext) private var modelContext +// @Query private var items: [Item] - private func addItem() { - withAnimation { - let newItem = Item(timestamp: Date()) - modelContext.insert(newItem) - } - } + private var listItems: [MonsterListItem] = MonsterListItem.generateMonsters() + + var body: some View { - private func deleteItems(offsets: IndexSet) { - withAnimation { - for index in offsets { - modelContext.delete(items[index]) - } + List { + ForEach(listItems) { item in + NavigationLink { + Text("Hello") + } label: { + Image(systemName: "sun.min") + Text("\(item.monster.name)") } + + } } + +// NavigationView { +// List { +// ForEach(listItems) { item in +// NavigationLink { +// Text("Item at \(item.monster., format: Date.FormatStyle(date: .numeric, time: .standard))") +// } label: { +// Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) +// } +// } +// .onDelete(perform: deleteItems) +// } +// .toolbar { +// ToolbarItem(placement: .navigationBarTrailing) { +// EditButton() +// } +// ToolbarItem { +// Button(action: addItem) { +// Label("Add Item", systemImage: "plus") +// } +// } +// } +// Text("Select an item") +// } + } + + private func addItem() { +// withAnimation { +// let newItem = Item(timestamp: Date()) +// modelContext.insert(newItem) +// } + } + + private func deleteItems(offsets: IndexSet) { +// withAnimation { +// for index in offsets { +// modelContext.delete(items[index]) +// } +// } + } } #Preview { - MonsterListView() - .modelContainer(for: Item.self, inMemory: true) + MonsterListView() + .modelContainer(for: Item.self, inMemory: true) } diff --git a/RunningRPG/Tabbar/MonsterList/Subviews/MonsterListItem.swift b/RunningRPG/Tabbar/MonsterList/Subviews/MonsterListItem.swift new file mode 100644 index 0000000..0db6f18 --- /dev/null +++ b/RunningRPG/Tabbar/MonsterList/Subviews/MonsterListItem.swift @@ -0,0 +1,30 @@ +// +// MonsterListItem.swift +// RunningRPG +// +// Created by 김호세 on 6/10/23. +// + +import Foundation + +internal final class MonsterListItem: Identifiable { + var monster: Monster + + internal init(monster: Monster) { + self.monster = monster + } +} + + +extension MonsterListItem { + static func generateMonsters() -> [MonsterListItem] { + return [ + MonsterListItem(monster: Monster.init(name: "Test1", hp: 10.0, currentHp: 10.0, avatar: "URL1", level: 0, isOpen: true)), + MonsterListItem(monster: Monster.init(name: "Test2", hp: 20.0, currentHp: 20.0, avatar: "URL1", level: 1, isOpen: true)), + MonsterListItem(monster: Monster.init(name: "Test3", hp: 30.0, currentHp: 30.0, avatar: "URL1", level: 2, isOpen: true)), + MonsterListItem(monster: Monster.init(name: "Test4", hp: 40.0, currentHp: 40.0, avatar: "URL1", level: 3, isOpen: true)), + MonsterListItem(monster: Monster.init(name: "Test5", hp: 50.0, currentHp: 50.0, avatar: "URL1", level: 4, isOpen: true)), + MonsterListItem(monster: Monster.init(name: "Test6", hp: 60.0, currentHp: 60.0, avatar: "URL1", level: 5, isOpen: true)) + ] + } +}