frenzy-ios/Reader/FervorController.swift

60 lines
1.9 KiB
Swift
Raw Normal View History

2021-12-08 02:58:02 +00:00
//
// 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?
2021-12-25 19:04:45 +00:00
private(set) var persistentContainer: PersistentContainer!
2021-12-08 02:58:02 +00:00
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
2021-12-25 19:04:45 +00:00
self.client.accessToken = account.accessToken
self.persistentContainer = PersistentContainer(account: account)
2021-12-08 02:58:02 +00:00
}
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
}
2021-12-25 19:04:45 +00:00
func syncGroupsAndFeeds() async {
async let groups = try! client.groups()
async let feeds = try! client.feeds()
try! await persistentContainer.sync(serverGroups: groups, serverFeeds: feeds)
}
2021-12-08 02:58:02 +00:00
}