frenzy-ios/Reader/FervorController.swift

60 lines
1.9 KiB
Swift

//
// FervorController.swift
// Reader
//
// Created by Shadowfacts on 11/25/21.
//
import Foundation
import Fervor
class FervorController {
static let oauthRedirectURI = URL(string: "frenzy://oauth-callback")!
let instanceURL: URL
private let client: FervorClient
private(set) var clientID: String?
private(set) var clientSecret: String?
private(set) var accessToken: String?
private(set) var persistentContainer: PersistentContainer!
init(instanceURL: URL) {
self.instanceURL = instanceURL
self.client = FervorClient(instanceURL: instanceURL, accessToken: nil)
}
convenience init(account: LocalData.Account) {
self.init(instanceURL: account.instanceURL)
self.clientID = account.clientID
self.clientSecret = account.clientSecret
self.accessToken = account.accessToken
self.client.accessToken = account.accessToken
self.persistentContainer = PersistentContainer(account: account)
}
func register() async throws -> ClientRegistration {
let registration = try await client.register(clientName: "Frenzy iOS", website: nil, redirectURI: FervorController.oauthRedirectURI)
clientID = registration.clientID
clientSecret = registration.clientSecret
return registration
}
func getToken(authCode: String) async throws {
let token = try await client.token(authCode: authCode, redirectURI: FervorController.oauthRedirectURI, clientID: clientID!, clientSecret: clientSecret!)
client.accessToken = token.accessToken
accessToken = token.accessToken
}
func syncGroupsAndFeeds() async {
async let groups = try! client.groups()
async let feeds = try! client.feeds()
try! await persistentContainer.sync(serverGroups: groups, serverFeeds: feeds)
}
}