Skip to content

Commit

Permalink
refactor : ResultViewModel에 좋아요 버튼액션 정의 #16
Browse files Browse the repository at this point in the history
- 필요한 UseCase 주입
  • Loading branch information
wongbingg committed Feb 7, 2023
1 parent b6b1d09 commit fea48cd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ final class MyPageSceneDIContainer {

func makeResultViewModel(model: [ProductCell],
actions: ResultViewModelAction) -> ResultViewModel {
return DefaultResultViewModel(cells: model, actions: actions)
return DefaultResultViewModel(
cells: model,
actions: actions,
handleLikedProductUseCase: makeHandleLikedProductUseCase(),
searchUserProfileUseCase: makeSearchUserProfileUseCase()
)
}

// MARK: - Product Detail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ final class SearchSceneDIContainer {

func makeResultViewModel(model: [ProductCell],
actions: ResultViewModelAction) -> ResultViewModel {
return DefaultResultViewModel(cells: model, actions: actions)
return DefaultResultViewModel(
cells: model,
actions: actions,
handleLikedProductUseCase: makeHandleLikedProductUseCase(),
searchUserProfileUseCase: makeSearchUserProfileUseCase()
)
}

// MARK: - Product Detail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,46 @@ struct ResultViewModelAction {
let cellTapped: (Int) -> Void
}
protocol ResultViewModelInput {
func fetchUserLikeList() async throws
func likeButtonTapped(id: Int, isSelected: Bool) async throws
func didSelectItemAt(indexPath: Int)
}
protocol ResultViewModelOutput {
var productCells: [ProductCell] { get }
var userLikeList: [Int] { get }
}

protocol ResultViewModel: ResultViewModelInput, ResultViewModelOutput {}

final class DefaultResultViewModel: ResultViewModel {
private let handleLikedProductUseCase: HandleLikedProductUseCase
private let searchUserProfileUseCase: SearchUserProfileUseCase
private(set) var userLikeList: [Int] = []
var productCells: [ProductCell]
let actions: ResultViewModelAction

init(cells: [ProductCell], actions: ResultViewModelAction) {
init(
cells: [ProductCell],
actions: ResultViewModelAction,
handleLikedProductUseCase: HandleLikedProductUseCase,
searchUserProfileUseCase: SearchUserProfileUseCase
) {
self.productCells = cells
self.actions = actions
self.handleLikedProductUseCase = handleLikedProductUseCase
self.searchUserProfileUseCase = searchUserProfileUseCase
}

func fetchUserLikeList() async throws {
let userProfile = try await searchUserProfileUseCase.execute()
userLikeList = userProfile.likedProductIds
}

func likeButtonTapped(id: Int, isSelected: Bool) async throws {
try await handleLikedProductUseCase.execute(
with: id,
isAdd: isSelected
)
}

func didSelectItemAt(indexPath: Int) {
Expand Down

0 comments on commit fea48cd

Please sign in to comment.