Skip to content

Commit

Permalink
Merge pull request #11783 from wordpress-mobile/issue/11653-class-to-…
Browse files Browse the repository at this point in the history
…execute-operations-when-back-online

Implements an initial version of UploadsManager class and Uploader protocol
  • Loading branch information
diegoreymendez authored Jun 1, 2019
2 parents cc727db + 55ece5f commit 63d0f4b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
5 changes: 4 additions & 1 deletion WordPress/Classes/Services/MediaCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import WordPressFlux
/// items, independently of a specific view controller. It should be accessed
/// via the `shared` singleton.
///
class MediaCoordinator: NSObject {
class MediaCoordinator: NSObject, Uploader {
func resume() {
}


@objc static let shared = MediaCoordinator()

Expand Down
9 changes: 9 additions & 0 deletions WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,12 @@ class PostCoordinator: NSObject {
}
}
}

extension PostCoordinator: Uploader {
func resume() {
// Resume the upload of all posts that are not synched.
//
// 1. Query posts with status == .failed
// 2. Call retrySave() for each post
}
}
17 changes: 16 additions & 1 deletion WordPress/Classes/System/WordPressAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
private var bgTask: UIBackgroundTaskIdentifier? = nil

private var shouldRestoreApplicationState = false
private var uploadsManager: UploadsManager = {
// It's not great that we're using singletons here. This change is a good opportunity to
// revisit if we can make the coordinators children to another owning object.
//
// We're leaving as-is for now to avoid digressing.
let uploaders: [Uploader] = [
MediaCoordinator.shared,
PostCoordinator.shared
]

return UploadsManager(uploaders: uploaders)
}()

@objc class var shared: WordPressAppDelegate? {
return UIApplication.shared.delegate as? WordPressAppDelegate
Expand Down Expand Up @@ -102,6 +114,8 @@ class WordPressAppDelegate: UIResponder, UIApplicationDelegate {

func applicationWillEnterForeground(_ application: UIApplication) {
DDLogInfo("\(self) \(#function)")

uploadsManager.resume()
}

func applicationWillResignActive(_ application: UIApplication) {
Expand Down Expand Up @@ -210,10 +224,11 @@ class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
PushNotificationsManager.shared.registerForRemoteNotifications()

// Deferred tasks to speed up app launch
DispatchQueue.global(qos: .background).async {
DispatchQueue.global(qos: .background).async { [weak self] in
MediaCoordinator.shared.refreshMediaStatus()
PostCoordinator.shared.refreshPostStatus()
MediaFileManager.clearUnusedMediaUploadFiles(onCompletion: nil, onError: nil)
self?.uploadsManager.resume()
}

setupWordPressExtensions()
Expand Down
6 changes: 6 additions & 0 deletions WordPress/Classes/Uploads/Uploader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

protocol Uploader {
/// Starts all pending uploads
func resume()
}
50 changes: 50 additions & 0 deletions WordPress/Classes/Uploads/UploadsManager.swift
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()
}
}
}
16 changes: 16 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,8 @@
F18B43781F849F580089B817 /* PostAttachmentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F18B43771F849F580089B817 /* PostAttachmentTests.swift */; };
F1D690161F82913F00200E30 /* FeatureFlag.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1D690151F828FF000200E30 /* FeatureFlag.swift */; };
F1D690171F82914200200E30 /* BuildConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1D690141F828FF000200E30 /* BuildConfiguration.swift */; };
F1DB8D292288C14400906E2F /* Uploader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1DB8D282288C14400906E2F /* Uploader.swift */; };
F1DB8D2B2288C24500906E2F /* UploadsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1DB8D2A2288C24500906E2F /* UploadsManager.swift */; };
F928EDA3226140620030D451 /* WPCrashLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = F928EDA2226140620030D451 /* WPCrashLogging.swift */; };
F9463A7321C05EE90081F11E /* ScreenshotCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9463A7221C05EE90081F11E /* ScreenshotCredentials.swift */; };
F98C58192228849E0073D752 /* XCTest+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF2716A01CABC7D40006E2D4 /* XCTest+Extensions.swift */; };
Expand Down Expand Up @@ -3917,6 +3919,8 @@
F18B43771F849F580089B817 /* PostAttachmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PostAttachmentTests.swift; path = Posts/PostAttachmentTests.swift; sourceTree = "<group>"; };
F1D690141F828FF000200E30 /* BuildConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuildConfiguration.swift; sourceTree = "<group>"; };
F1D690151F828FF000200E30 /* FeatureFlag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureFlag.swift; sourceTree = "<group>"; };
F1DB8D282288C14400906E2F /* Uploader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Uploader.swift; sourceTree = "<group>"; };
F1DB8D2A2288C24500906E2F /* UploadsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadsManager.swift; sourceTree = "<group>"; };
F262DFCA1F94418CE76D1D78 /* Pods-WordPressNotificationContentExtension.release-internal.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressNotificationContentExtension.release-internal.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPressNotificationContentExtension/Pods-WordPressNotificationContentExtension.release-internal.xcconfig"; sourceTree = "<group>"; };
F373612EEEEF10E500093FF3 /* Pods-WordPress.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPress.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPress/Pods-WordPress.release-alpha.xcconfig"; sourceTree = "<group>"; };
F47DB4A8EC2E6844E213A3FA /* Pods_WordPressShareExtension.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WordPressShareExtension.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -4219,6 +4223,7 @@
E1CA0A6A1FA73039004C4BBE /* Stores */,
8584FDB719243E550019C02E /* System */,
8584FDB4192437160019C02E /* Utility */,
F1DB8D272288C12A00906E2F /* Uploads */,
8584FDB31923EF4F0019C02E /* ViewRelated */,
);
path = Classes;
Expand Down Expand Up @@ -8515,6 +8520,15 @@
path = BuildInformation;
sourceTree = "<group>";
};
F1DB8D272288C12A00906E2F /* Uploads */ = {
isa = PBXGroup;
children = (
F1DB8D282288C14400906E2F /* Uploader.swift */,
F1DB8D2A2288C24500906E2F /* UploadsManager.swift */,
);
path = Uploads;
sourceTree = "<group>";
};
F928EDA1226140430030D451 /* Sentry */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -10380,6 +10394,7 @@
D858F30120E20106007E8A1C /* LikePost.swift in Sources */,
1703D04C20ECD93800D292E9 /* Routes+Post.swift in Sources */,
E11C4B702010930B00A6619C /* Blog+Jetpack.swift in Sources */,
F1DB8D292288C14400906E2F /* Uploader.swift in Sources */,
821738091FE04A9E00BEC94C /* DateAndTimeFormatSettingsViewController.swift in Sources */,
5D6C4B081B603E03005E3C43 /* WPContentSyncHelper.swift in Sources */,
E6A2158E1D10627500DE5270 /* ReaderMenuViewModel.swift in Sources */,
Expand Down Expand Up @@ -10708,6 +10723,7 @@
738B9A5221B85CF20005062B /* SiteCreationWizardLauncher.swift in Sources */,
D865722E2186F96D0023A99C /* SiteSegmentsWizardContent.swift in Sources */,
E66969E01B9E648100EC9C00 /* ReaderTopicToReaderDefaultTopic37to38.swift in Sources */,
F1DB8D2B2288C24500906E2F /* UploadsManager.swift in Sources */,
9822AFA021EECB8E007D922D /* AnnualSiteStatsCell.swift in Sources */,
82A062DE2017BCBA0084CE7C /* ActivityListSectionHeaderView.swift in Sources */,
73FEC871220B358500CEF791 /* WPAccount+RestApi.swift in Sources */,
Expand Down

0 comments on commit 63d0f4b

Please sign in to comment.