File tree Expand file tree Collapse file tree 3 files changed +53
-22
lines changed
Expand file tree Collapse file tree 3 files changed +53
-22
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import Foundation
99
1010
1111protocol Manager {
12- var tasks : [ TaskModel ] { get }
12+ var tasks : [ TaskModel ] { get set }
1313
1414 func load( ) -> [ TaskModel ]
1515 func save( task: TaskModel )
@@ -19,7 +19,7 @@ protocol Manager {
1919
2020@Observable
2121class TaskManager : Manager {
22- private ( set ) var tasks : [ TaskModel ]
22+ var tasks : [ TaskModel ]
2323
2424 init ( ) {
2525 tasks = [ ]
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ import SwiftUI
1010struct MainView : View {
1111 @Environment ( TaskManager . self) var taskManager
1212
13+ var tasks : [ TaskModel ] { taskManager. tasks. filter { $0. isDone == false } }
14+ var doneTasks : [ TaskModel ] { taskManager. tasks. filter { $0. isDone == true } }
15+
1316 var body : some View {
1417 @Bindable var taskManager = taskManager
1518
@@ -19,7 +22,7 @@ struct MainView: View {
1922 if taskManager. tasks. isEmpty {
2023 TaskEmptyView ( )
2124 } else {
22- ForEach ( taskManager . tasks) {
25+ ForEach ( tasks) {
2326 TaskListItemView ( task: $0)
2427 . swipeActions {
2528 Button ( " 삭제 " ) {
@@ -34,11 +37,19 @@ struct MainView: View {
3437 }
3538
3639 Section {
37-
40+ ForEach ( doneTasks) {
41+ TaskListItemView ( task: $0)
42+ . swipeActions {
43+ Button ( " 삭제 " ) {
44+
45+ } . tint ( Color . Todo. red)
46+ }
47+ }
3848 } header: {
3949 Text ( " 완료 " )
4050 }
4151 }
52+ . buttonStyle ( . borderless) // 리스트에서 아이템을 탭했을때 모든 버튼들이 이벤트를 동시에 받는 것을 방지
4253 . toolbar {
4354 ToolbarItem ( placement: . topBarLeading) {
4455 Text ( " Todoo " )
Original file line number Diff line number Diff line change 88import SwiftUI
99
1010struct TaskListItemView : View {
11+ @Environment ( TaskManager . self) var taskManager
1112 var task : TaskModel
1213
13- @State private var isDone : Bool = false
14- @State private var isFavorite : Bool = false
15-
14+ var taskIndex : Int {
15+ taskManager. tasks. firstIndex ( where: { $0. id == task. id } ) !
16+ }
17+
1618 var body : some View {
19+ @Bindable var taskManager = taskManager
20+
1721 HStack {
18- Button {
19- isDone. toggle ( )
20- } label: {
21- Image ( isDone ? " ic_notyet_fill " : " ic_notyet " )
22- . resizable ( )
23- . frame ( width: 30 , height: 30 )
24- }
22+ DoneButton ( isDone: $taskManager. tasks [ taskIndex] . isDone)
2523
2624 Text ( task. task)
2725 . font ( . Todo. r16)
2826 . lineLimit ( 1 )
2927
3028 Spacer ( )
3129
32- Button {
33- isFavorite. toggle ( )
34- } label: {
35- Image ( isFavorite ? " ic_tap_star_fill " : " ic_tap_star " )
36- . foregroundStyle ( isFavorite ? Color . Todo. red : Color . Todo. black)
37- }
38-
30+ FavoriteButton ( isFavorite: $taskManager. tasks [ taskIndex] . isFavorite)
31+ }
32+ }
33+ }
34+
35+ struct DoneButton : View {
36+ @Binding var isDone : Bool
37+
38+ var body : some View {
39+ Button {
40+ print ( " done button " )
41+ isDone. toggle ( )
42+ } label: {
43+ Image ( isDone ? " ic_notyet_fill " : " ic_notyet " )
44+ . resizable ( )
45+ . frame ( width: 30 , height: 30 )
46+ }
47+ }
48+ }
49+
50+ struct FavoriteButton : View {
51+ @Binding var isFavorite : Bool
52+
53+ var body : some View {
54+ Button {
55+ print ( " favorite button " )
56+ isFavorite. toggle ( )
57+ } label: {
58+ Image ( isFavorite ? " ic_tap_star_fill " : " ic_tap_star " )
59+ . foregroundStyle ( isFavorite ? Color . Todo. red : Color . Todo. black)
3960 }
40- . buttonStyle ( . borderless) // 리스트에서 아이템을 탭했을때 모든 버튼들이 이벤트를 동시에 받는 것을 방지
4161 }
4262}
4363
You can’t perform that action at this time.
0 commit comments