Skip to content

Commit

Permalink
[ITDS-75] fix: #61 - "추천 공고 조회" api 응답에 "infoPostName" 추가
Browse files Browse the repository at this point in the history
- 기타: "추천 공고 조회" api 응답값 캐시에 저장 후 변동사항 비교 추적
  • Loading branch information
wongbingg committed Feb 25, 2025
1 parent d9ba113 commit bea2bd5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Targets/DataLayer/Sources/Responses/BannerCellResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import DomainLayer
public struct BannerCellResponse: Decodable {
let featuredPostID: Int
let infoPostID: Int
let infoPostName: String
let bannerImageURL: String

enum CodingKeys: String, CodingKey {
case featuredPostID = "featuredPostId"
case infoPostID = "infoPostId"
case infoPostName = "infoPostName"
case bannerImageURL = "bannerImageUrl"
}
}
Expand All @@ -26,6 +28,7 @@ public extension BannerCellResponse {
func toDomain() -> BannerCellData {
.init(featuredPostID: featuredPostID,
infoPostID: infoPostID,
infoPostName: infoPostName,
bannerImageURL: bannerImageURL)
}
}
Expand Down
7 changes: 5 additions & 2 deletions Targets/DomainLayer/Sources/Entities/BannerCellData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import Foundation

public struct BannerCellData: Equatable {
public let featuredPostID, infoPostID: Int
public let bannerImageURL: String
public let infoPostName, bannerImageURL: String

public init(
featuredPostID: Int,
infoPostID: Int,
infoPostName: String,
bannerImageURL: String
) {
self.featuredPostID = featuredPostID
self.infoPostID = infoPostID
self.infoPostName = infoPostName
self.bannerImageURL = bannerImageURL
}
}
Expand All @@ -29,14 +31,15 @@ extension BannerCellData {
bannerImageURL: String = "www.naver.com") -> Self {
.init(featuredPostID: featuredPostID,
infoPostID: infoPostID,
infoPostName: "",
bannerImageURL: bannerImageURL)
}

public func toRecommendCellData() -> RecommendCellData {
.init(featuredPostID: featuredPostID,
infoID: infoPostID,
title: "",
subTitle: "부제목",
subTitle: infoPostName,
thumbnailURL: bannerImageURL)
}
}
14 changes: 7 additions & 7 deletions Targets/DomainLayer/Sources/Entities/RecommendCellData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ public struct RecommendCellData: Equatable, Codable {
public extension RecommendCellData {

static var initialData: [Self] = [
.init(featuredPostID: 1, infoID: 0, title: "첫 번째 추천", subTitle: "부제목", thumbnailURL: ""),
.init(featuredPostID: 2, infoID: 0, title: "두 번째 추천", subTitle: "부제목", thumbnailURL: ""),
.init(featuredPostID: 3, infoID: 0, title: "세 번째 추천", subTitle: "부제목", thumbnailURL: ""),
.init(featuredPostID: 4, infoID: 0, title: "네 번째 추천", subTitle: "부제목", thumbnailURL: ""),
.init(featuredPostID: 5, infoID: 0, title: "다섯 번째 추천", subTitle: "부제목", thumbnailURL: "")
.init(featuredPostID: 1, infoID: 0, title: "첫 번째 추천", subTitle: " ", thumbnailURL: ""),
.init(featuredPostID: 2, infoID: 0, title: "두 번째 추천", subTitle: " ", thumbnailURL: ""),
.init(featuredPostID: 3, infoID: 0, title: "세 번째 추천", subTitle: " ", thumbnailURL: ""),
.init(featuredPostID: 4, infoID: 0, title: "네 번째 추천", subTitle: " ", thumbnailURL: ""),
.init(featuredPostID: 5, infoID: 0, title: "다섯 번째 추천", subTitle: " ", thumbnailURL: "")
]

var isUploadAvailable: Bool {
infoID != 0 &&
subTitle.isEmpty == false &&
subTitle != "부제목" &&
subTitle != " " &&
imageData != nil
}

var isChanged: Bool {
(subTitle != "" && subTitle != "부제목") ||
(subTitle != " " && subTitle.isEmpty == false) ||
imageData != nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import Foundation

import ReactorKit
import RxSwift
import RxCocoa

struct PostRecommendCache {
@UserDefaultWrapper(key: "fetchedRecommendPost", defaultValue: [RecommendCellData]())
static var fetchedRecommendPost: [RecommendCellData]

@UserDefaultWrapper(key: "cachedRecommendPost", defaultValue: [RecommendCellData]())
static var cachedRecommendPost: [RecommendCellData]
}
Expand All @@ -25,6 +29,7 @@ final class PostRecommendReactor: Reactor {
private let recommendedPostStream: MutableRecommendedPostStream
private let fetchBannerUseCase: FetchBannerUseCaseType
private let updateBannerUseCase: UpdateBannerUseCaseType
private let startUpdateCache: BehaviorRelay<Bool> = .init(value: false)
private let disposeBag = DisposeBag()

// MARK: - Initializer
Expand Down Expand Up @@ -68,20 +73,27 @@ final class PostRecommendReactor: Reactor {

// MARK: - Methods
func bindStream() {
/// data binding용 Stream
recommendedPostStream.data
.distinctUntilChanged()
.observe(on: MainScheduler.asyncInstance)
.do(onNext: {
guard $0 != RecommendCellData.initialData else {
return
}
PostRecommendCache.cachedRecommendPost = $0
})
.subscribe(with: self) { owner, data in
owner.action.onNext(.refreshData(data))
owner.checkUploadable(from: data)
}
.disposed(by: disposeBag)

/// cache update용 Stream
recommendedPostStream.data
.filter { [weak self] _ in
guard let self = self else { return false }
return self.startUpdateCache.value
}
.filter { $0 != PostRecommendCache.fetchedRecommendPost } /// 공고 변경사항이 있는 경우만 필터링
.subscribe(with: self) { owner, data in
PostRecommendCache.cachedRecommendPost = data
}
.disposed(by: disposeBag)
}

func checkUploadable(from data: [RecommendCellData]) {
Expand All @@ -104,7 +116,8 @@ final class PostRecommendReactor: Reactor {
}
}

func askHistoryIfExistMutation() -> Observable<Mutation> { /// 작성중이던 정보 불러오기
/// 작성중이던 정보 있으면 불러올지 말지 선택하는 얼럿 띄우기
func askHistoryIfExistMutation() -> Observable<Mutation> {
for post in PostRecommendCache.cachedRecommendPost where post.isChanged {
return .just(.setAlertHistoryDataTrue)
}
Expand All @@ -114,19 +127,31 @@ final class PostRecommendReactor: Reactor {
func loadCacheMutation() -> Observable<Mutation> {
let cacheData = PostRecommendCache.cachedRecommendPost
recommendedPostStream.updateAllPost(posts: cacheData)
startUpdateCache.accept(true)
return .empty()
}

func loadDataMutation() -> Observable<Mutation> {
fetchBannerUseCase.execute()
.map { $0.map { $0.toRecommendCellData() } }
.withUnretained(self)
.map { owner, banners in
owner.recommendedPostStream.updateAllPost(posts: banners)
}
.flatMap {
return Observable<Mutation>.empty()
}
PostRecommendCache.cachedRecommendPost = []

if PostRecommendCache.fetchedRecommendPost.isEmpty {
return fetchBannerUseCase.execute()
.map { $0.map { $0.toRecommendCellData() } }
.withUnretained(self)
.map { owner, banners in
owner.recommendedPostStream.updateAllPost(posts: banners)
PostRecommendCache.fetchedRecommendPost = owner.recommendedPostStream.data.value
owner.startUpdateCache.accept(true)
}
.flatMap {
return Observable<Mutation>.empty()
}
} else {
let fetchedRecommendPost = PostRecommendCache.fetchedRecommendPost
recommendedPostStream.updateAllPost(posts: fetchedRecommendPost)
startUpdateCache.accept(true)
return .empty()
}
}

func setUplodableMutation() -> Observable<Mutation> {
Expand Down Expand Up @@ -186,7 +211,10 @@ final class PostRecommendReactor: Reactor {
case let .setLoading(bool): newState.isLoading = bool
case .setUploadableTrue: newState.isUploadable = true
case .setAlertHistoryDataTrue: newState.alertHistoryData = true
case .setIsUploadComplete: newState.isUploadComplete = true ; PostRecommendCache.cachedRecommendPost = []
case .setIsUploadComplete:
newState.isUploadComplete = true
PostRecommendCache.cachedRecommendPost = []
PostRecommendCache.fetchedRecommendPost = []
}
return newState
}
Expand Down

0 comments on commit bea2bd5

Please sign in to comment.