Merge pull request #2223 from rizwankce/feeds-settings-errors

Add feeds settings import/export errors
This commit is contained in:
Maurice Parker 2020-07-06 16:44:55 -05:00 committed by GitHub
commit 7564ee33b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 4 deletions

View File

@ -10,10 +10,49 @@ import Foundation
import SwiftUI
import Account
enum FeedsSettingsError: LocalizedError, Equatable {
case none, noActiveAccount, exportFailed(reason: String?), importFailed
var errorDescription: String? {
switch self {
case .noActiveAccount:
return NSLocalizedString("You must have at least one active account.", comment: "Missing active account")
case .exportFailed(let reason):
return reason
case .importFailed:
return NSLocalizedString(
"We were unable to process the selected file. Please ensure that it is a properly formatted OPML file.",
comment: "Import Failed Message"
)
default:
return nil
}
}
var title: String? {
switch self {
case .noActiveAccount:
return NSLocalizedString("Error", comment: "Error Title")
case .exportFailed:
return NSLocalizedString("OPML Export Error", comment: "Export Failed")
case .importFailed:
return NSLocalizedString("Import Failed", comment: "Import Failed")
default:
return nil
}
}
}
class FeedsSettingsModel: ObservableObject {
@Published var showingImportActionSheet = false
@Published var showingExportActionSheet = false
@Published var exportingFilePath = ""
@Published var feedsSettingsError: FeedsSettingsError? {
didSet {
feedsSettingsError != FeedsSettingsError.none ? (showError = true) : (showError = false)
}
}
@Published var showError: Bool = false
func onTapExportOPML(action: ((Account?) -> Void)) {
if AccountManager.shared.accounts.count == 1 {
@ -27,7 +66,7 @@ class FeedsSettingsModel: ObservableObject {
func onTapImportOPML(action: ((Account?) -> Void)) {
switch AccountManager.shared.activeAccounts.count {
case 0:
//TODO:- show error
feedsSettingsError = .noActiveAccount
return
case 1:
action(AccountManager.shared.activeAccounts.first)
@ -44,7 +83,7 @@ class FeedsSettingsModel: ObservableObject {
do {
try opmlString.write(to: tempFile, atomically: true, encoding: String.Encoding.utf8)
} catch {
//TODO:- show error
feedsSettingsError = .exportFailed(reason: error.localizedDescription)
return nil
}
@ -53,12 +92,12 @@ class FeedsSettingsModel: ObservableObject {
func processImportedFiles(_ urls: [URL],_ account: Account?) {
urls.forEach{
account?.importOPML($0, completion: { result in
account?.importOPML($0, completion: { [weak self] result in
switch result {
case .success:
break
case .failure:
//TODO:- show error
self?.feedsSettingsError = .importFailed
break
}
})

View File

@ -91,6 +91,14 @@ struct SettingsView: View {
.foregroundColor(.primary)
}
})
.alert(isPresented: $feedsSettingsModel.showError) {
Alert(
title: Text(feedsSettingsModel.feedsSettingsError!.title ?? "Oops"),
message: Text(feedsSettingsModel.feedsSettingsError!.localizedDescription),
dismissButton: Alert.Button.cancel({
feedsSettingsModel.feedsSettingsError = FeedsSettingsError.none
}))
}
}