Skip to content

Commit 09fb59a

Browse files
committed
Fix multiple pages are not shown in favorites
1 parent a5edfd5 commit 09fb59a

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

Modules/Sources/APIClient/APIClient.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct APIClient: Sendable {
5050
public var getHistory: @Sendable (_ offset: Int, _ perPage: Int) async throws -> History
5151

5252
// Favorites
53-
public var getFavorites: @Sendable (_ request: FavoritesRequest, _ policy: CachePolicy) async throws -> AsyncThrowingStream<[FavoriteInfo], any Error>
53+
public var getFavorites: @Sendable (_ request: FavoritesRequest, _ policy: CachePolicy) async throws -> AsyncThrowingStream<Favorite, any Error>
5454
public var setFavorite: @Sendable (_ request: SetFavoriteRequest) async throws -> Bool
5555
public var notifyFavorite: @Sendable (_ request: NotifyFavoriteRequest) async throws -> Bool
5656
public var readAllFavorites: @Sendable () async throws -> Bool
@@ -244,8 +244,8 @@ extension APIClient: DependencyKey {
244244
)
245245
let response = try await api.get(command)
246246
let favorites = try await parser.parseFavorites(response)
247-
await cache.setFavorites(favorites.favorites)
248-
return favorites.favorites
247+
await cache.setFavorites(favorites)
248+
return favorites
249249
},
250250
policy: policy
251251
)

Modules/Sources/CacheClient/CacheClient.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public struct CacheClient: Sendable {
2929
public var getUser: @Sendable (_ id: Int) async -> User?
3030

3131
// Favorites
32-
public var setFavorites: @Sendable ([FavoriteInfo]) async -> Void
33-
public var getFavorites: @Sendable () async -> [FavoriteInfo]?
32+
public var setFavorites: @Sendable (Favorite) async -> Void
33+
public var getFavorites: @Sendable () async -> Favorite?
3434

3535
// Forums List
3636
public var setForumsList: @Sendable ([ForumInfo]) async -> Void
@@ -200,12 +200,12 @@ private extension CacheClient {
200200
}
201201

202202
private static var favoritesKey: String { "favoritesKey" }
203-
private static var favoritesStorage: Storage<String, [FavoriteInfo]> {
203+
private static var favoritesStorage: Storage<String, Favorite> {
204204
return try! Storage(
205205
diskConfig: DiskConfig(name: "Favorites", expiry: .date(.days(30)), maxSize: .kilobytes(100)),
206206
memoryConfig: MemoryConfig(),
207207
fileManager: .default,
208-
transformer: TransformerFactory.forCodable(ofType: [FavoriteInfo].self)
208+
transformer: TransformerFactory.forCodable(ofType: Favorite.self)
209209
)
210210
}
211211

Modules/Sources/FavoritesFeature/FavoritesFeature.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public struct FavoritesFeature: Reducer, Sendable {
7373

7474
case sort(PresentationAction<SortFeature.Action>)
7575

76-
case _favoritesResponse(Result<[FavoriteInfo], any Error>)
76+
case _favoritesResponse(Result<Favorite, any Error>)
7777
case _loadFavorites(offset: Int)
7878
case _startUnreadLoadingIndicator(id: Int)
7979
case _jumpRequestFailed
@@ -105,6 +105,7 @@ public struct FavoritesFeature: Reducer, Sendable {
105105
case .onAppear:
106106
guard state.favorites.isEmpty && state.favoritesImportant.isEmpty else { return .none }
107107
return .merge([
108+
updatePageNavigation(&state, offset: 0),
108109
.send(._loadFavorites(offset: 0)),
109110
.run { send in
110111
for await _ in notificationCenter.observe(.favoritesUpdated) {
@@ -245,7 +246,7 @@ public struct FavoritesFeature: Reducer, Sendable {
245246
var favsImportant: [FavoriteInfo] = []
246247
var favorites: [FavoriteInfo] = []
247248

248-
for favorite in response {
249+
for favorite in response.favorites {
249250
if favorite.isImportant {
250251
favsImportant.append(favorite)
251252
} else {
@@ -257,14 +258,14 @@ public struct FavoritesFeature: Reducer, Sendable {
257258
state.favorites = favorites
258259

259260
// TODO: Is it ok?
260-
state.pageNavigation.count = response.count
261+
// state.pageNavigation.count = response.count
261262

262263
state.isLoading = false
263264
state.isRefreshing = false
264265

265266
reportFullyDisplayed(&state)
266267

267-
return .none
268+
return updatePageNavigation(&state, count: response.favoritesCount)
268269

269270
case let ._favoritesResponse(.failure(error)):
270271
print("FAVORITES RESPONSE FAILURE: \(error)")
@@ -286,12 +287,26 @@ public struct FavoritesFeature: Reducer, Sendable {
286287
}
287288
}
288289

290+
// MARK: - Shared logic
291+
289292
private func reportFullyDisplayed(_ state: inout State) {
290293
guard !state.didLoadOnce else { return }
291294
analyticsClient.reportFullyDisplayed()
292295
state.didLoadOnce = true
293296
}
294297

298+
private func updatePageNavigation(_ state: inout State, count: Int = 0, offset: Int? = nil) -> Effect<Action> {
299+
return PageNavigationFeature()
300+
.reduce(
301+
into: &state.pageNavigation,
302+
action: .update(
303+
count: count,
304+
offset: offset
305+
)
306+
)
307+
.map(Action.pageNavigation)
308+
}
309+
295310
private func goToEnd(id: Int) -> Effect<Action> {
296311
return .merge(
297312
.run { send in

Modules/Sources/FavoritesFeature/FavoritesScreen.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ public struct FavoritesScreen: View {
187187
@ViewBuilder
188188
private func FavoritesSection(favorites: [FavoriteInfo], important: Bool) -> some View {
189189
Section {
190-
if !important, store.pageNavigation.shouldShow {
191-
PageNavigation(store: store.scope(state: \.pageNavigation, action: \.pageNavigation))
192-
}
190+
Navigation(isShown: !important)
193191

194192
ForEach(favorites, id: \.hashValue) { favorite in
195193
Row(
@@ -226,9 +224,7 @@ public struct FavoritesScreen: View {
226224
}
227225
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
228226

229-
if !important, store.pageNavigation.shouldShow {
230-
PageNavigation(store: store.scope(state: \.pageNavigation, action: \.pageNavigation))
231-
}
227+
Navigation(isShown: !important)
232228
} header: {
233229
if !favorites.isEmpty {
234230
Header(title: important
@@ -239,6 +235,17 @@ public struct FavoritesScreen: View {
239235
.listRowBackground(Color(.Background.teritary))
240236
}
241237

238+
// MARK: - Navigation
239+
240+
@ViewBuilder
241+
private func Navigation(isShown: Bool) -> some View {
242+
if isShown, store.pageNavigation.shouldShow {
243+
PageNavigation(store: store.scope(state: \.pageNavigation, action: \.pageNavigation))
244+
.listRowBackground(Color.clear)
245+
.padding(.bottom, 4)
246+
}
247+
}
248+
242249
// MARK: - Row
243250

244251
@ViewBuilder

0 commit comments

Comments
 (0)