Skip to content

Commit

Permalink
Merge pull request #59 from alladinian/master
Browse files Browse the repository at this point in the history
Fix for an issue with the networking activity indicator
  • Loading branch information
s4cha authored Apr 2, 2019
2 parents a822db0 + b7d13e9 commit d145348
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions ws/WSNetworkIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,28 @@ import Foundation
occur in order to avoid flickering.

*/
class WSNetworkIndicator: NSObject {
class WSNetworkIndicator {

static let shared = WSNetworkIndicator()
private var runningRequests = 0

func startRequest() {
runningRequests += 1
// For some unowned reason using scheduledTimer does not work in this case.
let timer = Timer(timeInterval: 1, target: self, selector: #selector(tick), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in
self?.tick()
}
}

func stopRequest() {
runningRequests -= 1
// For some unowned reason using scheduledTimer does not work in this case.
let timer = Timer(timeInterval: 0.2, target: self, selector: #selector(tick), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak self] in
self?.tick()
}
}

@objc

func tick() {
let previousValue = UIApplication.shared.isNetworkActivityIndicatorVisible
let newValue = (runningRequests != 0)
let newValue = (runningRequests > 0)
if newValue != previousValue {
UIApplication.shared.isNetworkActivityIndicatorVisible = newValue
}
Expand Down

0 comments on commit d145348

Please sign in to comment.