-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathNetworkLoadingPlugin.swift
128 lines (106 loc) · 4.37 KB
/
NetworkLoadingPlugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// NetworkLoadingPlugin.swift
// RxNetworks
//
// Created by Condy on 2021/10/6.
// https://github.com/yangKJ/RxNetworks
///`MBProgressHUD`文档
/// https://github.com/jdg/MBProgressHUD
import Foundation
import Moya
import MBProgressHUD
import Booming
/// 加载插件,基于MBProgressHUD封装
/// Loading plugin, based on MBProgressHUD package
public struct NetworkLoadingPlugin: HasKeyAndDelayPropertyProtocol {
public var key: String?
public var delay: Double {
options.delayHideHUD
}
public var options: NetworkLoadingPlugin.Options
public init(options: NetworkLoadingPlugin.Options = .loading) {
self.options = options
}
/// Hide the loading hud.
public func hideMBProgressHUD() {
let vc = HUDs.removeHUD(key: key)
vc?.close()
}
}
extension NetworkLoadingPlugin {
public struct Options {
/// Default text hud display loading hud view controller.
public static let loading = Options.init(text: "正在加载...")
/// Loading will not be automatically hidden and display window.
public static let dontAutoHide = Options.init(text: "", autoHide: false)
/// Do you need to display an error message, the default is empty
let displayLoadText: String
/// Delay hidden, the default is zero seconds
let delayHideHUD: Double
/// Do you need to automatically hide the loading hud.
let autoHideLoading: Bool
public init(text: String = "", delay: Double = 0.0, autoHide: Bool = true) {
self.displayLoadText = text
self.delayHideHUD = delay
self.autoHideLoading = autoHide
}
var hudCallback: ((_ hud: MBProgressHUD) -> Void)?
/// Change hud related configuration closures.
public mutating func setChangeHudParameters(block: @escaping (_ hud: MBProgressHUD) -> Void) {
self.hudCallback = block
}
}
}
extension NetworkLoadingPlugin: PluginSubType {
public var pluginName: String {
return "Loading"
}
public func willSend(_ request: RequestType, target: TargetType) {
DispatchQueue.main.async {
self.showHUD()
}
}
public func outputResult(_ result: OutputResult, target: any TargetType, onNext: @escaping OutputResultBlock) {
if (hudViewController?.subtractLoadingCount() ?? 0) <= 0, options.autoHideLoading {
DispatchQueue.main.asyncAfter(deadline: .now() + options.delayHideHUD) {
if let hud = hudViewController, hud.canRemoveThisHud {
self.hideMBProgressHUD()
}
}
}
onNext(result)
}
}
extension NetworkLoadingPlugin {
private var hudViewController: LoadingHudViewController? {
HUDs.readHUD(suffix: pluginName).first as? LoadingHudViewController
}
private func showHUD() {
if let vc = hudViewController {
(vc.showUpView as? MBProgressHUD)?.detailsLabel.text = options.displayLoadText
vc.addedLoadingCount()
} else {
let vc = LoadingHudViewController(createShowUpViewCallback: {
// Set Activity Indicator View to white for hud loading.
let indicatorView = UIActivityIndicatorView.appearance(whenContainedInInstancesOf: [MBProgressHUD.self])
indicatorView.color = UIColor.white
let hud = MBProgressHUD.init(frame: .zero)//showAdded(to: vc.view, animated: true)
hud.mode = MBProgressHUDMode.indeterminate
hud.animationType = MBProgressHUDAnimation.zoom
hud.removeFromSuperViewOnHide = true
hud.bezelView.style = MBProgressHUDBackgroundStyle.solidColor
hud.bezelView.color = UIColor.black.withAlphaComponent(0.7)
hud.bezelView.layer.cornerRadius = 14
hud.detailsLabel.text = options.displayLoadText
hud.detailsLabel.font = UIFont.systemFont(ofSize: 16)
hud.detailsLabel.numberOfLines = 0
hud.detailsLabel.textColor = UIColor.white
// User defined the hud configuration.
self.options.hudCallback?(hud)
return hud
})
vc.key = self.key
vc.show()
}
}
}