From d776ba9c6ec94d6622e1f759c112fd2334b7fb8b Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 21 Mar 2022 19:36:43 +1100 Subject: Loading overlay to custom class + add to coordinator Just an idea I had, since it was quite messy that a TableViewController was handling all that. Instead now it should be reusable through the ForayCoordinator itself, which means e.g. on details screen, we can show the loading overlay. --- foray.xcodeproj/project.pbxproj | 4 ++++ foray/ForayCoordinator.swift | 10 ++++++++++ foray/ForayLoadingOverlay.swift | 37 ++++++++++++++++++++++++++++++++++++ foray/ForayTableViewController.swift | 12 ++---------- 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 foray/ForayLoadingOverlay.swift diff --git a/foray.xcodeproj/project.pbxproj b/foray.xcodeproj/project.pbxproj index b0a62f6..79eb5cb 100644 --- a/foray.xcodeproj/project.pbxproj +++ b/foray.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ C04B45B827DEF2ED001451A3 /* ForayTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */; }; C04EDE4227E428AB00D83005 /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = C04EDE4127E428AB00D83005 /* SnapKit */; }; C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */; }; + C09676BA27E86B6E00353D46 /* ForayLoadingOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */; }; C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */; }; /* End PBXBuildFile section */ @@ -32,6 +33,7 @@ C04B45B127DEF118001451A3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C04B45B727DEF2ED001451A3 /* ForayTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayTableViewController.swift; sourceTree = ""; }; C04EDE4327E4298D00D83005 /* ForayNewTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayNewTableViewCell.swift; sourceTree = ""; }; + C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayLoadingOverlay.swift; sourceTree = ""; }; C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -77,6 +79,7 @@ C04B45B127DEF118001451A3 /* Info.plist */, C049BBFD27E82B9E003820A9 /* Coordinator.swift */, C049BBFF27E82C90003820A9 /* ForayCoordinator.swift */, + C09676B927E86B6E00353D46 /* ForayLoadingOverlay.swift */, ); path = foray; sourceTree = ""; @@ -163,6 +166,7 @@ C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */, C04EDE4427E4298D00D83005 /* ForayNewTableViewCell.swift in Sources */, C04B45A427DEF117001451A3 /* AppDelegate.swift in Sources */, + C09676BA27E86B6E00353D46 /* ForayLoadingOverlay.swift in Sources */, C011E4F327E6216C00C248D6 /* ForayItems.swift in Sources */, C04B45A627DEF117001451A3 /* SceneDelegate.swift in Sources */, ); diff --git a/foray/ForayCoordinator.swift b/foray/ForayCoordinator.swift index 8fe6656..6497ec0 100644 --- a/foray/ForayCoordinator.swift +++ b/foray/ForayCoordinator.swift @@ -15,9 +15,11 @@ protocol ForayCoordinated: UIViewController { class ForayCoordinator: Coordinator { var childCoordinators = [Coordinator]() var navigationController: UINavigationController + var loadingOverlay: ForayLoadingOverlay init(navigationController: UINavigationController) { self.navigationController = navigationController + self.loadingOverlay = ForayLoadingOverlay(viewController: navigationController) } private func push(vc: ForayCoordinated, animated: Bool = true) { @@ -50,4 +52,12 @@ class ForayCoordinator: Coordinator { detailViewController.setDetails(name: item.name, description: description, image: image) push(vc: detailViewController) } + + func showLoading() { + loadingOverlay.show() + } + + func hideLoading() { + loadingOverlay.hide() + } } diff --git a/foray/ForayLoadingOverlay.swift b/foray/ForayLoadingOverlay.swift new file mode 100644 index 0000000..1aa6260 --- /dev/null +++ b/foray/ForayLoadingOverlay.swift @@ -0,0 +1,37 @@ +// +// ForayLoadingOverlay.swift +// foray +// +// Created by Nicholas Tay on 21/3/2022. +// + +import UIKit + +class ForayLoadingOverlay { + + var viewController: UIViewController + + let loadingIndicator: UIActivityIndicatorView = { + let aiv = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) + aiv.hidesWhenStopped = true + aiv.style = UIActivityIndicatorView.Style.medium + aiv.startAnimating() + return aiv + }() + + let alert: UIAlertController = UIAlertController(title: nil, message: "Grabbing data...", preferredStyle: .alert) + + init(viewController: UIViewController) { + self.viewController = viewController + alert.view.addSubview(self.loadingIndicator) + } + + func show() { + viewController.present(alert, animated: true) + } + + func hide() { + viewController.dismiss(animated: false) + } + +} diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 827d4ea..2b7ba65 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -33,18 +33,9 @@ class ForayTableViewController: UITableViewController, ForayCoordinated { tableView.register(ForayNewTableViewCell.self, forCellReuseIdentifier: "ForayNewTableViewCell") // Not sure if this is the right way to go about this... - let alert = UIAlertController(title: nil, message: "Grabbing data...", preferredStyle: .alert) - let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) - loadingIndicator.hidesWhenStopped = true - loadingIndicator.style = UIActivityIndicatorView.Style.medium - loadingIndicator.startAnimating(); - alert.view.addSubview(loadingIndicator) - present(alert, animated: true, completion: nil) - + coordinator?.showLoading() reloadApiData() - dismiss(animated: false, completion: nil) - self.refreshControl = UIRefreshControl() self.refreshControl?.addTarget(self, action: #selector(doRefresh), for: UIControl.Event.valueChanged) } @@ -71,6 +62,7 @@ class ForayTableViewController: UITableViewController, ForayCoordinated { self.tableView.reloadData() self.refreshControl?.endRefreshing() + self.coordinator?.hideLoading() }) } -- cgit