-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af4367c
commit f774666
Showing
8 changed files
with
404 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
Targets/D3N/Sources/Feature/MyPage/SolvedQuiz/Main/SolvedNewsStore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// SolvedNewsStore.swift | ||
// D3N | ||
// | ||
// Created by Younghoon Ahn on 11/23/23. | ||
// Copyright © 2023 sju. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import ComposableArchitecture | ||
|
||
public struct SolvedNewsStore: Reducer { | ||
let FIXED_PAGE_SIZE = 10 | ||
|
||
public struct State: Equatable, Identifiable { | ||
public let id: UUID | ||
|
||
var newsEntityList: [NewsEntity] = [] | ||
var pageIndex: Int = 0 | ||
|
||
var newsListItems: IdentifiedArrayOf<NewsListItemCellStore.State> = [] | ||
|
||
public init( | ||
id: UUID = .init() | ||
) { | ||
self.id = id | ||
} | ||
} | ||
|
||
@Dependency(\.newsClient) var newsClient | ||
|
||
public enum Action: Equatable { | ||
case onAppear | ||
|
||
case fetchNewsListRequest | ||
case fetchNewsListResponse(Result<[NewsEntity], D3NAPIError>) | ||
|
||
case newsListItems(id: NewsListItemCellStore.State.ID, action: NewsListItemCellStore.Action) | ||
case delegate(Delegate) | ||
|
||
public enum Delegate: Equatable { | ||
case select(NewsEntity) | ||
} | ||
} | ||
|
||
public var body: some ReducerOf<Self> { | ||
Reduce { state, action in | ||
switch action { | ||
case .onAppear: | ||
return .concatenate([ | ||
.send(.fetchNewsListRequest) | ||
]) | ||
|
||
case .fetchNewsListRequest: | ||
return .run { [pageIndex = state.pageIndex] send in | ||
let response = await newsClient.fetchNewsList(pageIndex, FIXED_PAGE_SIZE) | ||
await send(.fetchNewsListResponse(response)) | ||
} | ||
|
||
case let .fetchNewsListResponse(.success(newsEntityList)): | ||
let newsListItems = state.makeSolvedNewsListItems(from: newsEntityList) | ||
state.newsListItems.append(contentsOf: newsListItems) | ||
state.pageIndex += 1 | ||
return .none | ||
|
||
case let .newsListItems(id: id, action: .delegate(action)): | ||
switch action { | ||
case .onAppear: | ||
if id == state.newsListItems.ids.last { | ||
return .send(.fetchNewsListRequest) | ||
} | ||
case .tapped: | ||
if let newsEntity = state.newsListItems[id: id]?.newsEntity { | ||
return .send(.delegate(.select(newsEntity))) | ||
} | ||
} | ||
return .none | ||
|
||
default: | ||
return .none | ||
} | ||
} | ||
.forEach(\.newsListItems, action: /Action.newsListItems(id:action:)) { | ||
NewsListItemCellStore() | ||
} | ||
} | ||
} | ||
|
||
public extension SolvedNewsStore.State { | ||
func makeSolvedNewsListItems(from newsEntityList: [NewsEntity]) -> IdentifiedArrayOf<NewsListItemCellStore.State> { | ||
return .init( | ||
uniqueElements: newsEntityList.map { newsEntity in | ||
return .init(newsEntity: newsEntity) | ||
} | ||
) | ||
} | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
Targets/D3N/Sources/Feature/MyPage/SolvedQuiz/Main/SolvedNewsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// SolvedNewsView.swift | ||
// D3N | ||
// | ||
// Created by Younghoon Ahn on 11/23/23. | ||
// Copyright © 2023 sju. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
import ComposableArchitecture | ||
|
||
public struct SolvedNewsView: View { | ||
let store: StoreOf<SolvedNewsStore> | ||
|
||
public init(store: StoreOf<SolvedNewsStore>) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
WithViewStore(self.store, observe: { $0 }) { viewStore in | ||
ScrollView { | ||
VStack { | ||
newsListItemsView | ||
.padding(.horizontal) | ||
|
||
Spacer() | ||
} | ||
} | ||
.navigationTitle("내가 푼 뉴스") | ||
.onAppear { | ||
viewStore.send(.onAppear) | ||
} | ||
} | ||
} | ||
|
||
private var newsListItemsView: some View { | ||
LazyVStack { | ||
ForEachStore(self.store.scope(state: \.newsListItems, action: SolvedNewsStore.Action.newsListItems(id:action:))) { | ||
NewsListItemCellView(store: $0) | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.