Skip to content

Commit 814cf80

Browse files
committed
Add toasts to favorites
1 parent e9af791 commit 814cf80

File tree

2 files changed

+91
-31
lines changed

2 files changed

+91
-31
lines changed

Modules/Sources/FavoritesFeature/FavoritesFeature.swift

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public struct FavoritesFeature: Reducer, Sendable {
2121

2222
public init() {}
2323

24+
// MARK: - Localizations
25+
26+
public enum Localization {
27+
static let actionCompleted = LocalizedStringResource("Action completed", bundle: .module)
28+
static let markAsReadSuccess = LocalizedStringResource("Marked as read", bundle: .module)
29+
static let notifyTypeChanged = LocalizedStringResource("Notify type changed", bundle: .module)
30+
static let sortFiltersChanged = LocalizedStringResource("Sort filters are changed", bundle: .module)
31+
}
32+
2433
// MARK: - State
2534

2635
@ObservableState
@@ -112,10 +121,14 @@ public struct FavoritesFeature: Reducer, Sendable {
112121
return .send(.internal(.loadFavorites(offset: newOffset)))
113122

114123
case .sort(.presented(.saveButtonTapped)):
115-
return .run { send in
116-
await send(.internal(.refresh))
117-
await send(.sort(.presented(.cancelButtonTapped)))
118-
}
124+
return .concatenate(
125+
.run { _ in
126+
await toastClient.showToast(ToastMessage(text: Localization.sortFiltersChanged, haptic: .success))
127+
},
128+
129+
.send(.internal(.refresh)),
130+
.send(.sort(.presented(.cancelButtonTapped)))
131+
)
119132

120133
case .sort(.presented(.cancelButtonTapped)):
121134
state.sort = nil
@@ -178,12 +191,15 @@ public struct FavoritesFeature: Reducer, Sendable {
178191
return .none
179192

180193
case .markAllAsRead:
181-
return .run { send in
182-
_ = try await apiClient.readAllFavorites()
183-
// TODO: Display toast on success/error.
194+
return .concatenate(
195+
.run { send in
196+
let status = try await apiClient.readAllFavorites()
197+
let success = ToastMessage(text: Localization.markAsReadSuccess, haptic: .success)
198+
await toastClient.showToast(status ? success : .whoopsSomethingWentWrong)
199+
},
184200

185-
await send(.internal(.refresh))
186-
}
201+
.send(.internal(.refresh))
202+
)
187203
}
188204

189205
case .view(.commonContextMenu(let action, let isForum)):
@@ -194,28 +210,39 @@ public struct FavoritesFeature: Reducer, Sendable {
194210
return .none
195211

196212
case .delete(let id):
197-
return .run { send in
198-
let request = SetFavoriteRequest(id: id, action: .delete, type: isForum ? .forum : .topic)
199-
_ = try await apiClient.setFavorite(request)
200-
// TODO: Display toast on success/error.
213+
return .concatenate(
214+
.run { send in
215+
let request = SetFavoriteRequest(id: id, action: .delete, type: isForum ? .forum : .topic)
216+
let status = try await apiClient.setFavorite(request)
217+
let actionCompleted = ToastMessage(text: Localization.actionCompleted, haptic: .success)
218+
await toastClient.showToast(status ? actionCompleted : .whoopsSomethingWentWrong)
219+
},
201220

202-
await send(.internal(.refresh))
203-
}
221+
.send(.internal(.refresh))
222+
)
204223

205224
case .setImportant(let id, let pin):
206-
return .run { send in
207-
let request = SetFavoriteRequest(id: id, action: pin ? .pin : .unpin, type: isForum ? .forum : .topic)
208-
_ = try await apiClient.setFavorite(request)
209-
// TODO: Display toast on success/error.
225+
return .concatenate(
226+
.run { send in
227+
let request = SetFavoriteRequest(id: id, action: pin ? .pin : .unpin, type: isForum ? .forum : .topic)
228+
let status = try await apiClient.setFavorite(request)
229+
let actionCompleted = ToastMessage(text: Localization.actionCompleted, haptic: .success)
230+
await toastClient.showToast(status ? actionCompleted : .whoopsSomethingWentWrong)
231+
},
210232

211-
await send(.internal(.refresh))
212-
}
233+
.send(.internal(.refresh))
234+
)
213235

214236
case .markRead(let id):
215-
return .run { [id, isForum] send in
216-
let _ = try await apiClient.markRead(id: id, isTopic: !isForum)
217-
await send(.internal(.refresh))
218-
}
237+
return .concatenate(
238+
.run { [id, isForum] send in
239+
let status = try await apiClient.markRead(id: id, isTopic: !isForum)
240+
let actionCompleted = ToastMessage(text: Localization.actionCompleted, haptic: .success)
241+
await toastClient.showToast(status ? actionCompleted : .whoopsSomethingWentWrong)
242+
},
243+
244+
.send(.internal(.refresh))
245+
)
219246
}
220247

221248
case let .view(.topicContextMenu(action, favorite)):
@@ -229,13 +256,16 @@ public struct FavoritesFeature: Reducer, Sendable {
229256
}
230257

231258
case .notify(let flag, let notify):
232-
return .run { send in
233-
let request = NotifyFavoriteRequest(id: favorite.topic.id, flag: flag, type: notify)
234-
_ = try await apiClient.notifyFavorite(request)
235-
// TODO: Display toast on success/error.
259+
return .concatenate(
260+
.run { send in
261+
let request = NotifyFavoriteRequest(id: favorite.topic.id, flag: flag, type: notify)
262+
let status = try await apiClient.notifyFavorite(request)
263+
let notifyTypeChangedToast = ToastMessage(text: Localization.notifyTypeChanged, haptic: .success)
264+
await toastClient.showToast(status ? notifyTypeChangedToast : .whoopsSomethingWentWrong)
265+
},
236266

237-
await send(.internal(.refresh))
238-
}
267+
.send(.internal(.refresh))
268+
)
239269
}
240270

241271
case .internal(.refresh):

Modules/Sources/FavoritesFeature/Resources/Localizable.xcstrings

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
{
22
"sourceLanguage" : "en",
33
"strings" : {
4+
"Action completed" : {
5+
"localizations" : {
6+
"ru" : {
7+
"stringUnit" : {
8+
"state" : "translated",
9+
"value" : "Действие выполнено"
10+
}
11+
}
12+
}
13+
},
414
"Always" : {
515
"localizations" : {
616
"ru" : {
@@ -141,6 +151,16 @@
141151
}
142152
}
143153
},
154+
"Marked as read" : {
155+
"localizations" : {
156+
"ru" : {
157+
"stringUnit" : {
158+
"state" : "translated",
159+
"value" : "Отмечено прочитанным"
160+
}
161+
}
162+
}
163+
},
144164
"No favorites" : {
145165
"localizations" : {
146166
"ru" : {
@@ -171,6 +191,16 @@
171191
}
172192
}
173193
},
194+
"Notify type changed" : {
195+
"localizations" : {
196+
"ru" : {
197+
"stringUnit" : {
198+
"state" : "translated",
199+
"value" : "Тип уведомлений изменен"
200+
}
201+
}
202+
}
203+
},
174204
"Once" : {
175205
"localizations" : {
176206
"ru" : {

0 commit comments

Comments
 (0)