-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11783 from wordpress-mobile/issue/11653-class-to-…
…execute-operations-when-back-online Implements an initial version of UploadsManager class and Uploader protocol
- Loading branch information
Showing
6 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
protocol Uploader { | ||
/// Starts all pending uploads | ||
func resume() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
|
||
/// Takes care of coordinating all uploaders used by the app. | ||
/// | ||
@objc | ||
class UploadsManager: NSObject { | ||
private let uploaders: [Uploader] | ||
private lazy var reachabilityObserver: NSObjectProtocol = { | ||
return NotificationCenter.default.addObserver(forName: .reachabilityChanged, object: nil, queue: nil) { [weak self] notification in | ||
|
||
guard let self = self else { | ||
return | ||
} | ||
|
||
let internetIsReachable = notification.userInfo?[Foundation.Notification.reachabilityKey] as? Bool ?? false | ||
|
||
if internetIsReachable { | ||
self.resume() | ||
} | ||
} | ||
}() | ||
|
||
// MARK: Initialization & Finalization | ||
|
||
/// Default initializer. | ||
/// | ||
/// - Parameters: | ||
/// - uploaders: the uploaders that this object will be handling. | ||
/// | ||
required init(uploaders: [Uploader]) { | ||
self.uploaders = uploaders | ||
|
||
super.init() | ||
} | ||
|
||
deinit { | ||
NotificationCenter.default.removeObserver(reachabilityObserver) | ||
} | ||
|
||
// MARK: Interacting with Uploads | ||
|
||
/// Resumes all uploads handled by the uploaders. | ||
/// | ||
@objc | ||
func resume() { | ||
for uploader in uploaders { | ||
uploader.resume() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters