Skip to content

Commit

Permalink
[Feat] Runnect#134 - 업로드한 코스 UI 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-yeonwoo2 committed May 15, 2023
1 parent 59e9985 commit efef823
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ extension CourseStorageVC: PrivateCourseListViewDelegate {
deleteAlertVC.rightButtonTapAction = {
deleteAlertVC.dismiss(animated: false)
self.deleteCourse(courseIdList: courseId)



}
self.present(deleteAlertVC, animated: false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ final class PrivateCourseListView: UIView {
var courseDrawButtonTapped = PassthroughSubject<Void, Never>()

var cellDidTapped = PassthroughSubject<Int, Never>()
var courseDeleteButtonTapped = PassthroughSubject<Void, Never>()

var courseList = [PrivateCourse]()

Expand Down Expand Up @@ -80,8 +79,8 @@ final class PrivateCourseListView: UIView {
self.setUI()
self.setLayout()
self.setDelegate()
self.register()
self.setAddTarget()
self.register()
self.setDeleteButton()

}
Expand Down Expand Up @@ -136,8 +135,6 @@ extension PrivateCourseListView {
let course = courseList[indexPath.item]
deleteToCourseId.append(course.id)
}

var courseListCollectionView = courseListCollectionView
self.delegate?.deleteCourseButtonTapped(courseId: deleteToCourseId)

}
Expand Down Expand Up @@ -253,14 +250,10 @@ extension PrivateCourseListView: UICollectionViewDelegate, UICollectionViewDataS
let countSelectCells = selectedCells.count
self.deleteCourseButton.setTitle(title: "삭제하기(\(countSelectCells))")
cell.selectCell(didSelect: true)
// self.selectedIndexs = indexPath.item
courseDeleteButtonTapped.send()

} else {
collectionView.deselectItem(at: indexPath, animated: true)
self.deleteCourseButton.setTitle(title: "삭제하기")
cellDidTapped.send(indexPath.item)
// self.selectedIndexs = indexPath.item
self.deleteCourseButton.setEnabled(true)
}
}
Expand All @@ -276,7 +269,6 @@ extension PrivateCourseListView: UICollectionViewDelegate, UICollectionViewDataS
self.deleteCourseButton.isEnabled = true
let countSelectCells = selectedCells.count
self.deleteCourseButton.setTitle(title: "삭제하기(\(countSelectCells))")
// self.selectedIndexs = nil
cell.selectCell(didSelect: false)
} else {
collectionView.deselectItem(at: indexPath, animated: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import Moya
final class UploadedCourseInfoVC: UIViewController {

// MARK: - Properties

private var uploadedCourseProvider = Providers.publicCourseProvider

private var uploadedCourseList = [PublicCourse]()
var deleteToCourseId = [Int]()
var isEditMode: Bool = false

private let courseProvider = Providers.courseProvider

// MARK: - Constants

Expand All @@ -27,6 +31,9 @@ final class UploadedCourseInfoVC: UIViewController {
final let uplodaedCourseCellHeight: CGFloat = 124

// MARK: - UI Components
private let collectionViewLayout = UICollectionViewFlowLayout().then {
$0.scrollDirection = .vertical
}

private lazy var navibar = CustomNavigationBar(self, type: .titleWithLeftButton).setTitle("업로드한 코스")

Expand All @@ -41,6 +48,39 @@ final class UploadedCourseInfoVC: UIViewController {
return collectionView
}()

private let beforeEditTopView = UIView().then{
$0.backgroundColor = .clear
}
private lazy var totalNumOfRecordlabel = UILabel().then {
$0.font = .b6
$0.textColor = .g2
$0.text = "총 기록 0개"
}

private let editButton = UIButton(type: .custom).then {
$0.setTitle("편집", for: .normal)
$0.setTitleColor(.m1, for: .normal)
$0.titleLabel?.font = .b7
$0.layer.borderColor = UIColor.m1.cgColor
$0.layer.borderWidth = 1
$0.layer.cornerRadius = 11
}

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

private lazy var courseListCollectionView = UICollectionView(
frame: .zero,
collectionViewLayout:collectionViewLayout
).then {
$0.backgroundColor = .clear
}

private let emptyView = ListEmptyView(description: "아직 업로드한 코스가 없어요!\n내가 그린 코스를 공유해보세요",
buttonTitle: "코스 업로드하기")

// MARK: - View Life Cycle

override func viewDidLoad() {
Expand All @@ -51,6 +91,8 @@ final class UploadedCourseInfoVC: UIViewController {
register()
setDelegate()
getUploadedCourseInfo()
self.setAddTarget()
self.setDeleteButton()
}
}

Expand All @@ -60,16 +102,68 @@ extension UploadedCourseInfoVC {
private func setData(courseList: [PublicCourse]) {
self.uploadedCourseList = courseList
UploadedCourseInfoCollectionView.reloadData()
self.emptyView.isHidden = !courseList.isEmpty
self.beforeEditTopView.isHidden = courseList.isEmpty
totalNumOfRecordlabel.text = "총 기록 \(courseList.count)"
}

private func setDelegate() {
self.UploadedCourseInfoCollectionView.delegate = self
self.UploadedCourseInfoCollectionView.dataSource = self
emptyView.delegate = self
}

private func register() {
UploadedCourseInfoCollectionView.register(UploadedCourseInfoCVC.self,
forCellWithReuseIdentifier: UploadedCourseInfoCVC.className)
UploadedCourseInfoCollectionView.register(CourseListCVC.self,
forCellWithReuseIdentifier: CourseListCVC.className)
}
private func setAddTarget() {
self.editButton.addTarget(self, action: #selector(editButtonDidTap), for: .touchUpInside)

}

private func setDeleteButton(){
deleteCourseButton.addTarget(self, action: #selector(deleteCourseButtonDidTap), for: .touchUpInside)
}
}
// MARK: - @objc Function

extension UploadedCourseInfoVC {
@objc func deleteCourseButtonDidTap(_sender: UIButton) {
guard let selectedList = courseListCollectionView.indexPathsForSelectedItems else { return }
var deleteToCourseId = [Int]()
for indexPath in selectedList {
let course = uploadedCourseList[indexPath.item]
deleteToCourseId.append(course.id)
}
let deleteAlertVC = RNAlertVC(description: "삭제하시겠습니까?")
deleteAlertVC.modalPresentationStyle = .overFullScreen
deleteAlertVC.rightButtonTapAction = {
deleteAlertVC.dismiss(animated: false)
self.deleteCourse(courseIdList: deleteToCourseId)
}
self.present(deleteAlertVC, animated: false)
}

@objc func editButtonDidTap() {
if isEditMode {
self.totalNumOfRecordlabel.text = "총 기록 \(self.uploadedCourseList.count)"
self.editButton.setTitle("편집", for: .normal)
self.deleteCourseButton.isHidden = true
self.deleteCourseButton.isEnabled = false
self.deleteCourseButton.setTitle(title: "삭제하기")
self.courseListCollectionView.reloadData()

isEditMode = false
self.tabBarController?.tabBar.isHidden = false
} else {
self.totalNumOfRecordlabel.text = "기록 선택"
self.editButton.setTitle("취소", for: .normal)
self.deleteCourseButton.isHidden = false
self.courseListCollectionView.reloadData()
isEditMode = true
self.tabBarController?.tabBar.isHidden = true
}
}
}

Expand All @@ -89,15 +183,47 @@ extension UploadedCourseInfoVC {
private func setUI() {
view.backgroundColor = .w1
UploadedCourseInfoCollectionView.backgroundColor = .w1
self.emptyView.isHidden = true
self.beforeEditTopView.isHidden = false
}

private func setLayout() {
view.addSubview(UploadedCourseInfoCollectionView)
view.addSubviews(beforeEditTopView,UploadedCourseInfoCollectionView,deleteCourseButton)

UploadedCourseInfoCollectionView.snp.makeConstraints { make in
UploadedCourseInfoCollectionView.addSubview(emptyView)

beforeEditTopView.addSubviews(totalNumOfRecordlabel, editButton)
beforeEditTopView.snp.makeConstraints { make in
make.top.equalTo(navibar.snp.bottom)
make.leading.trailing.equalTo(view.safeAreaLayoutGuide)
make.bottom.equalTo(view.safeAreaLayoutGuide)
make.leading.trailing.equalToSuperview()
make.height.equalTo(38)
}

totalNumOfRecordlabel.snp.makeConstraints { make in
make.leading.equalToSuperview().inset(16)
make.top.equalToSuperview().offset(10)
}

editButton.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(16)
make.width.equalTo(47)
make.height.equalTo(22)
make.top.equalToSuperview().offset(5)
}

deleteCourseButton.snp.makeConstraints { make in
make.bottom.equalToSuperview().inset(32)
make.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(44)
}

UploadedCourseInfoCollectionView.snp.makeConstraints { make in
make.top.equalTo(editButton.snp.bottom)
make.leading.bottom.trailing.equalToSuperview()
}
emptyView.snp.makeConstraints { make in
make.center.equalToSuperview()
make.leading.trailing.equalToSuperview().inset(80)
}
}
}
Expand All @@ -119,15 +245,6 @@ extension UploadedCourseInfoVC: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return uploadedCourseInset
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let courseDetailVC = CourseDetailVC()
let courseModel = uploadedCourseList[indexPath.item]
courseDetailVC.setCourseId(courseId: courseModel.courseId, publicCourseId: courseModel.id)
courseDetailVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(courseDetailVC, animated: true)
}
}

// MARK: - UICollectionViewDataSource
Expand All @@ -136,11 +253,67 @@ extension UploadedCourseInfoVC: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return uploadedCourseList.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let uploadedCourseCell = collectionView.dequeueReusableCell(withReuseIdentifier: UploadedCourseInfoCVC.className, for: indexPath) as? UploadedCourseInfoCVC else { return UICollectionViewCell()}
uploadedCourseCell.setData(model: uploadedCourseList[indexPath.item])
return uploadedCourseCell
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CourseListCVC.className, for: indexPath)
as? CourseListCVC else { return UICollectionViewCell() }
cell.setCellType(type: .title)
if let selectedCells = collectionView.indexPathsForSelectedItems, selectedCells.contains(indexPath) {
cell.selectCell(didSelect: true)
} 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 = uploadedCourseList[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 selectedCells = collectionView.indexPathsForSelectedItems else { return }
guard let cell = collectionView.cellForItem(at: indexPath) as? CourseListCVC else { return }
let courseList = uploadedCourseList[indexPath.item]

if isEditMode {
self.deleteCourseButton.isEnabled = true
let countSelectCells = selectedCells.count
self.deleteCourseButton.setTitle(title: "삭제하기(\(countSelectCells))")
cell.selectCell(didSelect: true)
} else {
collectionView.deselectItem(at: indexPath, animated: true)
self.deleteCourseButton.setTitle(title: "삭제하기")
self.deleteCourseButton.setEnabled(true)

let courseDetailVC = CourseDetailVC()
let courseModel = uploadedCourseList[indexPath.item]
courseDetailVC.setCourseId(courseId: courseModel.courseId, publicCourseId: courseModel.id)
courseDetailVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(courseDetailVC, animated: true)
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
guard collectionView.cellForItem(at: indexPath) is CourseListCVC else { return }
guard let selectedCells = collectionView.indexPathsForSelectedItems else {
self.deleteCourseButton.isEnabled = false
self.deleteCourseButton.setTitle(title: "삭제하기")
return }
guard let cell = collectionView.cellForItem(at: indexPath) as? CourseListCVC else { return }

if isEditMode {
self.deleteCourseButton.isEnabled = true
let countSelectCells = selectedCells.count
self.deleteCourseButton.setTitle(title: "삭제하기(\(countSelectCells))")
// self.selectedIndexs = nil
cell.selectCell(didSelect: false)
} else {
collectionView.deselectItem(at: indexPath, animated: true)
self.deleteCourseButton.setTitle(title: "삭제하기")
}
}
}

Expand Down Expand Up @@ -174,4 +347,36 @@ extension UploadedCourseInfoVC {
}
}
}
private func deleteCourse(courseIdList: [Int]) {
LoadingIndicator.showLoading()
courseProvider.request(.deleteCourse(courseIdList: courseIdList)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
print("리절트", result)
let status = result.statusCode
if 200..<300 ~= status {
print("삭제 성공")
self.getUploadedCourseInfo()

}
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}

// MARK: - Section Heading

extension UploadedCourseInfoVC: ListEmptyViewDelegate {
func emptyViewButtonTapped() {

}
}

0 comments on commit efef823

Please sign in to comment.