2019-11-15 09:09:14 +01:00
|
|
|
//
|
|
|
|
// FeedlyLogoutOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 15/11/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
|
|
|
|
protocol FeedlyLogoutService {
|
2019-12-15 01:14:55 +01:00
|
|
|
func logout(completion: @escaping (Result<Void, Error>) -> ())
|
2019-11-15 09:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
final class FeedlyLogoutOperation: FeedlyOperation {
|
|
|
|
let service: FeedlyLogoutService
|
|
|
|
let account: Account
|
|
|
|
let log: OSLog
|
|
|
|
|
|
|
|
init(account: Account, service: FeedlyLogoutService, log: OSLog) {
|
|
|
|
self.service = service
|
|
|
|
self.account = account
|
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os_log("Requesting logout of %{public}@ account.", "\(account.type)")
|
2019-12-15 01:14:55 +01:00
|
|
|
service.logout(completion: didCompleteLogout(_:))
|
2019-11-15 09:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func didCompleteLogout(_ result: Result<Void, Error>) {
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
os_log("Logged out of %{public}@ account.", "\(account.type)")
|
|
|
|
do {
|
|
|
|
try account.removeCredentials(type: .oauthAccessToken)
|
|
|
|
try account.removeCredentials(type: .oauthRefreshToken)
|
|
|
|
} catch {
|
|
|
|
// oh well, we tried our best.
|
|
|
|
}
|
|
|
|
didFinish()
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
os_log("Logout failed because %{public}@.", error as NSError)
|
|
|
|
didFinish(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|