Add feeds settings import/export errors
This commit is contained in:
parent
6695fcfca7
commit
cbac7a85cb
|
@ -10,10 +10,49 @@ import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import Account
|
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 {
|
class FeedsSettingsModel: ObservableObject {
|
||||||
@Published var showingImportActionSheet = false
|
@Published var showingImportActionSheet = false
|
||||||
@Published var showingExportActionSheet = false
|
@Published var showingExportActionSheet = false
|
||||||
@Published var exportingFilePath = ""
|
@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)) {
|
func onTapExportOPML(action: ((Account?) -> Void)) {
|
||||||
if AccountManager.shared.accounts.count == 1 {
|
if AccountManager.shared.accounts.count == 1 {
|
||||||
|
@ -27,7 +66,7 @@ class FeedsSettingsModel: ObservableObject {
|
||||||
func onTapImportOPML(action: ((Account?) -> Void)) {
|
func onTapImportOPML(action: ((Account?) -> Void)) {
|
||||||
switch AccountManager.shared.activeAccounts.count {
|
switch AccountManager.shared.activeAccounts.count {
|
||||||
case 0:
|
case 0:
|
||||||
//TODO:- show error
|
feedsSettingsError = .noActiveAccount
|
||||||
return
|
return
|
||||||
case 1:
|
case 1:
|
||||||
action(AccountManager.shared.activeAccounts.first)
|
action(AccountManager.shared.activeAccounts.first)
|
||||||
|
@ -44,7 +83,7 @@ class FeedsSettingsModel: ObservableObject {
|
||||||
do {
|
do {
|
||||||
try opmlString.write(to: tempFile, atomically: true, encoding: String.Encoding.utf8)
|
try opmlString.write(to: tempFile, atomically: true, encoding: String.Encoding.utf8)
|
||||||
} catch {
|
} catch {
|
||||||
//TODO:- show error
|
feedsSettingsError = .exportFailed(reason: error.localizedDescription)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,12 +92,12 @@ class FeedsSettingsModel: ObservableObject {
|
||||||
|
|
||||||
func processImportedFiles(_ urls: [URL],_ account: Account?) {
|
func processImportedFiles(_ urls: [URL],_ account: Account?) {
|
||||||
urls.forEach{
|
urls.forEach{
|
||||||
account?.importOPML($0, completion: { result in
|
account?.importOPML($0, completion: { [weak self] result in
|
||||||
switch result {
|
switch result {
|
||||||
case .success:
|
case .success:
|
||||||
break
|
break
|
||||||
case .failure:
|
case .failure:
|
||||||
//TODO:- show error
|
self?.feedsSettingsError = .importFailed
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -91,6 +91,14 @@ struct SettingsView: View {
|
||||||
.foregroundColor(.primary)
|
.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
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue