Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class CourseStorageVC: UIViewController {
.addPagedView(pagedView: [privateCourseListView, scrapCourseListView])

private var deleteCourseButton = CustomButton(title: "삭제하기").then {
$0.isHidden = true
$0.isEnabled = false
}

Expand All @@ -49,12 +48,21 @@ final class CourseStorageVC: UIViewController {
self.setDelegate()
self.setDeleteButton()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard UserManager.shared.userType != .visitor else { return }
self.getPrivateCourseList()
self.getScrapCourseList()
}

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

if privateCourseListView.isEditMode {
self.finishEditMode(withDuration: 0)
}
}
}

// MARK: - Methods
Expand All @@ -63,7 +71,6 @@ extension CourseStorageVC {
private func setPrivateCourseData(courseList: [PrivateCourse]) {
self.privateCourseList = courseList
self.privateCourseListView.setData(courseList: courseList)
self.deleteCourseButton.isHidden = true
self.hideTabBar(wantsToHide: false)
}

Expand All @@ -77,6 +84,12 @@ extension CourseStorageVC {
}

private func bindUI() {
viewPager.$selectedTabIndex.sink { [weak self] selectedTabIndex in
guard let self = self else { return }
print(selectedTabIndex)
self.deleteCourseButton.isHidden = (selectedTabIndex != 0)
}.store(in: cancelBag)

privateCourseListView.courseDrawButtonTapped.sink { [weak self] in
guard let self = self else { return }
self.tabBarController?.selectedIndex = 0
Expand Down Expand Up @@ -111,27 +124,43 @@ extension CourseStorageVC {
privateCourseListView.delegate = self
}

private func showHiddenViews(withDuration: TimeInterval = 0) {
private func hideTabBarWithAnimation(duration: TimeInterval = 0.7) {
if let frame = tabBarController?.tabBar.frame {
let factor: CGFloat = -1
let factor: CGFloat = 1
let y = frame.origin.y + (frame.size.height * factor)
UIView.animate(withDuration: 0.7, animations: {
UIView.animate(withDuration: duration, animations: {
self.tabBarController?.tabBar.frame = CGRect(x: frame.origin.x, y: y, width: frame.width, height: frame.height)
})
}
UIView.animate(withDuration: withDuration) {
self.deleteCourseButton.transform = CGAffineTransform(translationX: 0, y: 34)
}
}

private func hideHiddenViews(withDuration: TimeInterval = 0) {
private func showTabBarWithAnimation(duration: TimeInterval = 0.7) {
if let frame = tabBarController?.tabBar.frame {
let factor: CGFloat = 1
let factor: CGFloat = -1
let y = frame.origin.y + (frame.size.height * factor)
UIView.animate(withDuration: 0.7, animations: {
UIView.animate(withDuration: duration, animations: {
self.tabBarController?.tabBar.frame = CGRect(x: frame.origin.x, y: y, width: frame.width, height: frame.height)
})
}
}

private func finishEditMode(withDuration: TimeInterval = 0) {
self.privateCourseListView.isEditMode = false

showTabBarWithAnimation(duration: withDuration)
self.deleteCourseButton.setEnabled(false)
self.deleteCourseButton.setTitle(title: "삭제하기")

UIView.animate(withDuration: withDuration) {
self.deleteCourseButton.transform = CGAffineTransform(translationX: 0, y: 34)
}
}

private func startEditMode(withDuration: TimeInterval = 0) {
self.privateCourseListView.isEditMode = true

hideTabBarWithAnimation(duration: withDuration)

view.bringSubviewToFront(deleteCourseButton)
UIView.animate(withDuration: withDuration) {
self.deleteCourseButton.transform = CGAffineTransform(translationX: 0, y: -34)
Expand All @@ -143,18 +172,24 @@ extension CourseStorageVC {

extension CourseStorageVC {
@objc func deleteCourseButtonDidTap(_sender: UIButton) {
guard let selectedList = privateCourseListView.courseListCollectionView.indexPathsForSelectedItems else { return }
guard let selectedList = privateCourseListView.courseListCollectionView.indexPathsForSelectedItems else {
return
}

var deleteToCourseId = [Int]()

for indexPath in selectedList {
let publicCourse = privateCourseList[indexPath.item]
deleteToCourseId.append(publicCourse.id)
let privateCourse = privateCourseList[indexPath.item]
deleteToCourseId.append(privateCourse.id)
}

let deleteAlertVC = RNAlertVC(description: "삭제하시겠습니까?")
deleteAlertVC.modalPresentationStyle = .overFullScreen
deleteAlertVC.rightButtonTapAction = {
deleteAlertVC.dismiss(animated: false)
self.deleteCourse(courseIdList: deleteToCourseId)
}

self.present(deleteAlertVC, animated: false)
}
}
Expand All @@ -165,6 +200,7 @@ extension CourseStorageVC {
private func setUI() {
view.backgroundColor = .w1
}

private func setLayout() {
view.addSubviews(naviBar)

Expand All @@ -184,8 +220,9 @@ extension CourseStorageVC {
make.top.equalTo(naviBar.snp.bottom)
make.leading.bottom.trailing.equalTo(view.safeAreaLayoutGuide)
}

deleteCourseButton.snp.makeConstraints { make in
make.bottom.equalToSuperview().inset(34)
make.top.equalTo(view.safeAreaLayoutGuide.snp.bottom)
make.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(44)
}
Expand All @@ -205,36 +242,24 @@ extension CourseStorageVC: ScrapCourseListViewDelegate {
extension CourseStorageVC: PrivateCourseListViewDelegate {

func courseListEditButtonTapped() {
if privateCourseListView.isEditMode == false {
self.deleteCourseButton.isHidden = false
self.deleteCourseButton.isEnabled = false
self.deleteCourseButton.setTitle(title: "삭제하기")
hideHiddenViews(withDuration: 0.7)
}
if privateCourseListView.isEditMode == true {
self.hideTabBar(wantsToHide: false)
self.deleteCourseButton.isHidden = false
showHiddenViews(withDuration: 0.7)
}
privateCourseListView.isEditMode ? startEditMode(withDuration: 0.7) : finishEditMode(withDuration: 0.7)
}

func selectCellDidTapped() {
guard let selectedCells = privateCourseListView.courseListCollectionView.indexPathsForSelectedItems else { return }

let countSelectCells = selectedCells.count

if privateCourseListView.isEditMode == true {
if privateCourseListView.isEditMode == false {
self.deleteCourseButton.isEnabled = false
self.deleteCourseButton.setTitle(title: "삭제하기")
}
self.deleteCourseButton.setTitle(title: "삭제하기(\(countSelectCells))")
self.deleteCourseButton.isEnabled = false
self.deleteCourseButton.setEnabled(true)
}
if selectedCells.count == 0 {
self.deleteCourseButton.isEnabled = false
}

self.deleteCourseButton.setEnabled(countSelectCells != 0)
}
}

// MARK: - Network

extension CourseStorageVC {
private func getPrivateCourseList() {
LoadingIndicator.showLoading()
Expand Down Expand Up @@ -319,15 +344,15 @@ extension CourseStorageVC {
courseProvider.request(.deleteCourse(courseIdList: courseIdList)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
self.privateCourseListView.isEditMode = false
switch response {
case .success(let result):
print("리절트", result)
let status = result.statusCode
if 200..<300 ~= status {
print("삭제 성공")
self.getPrivateCourseList()
self.showHiddenViews(withDuration: 0.7)

self.finishEditMode(withDuration: 0.7)
}
if status >= 400 {
print("400 error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ final class PrivateCourseListView: UIView {

weak var courseStorageVC: UIViewController?

var isEditMode: Bool = false
var isEditMode: Bool = false {
didSet {
isEditMode ? startEditMode() : finishEditMode()
}
}

final let collectionViewInset = UIEdgeInsets(top: 28, left: 16, bottom: 28, right: 16)
final let itemSpacing: CGFloat = 10
Expand Down Expand Up @@ -114,21 +118,28 @@ extension PrivateCourseListView {

extension PrivateCourseListView {
@objc func editButtonDidTap() {
if isEditMode {
self.totalNumOfRecordlabel.text = "총 코스 \(self.courseList.count)개"
self.editButton.setTitle("편집", for: .normal)
self.delegate?.courseListEditButtonTapped()
self.courseListCollectionView.reloadData()
isEditMode = false
} else {
self.totalNumOfRecordlabel.text = "코스 선택"
self.delegate?.courseListEditButtonTapped()
self.editButton.setTitle("취소", for: .normal)
self.courseListCollectionView.reloadData()
isEditMode = true
}
isEditMode.toggle()

self.delegate?.courseListEditButtonTapped()
}

private func startEditMode() {
self.totalNumOfRecordlabel.text = "코스 선택"
self.editButton.setTitle("취소", for: .normal)
}

private func finishEditMode() {
self.totalNumOfRecordlabel.text = "총 코스 \(self.courseList.count)개"
self.editButton.setTitle("편집", for: .normal)
self.deselectAllItems()
}

private func deselectAllItems() {
guard let selectedItems = courseListCollectionView.indexPathsForSelectedItems else { return }
for indexPath in selectedItems { courseListCollectionView.deselectItem(at: indexPath, animated: false) }
}
}

// MARK: - UI & Layout

extension PrivateCourseListView {
Expand Down Expand Up @@ -189,41 +200,39 @@ extension PrivateCourseListView: UICollectionViewDelegate, UICollectionViewDataS
} else {
cell.selectCell(didSelect: false)
}
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CourseListCVC.className, for: indexPath)
as? CourseListCVC else { return UICollectionViewCell() }
cell.setCellType(type: .title)

let model = courseList[indexPath.item]
let cellTitle = "\(model.departure.region) \(model.departure.city)"
cell.setData(imageURL: model.image, title: cellTitle, location: nil, didLike: nil)
return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard collectionView.cellForItem(at: indexPath) is CourseListCVC else { return }
guard let cell = collectionView.cellForItem(at: indexPath) as? CourseListCVC else { return }
if isEditMode {
cell.selectCell(didSelect: true)
delegate?.selectCellDidTapped()
} else {
collectionView.deselectItem(at: indexPath, animated: true)
cellDidTapped.send(indexPath.item)
delegate?.selectCellDidTapped()
}
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
guard collectionView.cellForItem(at: indexPath) is CourseListCVC else { return }
guard let selectedCells = collectionView.indexPathsForSelectedItems else {
return }
return
}

guard let cell = collectionView.cellForItem(at: indexPath) as? CourseListCVC else { return }

if isEditMode {
let countSelectCells = selectedCells.count
delegate?.selectCellDidTapped()
cell.selectCell(didSelect: false)
} else {
collectionView.deselectItem(at: indexPath, animated: true)
delegate?.selectCellDidTapped()
}

delegate?.selectCellDidTapped()
}
}

// MARK: - UICollectionViewDelegateFlowLayout

extension PrivateCourseListView: UICollectionViewDelegateFlowLayout {
Expand Down