Skip to content

Commit

Permalink
[#2] ADD: MonsterList Item, Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
psychehose committed Jun 10, 2023
1 parent 9f1cc76 commit 8428f25
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 41 deletions.
12 changes: 12 additions & 0 deletions RunningRPG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -104,6 +105,7 @@
770A002A2A34236700160BD6 /* Equiment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Equiment.swift; sourceTree = "<group>"; };
770A002C2A34240200160BD6 /* Monster.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Monster.swift; sourceTree = "<group>"; };
7DF63E732A342F3A0051985C /* HomeBottomView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeBottomView.swift; sourceTree = "<group>"; };
770A002F2A34303100160BD6 /* MonsterListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterListItem.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -234,6 +236,7 @@
2A9A06D62A342BD400EC8141 /* MonsterList */ = {
isa = PBXGroup;
children = (
770A002E2A34301B00160BD6 /* Subviews */,
2A9A06CE2A3426B800EC8141 /* MonsterListView.swift */,
);
path = MonsterList;
Expand Down Expand Up @@ -285,6 +288,14 @@
path = Model;
sourceTree = "<group>";
};
770A002E2A34301B00160BD6 /* Subviews */ = {
isa = PBXGroup;
children = (
770A002F2A34303100160BD6 /* MonsterListItem.swift */,
);
path = Subviews;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -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 */,
Expand Down
2 changes: 2 additions & 0 deletions RunningRPG/Model/Monster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 56 additions & 41 deletions RunningRPG/Tabbar/MonsterList/MonsterListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
30 changes: 30 additions & 0 deletions RunningRPG/Tabbar/MonsterList/Subviews/MonsterListItem.swift
Original file line number Diff line number Diff line change
@@ -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))
]
}
}

0 comments on commit 8428f25

Please sign in to comment.