Skip to content

Commit

Permalink
[FIX] #286 - 무한스크롤 오프셋 개선 및 로딩창 수정
Browse files Browse the repository at this point in the history
- 메인 로딩창 추가
- 명함생성시에 노티를 통해서 main 으로 isAfterCreation 을 true 로 해서 명함생성 후에는 스크롤 1로 이동
- 명함모음에서도 로딩창 추가
- 메인에 viewWillAppear 를 사용해서 스와이프로 명함 리스트에서 넘어와도 리로드
  • Loading branch information
hyun99999 committed Jan 7, 2022
1 parent dbb929e commit 2644dbc
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 66 deletions.
2 changes: 1 addition & 1 deletion NADA-iOS-forRelease.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@
F8FC43A026C01F040033E151 /* ViewControllers */ = {
isa = PBXGroup;
children = (
397B75182763B5DA004AEB03 /* TeamNADA */,
397B750E2763A5DD004AEB03 /* OpenSource */,
397B75182763B5DA004AEB03 /* TeamNADA */,
77812945275F6E6A004B34F4 /* CardDetail */,
F8518059275D0468006BD5ED /* Onboarding */,
393E3343275F69DB00965BBF /* Splash */,
Expand Down
1 change: 0 additions & 1 deletion NADA-iOS-forRelease/Resouces/Constants/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ extension Notification.Name {
static let passDataToDetail = Notification.Name("passDataToDetail")
static let reloadGroupViewController = Notification.Name("reloadGroupViewController")
static let creationReloadMainCardSwiper = Notification.Name("creationReloadMainCardSwiper")
static let listReloadMainCardSwiper = Notification.Name("listReloadMainCardSwiper")
static let dismissQRCodeCardResult = Notification.Name("dismissQRCodeCardResult")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina5_9" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="collection view cell content view" minToolsVersion="11.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ extension CardCreationPreviewViewController {
""")
.setHeight(587)
nextVC.modalPresentationStyle = .overFullScreen

presentingVC.present(nextVC, animated: true) {
UserDefaults.standard.set(false, forKey: Const.UserDefaultsKey.isFirstCard)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CardListViewController: UIViewController {

// MARK: - IBAction Properties
@IBAction func dismissToPreviousView(_ sender: UIButton) {
NotificationCenter.default.post(name: .listReloadMainCardSwiper, object: nil)
self.navigationController?.popViewController(animated: true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GroupViewController: UIViewController {

lazy var loadingBgView: UIView = {
let bgView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
bgView.backgroundColor = .bottomDimmedBackground
bgView.backgroundColor = .loadingBackground

return bgView
}()
Expand Down Expand Up @@ -103,10 +103,16 @@ class GroupViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

offset = 0
frontCards?.removeAll()

groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "")
DispatchQueue.main.async {
self.setActivityIndicator()

self.offset = 0
self.frontCards?.removeAll()
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "")
}
}
}

Expand Down Expand Up @@ -146,7 +152,7 @@ extension GroupViewController {
}

// MARK: - @objc Methods

@objc func didRecieveDataNotification(_ notification: Notification) {
selectedRow = notification.object as? Int ?? 0
}
Expand All @@ -155,7 +161,7 @@ extension GroupViewController {
private func relaodCardCollection() {
offset = 0
frontCards?.removeAll()

groupListFetchWithAPI(userID: UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID) ?? "")
}
}
Expand All @@ -172,7 +178,10 @@ extension GroupViewController {
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.cardsCollectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
if self.frontCards?.count != 0 {
self.cardsCollectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
}
self.isInfiniteScroll = true
}
}
case .requestErr(let message):
Expand All @@ -188,40 +197,30 @@ extension GroupViewController {
}

func cardListInGroupWithAPI(cardListInGroupRequest: CardListInGroupRequest, completion: @escaping () -> Void = { }) {
DispatchQueue.main.async {
self.setActivityIndicator()
}

DispatchQueue.main.async {
GroupAPI.shared.cardListFetchInGroup(cardListInGroupRequest: cardListInGroupRequest) { response in
switch response {
case .success(let data):
self.activityIndicator.stopAnimating()
self.loadingBgView.removeFromSuperview()

self.isInfiniteScroll = true

if let cards = data as? CardsInGroupResponse {
self.frontCards?.append(contentsOf: cards.cards)
if self.frontCards?.count == 0 {
self.emptyView.isHidden = false
} else {
self.emptyView.isHidden = true
}
self.cardsCollectionView.reloadData()
GroupAPI.shared.cardListFetchInGroup(cardListInGroupRequest: cardListInGroupRequest) { response in
switch response {
case .success(let data):
self.activityIndicator.stopAnimating()
self.loadingBgView.removeFromSuperview()

if let cards = data as? CardsInGroupResponse {
self.frontCards?.append(contentsOf: cards.cards)
if self.frontCards?.count == 0 {
self.emptyView.isHidden = false
} else {
self.emptyView.isHidden = true
}
case .requestErr(let message):
print("cardListInGroupWithAPI - requestErr: \(message)")
case .pathErr:
print("cardListInGroupWithAPI - pathErr")
case .serverErr:
print("cardListInGroupWithAPI - serverErr")
case .networkFail:
print("cardListInGroupWithAPI - networkFail")
}
if self.frontCards?.count != 0 {
completion()
self.cardsCollectionView.reloadData()
}
completion()
case .requestErr(let message):
print("cardListInGroupWithAPI - requestErr: \(message)")
case .pathErr:
print("cardListInGroupWithAPI - pathErr")
case .serverErr:
print("cardListInGroupWithAPI - serverErr")
case .networkFail:
print("cardListInGroupWithAPI - networkFail")
}
}
}
Expand Down Expand Up @@ -330,10 +329,19 @@ extension GroupViewController: UICollectionViewDataSource {
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))

DispatchQueue.main.async {
self.setActivityIndicator()
}

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,
offset: 0)) {
self.isInfiniteScroll = true
}
}
case cardsCollectionView:
cardDetailFetchWithAPI(cardID: frontCards?[indexPath.row].cardID ?? "")
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//

import UIKit
import VerticalCardSwiper

import KakaoSDKCommon
import NVActivityIndicatorView
import VerticalCardSwiper

class FrontViewController: UIViewController {

Expand All @@ -18,6 +20,27 @@ class FrontViewController: UIViewController {
private var cardDataList: [Card]? = []
private var userID: String?

var isAfterCreation = false

// MARK: - Components

lazy var loadingBgView: UIView = {
let bgView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
bgView.backgroundColor = .loadingBackground

return bgView
}()

lazy var activityIndicator: NVActivityIndicatorView = {
let activityIndicator = NVActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40),
type: .ballBeat,
color: .mainColorNadaMain,
padding: .zero)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false

return activityIndicator
}()

// MARK: - @IBOutlet Properties
@IBOutlet weak var cardSwiper: VerticalCardSwiper!

Expand All @@ -28,7 +51,28 @@ class FrontViewController: UIViewController {
setUserID()
setDelegate()
setNotification()
cardListFetchWithAPI(userID: userID, isList: false, offset: offset)
}

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

if !self.isAfterCreation {
DispatchQueue.main.async {

self.setActivityIndicator()

self.offset = 0
self.cardDataList?.removeAll()
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.cardListFetchWithAPI(userID: self.userID, isList: false, offset: self.offset) {
_ = self.cardSwiper.scrollToCard(at: 0, animated: false)
self.activityIndicator.stopAnimating()
self.loadingBgView.removeFromSuperview()
}
}
}
}

// MARK: - @IBAction Properties
Expand Down Expand Up @@ -66,13 +110,24 @@ extension FrontViewController {
private func setNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(didRecievePresentCardShare(_:)), name: .presentCardShare, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(setCreationReloadMainCardSwiper), name: .creationReloadMainCardSwiper, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(setListReloadMainCardSwiper), name: .listReloadMainCardSwiper, object: nil)
}

private func setUserID() {
userID = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.userID)
}

private func setActivityIndicator() {
view.addSubview(loadingBgView)
loadingBgView.addSubview(activityIndicator)

NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])

activityIndicator.startAnimating()
}

// MARK: - @objc Methods

@objc
Expand All @@ -91,18 +146,15 @@ extension FrontViewController {

@objc
private func setCreationReloadMainCardSwiper() {
isAfterCreation = true

cardDataList?.removeAll()
offset = 0
_ = cardSwiper.scrollToCard(at: 1, animated: false)
cardListFetchWithAPI(userID: userID, isList: false, offset: offset)
}

@objc
private func setListReloadMainCardSwiper() {
cardDataList?.removeAll()
offset = 0
_ = cardSwiper.scrollToCard(at: 0, animated: false)
cardListFetchWithAPI(userID: userID, isList: false, offset: offset)

cardListFetchWithAPI(userID: userID, isList: false, offset: offset) {
_ = self.cardSwiper.scrollToCard(at: 1, animated: false)
self.isAfterCreation = false
}
}
}

Expand All @@ -118,7 +170,9 @@ extension FrontViewController: VerticalCardSwiperDelegate {
isInfiniteScroll = false
offset += 1
guard let userID = userID else { return }
cardListFetchWithAPI(userID: userID, isList: false, offset: offset)
cardListFetchWithAPI(userID: userID, isList: false, offset: offset) {
self.isInfiniteScroll = true
}
}
}
}
Expand Down Expand Up @@ -152,17 +206,16 @@ extension FrontViewController: VerticalCardSwiperDatasource {

// MARK: - Network
extension FrontViewController {
func cardListFetchWithAPI(userID: String?, isList: Bool, offset: Int) {
func cardListFetchWithAPI(userID: String?, isList: Bool, offset: Int, completion: @escaping () -> Void = { }) {
guard let userID = userID else { return }
CardAPI.shared.cardListFetch(userID: userID, isList: isList, offset: offset) { response in
switch response {
case .success(let data):
self.isInfiniteScroll = true

if let cardListLookUp = data as? CardListLookUp {
self.cardDataList?.append(contentsOf: cardListLookUp.cards)
self.cardSwiper.reloadData()
}
completion()
case .requestErr(let message):
print("cardListFetchWithAPI - requestErr: \(message)")
case .pathErr:
Expand Down

0 comments on commit 2644dbc

Please sign in to comment.