Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

sujileelea: Model, View 수정 및 Asset 추가 #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DragDrop/DragDrop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
09B062AF2BAC1D7500C79B1D /* TaskEditModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskEditModal.swift; sourceTree = "<group>"; };
09B062B12BAC1D7C00C79B1D /* TaskAddModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskAddModal.swift; sourceTree = "<group>"; };
09B062B32BAC202500C79B1D /* TaskCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCard.swift; sourceTree = "<group>"; };
09E6637B2BD7904400530EE8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
09F0859A2BB2A84300EDB6C2 /* Board.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Board.swift; sourceTree = "<group>"; };
09F0859C2BB2C5A200EDB6C2 /* CategoryListSidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryListSidebar.swift; sourceTree = "<group>"; };
09F0859E2BB2C5C400EDB6C2 /* CategoryAddModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryAddModal.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -116,6 +117,7 @@
09B062742BAC186400C79B1D /* DragDrop */ = {
isa = PBXGroup;
children = (
09E6637B2BD7904400530EE8 /* Info.plist */,
09B0627B2BAC186500C79B1D /* Assets.xcassets */,
09B062752BAC186400C79B1D /* DragDropApp.swift */,
09B062772BAC186400C79B1D /* ContentView.swift */,
Expand Down Expand Up @@ -496,6 +498,7 @@
DEVELOPMENT_TEAM = 53DAD6BN6X;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DragDrop/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -525,6 +528,7 @@
DEVELOPMENT_TEAM = 53DAD6BN6X;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DragDrop/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"originHash" : "f85ec36b55e6c3bddc4e146f4fc996922ed5c902954d863dc9c50577c5aae4c9",
"pins" : [
{
"identity" : "popupview",
"kind" : "remoteSourceControl",
"location" : "https://github.com/exyte/PopupView.git",
"state" : {
"revision" : "8c85ff09d474dd3cfb69aba33046395bcf365573",
"version" : "2.9.1"
}
},
{
"identity" : "swiftui-introspect",
"kind" : "remoteSourceControl",
"location" : "https://github.com/siteline/swiftui-introspect",
"state" : {
"revision" : "0cd2a5a5895306bc21d54a2254302d24a9a571e4",
"version" : "1.1.3"
}
}
],
"version" : 3
}
13 changes: 1 addition & 12 deletions DragDrop/DragDrop/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,11 @@ struct ContentView: View {
var body: some View {
var _: Category = Category(name: "test")
NavigationStack {
MainPage()
BoardPage()
}
}
}

extension Category {
// TO DO : move to MainPage.swift
func addCategory(name: String) {
withAnimation {
let newCategory = Category(name: name)
modelContext?.insert(newCategory)
}
}
}


//struct ContentView: View {
// @Environment(\.modelContext) private var modelContext
// @Query private var items: [Item]
Expand Down
2 changes: 1 addition & 1 deletion DragDrop/DragDrop/DragDropApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftData
struct DragDropApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Category.self, Task.self
Category.self, TaskData.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

Expand Down
23 changes: 23 additions & 0 deletions DragDrop/DragDrop/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>com.public.data</string>
</array>
<key>UTTypeDescription</key>
<string>TaskItem</string>
<key>UTTypeIconFiles</key>
<array/>
<key>UTTypeIdentifier</key>
<string>com.sujileelea.DragDrop.taskItem</string>
<key>UTTypeTagSpecification</key>
<dict/>
</dict>
</array>
</dict>
</plist>
5 changes: 2 additions & 3 deletions DragDrop/DragDrop/Model/Board.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import SwiftData

@Model
final class Board {
var pinnedTasks: [Task]
var pinnedTasks: [TaskItem]
var categories: [Category] = [Category(name: "board")]

init(pinnedTasks: [Task]) {
init(pinnedTasks: [TaskItem]) {
self.pinnedTasks = pinnedTasks
}
}

2 changes: 1 addition & 1 deletion DragDrop/DragDrop/Model/Category.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftData
@Model
final class Category {
@Attribute(.unique) var name: String
@Relationship(deleteRule: .cascade) var tasks: [Task]?
@Relationship(deleteRule: .cascade) var taskItems: [TaskItem]?

init(name: String) {
self.name = name
Expand Down
37 changes: 24 additions & 13 deletions DragDrop/DragDrop/Model/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@

import Foundation
import SwiftData
import UniformTypeIdentifiers
import SwiftUI
Comment on lines +10 to +11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import 구문에도 나열 규칙이 있다면 좋을 것 같습니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전혀 고려하지 못했던 부분이네요. 퍼스트파티부터 순차적으로 나열하는 방법이 생각납니다.
혹시 재성님은 어떤식으로 하는지 여쭤볼 수 있을까요?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

선호도에 따라 다른데

보통 formatting이나 linting 규칙에서는 두가지를 사용합니다

  • 길이순
  • 알파벳순

전 보통 길이순을 선호합니다. 알파벳순하면 새 모듈 임포트할때마다 머릿속으로 abc 세야해서...



@Model
class Task {
class TaskData {
var taskItems: [TaskItem]?

init(taskItems: [TaskItem]? = nil) {
self.taskItems = taskItems
}
}

struct TaskItem: Equatable, Identifiable, Codable, Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(for: TaskItem.self, contentType: .taskItem)
}
var id: String
var name: String
@Attribute(.unique) var category: String
var category: String
var isPinned: Bool {
if category == "board" {
return true
} else {
return false
}
}
var isDone: Bool = false

init(name: String, category: String) {
self.name = name
self.category = category
return category == "board"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return category == "board"
category == "board"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

디테일한 부분까지...🥹 감사합니다.

}
var isDone: Bool
}

extension UTType {
static var taskItem = UTType(exportedAs: "com.sujileelea.DragDrop.taskItem")
}

8 changes: 5 additions & 3 deletions DragDrop/DragDrop/View/BoardEditPage.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//
// EditPage.swift
// BoardEditPage.swift
// DragDrop
//
// Created by Suji Lee on 3/21/24.
//

import SwiftUI

struct EditPage: View {
struct BoardEditPage: View {
@Environment(\.modelContext) private var modelContext

var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}

#Preview {
EditPage()
BoardEditPage()
}
39 changes: 31 additions & 8 deletions DragDrop/DragDrop/View/BoardPage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MainPage.swift
// BoardPage.swift
// DragDrop
//
// Created by Suji Lee on 3/21/24.
Expand All @@ -8,10 +8,12 @@
import SwiftUI
import SwiftData

struct MainPage: View {
struct BoardPage: View {
@Environment(\.modelContext) private var modelContext
// @Query private var tasks: [Task]
let today = Date().formatted(.iso8601.year().month().day())
var tasks: [Task] = [Task(name: "one", category: "One"), Task(name: "two", category: "Two")]
@State var taskItems: [TaskItem] = [TaskItem(id: "1", name: "one", category: "iOS", isDone: false), TaskItem(id: "2", name: "two", category: "Year", isDone: false), TaskItem(id: "3", name: "three", category: "iOS", isDone: false)]
@State private var draggingItem: TaskItem?

var body: some View {
NavigationView {
Expand All @@ -34,12 +36,33 @@ struct MainPage: View {
}
//Board List
VStack {
List {
ForEach(tasks) { task in
TaskCard(task: task, isOnBoard: true)
ScrollView {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

38번째줄 indent가 잘못 적용된거 같아요. 전체적으로 들여쓰기 한번씩 단축키로 체크해주세요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 부끄러운 실수를...지적해주셔서 감사합니다.

Comment on lines 38 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VStack 안에 ScrollView 밖에 없어서 불필요한거 같은데 의도하신게 아래 코드일까요?

ScrollView {
    LazyVStack { // lazy 하게 데이터 가져와서 불필요한 메모리 사용을 줄임
        // ForEach ...
    }
}

이게 아니라면 List 쓰는게 나아보입니다. 요즘 List 퍼포먼스 많이 개선된걸로 알아서요.

List(taskItems) { ... }

// 또는
List {
    ForEach(taskItems) { ... }  // 👈 이유는 모르겠으나 애플 샘플코드에는 List 를 이런식으로 쓰더라구요?
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에는 List였으나 draggable이 적용이 안돼서 ScrollView로 바꿨습니다.
ScrollView는 기본적으로 vertical한 성질이니
필요한 메모리만 로드해 뷰에서 볼 수 있게 LazyVStack을 사용하려던 의도가 맞습니다!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 List는 draggable 적용이 안되는군요! 하나 배워갑니다~

ForEach(taskItems) { taskItem in
TaskCard(task: taskItem, isOnBoard: true)
.listRowBackground(Color.clear)
.draggable(taskItem) {
/// Custom Preview View
EmptyView()
.onAppear {
draggingItem = taskItem
}
}
.dropDestination(for: TaskItem.self) { items, location in
draggingItem = nil
return false
} isTargeted: { status in
if let draggingItem, status, draggingItem != taskItem {
/// Moving Color from source to destination
if let sourceIndex = taskItems.firstIndex(of: draggingItem),
let destinationIndex = taskItems.firstIndex(of: taskItem) {
Comment on lines +54 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중첩으로 if let 을 사용하는 것보다 guard let을 사용하는 것이 어떨까요?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let draggingItem, status, draggingItem != taskItem {
/// Moving Color from source to destination
if let sourceIndex = taskItems.firstIndex(of: draggingItem),
let destinationIndex = taskItems.firstIndex(of: taskItem) {
guard let draggingItem, status, draggingItem != taskItem else { return }
guard let sourceIndex = taskItems.firstIndex(of: draggingItem) else { return }
guard let destinationIndex = taskItems.firstIndex(of: taskItem) else { return }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 간결해지겠네요 적용하겠습니다!

withAnimation(.bouncy) {
let sourceItem = taskItems.remove(at: sourceIndex)
taskItems.insert(sourceItem, at: destinationIndex)
}
}
}
}
}
.listRowSeparator(.hidden)
}
.cornerRadius(10)
}
Expand All @@ -59,5 +82,5 @@ struct MainPage: View {
}

#Preview {
MainPage()
BoardPage()
}
2 changes: 2 additions & 0 deletions DragDrop/DragDrop/View/CategoryAddModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import SwiftUI

struct CategoryAddModal: View {
@Environment(\.modelContext) private var modelContext

var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
Expand Down
9 changes: 9 additions & 0 deletions DragDrop/DragDrop/View/CategoryListSidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
import SwiftUI

struct CategoryListSidebar: View {
@Environment(\.modelContext) private var modelContext

var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}

func addCategory(name: String) {
withAnimation {
let newCategory = Category(name: name)
modelContext.insert(newCategory)
}
}
}

#Preview {
Expand Down
9 changes: 5 additions & 4 deletions DragDrop/DragDrop/View/Components/TaskCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import SwiftUI

struct TaskCard: View {
var task: Task

var task: TaskItem
var isOnBoard: Bool

var body: some View {
Expand Down Expand Up @@ -42,6 +43,6 @@ struct TaskCard: View {
}
}

#Preview {
TaskCard(task: Task(name: "preview", category: "Preview"), isOnBoard: false)
}
//#Preview {
// TaskCard(task: TaskItem(name: "preview", category: "Preview"), isOnBoard: false)
//}
Comment on lines +46 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안쓸거라면 주석보단 그냥 제거하는 것이 좋습니다. 사용할거라면 주석보단 수정하는 것이 좋구요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 수정하는 방향으로 적용하겠습니다!

1 change: 1 addition & 0 deletions DragDrop/DragDrop/View/TaskAddModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI

struct TaskAddModal: View {
@Environment(\.modelContext) private var modelContext
@State private var title: String = ""
@State private var isPinned: Bool = false
@State private var isCompleted: Bool = false
Expand Down
2 changes: 2 additions & 0 deletions DragDrop/DragDrop/View/TaskEditModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import SwiftUI

struct TaskEditModal: View {
@Environment(\.modelContext) private var modelContext

var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
Expand Down