This commit is contained in:
Maurice Parker
2020-07-25 05:01:23 -05:00
12 changed files with 328 additions and 45 deletions

View File

@@ -0,0 +1,167 @@
//
// FixAccountCredentialView.swift
// NetNewsWire
//
// Created by Stuart Breckenridge on 24/7/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
struct FixAccountCredentialView: View {
let accountSyncError: AccountSyncError
@Environment(\.presentationMode) var presentationMode
@StateObject private var editModel = EditAccountCredentialsModel()
var body: some View {
#if os(macOS)
MacForm
.onAppear {
editModel.retrieveCredentials(accountSyncError.account)
}
.onChange(of: editModel.accountCredentialsWereUpdated) { value in
if value == true {
presentationMode.wrappedValue.dismiss()
}
}
.alert(isPresented: $editModel.showError) {
Alert(title: Text("Error Adding Account"),
message: Text(editModel.error.description),
dismissButton: .default(Text("Dismiss"),
action: {
editModel.error = .none
}))
}
.frame(idealWidth: 300, idealHeight: 200, alignment: .top)
.padding()
#else
iOSForm
.onAppear {
editModel.retrieveCredentials(accountSyncError.account)
}
.onChange(of: editModel.accountCredentialsWereUpdated) { value in
if value == true {
presentationMode.wrappedValue.dismiss()
}
}
.alert(isPresented: $editModel.showError) {
Alert(title: Text("Error Adding Account"),
message: Text(editModel.error.description),
dismissButton: .default(Text("Dismiss"),
action: {
editModel.error = .none
}))
}
#endif
}
var MacForm: some View {
Form {
header
HStack(alignment: .center) {
VStack(alignment: .trailing, spacing: 12) {
Text("Username: ")
Text("Password: ")
if accountSyncError.account.type == .freshRSS {
Text("API URL: ")
}
}.frame(width: 75)
VStack(alignment: .leading, spacing: 12) {
accountFields
}
}
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
HStack{
if editModel.accountIsUpdatingCredentials {
ProgressView("Updating")
}
Spacer()
cancelButton
updateButton
}
}.frame(height: 220)
}
#if os(iOS)
var iOSForm: some View {
NavigationView {
List {
Section(header: header, content: {
accountFields
})
}
.listStyle(InsetGroupedListStyle())
.navigationBarItems(
leading:
cancelButton
, trailing:
HStack {
if editModel.accountIsUpdatingCredentials {
ProgressView()
.frame(width: 20 , height: 20)
.padding(.horizontal, 4)
}
updateButton
}
)
}
}
#endif
var header: some View {
HStack {
Spacer()
VStack {
Image(rsImage: accountSyncError.account.smallIcon!.image)
.resizable()
.frame(width: 30, height: 30)
Text(accountSyncError.account.nameForDisplay)
Text(accountSyncError.error.localizedDescription)
.multilineTextAlignment(.center)
.lineLimit(3)
.padding(.top, 4)
}
Spacer()
}.padding()
}
@ViewBuilder
var accountFields: some View {
TextField("Username", text: $editModel.userName)
SecureField("Password", text: $editModel.password)
if accountSyncError.account.type == .freshRSS {
TextField("API URL", text: $editModel.apiUrl)
}
}
@ViewBuilder
var updateButton: some View {
if accountSyncError.account.type != .freshRSS {
Button("Update", action: {
editModel.updateAccountCredentials(accountSyncError.account)
}).disabled(editModel.userName.count == 0 || editModel.password.count == 0)
} else {
Button("Update", action: {
editModel.updateAccountCredentials(accountSyncError.account)
}).disabled(editModel.userName.count == 0 || editModel.password.count == 0 || editModel.apiUrl.count == 0)
}
}
var cancelButton: some View {
Button("Cancel", action: {
presentationMode.wrappedValue.dismiss()
})
}
}

View File

@@ -21,8 +21,8 @@ final class SceneModel: ObservableObject {
@Published var extractorButtonState: ArticleExtractorButtonState?
@Published var openInBrowserButtonState: Bool?
@Published var shareButtonState: Bool?
@Published var accountErrorMessage = ""
@Published var accountSyncErrors: [AccountSyncError] = []
var selectedArticles: [Article] {
return [Article]()
@@ -45,6 +45,7 @@ final class SceneModel: ObservableObject {
self.articleIconSchemeHandler = ArticleIconSchemeHandler(sceneModel: self)
self.webViewProvider = WebViewProvider(articleIconSchemeHandler: self.articleIconSchemeHandler!)
subscribeToAccountSyncErrors()
subscribeToToolbarChangeEvents()
}
@@ -143,6 +144,16 @@ private extension SceneModel {
// }.store(in: &cancellables)
}
func subscribeToAccountSyncErrors() {
NotificationCenter.default.publisher(for: .AccountsDidFailToSyncWithErrors)
.sink { [weak self] notification in
guard let errors = notification.object as? [AccountSyncError] else {
return
}
self?.accountSyncErrors = errors
}.store(in: &cancellables)
}
// MARK: Button State Updates
func updateNextUnreadButtonState(accountManager: AccountManager) {

View File

@@ -14,8 +14,8 @@ struct SceneNavigationView: View {
@StateObject private var sceneModel = SceneModel()
@State private var showSheet = false
@State private var showShareSheet = false
@State private var showRefreshError = false
@State private var sheetToShow: ToolbarSheets = .none
@State private var sheetToShow: SidebarSheets = .none
@State private var showAccountSyncErrorAlert = false // multiple sync errors
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@@ -44,31 +44,58 @@ struct SceneNavigationView: View {
.onAppear {
sceneModel.startup()
}
.sheet(isPresented: $showSheet, onDismiss: { sheetToShow = .none }) {
if sheetToShow == .web {
AddWebFeedView()
}
if sheetToShow == .folder {
AddFolderView()
}
}
.onChange(of: sheetToShow) { value in
value != .none ? (showSheet = true) : (showSheet = false)
}
.onChange(of: showRefreshError) { value in
if !value {
sceneModel.accountErrorMessage = ""
.onReceive(sceneModel.$accountSyncErrors) { errors in
if errors.count == 0 {
showAccountSyncErrorAlert = false
} else {
if errors.count > 1 {
showAccountSyncErrorAlert = true
} else {
sheetToShow = .fixCredentials
}
}
}
.onReceive(sceneModel.$accountErrorMessage) { message in
if !message.isEmpty {
showRefreshError = true
}
}
.alert(isPresented: $showRefreshError) {
Alert(title: Text("Account Error"), message: Text(verbatim: sceneModel.accountErrorMessage), dismissButton: .default(Text("OK")))
.sheet(isPresented: $showSheet,
onDismiss: {
sheetToShow = .none
sceneModel.accountSyncErrors = []
}) {
if sheetToShow == .web {
AddWebFeedView()
}
if sheetToShow == .folder {
AddFolderView()
}
#if os(iOS)
if sheetToShow == .settings {
SettingsView()
}
#endif
if sheetToShow == .fixCredentials {
FixAccountCredentialView(accountSyncError: sceneModel.accountSyncErrors[0])
}
}
.alert(isPresented: $showAccountSyncErrorAlert, content: {
#if os(macOS)
return Alert(title: Text("Account Sync Error"),
message: Text("The following accounts failed to sync: ") + Text(sceneModel.accountSyncErrors.map({ $0.account.nameForDisplay }).joined(separator: ", ")) + Text(". You can update credentials in Preferences"),
dismissButton: .default(Text("Dismiss")))
#else
return Alert(title: Text("Account Sync Error"),
message: Text("The following accounts failed to sync: ") + Text(sceneModel.accountSyncErrors.map({ $0.account.nameForDisplay }).joined(separator: ", ")) + Text(". You can update credentials in Settings"),
primaryButton: .default(Text("Show Settings"), action: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
sheetToShow = .settings
})
}),
secondaryButton: .cancel(Text("Dismiss")))
#endif
})
.toolbar {
#if os(macOS)
@@ -84,7 +111,10 @@ struct SceneNavigationView: View {
}
ToolbarItem {
Button {
AccountManager.shared.refreshAll(errorHandler: handleRefreshError)
// AccountManager.shared.refreshAll(errorHandler: handleRefreshError)
AccountManager.shared.refreshAll(completion: nil)
} label: {
AppAssets.refreshImage
}

View File

@@ -8,14 +8,14 @@
import Foundation
enum ToolbarSheets {
case none, web, twitter, reddit, folder, settings
enum SidebarSheets {
case none, web, twitter, reddit, folder, settings, fixCredentials
}
class SidebarToolbarModel: ObservableObject {
@Published var showSheet: Bool = false
@Published var sheetToShow: ToolbarSheets = .none {
@Published var sheetToShow: SidebarSheets = .none {
didSet {
sheetToShow != .none ? (showSheet = true) : (showSheet = false)
}

View File

@@ -26,6 +26,7 @@ struct SidebarView: View {
@State private var scrollOffset: CGFloat = 0
@State var pulling: Bool = false
@State var refreshing: Bool = false
@ViewBuilder var body: some View {
#if os(macOS)
@@ -110,7 +111,7 @@ struct SidebarView: View {
// Crossing the threshold on the way down, we start the refresh process
if !pulling && (scrollOffset > threshold && previousScrollOffset <= threshold) {
pulling = true
AccountManager.shared.refreshAll(errorHandler: handleRefreshError)
AccountManager.shared.refreshAll()
}
// Crossing the threshold on the way UP, we end the refresh
@@ -123,10 +124,6 @@ struct SidebarView: View {
}
}
func handleRefreshError(_ error: Error) {
sceneModel.accountErrorMessage = error.localizedDescription
}
struct RefreshFixedView: View {
var body: some View {
GeometryReader { proxy in

View File

@@ -162,7 +162,9 @@ extension EditAccountCredentialsModel {
accountIsUpdatingCredentials = true
let updateAccount = OAuthAccountAuthorizationOperation(accountType: .feedly)
updateAccount.delegate = self
#if os(macOS)
updateAccount.presentationAnchor = NSApplication.shared.windows.last
#endif
MainThreadOperationQueue.shared.add(updateAccount)
}

View File

@@ -83,7 +83,6 @@ struct EditAccountCredentialsView: View {
}
.frame(idealWidth: 300, idealHeight: 200, alignment: .top)
.padding()
}
}