Skip to content

Commit 4319097

Browse files
authored
refactor: 얼리 리턴 패턴을 제거 및 뷰 렌더링 수 감소 패턴 적용 (#147)
1 parent 9b84502 commit 4319097

8 files changed

Lines changed: 39 additions & 18 deletions

DevLog/Presentation/ViewModel/AccountViewModel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ final class AccountViewModel: Store {
6464

6565
func reduce(with action: Action) -> [SideEffect] {
6666
var state = self.state
67+
var effects: [SideEffect] = []
6768

6869
switch action {
6970
case .onAppear:
70-
return [.fetch]
71+
effects = [.fetch]
7172
case .linkWithProvider(let value):
72-
return [.link(value)]
73+
effects = [.link(value)]
7374
case .unlinkFromProvider(let value):
74-
return [.unlink(value)]
75+
effects = [.unlink(value)]
7576
case .setAlert(let isPresented, let type):
7677
setAlert(&state, isPresented: isPresented, type: type)
7778
case .setToast(let isPresented, let type):
@@ -86,7 +87,7 @@ final class AccountViewModel: Store {
8687
}
8788

8889
self.state = state
89-
return []
90+
return effects
9091
}
9192

9293
func run(_ effect: SideEffect) {

DevLog/Presentation/ViewModel/LoginViewModel.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,23 @@ final class LoginViewModel: Store {
6060

6161
func reduce(with action: Action) -> [SideEffect] {
6262
var state = self.state
63+
var effects: [SideEffect] = []
6364

6465
switch action {
6566
case .setAlert(let isPresented):
6667
setAlert(&state, isPresented: isPresented)
6768
case .tapSignInButton(let authProvider):
68-
self.state = state
69-
return [.signIn(authProvider)]
69+
effects = [.signIn(authProvider)]
7070
case .tapSignOutButton, .signOutAuto:
71-
self.state = state
72-
return [.signOut]
71+
effects = [.signOut]
7372
case .setLoading(let value):
7473
state.isLoading = value
7574
case .setLogined(let result):
7675
state.signIn = result
7776
}
7877

7978
self.state = state
80-
return []
79+
return effects
8180
}
8281

8382
func run(_ effect: SideEffect) {

DevLog/Presentation/ViewModel/PushNotificationSettingsViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ final class PushNotificationSettingsViewModel: Store {
6161
func reduce(with action: Action) -> [SideEffect] {
6262
var state = self.state
6363
var effects: [SideEffect] = []
64+
6465
switch action {
6566
case .fetchSettings:
6667
effects = [.fetchPushNotificationSettings]

DevLog/Presentation/ViewModel/RootViewModel.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ final class RootViewModel: Store {
6666

6767
func reduce(with action: Action) -> [SideEffect] {
6868
var state = self.state
69+
var effects: [SideEffect] = []
6970

7071
switch action {
7172
case .setAlert(let isPresented):
@@ -82,13 +83,13 @@ final class RootViewModel: Store {
8283
case .setTheme(let theme):
8384
state.theme = theme
8485
case .signOutAuto:
85-
return [.signOut]
86+
effects = [.signOut]
8687
case .didLogined(let result):
8788
state.signIn = result
8889
}
8990

9091
self.state = state
91-
return []
92+
return effects
9293
}
9394

9495
func run(_ effect: SideEffect) {

DevLog/Presentation/ViewModel/SettingViewModel.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ final class SettingViewModel: Store {
6565
}
6666

6767
func reduce(with action: Action) -> [SideEffect] {
68+
var state = self.state
69+
var effects: [SideEffect] = []
70+
6871
switch action {
6972
case .setAlert(let isPresented, let type):
7073
setAlert(&state, isPresented: isPresented, type: type)
@@ -76,9 +79,9 @@ final class SettingViewModel: Store {
7679
case .updateDirSize:
7780
state.dirSize = dirSizeInBytes()
7881
case .tapDeleteAuthButton:
79-
return [.deleteAuth]
82+
effects = [.deleteAuth]
8083
case .tapSignOutButton:
81-
return [.signOut]
84+
effects = [.signOut]
8285
case .tapRemoveCacheButton:
8386
setAlert(&state, isPresented: true, type: .removeCache)
8487
case .confirmRemoveCache:
@@ -90,7 +93,9 @@ final class SettingViewModel: Store {
9093
setAlert(&state, isPresented: true, type: .error)
9194
}
9295
}
93-
return []
96+
97+
self.state = state
98+
return effects
9499
}
95100

96101
func run(_ effect: SideEffect) {

DevLog/Presentation/ViewModel/TodoDetailViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class TodoDetailViewModel: Store {
6262
case .setLoading(let value):
6363
state.isLoading = value
6464
case .upsertTodo(let todo):
65-
return [.upsertTodo(todo)]
65+
effects = [.upsertTodo(todo)]
6666
}
6767

6868
self.state = state

DevLog/Presentation/ViewModel/TodoEditorViewModel.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ final class TodoEditorViewModel: Store {
7878
}
7979

8080
func reduce(with action: Action) -> [SideEffect] {
81+
var state = self.state
82+
8183
switch action {
8284
case .addTag(let tag):
83-
if !tag.isEmpty { state.tags.append(tag) }
85+
if !tag.isEmpty {
86+
state.tags.append(tag)
87+
}
8488
case .removeTag(let tagText):
8589
state.tags.removeAll { $0 == tagText }
8690
case .setContent(let stringValue),
8791
.setTagText(let stringValue),
8892
.setTitle(let stringValue):
89-
handleStringAction(action, stringValue: stringValue)
93+
handleStringAction(action, stringValue: stringValue, state: &state)
9094
case .setDueDate(let dueDate):
9195
if let tomorrowDate = calendar.date(byAdding: .day, value: 1, to: Date()), let dueDate {
9296
state.dueDate = max(dueDate, tomorrowDate)
@@ -102,12 +106,18 @@ final class TodoEditorViewModel: Store {
102106
state.dueDate = calendar.date(byAdding: .day, value: 1, to: Date())
103107
}
104108
}
109+
110+
self.state = state
105111
return []
106112
}
107113
}
108114

109115
extension TodoEditorViewModel {
110-
private func handleStringAction(_ action: Action, stringValue: String) {
116+
private func handleStringAction(
117+
_ action: Action,
118+
stringValue: String,
119+
state: inout State
120+
) {
111121
switch action {
112122
case .setContent:
113123
state.content = stringValue

DevLog/Presentation/ViewModel/TodoManageViewModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ final class TodoManageViewModel: Store {
2626
}
2727

2828
func reduce(with action: Action) -> [SideEffect] {
29+
var state = self.state
30+
2931
switch action {
3032
case .moveItem(let from, let target):
3133
state.todoKindPreferences.move(fromOffsets: from, toOffset: target)
@@ -34,6 +36,8 @@ final class TodoManageViewModel: Store {
3436
state.todoKindPreferences[index].isVisible.toggle()
3537
}
3638
}
39+
40+
self.state = state
3741
return []
3842
}
3943
}

0 commit comments

Comments
 (0)