I’ve three state
enum State {
case loading
case loaded
case error
}
The code applies a shimmer impact to the tableView
when it masses. Right here’s the code:
extension RecipientsRootVC: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
viewModel?.numberOfSection() ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return state == .loading ? 5 : (viewModel?.numberofRow(in: part)).protected
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if state == .loading {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecipientShimmerCell", for: indexPath) as! RecipientShimmerCell
return cell
} else {
guard let mannequin = viewModel?.listing?[indexPath.row] else {
return UITableViewCell()
}
let cell: RecipientCell = tableView.dequeCell(RecipientCell.self,
for: indexPath)
cell.configure(knowledge: RecipientCellVM(mannequin: mannequin))
cell.selectionStyle = .none
cell.fullCellClicked = {
self.viewModel?.getRecipientDetails(mannequin.id.protected)
}
cell.onRightArrowClicked = {
self.viewModel?.getRecipientDetails(mannequin.id.protected)
}
return cell
}
}
}
The shimmer impact is seen when the tableView
first masses, nevertheless it doesn’t seem for newly loaded knowledge when customers scroll to set off pagination.
extension RecipientsRootVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if(scrollView != recentCollectionView) {
let contentOffsetY = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.peak - scrollView.body.dimension.peak
let threshold: CGFloat = 10.0
if maximumOffset - contentOffsetY <= threshold {
guard !loadingData else{
return
}
size += 10
viewModel?.loadMoreRecipients(size: size)
print("Paging known as (size)")
loadingData = true
}
}
}
}
There are two extra features that handle the dataLoadingStartedState
and dataLoadingCompletedState
states.
extension RecipientsRootVC: RecipientsViewModelDelegate {
func dataLoadingStarted() {
print("Information loading began.")
}
func dataLoadingCompleted(success: Bool) {
if success {
state = .loaded
print("Information loading accomplished efficiently.")
} else {
state = .error
print("Information loading failed.")
}
}
}
How can I implement a shimmer impact that shows whereas new knowledge masses because the consumer scrolls down?