NetNewsWire/Multiplatform/macOS/Preferences/Preference Panes/Accounts/Account Preferences/Add Account/AddAccountView.swift

275 lines
7.0 KiB
Swift
Raw Normal View History

2020-07-13 15:41:02 +02:00
//
// AddAccountView.swift
2020-12-02 13:37:42 +01:00
// NetNewsWire
2020-07-13 15:41:02 +02:00
//
2020-12-02 13:37:42 +01:00
// Created by Stuart Breckenridge on 28/10/20.
2020-07-13 15:41:02 +02:00
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
2020-12-02 13:37:42 +01:00
enum AddAccountSections: Int, CaseIterable {
case local = 0
case icloud
case web
case selfhosted
case allOrdered
var sectionHeader: 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")
case .allOrdered:
return ""
}
}
var sectionFooter: String {
switch self {
case .local:
return NSLocalizedString("Local accounts do not sync subscriptions across devices.", comment: "Local Account")
case .icloud:
return NSLocalizedString("Use your iCloud account to sync your subscriptions across your iOS and macOS devices.", comment: "iCloud Account")
case .web:
return NSLocalizedString("Web accounts sync your subscriptions across all your devices.", comment: "Web Account")
case .selfhosted:
return NSLocalizedString("Self-hosted accounts sync your subscriptions across all your devices.", comment: "Self hosted Account")
case .allOrdered:
return ""
}
}
2020-07-13 15:41:02 +02:00
2020-12-02 13:37:42 +01:00
var sectionContent: [AccountType] {
switch self {
case .local:
return [.onMyMac]
case .icloud:
return [.cloudKit]
case .web:
#if DEBUG
return [.bazQux, .feedbin, .feedly, .feedWrangler, .inoreader, .newsBlur, .theOldReader]
#else
return [.bazQux, .feedbin, .feedly, .feedWrangler, .inoreader, .newsBlur, .theOldReader]
#endif
case .selfhosted:
return [.freshRSS]
case .allOrdered:
return AddAccountSections.local.sectionContent +
AddAccountSections.icloud.sectionContent +
AddAccountSections.web.sectionContent +
AddAccountSections.selfhosted.sectionContent
}
}
}
2020-07-13 15:41:02 +02:00
2020-12-02 13:37:42 +01:00
struct AddAccountView: View {
@State private var selectedAccount: AccountType = .onMyMac
2020-12-04 02:15:37 +01:00
@Binding public var accountToAdd: AccountConfigurationSheets
2020-12-02 13:37:42 +01:00
@Environment(\.presentationMode) var presentationMode
2020-07-13 15:41:02 +02:00
var body: some View {
2020-12-02 13:37:42 +01:00
VStack(alignment: .leading, spacing: 8) {
Text("Choose an account type to add...")
.font(.headline)
.padding()
2020-12-02 13:37:42 +01:00
localAccount
icloudAccount
webAccounts
selfhostedAccounts
2020-12-02 13:37:42 +01:00
HStack(spacing: 12) {
Spacer()
if #available(OSX 11.0, *) {
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Cancel")
.frame(width: 80)
})
2020-12-02 13:37:42 +01:00
.help("Cancel")
.keyboardShortcut(.cancelAction)
} else {
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Cancel")
.frame(width: 80)
})
.accessibility(label: Text("Add Account"))
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
if #available(OSX 11.0, *) {
Button(action: {
presentationMode.wrappedValue.dismiss()
2020-12-04 02:15:37 +01:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
accountToAdd = AccountConfigurationSheets.addSelectedAccount(selectedAccount)
})
2020-12-02 13:37:42 +01:00
}, label: {
Text("Continue")
.frame(width: 80)
})
.help("Add Account")
.keyboardShortcut(.defaultAction)
} else {
Button(action: {
2020-12-05 14:09:34 +01:00
accountToAdd = AccountConfigurationSheets.addSelectedAccount(selectedAccount)
2020-12-02 13:37:42 +01:00
presentationMode.wrappedValue.dismiss()
2020-12-05 14:09:34 +01:00
2020-12-02 13:37:42 +01:00
}, label: {
Text("Continue")
.frame(width: 80)
})
2020-07-13 15:41:02 +02:00
}
}
2020-12-02 13:37:42 +01:00
.padding(.top, 12)
.padding(.bottom, 4)
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
.pickerStyle(RadioGroupPickerStyle())
.fixedSize(horizontal: false, vertical: true)
.frame(width: 420)
.padding()
}
var localAccount: some View {
VStack(alignment: .leading) {
Text("Local")
.font(.headline)
.padding(.horizontal)
Picker(selection: $selectedAccount, label: Text(""), content: {
ForEach(AddAccountSections.local.sectionContent, id: \.self, content: { account in
HStack(alignment: .center) {
account.image()
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 25, height: 25, alignment: .center)
.padding(.leading, 4)
Text(account.localizedAccountName())
}
.tag(account)
})
})
.pickerStyle(RadioGroupPickerStyle())
.offset(x: 7.5, y: 0)
2020-12-02 13:37:42 +01:00
Text(AddAccountSections.local.sectionFooter).foregroundColor(.gray)
.font(.caption)
.padding(.horizontal)
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
var icloudAccount: some View {
VStack(alignment: .leading) {
Text("iCloud")
.font(.headline)
.padding(.horizontal)
.padding(.top, 8)
Picker(selection: $selectedAccount, label: Text(""), content: {
ForEach(AddAccountSections.icloud.sectionContent, id: \.self, content: { account in
HStack(alignment: .center) {
account.image()
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 25, height: 25, alignment: .center)
.padding(.leading, 4)
Text(account.localizedAccountName())
}
.tag(account)
})
})
.offset(x: 7.5, y: 0)
.disabled(isCloudInUse())
Text(AddAccountSections.icloud.sectionFooter).foregroundColor(.gray)
.font(.caption)
.padding(.horizontal)
}
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
var webAccounts: some View {
VStack(alignment: .leading) {
Text("Web")
.font(.headline)
.padding(.horizontal)
.padding(.top, 8)
Picker(selection: $selectedAccount, label: Text(""), content: {
ForEach(AddAccountSections.web.sectionContent, id: \.self, content: { account in
HStack(alignment: .center) {
account.image()
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 25, height: 25, alignment: .center)
.padding(.leading, 4)
Text(account.localizedAccountName())
}
.tag(account)
})
})
.offset(x: 7.5, y: 0)
Text(AddAccountSections.web.sectionFooter).foregroundColor(.gray)
.font(.caption)
.padding(.horizontal)
}
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
var selfhostedAccounts: some View {
VStack(alignment: .leading) {
Text("Self-hosted")
.font(.headline)
.padding(.horizontal)
.padding(.top, 8)
Picker(selection: $selectedAccount, label: Text(""), content: {
ForEach(AddAccountSections.selfhosted.sectionContent, id: \.self, content: { account in
HStack(alignment: .center) {
account.image()
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 25, height: 25, alignment: .center)
.padding(.leading, 4)
Text(account.localizedAccountName())
}.tag(account)
})
})
.offset(x: 7.5, y: 0)
Text(AddAccountSections.selfhosted.sectionFooter).foregroundColor(.gray)
.font(.caption)
.padding(.horizontal)
}
2020-07-13 15:41:02 +02:00
}
2020-12-02 13:37:42 +01:00
private func isCloudInUse() -> Bool {
AccountManager.shared.accounts.contains(where: { $0.type == .cloudKit })
2020-07-15 03:04:33 +02:00
}
2020-12-02 13:37:42 +01:00
private func isRestricted(_ accountType: AccountType) -> Bool {
if AppDefaults.shared.isDeveloperBuild && (accountType == .feedly || accountType == .feedWrangler || accountType == .inoreader) {
return true
}
return false
2020-07-13 15:41:02 +02:00
}
}
2020-12-02 13:37:42 +01:00