diff --git a/NADA-iOS-forRelease/Resouces/Constants/Notification.swift b/NADA-iOS-forRelease/Resouces/Constants/Notification.swift index 4f2bed33..2bacb104 100644 --- a/NADA-iOS-forRelease/Resouces/Constants/Notification.swift +++ b/NADA-iOS-forRelease/Resouces/Constants/Notification.swift @@ -18,6 +18,7 @@ extension Notification.Name { static let presentCardShare = Notification.Name("presentCardShare") static let passDataToGroup = Notification.Name("passDataToGroup") static let passDataToDetail = Notification.Name("passDataToDetail") - static let listReloadMainCardSwiper = Notification.Name("listReloadMainCardSwiper") + static let reloadGroupViewController = Notification.Name("reloadGroupViewController") static let creationReloadMainCardSwiper = Notification.Name("creationReloadMainCardSwiper") + static let listReloadMainCardSwiper = Notification.Name("listReloadMainCardSwiper") } diff --git a/NADA-iOS-forRelease/Sources/Cells/GroupCell/CardInGroupCollectionViewCell.swift b/NADA-iOS-forRelease/Sources/Cells/GroupCell/CardInGroupCollectionViewCell.swift index 87d8b714..4356ee5f 100644 --- a/NADA-iOS-forRelease/Sources/Cells/GroupCell/CardInGroupCollectionViewCell.swift +++ b/NADA-iOS-forRelease/Sources/Cells/GroupCell/CardInGroupCollectionViewCell.swift @@ -26,7 +26,21 @@ class CardInGroupCollectionViewCell: UICollectionViewCell { override func awakeFromNib() { super.awakeFromNib() - setUI() + } + + override func prepareForReuse() { + super.prepareForReuse() + + backgroundImageView.image = UIImage() + titleLabel.text = "" + descriptionLabel.text = "" + userNameLabel.text = "" + birthLabel.text = "" + mbtiLabel.text = "" + instagramIDLabel.text = "" + mbtiLabel.text = "" + instagramIcon.image = UIImage() + urlIcon.image = UIImage() } static func nib() -> UINib { @@ -34,10 +48,3 @@ class CardInGroupCollectionViewCell: UICollectionViewCell { } } - -extension CardInGroupCollectionViewCell { - private func setUI() { - -// backgroundImageView.alpha = 0.4 - } -} diff --git a/NADA-iOS-forRelease/Sources/ViewControllers/BottomSheet/SelectGroupBottomSheetViewController.swift b/NADA-iOS-forRelease/Sources/ViewControllers/BottomSheet/SelectGroupBottomSheetViewController.swift index 792a400a..be0244ac 100644 --- a/NADA-iOS-forRelease/Sources/ViewControllers/BottomSheet/SelectGroupBottomSheetViewController.swift +++ b/NADA-iOS-forRelease/Sources/ViewControllers/BottomSheet/SelectGroupBottomSheetViewController.swift @@ -72,11 +72,13 @@ class SelectGroupBottomSheetViewController: CommonBottomSheetViewController { @objc func presentCardInfoViewController() { switch status { case .detail: + NotificationCenter.default.post(name: .reloadGroupViewController, object: nil) changeGroupWithAPI(request: ChangeGroupRequest(cardID: cardDataModel?.cardID ?? "", userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupID: groupId ?? 0, newGroupID: selectedGroup)) case .add, .addWithQR: + NotificationCenter.default.post(name: .reloadGroupViewController, object: nil) cardAddInGroupWithAPI(cardRequest: CardAddInGroupRequest(cardId: cardDataModel?.cardID ?? "", userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: selectedGroup)) diff --git a/NADA-iOS-forRelease/Sources/ViewControllers/CardDetail/CardDetailViewController.swift b/NADA-iOS-forRelease/Sources/ViewControllers/CardDetail/CardDetailViewController.swift index 56891226..8a6a727d 100644 --- a/NADA-iOS-forRelease/Sources/ViewControllers/CardDetail/CardDetailViewController.swift +++ b/NADA-iOS-forRelease/Sources/ViewControllers/CardDetail/CardDetailViewController.swift @@ -130,6 +130,7 @@ extension CardDetailViewController { self.makeCancelDeleteAlert(title: "명함 삭제", message: "명함을 정말 삭제하시겠습니까?", deleteAction: { _ in + NotificationCenter.default.post(name: .reloadGroupViewController, object: nil) // 명함 삭제 서버통신 self.cardDeleteInGroupWithAPI(groupID: self.groupId ?? 0, cardID: self.cardDataModel?.cardID ?? "") }) }) diff --git a/NADA-iOS-forRelease/Sources/ViewControllers/Group/GroupViewController.swift b/NADA-iOS-forRelease/Sources/ViewControllers/Group/GroupViewController.swift index 4cc0b675..8044ba3c 100644 --- a/NADA-iOS-forRelease/Sources/ViewControllers/Group/GroupViewController.swift +++ b/NADA-iOS-forRelease/Sources/ViewControllers/Group/GroupViewController.swift @@ -63,32 +63,22 @@ class GroupViewController: UIViewController { // 그룹 이름들을 담을 변수 생성 var serverGroups: Groups? - var serverCards: CardsInGroupResponse? + var frontCards: [FrontCard]? = [] var serverCardsWithBack: Card? var groupId: Int? var selectedRow = 0 + private var offset = 0 + private var isInfiniteScroll = true override func viewDidLoad() { super.viewDidLoad() + registerCell() + setNotification() setUI() - -// 그룹 속 명함 조회 테스트 -// cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: "nada", groupId: 5, offset: 0)) -// 명함 검색 테스트 -// cardDetailFetchWithAPI(cardID: "cardA") - - } - - override func viewWillAppear(_ animated: Bool) { - // 그룹 리스트 조회 서버 테스트 - super.viewWillAppear(true) - NotificationCenter.default.addObserver(self, selector: #selector(didRecieveDataNotification(_:)), name: Notification.Name.passDataToGroup, object: nil) groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "") - } - } extension GroupViewController { @@ -107,9 +97,26 @@ extension GroupViewController { navigationController?.navigationBar.isHidden = true } + private func setNotification() { + NotificationCenter.default.addObserver(self, selector: #selector(didRecieveDataNotification(_:)), name: Notification.Name.passDataToGroup, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(scrollToTop), name: .reloadGroupViewController, object: nil) + } + @objc func didRecieveDataNotification(_ notification: Notification) { selectedRow = notification.object as? Int ?? 0 } + + @objc + private func scrollToTop() { + offset = 0 + frontCards?.removeAll() + groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "") + // FIXME: - 스크롤 탑 + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + self.cardsCollectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false) + } +// cardsCollectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false) + } } // MARK: - Network @@ -123,9 +130,7 @@ extension GroupViewController { self.serverGroups = group self.groupCollectionView.reloadData() self.groupId = group.groups[self.selectedRow].groupID - if !group.groups.isEmpty { - self.cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: group.groups[self.selectedRow].groupID, offset: 0)) - } + self.cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: group.groups[self.selectedRow].groupID, offset: 0)) } case .requestErr(let message): print("groupListFetchWithAPI - requestErr: \(message)") @@ -143,9 +148,11 @@ extension GroupViewController { GroupAPI.shared.cardListFetchInGroup(cardListInGroupRequest: cardListInGroupRequest) { response in switch response { case .success(let data): + self.isInfiniteScroll = true + if let cards = data as? CardsInGroupResponse { - self.serverCards = cards - if cards.cards.count == 0 { + self.frontCards?.append(contentsOf: cards.cards) + if self.frontCards?.count == 0 { self.emptyView.isHidden = false } else { self.emptyView.isHidden = true @@ -191,7 +198,16 @@ extension GroupViewController { // MARK: - UICollectionViewDelegate extension GroupViewController: UICollectionViewDelegate { - + func scrollViewDidScroll(_ scrollView: UIScrollView) { + if cardsCollectionView.contentOffset.y > cardsCollectionView.contentSize.height - cardsCollectionView.bounds.height { + if isInfiniteScroll { + isInfiniteScroll = false + offset += 1 + + cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: serverGroups?.groups[self.selectedRow].groupID ?? -1, offset: offset)) + } + } + } } // MARK: - UICollectionViewDataSource @@ -201,7 +217,7 @@ extension GroupViewController: UICollectionViewDataSource { case groupCollectionView: return serverGroups?.groups.count ?? 0 case cardsCollectionView: - return serverCards?.cards.count ?? 0 + return frontCards?.count ?? 0 default: return 0 } @@ -224,21 +240,21 @@ extension GroupViewController: UICollectionViewDataSource { guard let cardCell = collectionView.dequeueReusableCell(withReuseIdentifier: Const.Xib.cardInGroupCollectionViewCell, for: indexPath) as? CardInGroupCollectionViewCell else { return UICollectionViewCell() } + guard let frontCards = frontCards else { return UICollectionViewCell() } + cardCell.backgroundImageView.updateServerImage(frontCards[indexPath.row].background) + cardCell.cardId = frontCards[indexPath.row].cardID + cardCell.titleLabel.text = frontCards[indexPath.row].title + cardCell.descriptionLabel.text = frontCards[indexPath.row].cardDescription + cardCell.userNameLabel.text = frontCards[indexPath.row].name + cardCell.birthLabel.text = frontCards[indexPath.row].birthDate + cardCell.mbtiLabel.text = frontCards[indexPath.row].mbti + cardCell.instagramIDLabel.text = frontCards[indexPath.row].instagram + cardCell.lineURLLabel.text = frontCards[indexPath.row].link - cardCell.backgroundImageView.updateServerImage(serverCards?.cards[indexPath.row].background ?? "") - cardCell.cardId = serverCards?.cards[indexPath.row].cardID - cardCell.titleLabel.text = serverCards?.cards[indexPath.row].title - cardCell.descriptionLabel.text = serverCards?.cards[indexPath.row].cardDescription - cardCell.userNameLabel.text = serverCards?.cards[indexPath.row].name - cardCell.birthLabel.text = serverCards?.cards[indexPath.row].birthDate - cardCell.mbtiLabel.text = serverCards?.cards[indexPath.row].mbti - cardCell.instagramIDLabel.text = serverCards?.cards[indexPath.row].instagram - cardCell.lineURLLabel.text = serverCards?.cards[indexPath.row].link - - if serverCards?.cards[indexPath.row].instagram == "" { + if frontCards[indexPath.row].instagram == "" { cardCell.instagramIcon.isHidden = true } - if serverCards?.cards[indexPath.row].link == "" { + if frontCards[indexPath.row].link == "" { cardCell.urlIcon.isHidden = true } @@ -253,14 +269,16 @@ extension GroupViewController: UICollectionViewDataSource { case groupCollectionView: selectedRow = indexPath.row groupId = serverGroups?.groups[indexPath.row].groupID + offset = 0 + frontCards?.removeAll() cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest(userId: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "", groupId: serverGroups?.groups[indexPath.row].groupID ?? 0, offset: 0)) case cardsCollectionView: - cardDetailFetchWithAPI(cardID: serverCards?.cards[indexPath.row].cardID ?? "") + cardDetailFetchWithAPI(cardID: frontCards?[indexPath.row].cardID ?? "") default: - print(indexPath.row) + return } } } diff --git a/NADA-iOS-forRelease/Sources/ViewControllers/Group/QRScanViewController.swift b/NADA-iOS-forRelease/Sources/ViewControllers/Group/QRScanViewController.swift index 83aa9c01..25d06a0a 100644 --- a/NADA-iOS-forRelease/Sources/ViewControllers/Group/QRScanViewController.swift +++ b/NADA-iOS-forRelease/Sources/ViewControllers/Group/QRScanViewController.swift @@ -33,13 +33,12 @@ class QRScanViewController: UIViewController { super.viewDidLoad() basicSetting() } - } extension QRScanViewController { @objc func dismissQRScanViewController() { + NotificationCenter.default.post(name: .reloadGroupViewController, object: nil) self.dismiss(animated: true, completion: nil) - presentingViewController?.viewWillAppear(true) } } diff --git a/NADA-iOS-forRelease/Sources/ViewControllers/GroupEdit/GroupEditViewController.swift b/NADA-iOS-forRelease/Sources/ViewControllers/GroupEdit/GroupEditViewController.swift index 10472209..bddac05c 100644 --- a/NADA-iOS-forRelease/Sources/ViewControllers/GroupEdit/GroupEditViewController.swift +++ b/NADA-iOS-forRelease/Sources/ViewControllers/GroupEdit/GroupEditViewController.swift @@ -30,11 +30,11 @@ class GroupEditViewController: UIViewController { super.viewWillAppear(animated) self.groupEditTableView.reloadData() - } // MARK: - @IBAction Properties @IBAction func dismissToPreviousView(_ sender: UIButton) { + NotificationCenter.default.post(name: .reloadGroupViewController, object: nil) self.navigationController?.popViewController(animated: true) }