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 @@ -19,7 +19,7 @@ struct PublicCourse: Codable {
let id, courseId: Int
let title: String
let image: String
let scarp: Bool?
let scrap: Bool?
let description: String?
let distance: Float?
let departure: CourseDiscoveryDeparture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Moya

enum UploadedCourseDetailRouter {
case getUploadedCourseDetail(publicCourseId: Int)
case createAndDeleteScrap(publicCourseId: Int, scrapTF: Bool)
}

extension UploadedCourseDetailRouter: TargetType {
Expand All @@ -26,26 +27,32 @@ extension UploadedCourseDetailRouter: TargetType {
switch self {
case .getUploadedCourseDetail(let publicCourseId):
return "/public-course/detail/\(publicCourseId)"
case .createAndDeleteScrap:
return "/scrap"
}
}

var method: Moya.Method {
switch self {
case .getUploadedCourseDetail:
return .get
case .createAndDeleteScrap:
return .post
}
}

var task: Moya.Task {
switch self {
case .getUploadedCourseDetail:
return .requestPlain
case .createAndDeleteScrap(let publicCourseId, let scrapTF):
return .requestParameters(parameters: ["publicCourseId": publicCourseId, "scrapTF": scrapTF], encoding: JSONEncoding.default)
}
}

var headers: [String: String]? {
switch self {
case .getUploadedCourseDetail:
case .getUploadedCourseDetail, .createAndDeleteScrap:
return Config.headerWithDeviceId
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final class CourseDetailVC: UIViewController {

extension CourseDetailVC {
@objc func likeButtonDidTap(_ sender: UIButton) {
sender.isSelected.toggle()
scrapCourse(scrapTF: !sender.isSelected)
}

@objc func startButtonDidTap() {
Expand Down Expand Up @@ -160,6 +160,9 @@ extension CourseDetailVC {
self.runningLevelLabel.text = "Lv. \(model.user.level)"
self.courseTitleLabel.text = model.publicCourse.title

guard let scrap = model.publicCourse.scrap else { return }
self.likeButton.isSelected = scrap

guard let distance = model.publicCourse.distance else { return }
self.courseDistanceInfoView.setDescriptionText(description: String(distance))
let location = "\(model.publicCourse.departure.region) \(model.publicCourse.departure.city)"
Expand Down Expand Up @@ -320,7 +323,6 @@ extension CourseDetailVC {

private func getCourseDetailWithPath(courseId: Int) {
LoadingIndicator.showLoading()

runningProvider.request(.getCourseDetail(courseId: courseId)) { [weak self] response in
guard let self = self else { return }
LoadingIndicator.hideLoading()
Expand All @@ -347,4 +349,27 @@ extension CourseDetailVC {
}
}
}

private func scrapCourse(scrapTF: Bool) {
guard let publicCourseId = self.publicCourseId else { return }
LoadingIndicator.showLoading()
courseDetailProvider.request(.createAndDeleteScrap(publicCourseId: publicCourseId, scrapTF: scrapTF)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
let status = result.statusCode
if 200..<300 ~= status {
self.likeButton.isSelected.toggle()
}
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import Moya
final class CourseDiscoveryVC: UIViewController {
// MARK: - Properties

let pickedMapListProvider = MoyaProvider<pickedMapListRouter>(
private let pickedMapListProvider = MoyaProvider<pickedMapListRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)

private let courseDetailProvider = MoyaProvider<UploadedCourseDetailRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)

Expand Down Expand Up @@ -167,9 +171,10 @@ extension CourseDiscoveryVC: UICollectionViewDelegate, UICollectionViewDataSourc
} else {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CourseListCVC.className, for: indexPath) as? CourseListCVC else { return UICollectionViewCell() }
cell.setCellType(type: .all)
cell.delegate = self
let model = self.courseList[indexPath.item]
let location = "\(model.departure.region) \(model.departure.city)"
cell.setData(imageURL: model.image, title: model.title, location: location, didLike: model.scarp)
cell.setData(imageURL: model.image, title: model.title, location: location, didLike: model.scrap, indexPath: indexPath.item)
return cell
}
}
Expand Down Expand Up @@ -230,6 +235,15 @@ extension CourseDiscoveryVC: UICollectionViewDelegateFlowLayout {
}
}

// MARK: - CourseListCVCDeleagte

extension CourseDiscoveryVC: CourseListCVCDeleagte {
func likeButtonTapped(wantsTolike: Bool, index: Int) {
let publicCourseId = courseList[index].id
scrapCourse(publicCourseId: publicCourseId, scrapTF: wantsTolike)
}
}

// MARK: - Network

extension CourseDiscoveryVC {
Expand Down Expand Up @@ -259,4 +273,26 @@ extension CourseDiscoveryVC {
}
}
}

private func scrapCourse(publicCourseId: Int, scrapTF: Bool) {
LoadingIndicator.showLoading()
courseDetailProvider.request(.createAndDeleteScrap(publicCourseId: publicCourseId, scrapTF: scrapTF)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
let status = result.statusCode
if 200..<300 ~= status {
self.getCourseData()
}
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ final class CourseSearchVC: UIViewController {
plugins: [NetworkLoggerPlugin(verbose: true)]
)

private let courseDetailProvider = MoyaProvider<UploadedCourseDetailRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)

private var courseList = [PublicCourse]()
private var keyword: String?

// MARK: - UI Components

Expand Down Expand Up @@ -71,7 +76,12 @@ final class CourseSearchVC: UIViewController {
setDelegate()
layout()
setTabBar()

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let keyword = self.keyword else { return }
searchCourseWithKeyword(keyword: keyword)
}
}
// MARK: - Methods
Expand Down Expand Up @@ -138,9 +148,10 @@ extension CourseSearchVC: UICollectionViewDelegate, UICollectionViewDataSource {
for: indexPath)
as? CourseListCVC else { return UICollectionViewCell() }
cell.setCellType(type: .all)
cell.delegate = self
let model = self.courseList[indexPath.item]
let location = "\(model.departure.region) \(model.departure.city)"
cell.setData(imageURL: model.image, title: model.title, location: location, didLike: model.scarp)
cell.setData(imageURL: model.image, title: model.title, location: location, didLike: model.scrap, indexPath: indexPath.item)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
Expand Down Expand Up @@ -184,8 +195,19 @@ extension CourseSearchVC: UICollectionViewDelegateFlowLayout {
extension CourseSearchVC: CustomNavigationBarDelegate {
func searchButtonDidTap(text: String) {
searchCourseWithKeyword(keyword: text)
self.keyword = text
}
}

// MARK: - CourseListCVCDeleagte

extension CourseSearchVC: CourseListCVCDeleagte {
func likeButtonTapped(wantsTolike: Bool, index: Int) {
let pubilcCourseId = courseList[index].id
scrapCourse(publicCourseId: pubilcCourseId, scrapTF: wantsTolike)
}
}

// MARK: - Network

extension CourseSearchVC {
Expand Down Expand Up @@ -215,4 +237,23 @@ extension CourseSearchVC {
}
}
}

private func scrapCourse(publicCourseId: Int, scrapTF: Bool) {
LoadingIndicator.showLoading()
courseDetailProvider.request(.createAndDeleteScrap(publicCourseId: publicCourseId, scrapTF: scrapTF)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
let status = result.statusCode
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ final class CourseStorageVC: UIViewController {

// MARK: - Properties

private let courseDetailProvider = MoyaProvider<UploadedCourseDetailRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)

private let courseStorageProvider = MoyaProvider<CourseStorageRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)
Expand Down Expand Up @@ -42,6 +46,7 @@ final class CourseStorageVC: UIViewController {
self.setUI()
self.setLayout()
self.bindUI()
self.setDelegate()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -92,6 +97,10 @@ extension CourseStorageVC {
self.navigationController?.pushViewController(courseDetailVC, animated: true)
}.store(in: cancelBag)
}

private func setDelegate() {
scrapCourseListView.delegate = self
}
}

// MARK: - UI & Layout
Expand All @@ -116,6 +125,14 @@ extension CourseStorageVC {
}
}

// MARK: - ScrapCourseListViewDelegate

extension CourseStorageVC: ScrapCourseListViewDelegate {
func likeButtonTapped(wantsTolike: Bool, publicCourseId: Int) {
scrapCourse(publicCourseId: publicCourseId, scrapTF: wantsTolike)
}
}

// MARK: - Network

extension CourseStorageVC {
Expand Down Expand Up @@ -174,4 +191,26 @@ extension CourseStorageVC {
}
}
}

private func scrapCourse(publicCourseId: Int, scrapTF: Bool) {
LoadingIndicator.showLoading()
courseDetailProvider.request(.createAndDeleteScrap(publicCourseId: publicCourseId, scrapTF: scrapTF)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
let status = result.statusCode
if 200..<300 ~= status {
self.getScrapCourseList()
}
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

protocol CourseListCVCDeleagte: AnyObject {
func likeButtonTapped(wantsTolike: Bool)
func likeButtonTapped(wantsTolike: Bool, index: Int)
}

@frozen
Expand All @@ -34,6 +34,8 @@ final class CourseListCVC: UICollectionViewCell {

weak var delegate: CourseListCVCDeleagte?

private var indexPath: Int?

// MARK: - UI Components

private let courseImageView = UIImageView().then {
Expand Down Expand Up @@ -65,7 +67,6 @@ final class CourseListCVC: UICollectionViewCell {
private let likeButton = UIButton(type: .custom).then {
$0.setImage(ImageLiterals.icHeartFill, for: .selected)
$0.setImage(ImageLiterals.icHeart, for: .normal)
$0.isSelected = true
$0.backgroundColor = .w1
}

Expand All @@ -90,9 +91,10 @@ extension CourseListCVC {
likeButton.addTarget(self, action: #selector(likeButtonDidTap), for: .touchUpInside)
}

func setData(imageURL: String, title: String, location: String?, didLike: Bool?) {
func setData(imageURL: String, title: String, location: String?, didLike: Bool?, indexPath: Int? = nil) {
self.courseImageView.setImage(with: imageURL)
self.titleLabel.text = title
self.indexPath = indexPath

if let location = location {
self.locationLabel.text = location
Expand All @@ -117,8 +119,9 @@ extension CourseListCVC {

extension CourseListCVC {
@objc func likeButtonDidTap(_ sender: UIButton) {
guard let indexPath = self.indexPath else { return }
sender.isSelected.toggle()
delegate?.likeButtonTapped(wantsTolike: (sender.isSelected == true))
delegate?.likeButtonTapped(wantsTolike: (sender.isSelected == true), index: indexPath)
}
}

Expand Down
Loading