Add network suspension support

This commit is contained in:
Anh Do 2020-03-13 22:23:54 -04:00
parent bf1a732e8f
commit 8f64f7230d
No known key found for this signature in database
GPG Key ID: 451E3092F917B62D
2 changed files with 43 additions and 0 deletions

View File

@ -25,6 +25,7 @@ final class NewsBlurAPICaller: NSObject {
private let baseURL = URL(string: "https://www.newsblur.com/")!
private var transport: Transport!
private var suspended = false
var credentials: Credentials?
weak var accountMetadata: AccountMetadata?
@ -34,11 +35,26 @@ final class NewsBlurAPICaller: NSObject {
self.transport = transport
}
/// Cancels all pending requests rejects any that come in later
func suspend() {
transport.cancelAll()
suspended = true
}
func resume() {
suspended = false
}
func validateCredentials(completion: @escaping (Result<Credentials?, Error>) -> Void) {
let url = baseURL.appendingPathComponent("api/login")
let request = URLRequest(url: url, credentials: credentials)
transport.send(request: request, resultType: NewsBlurLoginResponse.self) { result in
if self.suspended {
completion(.failure(TransportError.suspended))
return
}
switch result {
case .success(let response, let payload):
guard let url = response.url, let headerFields = response.allHeaderFields as? [String: String], payload?.code != -1 else {
@ -75,6 +91,11 @@ final class NewsBlurAPICaller: NSObject {
let request = URLRequest(url: url, credentials: credentials)
transport.send(request: request) { result in
if self.suspended {
completion(.failure(TransportError.suspended))
return
}
switch result {
case .success:
completion(.success(()))
@ -99,6 +120,11 @@ final class NewsBlurAPICaller: NSObject {
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: NewsBlurFeedsResponse.self) { result in
if self.suspended {
completion(.failure(TransportError.suspended))
return
}
switch result {
case .success((_, let payload)):
completion(.success((payload?.feeds, payload?.folders)))
@ -122,6 +148,11 @@ final class NewsBlurAPICaller: NSObject {
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: NewsBlurUnreadStoryHashesResponse.self, dateDecoding: .secondsSince1970) { result in
if self.suspended {
completion(.failure(TransportError.suspended))
return
}
switch result {
case .success((_, let payload)):
completion(.success(payload?.feeds.values.flatMap { $0 }))
@ -144,6 +175,11 @@ final class NewsBlurAPICaller: NSObject {
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request, resultType: NewsBlurStoriesResponse.self) { result in
if self.suspended {
completion(.failure(TransportError.suspended))
return
}
switch result {
case .success((_, let payload)):
completion(.success(payload?.stories))

View File

@ -213,14 +213,21 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
}
// MARK: Suspend and Resume (for iOS)
/// Suspend all network activity
func suspendNetwork() {
caller.suspend()
}
/// Suspend the SQLLite databases
func suspendDatabase() {
database.suspend()
}
/// Make sure no SQLite databases are open and we are ready to issue network requests.
func resume() {
caller.resume()
database.resume()
}
}