mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Change settings from using SwiftUI to using UIKit
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
//
|
||||
// SettingsAccountLabelView.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Maurice Parker on 6/11/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsAccountLabelView : View {
|
||||
let accountImage: String
|
||||
let accountLabel: String
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Image(accountImage)
|
||||
.resizable()
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.frame(height: 32)
|
||||
Text(verbatim: accountLabel).font(.title)
|
||||
}
|
||||
.foregroundColor(.primary).padding(4.0)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct SettingsAccountLabelView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: "On My Device")
|
||||
.previewLayout(.fixed(width: 300, height: 44))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,52 +0,0 @@
|
||||
//
|
||||
// SettingsAddAccountView.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Maurice Parker on 6/11/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Account
|
||||
|
||||
struct SettingsAddAccountView : View {
|
||||
@Environment(\.presentationMode) var presentation
|
||||
@State private var accountAddAction: Int? = nil
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
|
||||
NavigationLink(destination: SettingsLocalAccountView(name: ""), tag: 1, selection: $accountAddAction) {
|
||||
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: Account.defaultLocalAccountName)
|
||||
}
|
||||
.modifier(VibrantSelectAction(action: {
|
||||
self.accountAddAction = 1
|
||||
})).padding(.vertical, 16)
|
||||
|
||||
NavigationLink(destination: SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel()), tag: 2, selection: $accountAddAction) {
|
||||
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin")
|
||||
|
||||
}
|
||||
.modifier(VibrantSelectAction(action: {
|
||||
self.accountAddAction = 2
|
||||
})).padding(.vertical, 16)
|
||||
|
||||
// NavigationLink(destination: SettingsReaderAPIAccountView(viewModel: SettingsReaderAPIAccountView.ViewModel(accountType: .freshRSS)), tag: 3, selection: $accountAddAction) {
|
||||
// SettingsAccountLabelView(accountImage: "accountFreshRSS", accountLabel: "Fresh RSS")
|
||||
// }
|
||||
// .modifier(VibrantSelectAction(action: {
|
||||
// self.accountAddAction = 3
|
||||
// }))
|
||||
|
||||
}
|
||||
.navigationBarTitle(Text("Add Account"), displayMode: .inline)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct AddAccountView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsAddAccountView()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,137 +0,0 @@
|
||||
//
|
||||
// SettingsDetailAccountView.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 6/13/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Combine
|
||||
import Account
|
||||
import RSWeb
|
||||
|
||||
struct SettingsDetailAccountView : View {
|
||||
@Environment(\.presentationMode) var presentation
|
||||
@ObservedObject var viewModel: ViewModel
|
||||
@State private var credentialsAction: Int? = nil
|
||||
@State private var isDeleteAlertPresented = false
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section {
|
||||
HStack {
|
||||
TextField("Name", text: $viewModel.name)
|
||||
}
|
||||
Toggle(isOn: $viewModel.isActive) {
|
||||
Text("Active")
|
||||
}
|
||||
}
|
||||
if viewModel.isCreditialsAvailable {
|
||||
if viewModel.account.type == .feedbin {
|
||||
NavigationLink(destination: self.settingsFeedbinAccountView, tag: 1, selection: $credentialsAction) {
|
||||
Text("Credentials")
|
||||
}
|
||||
.modifier(VibrantSelectAction(action: {
|
||||
self.credentialsAction = 1
|
||||
}))
|
||||
}
|
||||
if viewModel.account.type == .freshRSS {
|
||||
NavigationLink(destination: self.settingsReaderAPIAccountView, tag: 1, selection: $credentialsAction) {
|
||||
Text("Credentials")
|
||||
}
|
||||
.modifier(VibrantSelectAction(action: {
|
||||
self.credentialsAction = 1
|
||||
}))
|
||||
}
|
||||
}
|
||||
if viewModel.isDeletable {
|
||||
Section {
|
||||
Button(action: {
|
||||
self.isDeleteAlertPresented.toggle()
|
||||
}) {
|
||||
Text("Delete Account").foregroundColor(.red)
|
||||
}
|
||||
.alert(isPresented: $isDeleteAlertPresented) {
|
||||
Alert(title: Text("Are you sure you want to delete \"\(viewModel.nameForDisplay)\"?"),
|
||||
primaryButton: Alert.Button.default(Text("Delete"), action: {
|
||||
self.viewModel.delete()
|
||||
self.presentation.wrappedValue.dismiss()
|
||||
}),
|
||||
secondaryButton: Alert.Button.cancel())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.buttonStyle(VibrantButtonStyle(alignment: .center))
|
||||
.navigationBarTitle(Text(verbatim: viewModel.nameForDisplay), displayMode: .inline)
|
||||
|
||||
}
|
||||
|
||||
var settingsFeedbinAccountView: SettingsFeedbinAccountView {
|
||||
let feedbinViewModel = SettingsFeedbinAccountView.ViewModel(account: viewModel.account)
|
||||
return SettingsFeedbinAccountView(viewModel: feedbinViewModel)
|
||||
}
|
||||
|
||||
var settingsReaderAPIAccountView: SettingsReaderAPIAccountView {
|
||||
let readerAPIModel = SettingsReaderAPIAccountView.ViewModel(account: viewModel.account)
|
||||
return SettingsReaderAPIAccountView(viewModel: readerAPIModel)
|
||||
}
|
||||
|
||||
class ViewModel: ObservableObject {
|
||||
|
||||
let objectWillChange = ObservableObjectPublisher()
|
||||
|
||||
let account: Account
|
||||
|
||||
init(_ account: Account) {
|
||||
self.account = account
|
||||
}
|
||||
|
||||
var nameForDisplay: String {
|
||||
account.nameForDisplay
|
||||
}
|
||||
|
||||
var name: String {
|
||||
get {
|
||||
account.name ?? ""
|
||||
}
|
||||
set {
|
||||
objectWillChange.send()
|
||||
account.name = newValue.isEmpty ? nil : newValue
|
||||
}
|
||||
}
|
||||
|
||||
var isActive: Bool {
|
||||
get {
|
||||
account.isActive
|
||||
}
|
||||
set {
|
||||
objectWillChange.send()
|
||||
account.isActive = newValue
|
||||
}
|
||||
}
|
||||
|
||||
var isCreditialsAvailable: Bool {
|
||||
return account.type != .onMyMac
|
||||
}
|
||||
|
||||
var isDeletable: Bool {
|
||||
return AccountManager.shared.defaultAccount != account
|
||||
}
|
||||
|
||||
func delete() {
|
||||
AccountManager.shared.deleteAccount(account)
|
||||
ActivityManager.cleanUp(account)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct SettingsDetailAccountView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
let viewModel = SettingsDetailAccountView.ViewModel(AccountManager.shared.defaultAccount)
|
||||
return SettingsDetailAccountView(viewModel: viewModel)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,161 +0,0 @@
|
||||
//
|
||||
// SettingsFeedbinAccountView.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Maurice Parker on 6/11/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Combine
|
||||
import Account
|
||||
import RSWeb
|
||||
|
||||
struct SettingsFeedbinAccountView : View {
|
||||
@Environment(\.presentationMode) var presentation
|
||||
@ObservedObject var viewModel: ViewModel
|
||||
@State var busy: Bool = false
|
||||
@State var error: String = ""
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section(header:
|
||||
HStack {
|
||||
Spacer()
|
||||
SettingsAccountLabelView(accountImage: "accountFeedbin", accountLabel: "Feedbin")
|
||||
.padding()
|
||||
.layoutPriority(1.0)
|
||||
Spacer()
|
||||
}
|
||||
) {
|
||||
TextField("Email", text: $viewModel.email)
|
||||
.keyboardType(.emailAddress)
|
||||
.textContentType(.emailAddress)
|
||||
PasswordField(password: $viewModel.password)
|
||||
}
|
||||
Section(footer:
|
||||
HStack {
|
||||
Spacer()
|
||||
Text(verbatim: error).foregroundColor(.red)
|
||||
Spacer()
|
||||
}
|
||||
) {
|
||||
Button(action: { self.addAccount() }) {
|
||||
if viewModel.isUpdate {
|
||||
Text("Update Account")
|
||||
} else {
|
||||
Text("Add Account")
|
||||
}
|
||||
}
|
||||
.buttonStyle(VibrantButtonStyle(alignment: .center))
|
||||
.disabled(!viewModel.isValid)
|
||||
}
|
||||
}
|
||||
// .disabled(busy) // Maybe someday we can do this, but right now it crashes on the iPad
|
||||
.navigationBarTitle(Text(""), displayMode: .inline)
|
||||
}
|
||||
|
||||
private func addAccount() {
|
||||
|
||||
busy = true
|
||||
error = ""
|
||||
|
||||
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
|
||||
let credentials = Credentials(type: .basic, username: emailAddress, secret: viewModel.password)
|
||||
|
||||
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
|
||||
|
||||
self.busy = false
|
||||
|
||||
switch result {
|
||||
case .success(let authenticated):
|
||||
|
||||
if (authenticated != nil) {
|
||||
|
||||
var newAccount = false
|
||||
let workAccount: Account
|
||||
if self.viewModel.account == nil {
|
||||
workAccount = AccountManager.shared.createAccount(type: .feedbin)
|
||||
newAccount = true
|
||||
} else {
|
||||
workAccount = self.viewModel.account!
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
do {
|
||||
try workAccount.removeCredentials(type: .basic)
|
||||
} catch {}
|
||||
try workAccount.storeCredentials(credentials)
|
||||
|
||||
if newAccount {
|
||||
workAccount.refreshAll() { result in }
|
||||
}
|
||||
|
||||
self.dismiss()
|
||||
|
||||
} catch {
|
||||
self.error = "Keychain error while storing credentials."
|
||||
}
|
||||
|
||||
} else {
|
||||
self.error = "Invalid email/password combination."
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.error = "Network error. Try again later."
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func dismiss() {
|
||||
presentation.wrappedValue.dismiss()
|
||||
}
|
||||
|
||||
class ViewModel: ObservableObject {
|
||||
|
||||
let objectWillChange = ObservableObjectPublisher()
|
||||
var account: Account? = nil
|
||||
|
||||
init() {
|
||||
}
|
||||
|
||||
init(account: Account) {
|
||||
self.account = account
|
||||
if let credentials = try? account.retrieveCredentials(type: .basic) {
|
||||
self.email = credentials.username
|
||||
}
|
||||
}
|
||||
|
||||
var email: String = "" {
|
||||
willSet {
|
||||
objectWillChange.send()
|
||||
}
|
||||
}
|
||||
|
||||
var password: String = "" {
|
||||
willSet {
|
||||
objectWillChange.send()
|
||||
}
|
||||
}
|
||||
|
||||
var isUpdate: Bool {
|
||||
return account != nil
|
||||
}
|
||||
|
||||
var isValid: Bool {
|
||||
return !email.isEmpty && !password.isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct SettingsFeedbinAccountView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsFeedbinAccountView(viewModel: SettingsFeedbinAccountView.ViewModel())
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,59 +0,0 @@
|
||||
//
|
||||
// SettingsLocalAccountView.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Maurice Parker on 6/11/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Account
|
||||
|
||||
struct SettingsLocalAccountView : View {
|
||||
@Environment(\.presentationMode) var presentation
|
||||
@State var name: String
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section(header:
|
||||
HStack {
|
||||
Spacer()
|
||||
SettingsAccountLabelView(accountImage: "accountLocal", accountLabel: Account.defaultLocalAccountName)
|
||||
.padding()
|
||||
.layoutPriority(1.0)
|
||||
Spacer()
|
||||
}
|
||||
) {
|
||||
HStack {
|
||||
TextField("Name", text: $name)
|
||||
}
|
||||
}
|
||||
Section {
|
||||
Button(action: { self.addAccount() }) {
|
||||
Text("Add Account")
|
||||
}
|
||||
.buttonStyle(VibrantButtonStyle(alignment: .center))
|
||||
}
|
||||
}
|
||||
.navigationBarTitle(Text(""), displayMode: .inline)
|
||||
}
|
||||
|
||||
private func addAccount() {
|
||||
let account = AccountManager.shared.createAccount(type: .onMyMac)
|
||||
account.name = name
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private func dismiss() {
|
||||
presentation.wrappedValue.dismiss()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct SettingsLocalAccountView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsLocalAccountView(name: "")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,178 +0,0 @@
|
||||
//
|
||||
// SettingsReaderAPIAccountView.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Jeremy Beker on 5/28/2019.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Combine
|
||||
import Account
|
||||
import RSWeb
|
||||
|
||||
struct SettingsReaderAPIAccountView : View {
|
||||
@Environment(\.presentationMode) var presentation
|
||||
@ObservedObject var viewModel: ViewModel
|
||||
|
||||
@State var busy: Bool = false
|
||||
@State var error: String = ""
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section(header:
|
||||
HStack {
|
||||
Spacer()
|
||||
SettingsAccountLabelView(accountImage: "accountFreshRSS", accountLabel: "FreshRSS")
|
||||
.padding()
|
||||
.layoutPriority(1.0)
|
||||
Spacer()
|
||||
}
|
||||
) {
|
||||
TextField("Email", text: $viewModel.email)
|
||||
.keyboardType(.emailAddress)
|
||||
.textContentType(.emailAddress)
|
||||
SecureField("Password", text: $viewModel.password)
|
||||
TextField("API URL:", text: $viewModel.apiURL).textContentType(.URL)
|
||||
}
|
||||
|
||||
Section(footer:
|
||||
HStack {
|
||||
Spacer()
|
||||
Text(verbatim: error).foregroundColor(.red)
|
||||
Spacer()
|
||||
}
|
||||
) {
|
||||
Button(action: { self.addAccount() }) {
|
||||
if viewModel.isUpdate {
|
||||
Text("Update Account")
|
||||
} else {
|
||||
Text("Add Account")
|
||||
}
|
||||
}
|
||||
.buttonStyle(VibrantButtonStyle(alignment: .center))
|
||||
.disabled(!viewModel.isValid)
|
||||
}
|
||||
}
|
||||
// .disabled(busy)
|
||||
}
|
||||
|
||||
private func addAccount() {
|
||||
|
||||
busy = true
|
||||
error = ""
|
||||
|
||||
let emailAddress = viewModel.email.trimmingCharacters(in: .whitespaces)
|
||||
let credentials = Credentials(type: .readerBasic, username: emailAddress, secret: viewModel.password)
|
||||
guard let apiURL = URL(string: viewModel.apiURL) else {
|
||||
self.error = "Invalid API URL."
|
||||
return
|
||||
}
|
||||
|
||||
Account.validateCredentials(type: viewModel.accountType, credentials: credentials, endpoint: apiURL) { result in
|
||||
|
||||
self.busy = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.error = "Invalid email/password combination."
|
||||
return
|
||||
}
|
||||
|
||||
var newAccount = false
|
||||
let workAccount: Account
|
||||
if self.viewModel.account == nil {
|
||||
workAccount = AccountManager.shared.createAccount(type: self.viewModel.accountType)
|
||||
newAccount = true
|
||||
} else {
|
||||
workAccount = self.viewModel.account!
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
do {
|
||||
try workAccount.removeCredentials(type: .readerBasic)
|
||||
try workAccount.removeCredentials(type: .readerAPIKey)
|
||||
} catch {}
|
||||
|
||||
workAccount.endpointURL = apiURL
|
||||
|
||||
try workAccount.storeCredentials(credentials)
|
||||
try workAccount.storeCredentials(validatedCredentials)
|
||||
|
||||
if newAccount {
|
||||
workAccount.refreshAll() { result in }
|
||||
}
|
||||
|
||||
self.dismiss()
|
||||
|
||||
} catch {
|
||||
self.error = "Keychain error while storing credentials."
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.error = "Network error. Try again later."
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func dismiss() {
|
||||
presentation.wrappedValue.dismiss()
|
||||
}
|
||||
|
||||
class ViewModel: ObservableObject {
|
||||
|
||||
let objectWillChange = ObservableObjectPublisher()
|
||||
var accountType: AccountType
|
||||
var account: Account? = nil
|
||||
|
||||
init(accountType: AccountType) {
|
||||
self.accountType = accountType
|
||||
}
|
||||
|
||||
init(account: Account) {
|
||||
self.account = account
|
||||
self.accountType = account.type
|
||||
if let credentials = try? account.retrieveCredentials(type: .readerBasic) {
|
||||
self.email = credentials.username
|
||||
self.apiURL = account.endpointURL?.absoluteString ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
var email: String = "" {
|
||||
willSet {
|
||||
objectWillChange.send()
|
||||
}
|
||||
}
|
||||
var password: String = "" {
|
||||
willSet {
|
||||
objectWillChange.send()
|
||||
}
|
||||
}
|
||||
var apiURL: String = "" {
|
||||
willSet {
|
||||
objectWillChange.send()
|
||||
}
|
||||
}
|
||||
var isUpdate: Bool {
|
||||
return account != nil
|
||||
}
|
||||
|
||||
var isValid: Bool {
|
||||
return !email.isEmpty && !password.isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
struct SettingsReaderAPIAccountView_Previews : PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsReaderAPIAccountView(viewModel: SettingsReaderAPIAccountView.ViewModel(accountType: .freshRSS))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user