Files
NetNewsWire/iOS/Settings/AddAccountView.swift
T
Dave MarquardandIngmar Stein ba63d58712 Add native Miniflux account type and sync support
Adds Miniflux as a first-class account type alongside Feedbin, Feedly,
NewsBlur, and Reader API accounts, on both macOS and iOS.

Refreshes are incremental: the changed_after parameter means read and
starred status ride along with the entries in a single pass instead of
needing separate status scans. Articles missing locally are backfilled by
ID via GET /v1/entries/{id}; a 404 means the entry is gone server-side, so
its statuses are deleted and it is not requested again on every refresh.
Starred changes go out as idempotent batch updates.

Requires Miniflux 2.0.49 or newer. The server version is detected at
runtime via GET /v1/version and persisted in AccountSettings. Servers
older than 2.3.2 have no /v1/entries/ids and no batch starred updates, so
those paths fall back transparently to paged /v1/entries and per-entry
bookmark toggles.

Both HTTP Basic and API-token authentication are supported, selectable in
the account sheet on either platform. Entered URLs are normalized (scheme
added, trailing slash and /v1 suffix stripped), and adding a second
account for a server that already has one is rejected.

Tests cover folder sync, model decoding, version parsing, the pre-2.3.2
fallback paths, and duplicate-endpoint detection, with JSON fixtures for
the API responses.

The runtime version detection and the duplicate-endpoint check were
informed by IngmarStein's independent Miniflux implementation in #5335.

Written with help from Claude Code.

Co-Authored-By: Ingmar Stein <490610+IngmarStein@users.noreply.github.com>
2026-11-22 07:06:11 -08:00

184 lines
5.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// AddAccountView.swift
// NetNewsWire-iOS
//
// Created by Brent Simmons on 9/18/26.
//
import SwiftUI
import AuthenticationServices
import RSCore
import Account
/// The list of account types the user picks from. Pushed onto the Settings navigation stack.
struct AddAccountView: View {
/// Window the Feedly OAuth sheet attaches to. Feedly can’t be added without one.
let presentationAnchor: ASPresentationAnchor?
/// Called after an account is added so the host can pop this screen.
let didAddAccount: () -> Void
@State private var presentedSheet: AccountSheet?
@State private var errorMessage: String?
@State private var oauthHandler = OAuthAuthorizationHandler()
private static let iconSize: CGFloat = 25
private static let rowSpacing: CGFloat = 16
private enum AccountGroup: Int, CaseIterable {
case local
case icloud
case web
case selfhosted
var header: String {
switch self {
case .local:
return NSLocalizedString("Local", comment: "Local Account")
case .icloud:
return NSLocalizedString("iCloud", comment: "iCloud Account")
case .web:
return NSLocalizedString("Web", comment: "Web Account")
case .selfhosted:
return NSLocalizedString("Self-hosted", comment: "Self hosted Account")
}
}
var footer: String {
switch self {
case .local:
return NSLocalizedString("Local accounts do not sync your feeds across devices.", comment: "Local")
case .icloud:
return NSLocalizedString("Your iCloud account syncs your feeds across your Mac and iOS devices.", comment: "iCloud Account")
case .web:
return NSLocalizedString("Web accounts sync your feeds across all your devices.", comment: "Web Account")
case .selfhosted:
return NSLocalizedString("Self-hosted accounts sync your feeds across all your devices.", comment: "Self hosted Account")
}
}
var accountTypes: [AccountType] {
switch self {
case .local:
return [.onMyMac]
case .icloud:
return [.cloudKit]
case .web:
return [.bazQux, .feedbin, .feedly, .inoreader, .newsBlur, .theOldReader]
case .selfhosted:
return [.freshRSS, .miniflux]
}
}
}
private struct AccountSheet: Identifiable {
let accountType: AccountType
var id: Int {
accountType.rawValue
}
}
var body: some View {
List {
ForEach(visibleGroups, id: \.self) { group in
Section {
ForEach(visibleAccountTypes(in: group), id: \.rawValue) { accountType in
accountRow(accountType)
}
} header: {
Text(group.header)
} footer: {
Text(group.footer)
}
}
}
.navigationTitle(NSLocalizedString("Add Account", comment: "Add Account"))
.sheet(item: $presentedSheet) { sheet in
switch sheet.accountType {
case .onMyMac:
LocalAccountView(didAddAccount: didAddAccount)
case .cloudKit:
CloudKitAccountView(didAddAccount: didAddAccount)
case .miniflux:
MinifluxAccountView(account: nil, didAddAccount: didAddAccount)
default:
CredentialsAccountView(accountType: sheet.accountType, account: nil, didAddAccount: didAddAccount)
}
}
.errorAlert(message: $errorMessage)
}
private func accountRow(_ accountType: AccountType) -> some View {
Button {
select(accountType)
} label: {
HStack(spacing: Self.rowSpacing) {
Image(uiImage: Assets.accountImage(accountType))
.resizable()
.scaledToFit()
.frame(width: Self.iconSize, height: Self.iconSize)
Text(verbatim: accountType.displayName)
}
}
.foregroundStyle(isDisabled(accountType) ? .secondary : .primary)
.disabled(isDisabled(accountType))
}
/// Groups with at least one account type to show. Developer builds hide the restricted types, as the Mac app does.
private var visibleGroups: [AccountGroup] {
AccountGroup.allCases.filter { !visibleAccountTypes(in: $0).isEmpty }
}
private func visibleAccountTypes(in group: AccountGroup) -> [AccountType] {
group.accountTypes.filter { !(AppDefaults.isDeveloperBuild && $0.isDeveloperRestricted) }
}
private func isDisabled(_ accountType: AccountType) -> Bool {
accountType == .cloudKit && AccountManager.shared.hasiCloudAccount
}
private func select(_ accountType: AccountType) {
switch accountType {
case .feedly:
startOAuth(for: accountType)
default:
presentedSheet = AccountSheet(accountType: accountType)
}
}
private func startOAuth(for accountType: AccountType) {
guard let presentationAnchor else {
return
}
oauthHandler.didCreateAccount = { account in
account.triggerRefreshAll()
didAddAccount()
}
oauthHandler.didFail = { error in
errorMessage = error.localizedDescription
}
let operation = OAuthAccountAuthorizationOperation(accountType: accountType)
operation.delegate = oauthHandler
operation.presentationAnchor = presentationAnchor
MainThreadOperationQueue.shared.add(operation)
}
}
/// Forwards the OAuth operation’s delegate callbacks to closures, since a View can’t be the delegate.
@MainActor private final class OAuthAuthorizationHandler: OAuthAccountAuthorizationOperationDelegate {
var didCreateAccount: ((Account) -> Void)?
var didFail: ((Error) -> Void)?
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
didCreateAccount?(account)
}
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
didFail?(error)
}
}