Skip to content

Commit

Permalink
๐Ÿ”€:: #253 from MaeumgaGym/refactoring/#251-postureWholeRefactoring
Browse files Browse the repository at this point in the history
:: [#251] ์ž์„ธ - ์ „์ฒด ๋ฆฌํŽ™ํ† ๋ง
  • Loading branch information
jjunhaa0211 authored Feb 27, 2024
2 parents 9cd4a0a + eab25e1 commit 4f15b1c
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 221 deletions.
4 changes: 4 additions & 0 deletions Projects/Data/Sources/Repository/PostureRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class PostureRepository: PostureRepositoryInterface {
return networkService.requestDetailData(type: type)
}

public func getSearchData() -> Single<PostureSearchModel> {
return networkService.requestSearchData()
}

public init(networkService: PostureService) {
self.networkService = networkService
}
Expand Down
24 changes: 24 additions & 0 deletions Projects/Domain/Sources/Model/Posture/PostureSearchModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import UIKit

public struct PostureSearchModel {
public var searchResultData: [PostureSearchContentModel]

public init(searchResultData: [PostureSearchContentModel]) {
self.searchResultData = searchResultData
}
}

public struct PostureSearchContentModel {
public var exerciseImage: UIImage
public var exerciseName: String
public var exercisePart: String

public init(exerciseImage: UIImage,
exerciseName: String,
exercisePart: String
) {
self.exerciseImage = exerciseImage
self.exerciseName = exerciseName
self.exercisePart = exercisePart
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public protocol PostureRepositoryInterface {
func getRecommandData() -> Single<[PostureRecommandModel]>
func getPartData(type: PosturePartType) -> Single<PosturePartModel>
func getDetailData(type: PostureDetailType) -> Single<PostureDetailModel>
func getSearchData() -> Single<PostureSearchModel>
}
13 changes: 13 additions & 0 deletions Projects/Domain/Sources/UseCase/PostureUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ public protocol PostureUseCase {
var recommandData: PublishSubject<[PostureRecommandModel]> { get }
var partData: PublishSubject<PosturePartModel> { get }
var detailData: PublishSubject<PostureDetailModel> { get }
var searchData: PublishSubject<PostureSearchModel> { get }

func getRecommandData()
func getPartData(type: PosturePartType)
func getDetailData(type: PostureDetailType)
func getSearchData()
}

public class DefaultPostureUseCase {
Expand All @@ -19,6 +21,7 @@ public class DefaultPostureUseCase {
public let recommandData = PublishSubject<[PostureRecommandModel]>()
public let partData = PublishSubject<PosturePartModel>()
public let detailData = PublishSubject<PostureDetailModel>()
public let searchData = PublishSubject<PostureSearchModel>()

public init(repository: PostureRepositoryInterface) {
self.repository = repository
Expand Down Expand Up @@ -56,4 +59,14 @@ extension DefaultPostureUseCase: PostureUseCase {
print("PostureUseCase getDetailData error occurred: \(error)")
}).disposed(by: disposeBag)
}

public func getSearchData() {
repository.getSearchData()
.subscribe(onSuccess: { [weak self] searchData in
self?.searchData.onNext(searchData)
},
onFailure: { error in
print("PostureUseCase getSearchData error occurred: \(error)")
}).disposed(by: disposeBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let useCase = DefaultPostureUseCase(repository: PostureRepository(networkService: PostureService()))
let viewModel = PostureDetailViewModel(useCase: useCase)
let viewController = PostureDetailViewController(viewModel)
let viewModel = PostureSearchViewModel(useCase: useCase)
let viewController = PostureSearchViewController(viewModel)
window?.rootViewController = UINavigationController(
rootViewController: viewController)
window?.makeKeyAndVisible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class PostureDetailViewController: BaseViewController<PostureDetailViewMo
let output = viewModel.transform(input, action: { optput in
optput.detailData
.subscribe(onNext: { detailData in
MGLogger.debug("Chest Data: \(detailData)")
MGLogger.debug("detailData: \(detailData)")
self.postureDetailModel = detailData
}).disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,81 @@
import UIKit

import RxFlow
import RxCocoa
import RxSwift

import SnapKit
import Then

import Core
import PostureFeatureInterface
import DSKit
import Domain

import MGLogger
import MGNetworks
import Data

import PostureFeatureInterface

public class PostureSearchViewController: BaseViewController<PostureSearchViewModel> {
private var searchModel = PostureSearchModel.second
private var searchBarView = MGSearchView(backgroundColor: PostureResourcesService.Colors.gray50)

private var searchModel = PostureSearchModel(searchResultData: [])

private var searchBarView = MGSearchView()

private var postureSearchTableView = UITableView().then {
$0.showsVerticalScrollIndicator = false
$0.showsHorizontalScrollIndicator = false
$0.backgroundColor = .white
$0.separatorStyle = .none

$0.register(PostureSearchTableViewCell.self, forCellReuseIdentifier: PostureSearchTableViewCell.identifier)
}

public override func attribute() {
super.attribute()

postureSearchTableView.dataSource = self
postureSearchTableView.delegate = self
}

override public func layout() {
[searchBarView, postureSearchTableView].forEach { view.addSubview($0) }

super.layout()

view.addSubviews([searchBarView, postureSearchTableView])

searchBarView.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide)
$0.leading.trailing.equalToSuperview().inset(20.0)
$0.height.equalTo(40.0)
}

postureSearchTableView.snp.makeConstraints {
$0.top.equalTo(searchBarView.snp.bottom)
$0.leading.trailing.equalToSuperview()
$0.width.equalToSuperview()
$0.height.equalToSuperview()
$0.bottom.equalToSuperview()
}
}

public override func bindViewModel() {
super.bindViewModel()

let useCase = DefaultPostureUseCase(repository: PostureRepository(networkService: PostureService()))

viewModel = PostureSearchViewModel(useCase: useCase)

let input = PostureSearchViewModel.Input(
getSearchData: Observable.just(()).asDriver(onErrorDriveWith: .never())
)

let output = viewModel.transform(input, action: { optput in
optput.searchData
.subscribe(onNext: { searchData in
MGLogger.debug("searchData: \(searchData)")
self.searchModel = searchData
}).disposed(by: disposeBag)
}
)
}
}

extension PostureSearchViewController: UITableViewDelegate {
Expand All @@ -60,17 +89,14 @@ extension PostureSearchViewController: UITableViewDelegate {

extension PostureSearchViewController: UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
searchModel.data.count
searchModel.searchResultData.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: PostureSearchTableViewCell.identifier, for: indexPath) as? PostureSearchTableViewCell

let exercise = searchModel.data[indexPath.row]
cell?.setup(image: exercise.image, name: exercise.exerciseName, part: exercise.exercisePart)
let model = searchModel.searchResultData[indexPath.row]
cell?.setup(with: model)
cell?.selectionStyle = .none
return cell ?? UITableViewCell()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ public class PostureBackViewController: BaseViewController<PostureBackViewModel>
let output = viewModel.transform(input, action: { output in
output.backData
.subscribe(onNext: { backData in
MGLogger.debug("Chest Data: \(backData)")
MGLogger.debug("backData: \(backData)")
self.backData = backData
self.backExerciesData = backData.allExerciseData
}).disposed(by: disposeBag)

output.backModelState
.subscribe(onNext: { chestModelState in
MGLogger.debug("Chest Model State: \(chestModelState)")
switch chestModelState {
.subscribe(onNext: { backModelState in
MGLogger.debug("backModelState: \(backModelState)")
switch backModelState {
case .all:
self.backExerciesData = self.backData.allExerciseData
case .body:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,51 @@ import RxCocoa
import RxSwift

import Core
import Domain
import MGLogger

public class PostureSearchViewModel: BaseViewModel {
public func transform(_ input: Input, action: (Output) -> Void) -> Output {
return Output()
}

public struct Input {
private let disposeBag = DisposeBag()

private let useCase: PostureUseCase

public struct Input {
let getSearchData: Driver<Void>
}

public struct Output {
var searchData: Observable<PostureSearchModel>
}

private let searchDataSubject = PublishSubject<PostureSearchModel>()

public init(useCase: PostureUseCase) {
self.useCase = useCase
}

public init() {
public func transform(_ input: Input, action: (Output) -> Void) -> Output {
let output = Output(
searchData:
searchDataSubject.asObservable()
)

action(output)

self.bindOutput(output: output)

input.getSearchData
.drive(onNext: { [weak self] _ in
self?.useCase.getSearchData()
}).disposed(by: disposeBag)

return output
}
}

private func bindOutput(output: Output) {
useCase.searchData
.subscribe(onNext: { searchData in
self.searchDataSubject.onNext(searchData)
}).disposed(by: disposeBag)
}
}

This file was deleted.

Loading

0 comments on commit 4f15b1c

Please sign in to comment.