Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[리펙토링] BaseViewController 생성 #323

Merged
merged 10 commits into from
Nov 30, 2021
Merged
70 changes: 23 additions & 47 deletions BBus/BBus/Foreground/AlarmSetting/AlarmSettingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,12 @@
import UIKit
import Combine

final class AlarmSettingViewController: UIViewController {

final class AlarmSettingViewController: UIViewController, BaseViewControllerType {
weak var coordinator: AlarmSettingCoordinator?
private lazy var alarmSettingView = AlarmSettingView()
private lazy var customNavigationBar = CustomNavigationBar()
private lazy var refreshButton: ThrottleButton = {
let radius: CGFloat = 25

let button = ThrottleButton()
button.setImage(BBusImage.refresh, for: .normal)
button.layer.cornerRadius = radius
button.tintColor = BBusColor.white
button.backgroundColor = BBusColor.darkGray
button.addTouchUpEventWithThrottle(delay: ThrottleButton.refreshInterval) { [weak self] in
self?.viewModel?.refresh()
}
return button
}()
private let viewModel: AlarmSettingViewModel?
private lazy var alarmSettingView = AlarmSettingView()

private var cancellables: Set<AnyCancellable> = []

init(viewModel: AlarmSettingViewModel) {
Expand All @@ -41,12 +28,9 @@ final class AlarmSettingViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.baseViewDidLoad()

self.configureColor()
self.configureLayout()
self.configureDelegate()

self.binding()
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -63,45 +47,26 @@ final class AlarmSettingViewController: UIViewController {
}

// MARK: - Configure
private func configureLayout() {
let refreshButtonWidthAnchor: CGFloat = 50
let refreshTrailingBottomInterval: CGFloat = -16

self.view.addSubviews(self.customNavigationBar, self.alarmSettingView, self.refreshButton)

NSLayoutConstraint.activate([
self.customNavigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.customNavigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.customNavigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])

func configureLayout() {
self.view.addSubviews(self.alarmSettingView)
NSLayoutConstraint.activate([
self.alarmSettingView.topAnchor.constraint(equalTo: self.customNavigationBar.bottomAnchor),
self.alarmSettingView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: CustomNavigationBar.height),
self.alarmSettingView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
self.alarmSettingView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.alarmSettingView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])

NSLayoutConstraint.activate([
self.refreshButton.widthAnchor.constraint(equalToConstant: refreshButtonWidthAnchor),
self.refreshButton.heightAnchor.constraint(equalToConstant: refreshButtonWidthAnchor),
self.refreshButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: refreshTrailingBottomInterval),
self.refreshButton.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: refreshTrailingBottomInterval)
])
}

private func configureDelegate() {
func configureDelegate() {
self.alarmSettingView.configureDelegate(self)
self.customNavigationBar.configureDelegate(self)
}

private func configureColor() {
self.view.backgroundColor = BBusColor.white
self.customNavigationBar.configureTintColor(color: BBusColor.black)
self.customNavigationBar.configureAlpha(alpha: 1)
self.alarmSettingView.configureColor(color: BBusColor.black)
}

private func binding() {
func bindAll() {
self.bindBusArriveInfos()
self.bindBusStationInfos()
self.bindErrorMessage()
Expand All @@ -127,7 +92,7 @@ final class AlarmSettingViewController: UIViewController {
self?.alarmSettingView.reload()
if let viewModel = self?.viewModel,
let stationName = infos.first?.name {
self?.customNavigationBar.configureTitle(busName: viewModel.busName,
self?.alarmSettingView.configureTitle(busName: viewModel.busName,
stationName: stationName,
routeType: viewModel.routeType)
}
Expand Down Expand Up @@ -160,6 +125,10 @@ final class AlarmSettingViewController: UIViewController {
.store(in: &self.cancellables)
}

func refresh() {
self.viewModel?.refresh()
}

private func alarmSettingAlert(message: String) {
let controller = UIAlertController()
let action = UIAlertAction(title: message, style: .cancel, handler: nil)
Expand Down Expand Up @@ -342,6 +311,13 @@ extension AlarmSettingViewController: BackButtonDelegate {
}
}

// MARK: - Delegate: RefreshButton
extension AlarmSettingViewController: RefreshButtonDelegate {
func buttonTapped() {
self.refresh()
}
}

// MARK: - Delegate: GetOffAlarmButton
extension AlarmSettingViewController: GetOffAlarmButtonDelegate {
func shouldGoToMovingStatusScene(from cell: UITableViewCell) {
Expand Down
34 changes: 33 additions & 1 deletion BBus/BBus/Foreground/AlarmSetting/View/AlarmSettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ final class AlarmSettingView: UIView {

static let tableViewSectionCount = 2
static let tableViewHeaderHeight: CGFloat = 35

private weak var refreshButtonDelegate: RefreshButtonDelegate? {
didSet {
self.refreshButton.addTouchUpEventWithThrottle(delay: ThrottleButton.refreshInterval) { [weak self] in
self?.refreshButtonDelegate?.buttonTapped()
}
}
}

private lazy var alarmTableView: UITableView = {
let tableViewLeftInset: CGFloat = 90
Expand All @@ -29,6 +37,17 @@ final class AlarmSettingView: UIView {
let loader = UIActivityIndicatorView(style: .large)
return loader
}()
private lazy var refreshButton: ThrottleButton = {
let radius: CGFloat = 25

let button = ThrottleButton()
button.setImage(BBusImage.refresh, for: .normal)
button.layer.cornerRadius = radius
button.tintColor = BBusColor.white
button.backgroundColor = BBusColor.darkGray
return button
}()
private lazy var customNavigationBar = CustomNavigationBar()

convenience init() {
self.init(frame: CGRect())
Expand All @@ -54,9 +73,22 @@ final class AlarmSettingView: UIView {
])
}

func configureDelegate(_ delegate: UITableViewDelegate & UITableViewDataSource) {
func configureDelegate(_ delegate: UITableViewDelegate & UITableViewDataSource & BackButtonDelegate & RefreshButtonDelegate) {
self.alarmTableView.delegate = delegate
self.alarmTableView.dataSource = delegate
self.customNavigationBar.configureDelegate(delegate)
self.refreshButtonDelegate = delegate
}

func configureColor(color: UIColor?) {
self.customNavigationBar.configureTintColor(color: color)
self.customNavigationBar.configureAlpha(alpha: 1)
}

func configureTitle(busName: String, stationName: String, routeType: RouteType?) {
self.customNavigationBar.configureTitle(busName: busName,
stationName: stationName,
routeType: routeType)
}

func reload() {
Expand Down
130 changes: 25 additions & 105 deletions BBus/BBus/Foreground/BusRoute/BusRouteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,13 @@
import UIKit
import Combine

final class BusRouteViewController: UIViewController {

final class BusRouteViewController: UIViewController, BaseViewControllerType {
weak var coordinator: BusRouteCoordinator?
private lazy var customNavigationBar = CustomNavigationBar()
private lazy var busRouteView = BusRouteView()
private let viewModel: BusRouteViewModel?
private lazy var busRouteView = BusRouteView()

private var cancellables: Set<AnyCancellable> = []
private var busTags: [BusTagView] = []
private var busIcon: UIImage?

private lazy var refreshButton: UIButton = {
let radius: CGFloat = 25

let button = UIButton()
button.setImage(BBusImage.refresh, for: .normal)
button.layer.cornerRadius = radius
button.tintColor = BBusColor.white
button.backgroundColor = BBusColor.darkGray

button.addAction(UIAction(handler: { [weak self] _ in
self?.viewModel?.refreshBusPos()
}), for: .touchUpInside)
return button
}()

init(viewModel: BusRouteViewModel) {
self.viewModel = viewModel
Expand All @@ -45,132 +28,69 @@ final class BusRouteViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.baseViewDidLoad()

self.busRouteView.startLoader()
self.binding()
self.configureLayout()
self.configureDelegate()
self.configureBaseColor()
}

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

self.viewModel?.configureObserver()
self.viewModel?.refreshBusPos()
}

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

self.viewModel?.cancelObserver()
}

// MARK: - Configure
private func configureLayout() {
let refreshButtonWidthAnchor: CGFloat = 50
let refreshTrailingBottomInterval: CGFloat = -16

self.view.addSubviews(self.busRouteView, self.customNavigationBar, self.refreshButton)
func configureLayout() {
self.view.addSubviews(self.busRouteView)

NSLayoutConstraint.activate([
self.busRouteView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.busRouteView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.busRouteView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.busRouteView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])

NSLayoutConstraint.activate([
self.customNavigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.customNavigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.customNavigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])

self.busRouteView.configureTableViewHeight(count: 20)
NSLayoutConstraint.activate([
self.refreshButton.widthAnchor.constraint(equalToConstant: refreshButtonWidthAnchor),
self.refreshButton.heightAnchor.constraint(equalToConstant: refreshButtonWidthAnchor),
self.refreshButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: refreshTrailingBottomInterval),
self.refreshButton.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: refreshTrailingBottomInterval)
])
}

private func configureDelegate() {
func configureDelegate() {
self.busRouteView.configureDelegate(self)
self.customNavigationBar.configureDelegate(self)
}

private func configureBaseColor() {
self.view.backgroundColor = BBusColor.gray
self.customNavigationBar.configureBackgroundColor(color: BBusColor.gray)
self.customNavigationBar.configureTintColor(color: BBusColor.white)
self.customNavigationBar.configureAlpha(alpha: 0)
self.busRouteView.configureColor(to: BBusColor.gray)
}

private func configureBusColor(type: RouteType) {
let color: UIColor?

switch type {
case .mainLine:
color = BBusColor.bbusTypeBlue
self.busIcon = BBusImage.blueBusIcon
case .broadArea:
color = BBusColor.bbusTypeRed
self.busIcon = BBusImage.redBusIcon
case .customized:
color = BBusColor.bbusTypeGreen
self.busIcon = BBusImage.greenBusIcon
case .circulation:
color = BBusColor.bbusTypeCirculation
self.busIcon = BBusImage.circulationBusIcon
case .lateNight:
color = BBusColor.bbusTypeBlue
self.busIcon = BBusImage.blueBusIcon
case .localLine:
color = BBusColor.bbusTypeGreen
self.busIcon = BBusImage.greenBusIcon
}

self.view.backgroundColor = color
self.customNavigationBar.configureBackgroundColor(color: color)
self.customNavigationBar.configureTintColor(color: BBusColor.white)
self.customNavigationBar.configureAlpha(alpha: 0)
self.busRouteView.configureColor(to: color)
}

private func configureBusTags(buses: [BusPosInfo]) {
self.busTags.forEach { $0.removeFromSuperview() }
self.busTags.removeAll()

buses.forEach { [weak self] bus in
guard let self = self else { return }
let tag = self.busRouteView.createBusTag(location: bus.location,
busIcon: self.busIcon,
busNumber: bus.number,
busCongestion: bus.congestion.toString(),
isLowFloor: bus.islower)
self.busTags.append(tag)
}
func refresh() {
self.viewModel?.refreshBusPos()
}

private func binding() {
func bindAll() {
self.bindLoader()
self.bindBusRouteHeaderResult()
self.bindBusRouteBodyResult()
self.bindBusesPosInfo()
self.bindNetworkError()
}

private func configureBaseColor() {
self.view.backgroundColor = BBusColor.gray
self.busRouteView.configureColor(to: BBusColor.gray)
}

private func bindBusRouteHeaderResult() {
self.viewModel?.$header
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] header in
if let header = header {
self?.customNavigationBar.configureBackButtonTitle(header.busRouteName)
self?.busRouteView.configureBackButtonTitle(title: header.busRouteName)
self?.busRouteView.configureHeaderView(busType: header.routeType.rawValue+"버스",
busNumber: header.busRouteName,
fromStation: header.startStation,
toStation: header.endStation)
self?.configureBusColor(type: header.routeType)
self?.view.backgroundColor = self?.busRouteView.configureBusColor(type: header.routeType)
}
})
.store(in: &self.cancellables)
Expand All @@ -192,7 +112,7 @@ final class BusRouteViewController: UIViewController {
.sink(receiveValue: { [weak self] buses in
guard let viewModel = self?.viewModel else { return }

self?.configureBusTags(buses: buses)
self?.busRouteView.configureBusTags(buses: buses)

if viewModel.stopLoader {
self?.busRouteView.stopLoader()
Expand Down Expand Up @@ -279,10 +199,10 @@ extension BusRouteViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let baseLineContentOffset = BusRouteHeaderView.headerHeight - CustomNavigationBar.height
if scrollView.contentOffset.y >= baseLineContentOffset {
self.customNavigationBar.configureAlpha(alpha: 1)
self.busRouteView.configureNavigationAlpha(alpha: 1)
}
else {
self.customNavigationBar.configureAlpha(alpha: CGFloat(scrollView.contentOffset.y/baseLineContentOffset))
self.busRouteView.configureNavigationAlpha(alpha: CGFloat(scrollView.contentOffset.y/baseLineContentOffset))
}
}

Expand Down
Loading