Skip to content

Commit

Permalink
Merge pull request #305 from depromeet/feat/#301-add-paigination
Browse files Browse the repository at this point in the history
feat: 페이지네이션 추가
  • Loading branch information
akrudal authored Jan 20, 2024
2 parents 39357bd + 38a9ac2 commit 54d0e52
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class CalendarViewReactor: Reactor {
public func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .fetchFamilyMembers:
let query: SearchFamilyQuery = SearchFamilyQuery(type: "FAMILY", page: 1, size: 20)
let query: SearchFamilyQuery = SearchFamilyQuery(page: 1, size: 20)
return familyUseCase.excute(query: query)
.asObservable()
.flatMap {_ in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import Core


public final class HomeDIContainer {
public typealias ViewContrller = HomeViewController
public typealias Reactor = HomeViewReactor

public func makeViewController() -> ViewContrller {
func makeViewController() -> HomeViewController {
return HomeViewController(reactor: makeReactor())
}

Expand Down Expand Up @@ -43,7 +40,7 @@ public final class HomeDIContainer {
return FamilyViewUseCase(familyRepository: makeInviteFamilyRepository())
}

public func makeReactor() -> Reactor {
func makeReactor() -> HomeViewReactor {
return HomeViewReactor(postRepository: makePostUseCase())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ extension SectionOfFamily: SectionModelType {
self.items = items
}
}


struct FamilySection {
typealias Model = SectionModel<Int, Item>

enum Item {
case main(ProfileData)
}
}

extension FamilySection.Item: Equatable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ extension SectionOfFeed: SectionModelType {
self.items = items
}
}

struct PostSection {
typealias Model = SectionModel<Int, Item>

enum Item {
case main(PostListData)
}
}

extension PostSection.Item: Equatable {

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,39 @@ import Domain

import ReactorKit
import RxDataSources
import Kingfisher

public final class HomeFamilyViewReactor: Reactor {
public enum Action {
case getFamilyMembers
final class HomeFamilyViewReactor: Reactor {
enum Action {
case viewWillAppear
case prefetchItems([FamilySection.Item])
case tapInviteFamily
case pagination(
contentHeight: CGFloat,
contentOffsetY: CGFloat,
scrollViewHeight: CGFloat
)
}

public enum Mutation {
enum Mutation {
case setLoading(Bool)
case showShareAcitivityView(URL?)
case showInviteFamilyView
case setFamilyCollectionView([SectionModel<String, ProfileData>])
case setCopySuccessToastMessageView
case setFetchFailureToastMessageView
case setSharePanel(String)
case initDataSource([FamilySection.Item])
case updateDataSource([FamilySection.Item])
}

public struct State {
var showLoading: Bool = true
var isShowingInviteFamilyView: Bool = false
var familySections: [SectionModel<String, ProfileData>] = []
var familySections = FamilySection.Model(
model: 0,
items: []
)

@Pulse var familyInvitationLink: URL?
@Pulse var shouldPresentCopySuccessToastMessageView: Bool = false
@Pulse var shouldPresentFetchFailureToastMessageView: Bool = false
Expand All @@ -43,14 +55,17 @@ public final class HomeFamilyViewReactor: Reactor {
private let searchFamilyUseCase: SearchFamilyMemberUseCaseProtocol
private let inviteFamilyUseCase: FamilyViewUseCaseProtocol

private var currentPage: Int = 0
private var isLast: Bool = false

init(searchFamilyUseCase: SearchFamilyMemberUseCaseProtocol, inviteFamilyUseCase: FamilyViewUseCaseProtocol) {
self.inviteFamilyUseCase = inviteFamilyUseCase
self.searchFamilyUseCase = searchFamilyUseCase
}
}

extension HomeFamilyViewReactor {
public func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
let eventMutation = provider.activityGlobalState.event
.flatMap { event -> Observable<Mutation> in
switch event {
Expand All @@ -64,18 +79,13 @@ extension HomeFamilyViewReactor {
return Observable<Mutation>.merge(mutation, eventMutation)
}

public func mutate(action: Action) -> Observable<Mutation> {
func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .tapInviteFamily:
return inviteFamilyUseCase.executeFetchInvitationUrl()
.map {
guard let invitationLink = $0?.url else {
return .setFetchFailureToastMessageView
}
return .setSharePanel(invitationLink)
}
case .getFamilyMembers:
let query: SearchFamilyQuery = SearchFamilyQuery(type: "FAMILY", page: 1, size: 20)
case .viewWillAppear:
currentPage = 1
isLast = false

let query = SearchFamilyQuery(page: currentPage, size: 10)
return searchFamilyUseCase.excute(query: query)
.asObservable()
.flatMap { familyMembers in
Expand All @@ -84,22 +94,49 @@ extension HomeFamilyViewReactor {
return Observable.just(Mutation.showInviteFamilyView)
}

var observables = [Observable.just(Mutation.setFamilyCollectionView([
SectionModel<String, ProfileData>(model: "section1", items: familyMembers.members)]))]

return Observable.concat(observables)
let familySectionItem = familyMembers.members.map(FamilySection.Item.main)
return Observable.concat([
// Observable.just(Mutation.show)
Observable.just(Mutation.initDataSource(familySectionItem))
])
}
case .tapInviteFamily:
return inviteFamilyUseCase.executeFetchInvitationUrl()
.map {
guard let invitationLink = $0?.url else {
return .setFetchFailureToastMessageView
}
return .setSharePanel(invitationLink)
}
case let .pagination(contentHeight, contentOffsetY, scrollViewHeight):
let paddingSpace = contentHeight - contentOffsetY
if paddingSpace < scrollViewHeight {
return getFamilyMembers()
} else {
return .empty()
}
case let .prefetchItems(items):
var urls = [URL]()
items.forEach {
if case let .main(profile) = $0,
let imageURL = profile.profileImageURL,
let url = URL(string: imageURL) {
urls.append(url)
}
}
ImagePrefetcher(resources: urls).start()
return .empty()
}
}

public func reduce(state: State, mutation: Mutation) -> State {
func reduce(state: State, mutation: Mutation) -> State {
var newState = state

switch mutation {
case .showInviteFamilyView:
newState.isShowingInviteFamilyView = true
case let .setFamilyCollectionView(data):
newState.familySections = data
case let .updateDataSource(sectionItem):
newState.familySections.items.append(contentsOf: sectionItem)
case let .showShareAcitivityView(url):
newState.familyInvitationLink = url
case .setLoading:
Expand All @@ -110,8 +147,34 @@ extension HomeFamilyViewReactor {
newState.shouldPresentFetchFailureToastMessageView = true
case let .setSharePanel(urlString):
newState.familyInvitationLink = URL(string: urlString)
case let .initDataSource(sectionItem):
newState.familySections.items = sectionItem
}

return newState
}
}

extension HomeFamilyViewReactor {
private func getFamilyMembers() -> Observable<Mutation> {
self.currentPage += 1

guard !isLast else {
return Observable.empty()
}

let query: SearchFamilyQuery = SearchFamilyQuery(page: currentPage, size: 10)
return searchFamilyUseCase.excute(query: query)
.asObservable()
.flatMap { familyMembers -> Observable<Mutation> in
guard let familyMembers else {
return Observable.empty()
}

self.isLast = true

let familySectionItem = familyMembers.members.map(FamilySection.Item.main)
return Observable.just(Mutation.updateDataSource(familySectionItem))
}
}
}
Loading

0 comments on commit 54d0e52

Please sign in to comment.