Skip to content

Commit

Permalink
create superClass for loading view and empty state views
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Apr 12, 2020
1 parent c0b7c87 commit 6a1c689
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 55 deletions.
4 changes: 4 additions & 0 deletions GHFollowers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
7DB7350D243F523700E7B09B /* PersistanceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7350C243F523700E7B09B /* PersistanceManager.swift */; };
7DB7350F243F8AC100E7B09B /* FavoriteCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7350E243F8AC100E7B09B /* FavoriteCell.swift */; };
7DB7351224409BFE00E7B09B /* GFTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7351124409BFE00E7B09B /* GFTabBarController.swift */; };
7DB735142443929200E7B09B /* GFDataLoadingVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB735132443929200E7B09B /* GFDataLoadingVC.swift */; };
7DFAC88A242A86D200F5780C /* GFAlertContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -81,6 +82,7 @@
7DB7350C243F523700E7B09B /* PersistanceManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistanceManager.swift; sourceTree = "<group>"; };
7DB7350E243F8AC100E7B09B /* FavoriteCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteCell.swift; sourceTree = "<group>"; };
7DB7351124409BFE00E7B09B /* GFTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFTabBarController.swift; sourceTree = "<group>"; };
7DB735132443929200E7B09B /* GFDataLoadingVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFDataLoadingVC.swift; sourceTree = "<group>"; };
7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFAlertContainerView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -270,6 +272,7 @@
7D7C66592438A2B300733687 /* ItemInfoVCs-SuperClass+SubClass */,
7D98BB49242A55ED006C53E9 /* GFAlertVC.swift */,
7DA45F842433FAF800AB426F /* GFUserInfoheaderVC.swift */,
7DB735132443929200E7B09B /* GFDataLoadingVC.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -358,6 +361,7 @@
7D98BB40242917E4006C53E9 /* GFButton.swift in Sources */,
7D98BB3C24290771006C53E9 /* SearchVC.swift in Sources */,
7DA45F7A242FB95700AB426F /* GFEmptyStateView.swift in Sources */,
7DB735142443929200E7B09B /* GFDataLoadingVC.swift in Sources */,
7D98BB4A242A55ED006C53E9 /* GFAlertVC.swift in Sources */,
7DA45F66242B760500AB426F /* User.swift in Sources */,
7DA45F6F242BC47D00AB426F /* FollowerCell.swift in Sources */,
Expand Down
Binary file not shown.
67 changes: 67 additions & 0 deletions GHFollowers/Components/ViewControllers/GFDataLoadingVC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// GFDataLoadingVC.swift
// GHFollowers
//
// Created by Vasileios Gkreen on 12/04/2020.
// Copyright © 2020 Vasileios Gkreen. All rights reserved.
//

import UIKit

class GFDataLoadingVC: UIViewController {

var containerView: UIView!

func showLoadingView() {
// init container view and set it to fill the entire screen
containerView = UIView(frame: view.bounds)
// add the container view into the VC view (which ever is going to call this func)
view.addSubview(containerView)

containerView.backgroundColor = .systemBackground
containerView.alpha = 0

// animate alpha
UIView.animate(withDuration: 0.25) {
self.containerView.alpha = 0.8
}


// add activity indicator
let activityIndicator = UIActivityIndicatorView(style: .large)
containerView.addSubview(activityIndicator)

activityIndicator.translatesAutoresizingMaskIntoConstraints = false

// center indicator on containerView
NSLayoutConstraint.activate([
activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor),
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])

activityIndicator.startAnimating()

}

func dismissLoadingView() {
// We will always going to dismiss Loading View from a background thread
// because we call this function from the Network manager closure (promise)
// to avoid that we need to throw it in the main thread
DispatchQueue.main.async {
self.containerView.removeFromSuperview()
self.containerView = nil
}
}


// calling this function from a VC we wiil pass the message and a the view so we know where to constrain it
func showEmptyStateView(with message: String, in view: UIView) {
let emptyStateView = GFEmptyStateView(message: message)
// fill up the entire screen
emptyStateView.frame = view.bounds
// add it to the VC subView
view.addSubview(emptyStateView)
}


}
54 changes: 1 addition & 53 deletions GHFollowers/Extensions/UIViewController+EXT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SafariServices

// We cant save variables inside an extension so we decalre a 'global' variable inside this file
// This global var with the keyword 'fileprivate' is available only in this file and not the entire programm
fileprivate var containerView: UIView!
// fileprivate var containerView: UIView!


// This extension is to be applied to all UIViewControllers used in this app
Expand All @@ -35,58 +35,6 @@ extension UIViewController {
}


func showLoadingView() {
// init container view and set it to fill the entire screen
containerView = UIView(frame: view.bounds)
// add the container view into the VC view (which ever is going to call this func)
view.addSubview(containerView)

containerView.backgroundColor = .systemBackground
containerView.alpha = 0

// animate alpha
UIView.animate(withDuration: 0.25) {
containerView.alpha = 0.8
}


// add activity indicator
let activityIndicator = UIActivityIndicatorView(style: .large)
containerView.addSubview(activityIndicator)

activityIndicator.translatesAutoresizingMaskIntoConstraints = false

// center indicator on containerView
NSLayoutConstraint.activate([
activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor),
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])

activityIndicator.startAnimating()

}

func dismissLoadingView() {
// We will always going to dismiss Loading View from a background thread
// because we call this function from the Network manager closure (promise)
// to avoid that we need to throw it in the main thread
DispatchQueue.main.async {
containerView.removeFromSuperview()
containerView = nil
}
}


// calling this function from a VC we wiil pass the message and a the view so we know where to constrain it
func showEmptyStateView(with message: String, in view: UIView) {
let emptyStateView = GFEmptyStateView(message: message)
// fill up the entire screen
emptyStateView.frame = view.bounds
// add it to the VC subView
view.addSubview(emptyStateView)
}


func preserntSafariVC(with url: URL) {
let safariVC = SFSafariViewController(url: url)
safariVC.preferredControlTintColor = .systemGreen
Expand Down
2 changes: 1 addition & 1 deletion GHFollowers/Screens/FavoriteListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

class FavoriteListVC: UIViewController {
class FavoriteListVC: GFDataLoadingVC {

let tableView = UITableView()
var favorites: [Follower] = []
Expand Down
2 changes: 1 addition & 1 deletion GHFollowers/Screens/FollowerListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protocol FollowerListVCDelegate: class {



class FollowerListVC: UIViewController {
class FollowerListVC: GFDataLoadingVC {

// enums are hashable by default
enum Section {
Expand Down

0 comments on commit 6a1c689

Please sign in to comment.