Skip to content

Commit 9056ee5

Browse files
authored
[#146] 뷰모델의 State 구조체가 Equatable을 채택하도록 개선한다
1 parent 65f15db commit 9056ee5

13 files changed

Lines changed: 53 additions & 53 deletions

DevLog/Domain/Entity/TodoQuery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
struct TodoQuery {
10+
struct TodoQuery: Equatable {
1111
enum SortTarget: Equatable, Hashable {
1212
case createdAt
1313
case updatedAt

DevLog/Presentation/ViewModel/AccountViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
@Observable
1111
final class AccountViewModel: Store {
12-
struct State {
12+
struct State: Equatable {
1313
var currentProvider: AuthProvider?
1414
var connectedProviders: [AuthProvider] = []
1515
var disconnectedProviders: [AuthProvider] = []
@@ -87,7 +87,7 @@ final class AccountViewModel: Store {
8787
.filter { !allProviders.contains($0) }
8888
}
8989

90-
self.state = state
90+
if self.state != state { self.state = state }
9191
return effects
9292
}
9393

DevLog/Presentation/ViewModel/LoginViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import GoogleSignIn
1212

1313
@Observable
1414
final class LoginViewModel: Store {
15-
struct State {
15+
struct State: Equatable {
1616
var signIn: Bool?
1717
var isLoading = false
1818
var showAlert: Bool = false
@@ -76,7 +76,7 @@ final class LoginViewModel: Store {
7676
state.signIn = result
7777
}
7878

79-
self.state = state
79+
if self.state != state { self.state = state }
8080
return effects
8181
}
8282

DevLog/Presentation/ViewModel/ProfileViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
@Observable
1111
final class ProfileViewModel: Store {
12-
struct State {
12+
struct State: Equatable {
1313
var name: String = ""
1414
var email: String = ""
1515
var statusMessage: String = ""
@@ -185,7 +185,7 @@ final class ProfileViewModel: Store {
185185
case .updateStatusTextFieldFocus(let focused):
186186
state.showDoneButton = focused
187187
}
188-
self.state = state
188+
if self.state != state { self.state = state }
189189
return effects
190190
}
191191
// swiftlint:enable cyclomatic_complexity

DevLog/Presentation/ViewModel/PushNotificationListViewModel.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
@Observable
1111
final class PushNotificationListViewModel: Store {
12-
struct State {
12+
struct State: Equatable {
1313
var notifications: [PushNotificationItem] = []
1414
var showAlert: Bool = false
1515
var showToast: Bool = false
@@ -19,7 +19,6 @@ final class PushNotificationListViewModel: Store {
1919
var isLoading: Bool = false
2020
var hasMore: Bool = false
2121
var nextCursor: PushNotificationCursor?
22-
var pendingTask: (PushNotificationItem, Int)?
2322
var query: PushNotificationQuery
2423
var selectedTodoID: TodoIDItem?
2524
}
@@ -57,6 +56,7 @@ final class PushNotificationListViewModel: Store {
5756
private let toggleReadUseCase: TogglePushNotificationReadUseCase
5857
private let fetchQueryUseCase: FetchPushNotificationQueryUseCase
5958
private let updateQueryUseCase: UpdatePushNotificationQueryUseCase
59+
private var pendingTask: (PushNotificationItem, Int)?
6060

6161
init(
6262
fetchUseCase: FetchPushNotificationsUseCase,
@@ -99,7 +99,7 @@ final class PushNotificationListViewModel: Store {
9999
effects = reduceByRun(action, state: &state)
100100
}
101101

102-
self.state = state
102+
if self.state != state { self.state = state }
103103
return effects
104104
}
105105

@@ -158,12 +158,12 @@ private extension PushNotificationListViewModel {
158158
switch action {
159159
case .deleteNotification(let item):
160160
var effects: [SideEffect] = []
161-
if let (pendingItem, _) = state.pendingTask {
161+
if let (pendingItem, _) = pendingTask {
162162
effects = [.delete(pendingItem)]
163163
}
164164

165165
if let index = state.notifications.firstIndex(where: { $0.id == item.id }) {
166-
state.pendingTask = (item, index)
166+
pendingTask = (item, index)
167167
state.notifications.remove(at: index)
168168
setToast(&state, isPresented: true)
169169
}
@@ -175,9 +175,9 @@ private extension PushNotificationListViewModel {
175175
return [.toggleRead(item.todoID)]
176176
}
177177
case .undoDelete:
178-
guard let (item, index) = state.pendingTask else { return [] }
178+
guard let (item, index) = pendingTask else { return [] }
179179
state.notifications.insert(item, at: index)
180-
state.pendingTask = nil
180+
pendingTask = nil
181181
case .setAlert(let isPresented):
182182
setAlert(&state, isPresented: isPresented)
183183
case .toggleSortOption:
@@ -218,11 +218,11 @@ private extension PushNotificationListViewModel {
218218
state.nextCursor = nil
219219
return [.fetchNotifications(state.query, cursor: nil)]
220220
case .loadNextPage:
221-
guard state.hasMore, !state.isLoading, state.pendingTask == nil else { return [] }
221+
guard state.hasMore, !state.isLoading, pendingTask == nil else { return [] }
222222
return [.fetchNotifications(state.query, cursor: state.nextCursor)]
223223
case .confirmDelete:
224-
guard let (item, _) = state.pendingTask else { return [] }
225-
state.pendingTask = nil
224+
guard let (item, _) = pendingTask else { return [] }
225+
pendingTask = nil
226226
return [.delete(item)]
227227
case .setToast(let isPresented):
228228
setToast(&state, isPresented: isPresented)
@@ -245,7 +245,7 @@ private extension PushNotificationListViewModel {
245245
state.nextCursor = nil
246246
case .appendNotifications(let notifications, let nextCursor):
247247
let filteredNotifications: [PushNotificationItem]
248-
if let (pendingItem, _) = state.pendingTask {
248+
if let (pendingItem, _) = pendingTask {
249249
filteredNotifications = notifications.filter { $0.id != pendingItem.id }
250250
} else {
251251
filteredNotifications = notifications

DevLog/Presentation/ViewModel/PushNotificationSettingsViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
@Observable
1111
final class PushNotificationSettingsViewModel: Store {
12-
struct State {
12+
struct State: Equatable {
1313
var pushNotificationEnable: Bool = false
1414
var viewPushNotificationTime: Date = .init()
1515
var sheetPushNotificationTime: Date = .init()
@@ -99,7 +99,7 @@ final class PushNotificationSettingsViewModel: Store {
9999
state.showTimePicker = false
100100
state.sheetPushNotificationTime = state.viewPushNotificationTime
101101
}
102-
self.state = state
102+
if self.state != state { self.state = state }
103103
return effects
104104
}
105105

DevLog/Presentation/ViewModel/RootViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Combine
1010

1111
@Observable
1212
final class RootViewModel: Store {
13-
struct State {
13+
struct State: Equatable {
1414
var showAlert: Bool = false
1515
var alertTitle: String = ""
1616
var alertMessage: String = ""
@@ -89,7 +89,7 @@ final class RootViewModel: Store {
8989
state.signIn = result
9090
}
9191

92-
self.state = state
92+
if self.state != state { self.state = state }
9393
return effects
9494
}
9595

DevLog/Presentation/ViewModel/SearchViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import OrderedCollections
1010

1111
@Observable
1212
final class SearchViewModel: Store {
13-
struct State {
13+
struct State: Equatable {
1414
var isLoading: Bool = false
1515
var isSearching: Bool = false
1616
var searchQuery: String = ""
@@ -124,7 +124,7 @@ final class SearchViewModel: Store {
124124
state.showAllWebPages = shouldShowAll
125125
}
126126

127-
self.state = state
127+
if self.state != state { self.state = state }
128128
return effects
129129
}
130130

DevLog/Presentation/ViewModel/SettingViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Combine
1010

1111
@Observable
1212
final class SettingViewModel: Store {
13-
struct State {
13+
struct State: Equatable {
1414
var theme: SystemTheme = .automatic
1515
var dirSize: Int64 = 0
1616
var isLoading = false
@@ -95,7 +95,7 @@ final class SettingViewModel: Store {
9595
}
9696
}
9797

98-
self.state = state
98+
if self.state != state { self.state = state }
9999
return effects
100100
}
101101

DevLog/Presentation/ViewModel/TodoDetailViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
@Observable
1111
final class TodoDetailViewModel: Store {
12-
struct State {
12+
struct State: Equatable {
1313
var todo: Todo?
1414
var isLoading: Bool = false
1515
var showAlert: Bool = false
@@ -70,7 +70,7 @@ final class TodoDetailViewModel: Store {
7070
effects = [.upsertTodo(todo)]
7171
}
7272

73-
self.state = state
73+
if self.state != state { self.state = state }
7474
return effects
7575
}
7676

0 commit comments

Comments
 (0)