-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathNetworkIndicatorPlugin.swift
47 lines (36 loc) · 1.15 KB
/
NetworkIndicatorPlugin.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
//
// NetworkIndicatorPlugin.swift
// RxNetworks
//
// Created by Condy on 2021/10/6.
// https://github.com/yangKJ/RxNetworks
import Foundation
import Moya
#if canImport(UIKit)
import UIKit
/// 指示器插件,该插件已被设置为全局使用
/// Indicator plug-in, the plug-in has been set for global use
public final class NetworkIndicatorPlugin {
public static let shared = NetworkIndicatorPlugin()
private var numberOfRequests: Int = 0 {
didSet {
if numberOfRequests > 1 { return }
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = self.numberOfRequests > 0
}
}
}
private init() { }
}
extension NetworkIndicatorPlugin: PluginSubType {
public var pluginName: String {
return "Indicator"
}
public func willSend(_ request: RequestType, target: TargetType) {
NetworkIndicatorPlugin.shared.numberOfRequests += 1
}
public func didReceive(_ result: Result<Moya.Response, MoyaError>, target: TargetType) {
NetworkIndicatorPlugin.shared.numberOfRequests -= 1
}
}
#endif