Skip to content

Commit 83f6821

Browse files
committed
Added loading view
1 parent 12192d7 commit 83f6821

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

Sources/CoreUsefulSDK/Extensions/UIApplication+TopViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#if !os(macOS) && !os(watchOS)
2424
import UIKit
2525

26-
extension UIApplication {
26+
public extension UIApplication {
2727

2828
/// Returns very top view controller.
2929
class func topViewController() -> UIViewController? {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Burak Uzunboy on 25.08.20.
6+
//
7+
8+
#if !os(macOS) && !os(watchOS)
9+
import UIKit
10+
11+
/**
12+
Shows Loading Indicator View on top of the root view controller when needed.
13+
*/
14+
open class LoadingIndicator {
15+
16+
/// Tint Color that will be applied to the activity indicator.
17+
public static var tintColor: UIColor = .red {
18+
didSet {
19+
LoadingIndicator.shared.activityIndicator.color = LoadingIndicator.tintColor
20+
}}
21+
22+
/// Background color of the container view.
23+
public static var backgroundColor: UIColor = UIColor.gray.withAlphaComponent(0.5) {
24+
didSet {
25+
LoadingIndicator.shared.activityContainer.backgroundColor = LoadingIndicator.tintColor
26+
}}
27+
28+
/// Singleton instance.
29+
private static let shared = LoadingIndicator()
30+
/// Activity indicator view.
31+
private var activityIndicator: UIActivityIndicatorView
32+
/// Container view of the activity indicator.
33+
private var activityContainer: UIView
34+
35+
/// Private default initializer.
36+
private init() {
37+
self.activityContainer = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
38+
self.activityContainer.backgroundColor = LoadingIndicator.backgroundColor
39+
self.activityContainer.cornerRadius = 16
40+
41+
self.activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
42+
self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
43+
self.activityIndicator.color = LoadingIndicator.tintColor
44+
self.activityIndicator.autoresizingMask = [.flexibleBottomMargin, .flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin]
45+
self.activityIndicator.alpha = 1.0
46+
self.activityIndicator.hidesWhenStopped = false
47+
self.activityIndicator.startAnimating()
48+
49+
self.activityContainer.addSubview(self.activityIndicator)
50+
self.activityContainer.bringSubviewToFront(self.activityIndicator)
51+
self.activityIndicator.center = self.activityContainer.center
52+
}
53+
54+
/// Starts to show the loading indicator in top view controller.
55+
public class func show() {
56+
guard let topVC = UIApplication.topViewController() else { return }
57+
58+
let instance = LoadingIndicator.shared
59+
instance.activityContainer.removeFromSuperview()
60+
61+
instance.activityContainer.alpha = 0.0
62+
63+
topVC.view.addSubview(instance.activityContainer)
64+
topVC.view.bringSubviewToFront(instance.activityContainer)
65+
66+
instance.activityContainer.center = topVC.view.center
67+
UIView.animate(withDuration: 0.3) {
68+
instance.activityContainer.alpha = 1.0
69+
}
70+
}
71+
72+
/// Stops and hides the loading indicator from the view even view is changed.
73+
public class func stop(animated: Bool = true) {
74+
let instance = LoadingIndicator.shared
75+
76+
if animated {
77+
UIView.animate(withDuration: 0.3, animations: {
78+
instance.activityContainer.alpha = 0.0
79+
}) { (_) in
80+
instance.activityContainer.removeFromSuperview()
81+
}
82+
} else {
83+
instance.activityContainer.removeFromSuperview()
84+
}
85+
}
86+
87+
}
88+
89+
#endif

0 commit comments

Comments
 (0)