Skip to content

Commit

Permalink
feat: 검색 결과 > 밈 상세 갔다온 후 해당 밈만 리프레시 되도록 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
lina0322 committed Oct 4, 2024
1 parent 2229ba9 commit 5edee4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public final class SearchResultRouter: Router, SearchResultRouting {
text: text,
router: self,
searchKeywordUseCase: SearchKeywordUseCaseImpl(repository: repository),
searchByTextUseCase: SearchByTextUseCaseImpl(repository: repository),
searchByTextUseCase: SearchByTextUseCaseImpl(repository: repository),
getMemeDetailUseCase: GetMemeDetailUseCaseImpl(repository: repository),
copyImageUseCase: CopyImageUseCaseImpl(),
watchMemeUseCase: WatchMemeUseCaseImpl(repository: repository)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {
var text: String
var memeList: [MemeDetail]
var memePagination: MemeListWithPagination.Pagination
var currentMeme: MemeDetail?
var isActiveCopyPopup: Bool = false
var isLoading: Bool = true
}
Expand All @@ -51,6 +52,7 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {

private let searchKeywordUseCase: SearchKeywordUseCase
private let searchByTextUseCase: SearchByTextUseCase
private let getMemeDetailUseCase: GetMemeDetailUseCase
private let copyImageUseCase: CopyImageUseCase
private let watchMemeUseCase: WatchMemeUseCase

Expand All @@ -62,6 +64,7 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {
router: SearchResultRouting?,
searchKeywordUseCase: SearchKeywordUseCase,
searchByTextUseCase: SearchByTextUseCase,
getMemeDetailUseCase: GetMemeDetailUseCase,
copyImageUseCase: CopyImageUseCase,
watchMemeUseCase: WatchMemeUseCase
) {
Expand All @@ -74,6 +77,7 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {
)
self.searchKeywordUseCase = searchKeywordUseCase
self.searchByTextUseCase = searchByTextUseCase
self.getMemeDetailUseCase = getMemeDetailUseCase
self.copyImageUseCase = copyImageUseCase
self.watchMemeUseCase = watchMemeUseCase
}
Expand All @@ -86,19 +90,20 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {
switch type {
case .viewWillAppear:
await fetchData()
await refreshCurrentMeme()
case .refresh:
if state.text.isEmpty == false {
await fetchData(with: state.text)
} else if state.keyword.isEmpty == false {
await fetchData(with: state.keyword)
}

case .search(text: let text):
await fetchData(with: text)
case .memeDetailTapped(let meme):
router?.showMemeDetail(memeDetail: meme)
logSearch(event: .meme, keyword: state.keyword)
await postShownMeme(memeId: meme.id)
state.currentMeme = meme
case .memeCopyTapped(let meme):
await copyImage(meme: meme)
break
Expand Down Expand Up @@ -162,6 +167,22 @@ public final class SearchResultViewModel: ViewModelType, ObservableObject {
}
}

@MainActor
private func refreshCurrentMeme() async {
guard let currentMeme = state.currentMeme else { return }

do {
let meme = try await getMemeDetailUseCase.execute(memeId: currentMeme.id)
if let index = state.memeList.firstIndex(where: { $0.id == meme.id }) {
state.memeList[index] = meme
}
} catch(let error) {
debugPrint("error = \(error)")
}

state.currentMeme = nil
}

@MainActor
private func copyImage(meme: MemeDetail) async {
do {
Expand Down

0 comments on commit 5edee4e

Please sign in to comment.