2019-10-19 01:31:00 +02:00
|
|
|
//
|
|
|
|
// AddFeedIntentHandler.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/18/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Intents
|
2019-10-19 02:37:41 +02:00
|
|
|
import Account
|
2019-10-19 01:31:00 +02:00
|
|
|
|
|
|
|
public class AddFeedIntentHandler: NSObject, AddFeedIntentHandling {
|
|
|
|
|
2019-10-19 02:37:41 +02:00
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
DispatchQueue.main.sync {
|
|
|
|
AccountManager.shared = AccountManager()
|
|
|
|
}
|
2019-10-19 01:31:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 02:37:41 +02:00
|
|
|
public func resolveUrl(for intent: AddFeedIntent, with completion: @escaping (AddFeedUrlResolutionResult) -> Void) {
|
|
|
|
guard let url = intent.url else {
|
|
|
|
completion(.unsupported(forReason: .required))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completion(.success(with: url))
|
2019-10-19 01:31:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 02:37:41 +02:00
|
|
|
public func handle(intent: AddFeedIntent, completion: @escaping (AddFeedIntentResponse) -> Void) {
|
|
|
|
guard let url = intent.url else {
|
|
|
|
completion(AddFeedIntentResponse(code: .failure, userActivity: nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let account = AccountManager.shared.activeAccounts.first else {
|
|
|
|
completion(AddFeedIntentResponse(code: .failure, userActivity: nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
account.createFeed(url: url.absoluteString, name: nil, container: account) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
completion(AddFeedIntentResponse(code: .success, userActivity: nil))
|
|
|
|
case .failure:
|
|
|
|
completion(AddFeedIntentResponse(code: .failure, userActivity: nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-19 01:31:00 +02:00
|
|
|
|
|
|
|
}
|