Skip to content

Commit

Permalink
feat: judgeStatus 함수 변경 (TeamNADA#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwns33 committed Apr 15, 2023
1 parent 0b045b6 commit 0e4f948
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
9 changes: 2 additions & 7 deletions NADA-iOS-forRelease/Sources/NetworkModel/Group/Groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ struct Groups: Codable {
// MARK: - Group

struct Group: Codable {
var groupID: Int
var groupName: String

enum CodingKeys: String, CodingKey {
case groupID = "groupId"
case groupName
}
var cardGroupId: Int
var cardGroupName: String
}
21 changes: 19 additions & 2 deletions NADA-iOS-forRelease/Sources/NetworkService/Group/GroupAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class GroupAPI {
let statusCode = response.statusCode
let data = response.data

let networkResult = self.judgeGroupListFetchStatus(by: statusCode, data)
let networkResult = self.judgeStatus(by: statusCode, data: data, type: [Group].self)
completion(networkResult)

case .failure(let err):
Expand Down Expand Up @@ -202,10 +202,27 @@ public class GroupAPI {
}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<String>.self, from: data)
else { return .pathErr }

switch statusCode {
case 200:
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.message ?? "error message")
case 500:
return .serverErr
default:
return .networkFail
}
}

private func judgeStatus<T: Codable>(by statusCode: Int, data: Data, type: T.Type) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<T>.self, from: data)
else { return .pathErr }

switch statusCode {
case 200:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class GroupNameEditBottomSheetViewController: CommonBottomSheetViewController, U
extension GroupNameEditBottomSheetViewController {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
groupEditWithAPI(groupRequest: GroupEditRequest(groupId: nowGroup?.groupID ?? 0, groupName: addGroupTextField.text ?? ""))
groupEditWithAPI(groupRequest: GroupEditRequest(groupId: nowGroup?.cardGroupId ?? 0, groupName: addGroupTextField.text ?? ""))
nowHideBottomSheetAndGoBack()

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SelectGroupBottomSheetViewController: CommonBottomSheetViewController {
private func setupUI() {
view.addSubview(groupPicker)
view.addSubview(doneButton)
selectedGroup = serverGroups?.groups[0].groupID ?? 0
selectedGroup = serverGroups?.groups[0].cardGroupId ?? 0
groupPicker.delegate = self
groupPicker.dataSource = self
setupLayout()
Expand Down Expand Up @@ -113,16 +113,16 @@ extension SelectGroupBottomSheetViewController: UIPickerViewDelegate, UIPickerVi
label.textAlignment = .center

if pickerView.selectedRow(inComponent: component) == row {
label.attributedText = NSAttributedString(string: serverGroups?.groups[row].groupName ?? "", attributes: [NSAttributedString.Key.font: UIFont.textBold01, NSAttributedString.Key.foregroundColor: UIColor.mainColorNadaMain])
label.attributedText = NSAttributedString(string: serverGroups?.groups[row].cardGroupName ?? "", attributes: [NSAttributedString.Key.font: UIFont.textBold01, NSAttributedString.Key.foregroundColor: UIColor.mainColorNadaMain])
} else {
label.attributedText = NSAttributedString(string: serverGroups?.groups[row].groupName ?? "", attributes: [NSAttributedString.Key.font: UIFont.textRegular03, NSAttributedString.Key.foregroundColor: UIColor.quaternary])
label.attributedText = NSAttributedString(string: serverGroups?.groups[row].cardGroupName ?? "", attributes: [NSAttributedString.Key.font: UIFont.textRegular03, NSAttributedString.Key.foregroundColor: UIColor.quaternary])
}

return label
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedGroup = serverGroups?.groups[row].groupID ?? 0
selectedGroup = serverGroups?.groups[row].cardGroupId ?? 0
selectedGroupIndex = row
pickerView.reloadAllComponents()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,20 @@ extension GroupViewController {
GroupAPI.shared.groupListFetch(userID: userID) { response in
switch response {
case .success(let data):
/*
if let group = data as? Groups {
self.serverGroups = group
self.groupCollectionView.reloadData()
self.groupId = group.groups[self.selectedRow].groupID
self.cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: group.groups[self.selectedRow].groupID, offset: 0)) {
self.groupId = group.groups[self.selectedRow].cardGroupId
self.cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: group.groups[self.selectedRow].cardGroupId, offset: 0)) {
if self.frontCards?.count != 0 {
self.cardsCollectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
}
self.isInfiniteScroll = true
}
}
*/
print("success")
case .requestErr(let message):
print("groupListFetchWithAPI - requestErr: \(message)")
case .pathErr:
Expand Down Expand Up @@ -258,7 +261,7 @@ extension GroupViewController: UICollectionViewDelegate {
isInfiniteScroll = false
offset += 1

cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: serverGroups?.groups[self.selectedRow].groupID ?? -1, offset: offset)) {
cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: serverGroups?.groups[self.selectedRow].cardGroupId ?? -1, offset: offset)) {
self.isInfiniteScroll = true
}
}
Expand Down Expand Up @@ -286,7 +289,7 @@ extension GroupViewController: UICollectionViewDataSource {
guard let groupCell = collectionView.dequeueReusableCell(withReuseIdentifier: Const.Xib.groupCollectionViewCell, for: indexPath) as? GroupCollectionViewCell else {
return UICollectionViewCell()
}
groupCell.groupName.text = serverGroups?.groups[indexPath.row].groupName
groupCell.groupName.text = serverGroups?.groups[indexPath.row].cardGroupName

if indexPath.row == selectedRow {
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .init())
Expand Down Expand Up @@ -324,7 +327,7 @@ extension GroupViewController: UICollectionViewDataSource {
switch collectionView {
case groupCollectionView:
selectedRow = indexPath.row
groupId = serverGroups?.groups[indexPath.row].groupID
groupId = serverGroups?.groups[indexPath.row].cardGroupId
offset = 0
frontCards?.removeAll()

Expand All @@ -335,7 +338,7 @@ extension GroupViewController: UICollectionViewDataSource {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.cardListInGroupWithAPI(cardListInGroupRequest:
CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "",
groupId: self.serverGroups?.groups[indexPath.row].groupID ?? 0,
groupId: self.serverGroups?.groups[indexPath.row].cardGroupId ?? 0,
offset: 0)) {
self.isInfiniteScroll = true
}
Expand All @@ -360,7 +363,7 @@ extension GroupViewController: UICollectionViewDelegateFlowLayout {
guard let cell = groupCollectionView.dequeueReusableCell(withReuseIdentifier: Const.Xib.groupCollectionViewCell, for: indexPath) as? GroupCollectionViewCell else {
return .zero
}
cell.groupName.text = serverGroups?.groups[indexPath.row].groupName
cell.groupName.text = serverGroups?.groups[indexPath.row].cardGroupName
cell.groupName.sizeToFit()
width = cell.groupName.frame.width + 30
height = collectionView.frame.size.height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension GroupEditViewController: UITableViewDelegate {
// 취소 눌렀을 때 액션이 들어갈 부분
}, deleteAction: { _ in
self.groupDeleteWithAPI(
groupID: self.serverGroups?.groups[indexPath.row].groupID ?? 0,
groupID: self.serverGroups?.groups[indexPath.row].cardGroupId ?? 0,
defaultGroupId: self.unClass ?? 0)
self.groupEditTableView.reloadData()
NotificationCenter.default.post(name: Notification.Name.passDataToGroup, object: 0, userInfo: nil)
Expand All @@ -93,7 +93,7 @@ extension GroupEditViewController: UITableViewDelegate {
.setTitle("그룹명 변경")
.setHeight(184)
nextVC.modalPresentationStyle = .overFullScreen
nextVC.text = serverGroups?.groups[indexPath.row].groupName ?? ""
nextVC.text = serverGroups?.groups[indexPath.row].cardGroupName ?? ""
nextVC.returnToGroupEditViewController = {
self.groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "")
}
Expand Down Expand Up @@ -121,7 +121,7 @@ extension GroupEditViewController: UITableViewDataSource {
} else {
guard let serviceCell = tableView.dequeueReusableCell(withIdentifier: Const.Xib.groupEditTableViewCell, for: indexPath) as? GroupEditTableViewCell else { return UITableViewCell() }

serviceCell.initData(title: serverGroups?.groups[indexPath.row].groupName ?? "")
serviceCell.initData(title: serverGroups?.groups[indexPath.row].cardGroupName ?? "")
return serviceCell
}
}
Expand All @@ -130,7 +130,7 @@ extension GroupEditViewController: UITableViewDataSource {
// MARK: - Extensions
extension GroupEditViewController {
func serverGroupList() {
self.unClass = serverGroups?.groups[0].groupID
self.unClass = serverGroups?.groups[0].cardGroupId
serverGroups?.groups.remove(at: 0)
}
}
Expand All @@ -143,7 +143,7 @@ extension GroupEditViewController {
case .success(let data):
if let group = data as? Groups {
self.serverGroups = group
self.unClass = self.serverGroups?.groups[0].groupID
self.unClass = self.serverGroups?.groups[0].cardGroupId
self.serverGroups?.groups.remove(at: 0)
self.groupEditTableView.reloadData()
}
Expand Down

0 comments on commit 0e4f948

Please sign in to comment.