Skip to content

Commit

Permalink
refactor: 리뷰 피드백 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
k2645 committed Nov 20, 2024
1 parent ee6f6c2 commit 2a6021a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import MHCore
import UIKit
import Photos
import Combine

final class CustomAlbumViewController: UIViewController {
// MARK: - Properties
// MARK: - UI Components
private lazy var albumCollectionView: UICollectionView = {
let flowLayout = UICollectionViewFlowLayout()
let cellSize = (self.view.bounds.inset(by: self.view.safeAreaInsets).width - 6) / 3
Expand All @@ -16,6 +17,8 @@ final class CustomAlbumViewController: UIViewController {

return collectionView
}()

// MARK: - Properties
private let viewModel: CustomAlbumViewModel
private let input = PassthroughSubject<CustomAlbumViewModel.Input, Never>()
private var cancellables = Set<AnyCancellable>()
Expand All @@ -41,43 +44,17 @@ final class CustomAlbumViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

bind()
setup()
configureConstraints()
configureNavigationBar()
bind()
input.send(.viewDidLoad)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

configureNavagationBar()
}

// MARK: - Binding
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .fetchAssets:
self?.albumCollectionView.reloadData()
case .changedAssets(let changes):
self?.albumCollectionView.performBatchUpdates {
if let inserted = changes.insertedIndexes, !inserted.isEmpty {
self?.albumCollectionView.insertItems(
at: inserted.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
if let removed = changes.removedIndexes, !removed.isEmpty {
self?.albumCollectionView.deleteItems(
at: removed.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
}
}
}
.store(in: &cancellables)
configureNavigationAppearance()
}

// MARK: - Setup & Configure
Expand All @@ -97,26 +74,12 @@ final class CustomAlbumViewController: UIViewController {
albumCollectionView.fillSuperview()
}

private func configureNavagationBar() {
private func configureNavigationBar() {
// TODO: - 추후 삭제 필요
self.navigationController?.navigationBar.isHidden = false

// Navigation Bar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .baseBackground
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.ownglyphBerry(size: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.isHidden = false
navigationItem.title = "사진 선택"
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.ownglyphBerry(size: 17),
NSAttributedString.Key.foregroundColor: UIColor.mhTitle]

// TODO: - 추후 Convenience 생성자로 수정 필요
// Left Bar BarButton
let closeAction = UIAction { [weak self] _ in
guard let self else { return }
Expand All @@ -131,6 +94,46 @@ final class CustomAlbumViewController: UIViewController {
navigationItem.leftBarButtonItem = leftBarButton
}

private func configureNavigationAppearance() {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .baseBackground
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.ownglyphBerry(size: 17),
NSAttributedString.Key.foregroundColor: UIColor.mhTitle
]
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
}

// MARK: - Binding
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .fetchAssets:
self?.albumCollectionView.reloadData()
case .changedAssets(let changes):
self?.albumCollectionView.performBatchUpdates {
if let inserted = changes.insertedIndexes, !inserted.isEmpty {
self?.albumCollectionView.insertItems(
at: inserted.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
if let removed = changes.removedIndexes, !removed.isEmpty {
self?.albumCollectionView.deleteItems(
at: removed.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
}
}
}
.store(in: &cancellables)
}

// MARK: - Camera
private func checkCameraAuthorization() {
let authorization = AVCaptureDevice.authorizationStatus(for: .video)
Expand All @@ -144,16 +147,16 @@ final class CustomAlbumViewController: UIViewController {
}
} else {
// TODO: 카메라 권한 설정 페이지로 이동
print("카메라 권한 거부")
MHLogger.info("카메라 권한 거부")
}
}
case .authorized:
openCamera()
case .restricted, .denied:
// TODO: 카메라 권한 설정 페이지로 이동
print("카메라 권한 거부")
@unknown default:
fatalError("Unknown case")
MHLogger.info("카메라 권한 거부")
default:
MHLogger.error(authorization)
}
}

Expand Down Expand Up @@ -242,7 +245,7 @@ extension CustomAlbumViewController: UIImagePickerControllerDelegate, UINavigati
extension CustomAlbumViewController: PHPhotoLibraryChangeObserver {
nonisolated func photoLibraryDidChange(_ changeInstance: PHChange) {
Task { @MainActor in
input.send(.photoDidChanged(changeInstance))
input.send(.photoDidChanged(to: changeInstance))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import Combine
final class CustomAlbumViewModel: ViewModelType {
enum Input {
case viewDidLoad
case photoDidChanged(_ changeInstance: PHChange)
case photoDidChanged(to: PHChange)
}

enum Output {
case fetchAssets
case changedAssets(_ changes: PHFetchResultChangeDetails<PHAsset>)
case changedAssets(with: PHFetchResultChangeDetails<PHAsset>)
}

private let output = PassthroughSubject<Output, Never>()
private var cancellables = Set<AnyCancellable>()
private(set) var photoAsset: PHFetchResult<PHAsset>?

func transform(input: AnyPublisher<Input, Never>) -> AnyPublisher<Output, Never> {
input.sink { [weak self] events in
switch events {
input.sink { [weak self] event in
switch event {
case .viewDidLoad:
self?.fetchPhotoAssets()
case .photoDidChanged(let changeInstance):
Expand All @@ -31,7 +31,6 @@ final class CustomAlbumViewModel: ViewModelType {
return output.eraseToAnyPublisher()
}


private func fetchPhotoAssets() {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
Expand All @@ -48,7 +47,7 @@ final class CustomAlbumViewModel: ViewModelType {
self.photoAsset = changes.fetchResultAfterChanges

if changes.hasIncrementalChanges {
output.send(.changedAssets(changes))
output.send(.changedAssets(with: changes))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

final class EditPhotoViewController: UIViewController {
// MARK: - Properties
// MARK: - UI Components
private let clearView = UIView.dimmedView(opacity: 0)
private let dimmedView1 = UIView.dimmedView(opacity: 0.5)
private let dimmedView2 = UIView.dimmedView(opacity: 0.5)
Expand Down Expand Up @@ -75,6 +75,7 @@ final class EditPhotoViewController: UIViewController {
super.viewDidLoad()

setup()
configureNavigationBar()
configureAddSubView()
configureConstraints()
configureButtonAction()
Expand All @@ -83,7 +84,7 @@ final class EditPhotoViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

configureNavagationBar()
configureNavigationAppearance()
}

// MARK: - Setup
Expand All @@ -106,20 +107,10 @@ final class EditPhotoViewController: UIViewController {
}

// MARK: - Configure Navigation
private func configureNavagationBar() {
// Navigation Bar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .black
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.ownglyphBerry(size: 17),
NSAttributedString.Key.foregroundColor: UIColor.white
]
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
private func configureNavigationBar() {
navigationItem.title = "사진 편집"

// TODO: - 추후 Convenience 생성자로 수정 필요
// Left Bar BarButton
let closeAction = UIAction { [weak self] _ in
guard let self else { return }
Expand Down Expand Up @@ -150,6 +141,19 @@ final class EditPhotoViewController: UIViewController {
navigationItem.rightBarButtonItem = rightBarButton
}

private func configureNavigationAppearance() {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .black
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.ownglyphBerry(size: 17),
NSAttributedString.Key.foregroundColor: UIColor.white
]
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
}

// MARK: - Add SubView & Constraints
private func configureAddSubView() {
editButtonStackView.addArrangedSubview(rotateButton)
Expand Down

0 comments on commit 2a6021a

Please sign in to comment.