Skip to content

Commit aa04ca7

Browse files
committed
[Feat] todo 체크에 대한 ViewModel 적용 (#10)
1 parent 0746651 commit aa04ca7

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

AND-SOPT-iOS/AND-SOPT-iOS/Presentation/week7/TodoListView.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
struct TodoListView: View {
11-
@State var todos: [(text: String, isChecked: Bool, time: String?)]
11+
@ObservedObject var viewModel: TodoViewModel
1212

1313
var body: some View {
1414
VStack(alignment: .leading) {
@@ -32,19 +32,19 @@ struct TodoListView: View {
3232
}
3333
.padding(.bottom, 10)
3434

35-
ForEach(todos.indices, id: \.self) { index in
35+
ForEach(viewModel.todos.indices, id: \.self) { index in
3636
HStack(alignment: .top) {
3737
Button(action: {
38-
todos[index].isChecked.toggle()
38+
viewModel.toggleTodoCheck(at: index)
39+
viewModel.reorderTodos()
3940
}) {
4041
ZStack {
4142
Image("todo_none")
4243
.resizable()
4344
.renderingMode(.template)
4445
.frame(width: 25, height: 25)
45-
.foregroundColor(todos[index].isChecked ? .todoBrown : .todoGray)
46-
47-
if todos[index].isChecked {
46+
.foregroundColor(viewModel.todos[index].isChecked ? .todoBrown : .todoGray)
47+
if viewModel.todos[index].isChecked {
4848
Image("check")
4949
.resizable()
5050
.frame(width: 15, height: 11)
@@ -53,11 +53,11 @@ struct TodoListView: View {
5353
}
5454

5555
VStack(alignment: .leading) {
56-
Text(todos[index].text)
56+
Text(viewModel.todos[index].text)
5757
.font(.body)
5858
.foregroundColor(.primary)
5959

60-
if let time = todos[index].time {
60+
if let time = viewModel.todos[index].time {
6161
HStack {
6262
Image(systemName: "clock")
6363
.font(.caption)

AND-SOPT-iOS/AND-SOPT-iOS/Presentation/week7/TodoMateView.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ struct TodoMateView: View {
1212
let weekDays = ["", "", "", "", "", "", ""]
1313
let columns = Array(repeating: GridItem(.flexible()), count: 7)
1414

15-
let todos = [
16-
(text: "힙콘 연말 파티 장소 찾아보기", isChecked: false, time: nil),
17-
(text: "스유 스터디 회식 투표 올리기", isChecked: false, time: nil),
18-
(text: "모각작 in 강남", isChecked: true, time: "PM 2:00"),
19-
(text: "기획 경선 자료 확인하기", isChecked: true, time: nil)
20-
]
15+
@StateObject private var viewModel = TodoViewModel(todos: [
16+
(text: "힙콘 연말 파티 장소 찾아보기", isChecked: false, time: nil),
17+
(text: "스유 스터디 회식 투표 올리기", isChecked: false, time: nil),
18+
(text: "모각작 in 강남", isChecked: false, time: "PM 2:00"),
19+
(text: "기획 경선 자료 확인하기", isChecked: true, time: nil)
20+
])
2121

2222
var body: some View {
2323
ScrollView {
@@ -34,7 +34,7 @@ struct TodoMateView: View {
3434
CalendarView(weekDays: weekDays, days: days, columns: columns)
3535
.padding(.bottom, 20)
3636

37-
TodoListView(todos: todos)
37+
TodoListView(viewModel: viewModel)
3838
.padding(.top, 10)
3939

4040
Spacer()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// TodoViewModel.swift
3+
// AND-SOPT-iOS
4+
//
5+
// Created by 김민서 on 12/12/24.
6+
//
7+
8+
import SwiftUI
9+
10+
class TodoViewModel: ObservableObject {
11+
@Published var todos: [(text: String, isChecked: Bool, time: String?)]
12+
13+
init(todos: [(text: String, isChecked: Bool, time: String?)]) {
14+
self.todos = todos
15+
}
16+
17+
func toggleTodoCheck(at index: Int) {
18+
todos[index].isChecked.toggle()
19+
20+
if todos[index].isChecked {
21+
let todo = todos.remove(at: index)
22+
todos.append(todo)
23+
} else {
24+
let todo = todos.remove(at: index)
25+
todos.insert(todo, at: findInsertIndex(for: todo))
26+
}
27+
}
28+
29+
private func findInsertIndex(for todo: (text: String, isChecked: Bool, time: String?)) -> Int {
30+
if let index = todos.firstIndex(where: { !$0.isChecked && $0.text > todo.text }) {
31+
return index
32+
}
33+
return todos.count - 1
34+
}
35+
36+
func reorderTodos() {
37+
todos = todos.filter { !$0.isChecked } + todos.filter { $0.isChecked }
38+
}
39+
}

0 commit comments

Comments
 (0)