Skip to content

Commit

Permalink
♻️#294: 마이페이지 코드 정리 및 ViewWillAppearEvent 바인딩 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
thoonk committed Jul 10, 2024
1 parent 2c40d34 commit 3912cb5
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 70 deletions.
8 changes: 8 additions & 0 deletions StreetDrop/StreetDrop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
6A26BF322B33D1E40007B6B7 /* FetchingSingleMusicUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A26BF312B33D1E40007B6B7 /* FetchingSingleMusicUseCase.swift */; };
6A26BF342B33D2210007B6B7 /* DefaultFetchingSingleMusicUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A26BF332B33D2210007B6B7 /* DefaultFetchingSingleMusicUseCase.swift */; };
6A3AFB582B8DB399003BD144 /* GADUnitID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A3AFB572B8DB399003BD144 /* GADUnitID.swift */; };
6A51EC3C2C3E52FE00DEF6F3 /* UIViewController+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A51EC3B2C3E52FE00DEF6F3 /* UIViewController+Rx.swift */; };
6A51EC3E2C3E536000DEF6F3 /* Map+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A51EC3D2C3E536000DEF6F3 /* Map+Rx.swift */; };
6A7D73D92BB11A0E009340E3 /* LevelUpGuideView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A7D73D82BB11A0E009340E3 /* LevelUpGuideView.swift */; };
6A7D73DD2BB14015009340E3 /* GradientProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A7D73DC2BB14015009340E3 /* GradientProgressBar.swift */; };
6A7D73DF2BB15826009340E3 /* UIView+RoundCorners.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A7D73DE2BB15826009340E3 /* UIView+RoundCorners.swift */; };
Expand Down Expand Up @@ -410,6 +412,8 @@
6A26BF312B33D1E40007B6B7 /* FetchingSingleMusicUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchingSingleMusicUseCase.swift; sourceTree = "<group>"; };
6A26BF332B33D2210007B6B7 /* DefaultFetchingSingleMusicUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultFetchingSingleMusicUseCase.swift; sourceTree = "<group>"; };
6A3AFB572B8DB399003BD144 /* GADUnitID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GADUnitID.swift; sourceTree = "<group>"; };
6A51EC3B2C3E52FE00DEF6F3 /* UIViewController+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Rx.swift"; sourceTree = "<group>"; };
6A51EC3D2C3E536000DEF6F3 /* Map+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Map+Rx.swift"; sourceTree = "<group>"; };
6A7D73D82BB11A0E009340E3 /* LevelUpGuideView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LevelUpGuideView.swift; sourceTree = "<group>"; };
6A7D73DC2BB14015009340E3 /* GradientProgressBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientProgressBar.swift; sourceTree = "<group>"; };
6A7D73DE2BB15826009340E3 /* UIView+RoundCorners.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+RoundCorners.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1475,6 +1479,8 @@
6A1386AA2B4F8A5000E49BB5 /* String+Base64.swift */,
C49EDACD2BBD7EFE0025DB55 /* GradientLabel.swift */,
F4AA84DE2C1F3B0200CADB1A /* Array+Extension.swift */,
6A51EC3B2C3E52FE00DEF6F3 /* UIViewController+Rx.swift */,
6A51EC3D2C3E536000DEF6F3 /* Map+Rx.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1954,6 +1960,7 @@
41396DA42A51B61000B69341 /* EditCommentViewModel.swift in Sources */,
049F50222A122C8A001528CB /* DropMusicRequestDTO+Mapping.swift in Sources */,
C49EDACC2BBD75480025DB55 /* CongratulationsLevelUpPopUpViewController.swift in Sources */,
6A51EC3C2C3E52FE00DEF6F3 /* UIViewController+Rx.swift in Sources */,
F4C996AF2C1EF6C100FF7B9A /* Notice.swift in Sources */,
41A9BEB92A4DCA4A00F3605C /* ClaimCommentRepository.swift in Sources */,
C45BF3A32A1D179C00CEDE74 /* RecentMusicQueriesStorage.swift in Sources */,
Expand Down Expand Up @@ -2002,6 +2009,7 @@
188D2C7B2A1E448C0088F49C /* DroppedMusicWithinAreaCollectionViewCell.swift in Sources */,
41396D9B2A4F1C5600B69341 /* OptionModalViewController.swift in Sources */,
C47C1D1F2A643C07007317EA /* SplashViewController.swift in Sources */,
6A51EC3E2C3E536000DEF6F3 /* Map+Rx.swift in Sources */,
C449807C2BC3B07E0001E6C3 /* PostingPopUpUserReadingUseCase.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class MyPageViewController: UIViewController, Toastable, Alertable {
private let selectedMusicEvent = PublishRelay<Int>()
private let disposeBag = DisposeBag()

// MARK: - Init

init(viewModel: MyPageViewModel = MyPageViewModel()) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
Expand All @@ -47,12 +49,8 @@ final class MyPageViewController: UIViewController, Toastable, Alertable {
self.bindViewModel()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewWillAppearEvent.accept(Void())
}

// MARK: - UI

private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.backgroundColor = UIColor.gray900
Expand Down Expand Up @@ -214,6 +212,8 @@ final class MyPageViewController: UIViewController, Toastable, Alertable {
private lazy var bottomDimmedView: UIView = self.createDimmedView(isFromTop: false)
}

// MARK: - UI Methods

private extension MyPageViewController {
// MARK: - UI

Expand Down Expand Up @@ -474,9 +474,62 @@ private extension MyPageViewController {
return newStackView
}

// MARK: - Action Binding
func updateLevelupGuideViewConstraints(by isShow: Bool) {
if isShow == false {
if levelUpGuideView.isDescendant(of: view) {
levelUpGuideView.removeFromSuperview()

tapListStackView.snp.remakeConstraints { make in
make.top.equalTo(levelImageView.snp.bottom).offset(24)
make.leading.trailing.equalToSuperview().inset(24)
}
}
}
}

private func bindTapButtonAction(dropTapButton: UIButton, likeTapButton: UIButton) {
func updateTapListUI(by type: MyMusicType) {
switch type {
case .drop:
tapListStackView.removeArrangedSubview(self.likeCountLabel)
likeCountLabel.isHidden = true
tapListStackView.addArrangedSubview(self.dropCountLabel)
dropCountLabel.isHidden = false

dropTapButton.setTitleColor(.white, for: .normal)
dropTapButton.setTitleColor(.lightGray, for: .highlighted)
dropTapButton.setTitleColor(.white, for: .normal)
dropTapButton.setTitleColor(.lightGray, for: .highlighted)
likeTapButton.setTitleColor(UIColor.gray400, for: .normal)
likeTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
likeTapButton.setTitleColor(UIColor.gray400, for: .normal)
likeTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)

case.like:
tapListStackView.removeArrangedSubview(self.dropCountLabel)
dropCountLabel.isHidden = true
tapListStackView.addArrangedSubview(self.likeCountLabel)
likeCountLabel.isHidden = false

dropTapButton.setTitleColor(UIColor.gray400, for: .normal)
dropTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
dropTapButton.setTitleColor(UIColor.gray400, for: .normal)
dropTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
likeTapButton.setTitleColor(.white, for: .normal)
likeTapButton.setTitleColor(.lightGray, for: .highlighted)
likeTapButton.setTitleColor(.white, for: .normal)
likeTapButton.setTitleColor(.lightGray, for: .highlighted)
}

if scrollView.contentOffset.y > 343 {
scrollView.setContentOffset(CGPoint(x: 0, y: 343), animated: true)
}
}
}

// MARK: - Binding Methods

private extension MyPageViewController {
func bindTapButtonAction(dropTapButton: UIButton, likeTapButton: UIButton) {
dropTapButton.rx.tap
.bind(with: self) { owner, _ in
owner.listTypeTapEvent.accept(.drop)
Expand All @@ -492,7 +545,12 @@ private extension MyPageViewController {
.disposed(by: disposeBag)
}

private func bindAction() {
func bindAction() {
rx.viewWillAppear
.mapVoid()
.bind(to: viewWillAppearEvent)
.disposed(by: disposeBag)

bindTapButtonAction(dropTapButton: self.dropTapButton, likeTapButton: self.likeTapButton)

scrollToTopButton.rx.tap
Expand Down Expand Up @@ -544,10 +602,8 @@ private extension MyPageViewController {
}
.disposed(by: disposeBag)
}

// MARK: - Data Binding

private func bindViewModel() {

func bindViewModel() {
let input = MyPageViewModel.Input(
viewWillAppearEvent: viewWillAppearEvent.asObservable(),
listTypeTapEvent: listTypeTapEvent.asObservable(),
Expand Down Expand Up @@ -578,7 +634,6 @@ private extension MyPageViewController {

output.myMusicsSections
.bind(with: self) { owner, sections in
owner.updateCollectionViewHeight()
owner.displayMusicList(sections)
}
.disposed(by: disposeBag)
Expand Down Expand Up @@ -651,9 +706,9 @@ private extension MyPageViewController {
}
}

// MARK: - ScrollView Delegate
// MARK: - ScrollView Methods

extension MyPageViewController {
private extension MyPageViewController {
func handleScrollEvent(contentOffset: CGPoint, type: MyMusicType) {
// 맨 위로 스크롤하기 버튼 활성화
let offsetY = contentOffset.y
Expand Down Expand Up @@ -697,7 +752,7 @@ extension MyPageViewController {
}
}

// MARK: - Private Methods
// MARK: - UICollectionView Methods

private extension MyPageViewController {
func configureMusicDataSource() -> MusicDataSource {
Expand Down Expand Up @@ -803,55 +858,4 @@ private extension MyPageViewController {
}
}
}

func updateLevelupGuideViewConstraints(by isShow: Bool) {
if isShow == false {
if levelUpGuideView.isDescendant(of: view) {
levelUpGuideView.removeFromSuperview()

tapListStackView.snp.remakeConstraints { make in
make.top.equalTo(levelImageView.snp.bottom).offset(24)
make.leading.trailing.equalToSuperview().inset(24)
}
}
}
}

func updateTapListUI(by type: MyMusicType) {
switch type {
case .drop:
tapListStackView.removeArrangedSubview(self.likeCountLabel)
likeCountLabel.isHidden = true
tapListStackView.addArrangedSubview(self.dropCountLabel)
dropCountLabel.isHidden = false

dropTapButton.setTitleColor(.white, for: .normal)
dropTapButton.setTitleColor(.lightGray, for: .highlighted)
dropTapButton.setTitleColor(.white, for: .normal)
dropTapButton.setTitleColor(.lightGray, for: .highlighted)
likeTapButton.setTitleColor(UIColor.gray400, for: .normal)
likeTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
likeTapButton.setTitleColor(UIColor.gray400, for: .normal)
likeTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)

case.like:
tapListStackView.removeArrangedSubview(self.dropCountLabel)
dropCountLabel.isHidden = true
tapListStackView.addArrangedSubview(self.likeCountLabel)
likeCountLabel.isHidden = false

dropTapButton.setTitleColor(UIColor.gray400, for: .normal)
dropTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
dropTapButton.setTitleColor(UIColor.gray400, for: .normal)
dropTapButton.setTitleColor(UIColor(hexString: "#43464B"), for: .highlighted)
likeTapButton.setTitleColor(.white, for: .normal)
likeTapButton.setTitleColor(.lightGray, for: .highlighted)
likeTapButton.setTitleColor(.white, for: .normal)
likeTapButton.setTitleColor(.lightGray, for: .highlighted)
}

if scrollView.contentOffset.y > 343 {
scrollView.setContentOffset(CGPoint(x: 0, y: 343), animated: true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ final class MyPageViewModel {
networkManager: NetworkManager()
)
)

private var myDropMusicSections: [MyMusicsSectionType] = .init()
private var myLikeMusicSections: [MyMusicsSectionType] = .init()
}

extension MyPageViewModel: ViewModel {
Expand Down
20 changes: 20 additions & 0 deletions StreetDrop/StreetDrop/Util/Extensions/Map+Rx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Map+Rx.swift
// StreetDrop
//
// Created by thoonk on 7/10/24.
//

import Foundation

import RxSwift

extension ObservableType {
func map<T>(_ element: T) -> Observable<T> {
self.map({ _ in element })
}

func mapVoid() -> Observable<Void> {
self.map(Void())
}
}
73 changes: 73 additions & 0 deletions StreetDrop/StreetDrop/Util/Extensions/UIViewController+Rx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// UIViewController+Rx.swift
// StreetDrop
//
// Created by thoonk on 7/10/24.
//

import Foundation
import UIKit

import RxSwift
import RxCocoa

extension Reactive where Base: UIViewController {
var viewDidLoad: ControlEvent<Void> {
let source = self.methodInvoked(#selector(Base.viewDidLoad)).map { _ in }
return ControlEvent(events: source)
}

var viewWillAppear: ControlEvent<Bool> {
let source = self.methodInvoked(#selector(Base.viewWillAppear)).map { $0.first as? Bool ?? false }
return ControlEvent(events: source)
}
var viewDidAppear: ControlEvent<Bool> {
let source = self.methodInvoked(#selector(Base.viewDidAppear)).map { $0.first as? Bool ?? false }
return ControlEvent(events: source)
}

var viewWillDisappear: ControlEvent<Bool> {
let source = self.methodInvoked(#selector(Base.viewWillDisappear)).map { $0.first as? Bool ?? false }
return ControlEvent(events: source)
}
var viewDidDisappear: ControlEvent<Bool> {
let source = self.methodInvoked(#selector(Base.viewDidDisappear)).map { $0.first as? Bool ?? false }
return ControlEvent(events: source)
}

var viewWillLayoutSubviews: ControlEvent<Void> {
let source = self.methodInvoked(#selector(Base.viewWillLayoutSubviews)).map { _ in }
return ControlEvent(events: source)
}
var viewDidLayoutSubviews: ControlEvent<Void> {
let source = self.methodInvoked(#selector(Base.viewDidLayoutSubviews)).map { _ in }
return ControlEvent(events: source)
}

var willMoveToParentViewController: ControlEvent<UIViewController?> {
let source = self.methodInvoked(#selector(Base.willMove)).map { $0.first as? UIViewController }
return ControlEvent(events: source)
}
var didMoveToParentViewController: ControlEvent<UIViewController?> {
let source = self.methodInvoked(#selector(Base.didMove)).map { $0.first as? UIViewController }
return ControlEvent(events: source)
}

var didReceiveMemoryWarning: ControlEvent<Void> {
let source = self.methodInvoked(#selector(Base.didReceiveMemoryWarning)).map { _ in }
return ControlEvent(events: source)
}

/// Rx observable, triggered when the ViewController appearance state changes (true if the View is being displayed, false otherwise)
var isVisible: Observable<Bool> {
let viewDidAppearObservable = self.base.rx.viewDidAppear.map { _ in true }
let viewWillDisappearObservable = self.base.rx.viewWillDisappear.map { _ in false }
return Observable<Bool>.merge(viewDidAppearObservable, viewWillDisappearObservable)
}

/// Rx observable, triggered when the ViewController is being dismissed
var isDismissing: ControlEvent<Bool> {
let source = self.sentMessage(#selector(Base.dismiss)).map { $0.first as? Bool ?? false }
return ControlEvent(events: source)
}
}

0 comments on commit 3912cb5

Please sign in to comment.