Add AccountRefreshMode to be able to choose between background and foreground download sessions

This commit is contained in:
Maurice Parker 2019-04-25 09:25:13 -05:00
parent f3c2d8c1f8
commit f5941fda64
9 changed files with 62 additions and 21 deletions

View File

@ -209,9 +209,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
// MARK: - API
public func refreshAll() {
public func restore() {
delegate.restore()
}
public func refreshAll(refreshMode: AccountRefreshMode) {
delegate.refreshAll(for: self)
delegate.refreshAll(for: self, refreshMode: refreshMode)
}
public func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: @escaping RSVoidCompletionBlock) {
@ -356,7 +360,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
structureDidChange()
DispatchQueue.main.async {
self.refreshAll()
self.refreshAll(refreshMode: .forground)
}
}

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
519881F52271EF43008D4FA8 /* AccountRefreshMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 519881F42271EF43008D4FA8 /* AccountRefreshMode.swift */; };
841973FE1F6DD1BC006346C4 /* RSCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 841973EF1F6DD19E006346C4 /* RSCore.framework */; };
841973FF1F6DD1C5006346C4 /* RSParser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 841973FA1F6DD1AC006346C4 /* RSParser.framework */; };
841974011F6DD1EC006346C4 /* Folder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841974001F6DD1EC006346C4 /* Folder.swift */; };
@ -83,6 +84,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
519881F42271EF43008D4FA8 /* AccountRefreshMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountRefreshMode.swift; sourceTree = "<group>"; };
841973E81F6DD19E006346C4 /* RSCore.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RSCore.xcodeproj; path = ../RSCore/RSCore.xcodeproj; sourceTree = "<group>"; };
841973F41F6DD1AC006346C4 /* RSParser.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RSParser.xcodeproj; path = ../RSParser/RSParser.xcodeproj; sourceTree = "<group>"; };
841974001F6DD1EC006346C4 /* Folder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Folder.swift; sourceTree = "<group>"; };
@ -203,6 +205,7 @@
isa = PBXGroup;
children = (
846E77531F6F00E300A165E2 /* AccountManager.swift */,
519881F42271EF43008D4FA8 /* AccountRefreshMode.swift */,
848935101F62486800CEBD24 /* Account.swift */,
84AF4EA3222CFDD100F6A800 /* AccountSettings.swift */,
841974241F6DDCE4006346C4 /* AccountDelegate.swift */,
@ -427,6 +430,7 @@
84B99C9F1FAE8D3200ECDEDB /* ContainerPath.swift in Sources */,
846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */,
84D09623217418DC00D77525 /* FeedbinTagging.swift in Sources */,
519881F52271EF43008D4FA8 /* AccountRefreshMode.swift in Sources */,
84CAD7161FDF2E22000F0755 /* FeedbinArticle.swift in Sources */,
84D0962521741B8500D77525 /* FeedbinSavedSearch.swift in Sources */,
841974011F6DD1EC006346C4 /* Folder.swift in Sources */,

View File

@ -16,7 +16,9 @@ public protocol AccountDelegate {
var refreshProgress: DownloadProgress { get }
func refreshAll(for: Account)
func restore()
func refreshAll(for: Account, refreshMode: AccountRefreshMode)
// Called at the end of accounts init method.

View File

@ -84,9 +84,13 @@ public final class AccountManager: UnreadCountProvider {
return accountsDictionary[accountID]
}
public func refreshAll() {
public func restore() {
accounts.forEach { $0.restore() }
}
public func refreshAll(refreshMode: AccountRefreshMode = .forground) {
accounts.forEach { $0.refreshAll() }
accounts.forEach { $0.refreshAll(refreshMode: refreshMode) }
}
public func anyAccountHasAtLeastOneFeed() -> Bool {

View File

@ -0,0 +1,14 @@
//
// AccountRefreshMode.swift
// Account
//
// Created by Maurice Parker on 4/25/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public enum AccountRefreshMode {
case background
case forground
}

View File

@ -18,9 +18,13 @@ final class LocalAccountDelegate: AccountDelegate {
return refresher.progress
}
func refreshAll(for account: Account) {
func restore() {
refresher.restore()
}
func refreshAll(for account: Account, refreshMode: AccountRefreshMode) {
refresher.refreshFeeds(account.flattenedFeeds())
refresher.refreshFeeds(account.flattenedFeeds(), refreshMode: refreshMode)
}
func accountDidInitialize(_ account: Account) {

View File

@ -14,18 +14,29 @@ import Articles
final class LocalAccountRefresher {
private lazy var downloadSession: DownloadSession = {
return DefaultDownloadSession(delegate: self)
var progress = DownloadProgress(numberOfTasks: 0)
private lazy var backgroundDownloadSession: DownloadSession = {
return BackgroundDownloadSession(delegate: self, progress: DownloadProgress(numberOfTasks: 0))
}()
private lazy var defaultDownloadSession: DownloadSession = {
return DefaultDownloadSession(delegate: self, progress: progress)
}()
var progress: DownloadProgress {
return downloadSession.progress
public func restore() {
_ = backgroundDownloadSession
_ = defaultDownloadSession
}
public func refreshFeeds(_ feeds: Set<Feed>) {
downloadSession.downloadObjects(feeds as NSSet)
public func refreshFeeds(_ feeds: Set<Feed>, refreshMode: AccountRefreshMode) {
if refreshMode == .forground {
defaultDownloadSession.downloadObjects(feeds as NSSet)
} else {
backgroundDownloadSession.downloadObjects(feeds as NSSet)
}
}
}
// MARK: - DownloadSessionDelegate

View File

@ -45,9 +45,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
// Initialize the AccountManager as soon as possible or it will cause problems
// if the application is restoring preserved state.
_ = AccountManager.shared
AccountManager.shared.restore()
}
@ -185,7 +183,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
logDebugMessage("Woken to fetch articles.")
AccountManager.shared.refreshAll()
AccountManager.shared.refreshAll(refreshMode: .background)
completionHandler(.newData)
}

@ -1 +1 @@
Subproject commit a1b94ee858506af6a70b5b7fdfd9f6227b9eae45
Subproject commit 8b517a8b53bdbbbf2c3bf2b809ca51d586fcf6ad