Add subscriptions import/export navigation link with list

- refactors export/import action sheet to be a new screen with list options
This commit is contained in:
Rizwan Mohamed Ibrahim 2020-07-24 20:50:43 +05:30
parent 75b9264d44
commit cb409728aa
No known key found for this signature in database
GPG Key ID: D5BEE468D448BCC5
3 changed files with 63 additions and 53 deletions

View File

@ -44,8 +44,6 @@ enum FeedsSettingsError: LocalizedError, Equatable {
} }
class FeedsSettingsModel: ObservableObject { class FeedsSettingsModel: ObservableObject {
@Published var showingImportActionSheet = false
@Published var showingExportActionSheet = false
@Published var exportingFilePath = "" @Published var exportingFilePath = ""
@Published var feedsSettingsError: FeedsSettingsError? { @Published var feedsSettingsError: FeedsSettingsError? {
didSet { didSet {
@ -54,25 +52,12 @@ class FeedsSettingsModel: ObservableObject {
} }
@Published var showError: Bool = false @Published var showError: Bool = false
func onTapExportOPML(action: ((Account?) -> Void)) { func checkForActiveAccount() -> Bool {
if AccountManager.shared.accounts.count == 1 { if AccountManager.shared.activeAccounts.count == 0 {
action(AccountManager.shared.accounts.first)
}
else {
showingExportActionSheet = true
}
}
func onTapImportOPML(action: ((Account?) -> Void)) {
switch AccountManager.shared.activeAccounts.count {
case 0:
feedsSettingsError = .noActiveAccount feedsSettingsError = .noActiveAccount
return return false
case 1:
action(AccountManager.shared.activeAccounts.first)
default:
showingImportActionSheet = true
} }
return true
} }
func generateExportURL(for account: Account) -> URL? { func generateExportURL(for account: Account) -> URL? {

View File

@ -46,6 +46,15 @@ class SettingsModel: ObservableObject {
} }
} }
var activeAccounts: [Account] {
get {
AccountManager.shared.sortedActiveAccounts
}
set {
}
}
// MARK: Init // MARK: Init
init() { init() {

View File

@ -75,19 +75,30 @@ struct SettingsView: View {
var importExport: some View { var importExport: some View {
Section(header: Text("Feeds"), content: { Section(header: Text("Feeds"), content: {
Button(action:{ if viewModel.activeAccounts.count > 1 {
feedsSettingsModel.onTapImportOPML(action: importOPML) NavigationLink("Import Subscriptions", destination: importOptions)
}) {
Text("Import Subscriptions")
.actionSheet(isPresented: $feedsSettingsModel.showingImportActionSheet, content: importActionSheet)
.foregroundColor(.primary)
} }
Button(action:{ else {
feedsSettingsModel.onTapExportOPML(action: exportOPML) Button(action:{
}) { if feedsSettingsModel.checkForActiveAccount() {
Text("Export Subscriptions") importOPML(account: viewModel.activeAccounts.first)
.actionSheet(isPresented: $feedsSettingsModel.showingExportActionSheet, content: exportActionSheet) }
.foregroundColor(.primary) }) {
Text("Import Subscriptions")
.foregroundColor(.primary)
}
}
if viewModel.accounts.count > 1 {
NavigationLink("Export Subscriptions", destination: exportOptions)
}
else {
Button(action:{
exportOPML(account: viewModel.accounts.first)
}) {
Text("Export Subscriptions")
.foregroundColor(.primary)
}
} }
}) })
.alert(isPresented: $feedsSettingsModel.showError) { .alert(isPresented: $feedsSettingsModel.showError) {
@ -98,35 +109,40 @@ struct SettingsView: View {
feedsSettingsModel.feedsSettingsError = FeedsSettingsError.none feedsSettingsModel.feedsSettingsError = FeedsSettingsError.none
})) }))
} }
} }
private func importActionSheet() -> ActionSheet { var importOptions: some View {
var buttons = viewModel.accounts.map { (account) -> ActionSheet.Button in List {
ActionSheet.Button.default(Text(account.nameForDisplay)) { Section(header: Text("Choose an account to receive the imported feeds and folders"), content: {
importOPML(account: account) ForEach(0..<viewModel.activeAccounts.count, id: \.hashValue , content: { i in
} Button {
importOPML(account: viewModel.activeAccounts[i])
} label: {
Text(viewModel.activeAccounts[i].nameForDisplay)
}
})
})
} }
buttons.append(.cancel()) .listStyle(InsetGroupedListStyle())
return ActionSheet( .navigationBarTitle("Import Subscriptions", displayMode: .inline)
title: Text("Choose an account to receive the imported feeds and folders"),
buttons: buttons
)
} }
private func exportActionSheet() -> ActionSheet { var exportOptions: some View {
var buttons = viewModel.accounts.map { (account) -> ActionSheet.Button in List {
ActionSheet.Button.default(Text(account.nameForDisplay)) { Section(header: Text("Choose an account with the subscriptions to export"), content: {
exportOPML(account: account) ForEach(0..<viewModel.accounts.count, id: \.hashValue , content: { i in
} Button {
exportOPML(account: viewModel.accounts[i])
} label: {
Text(viewModel.accounts[i].nameForDisplay)
}
})
})
} }
buttons.append(.cancel()) .listStyle(InsetGroupedListStyle())
return ActionSheet( .navigationBarTitle("Export Subscriptions", displayMode: .inline)
title: Text("Choose an account with the subscriptions to export"),
buttons: buttons
)
} }
var timeline: some View { var timeline: some View {
Section(header: Text("Timeline"), content: { Section(header: Text("Timeline"), content: {
Toggle("Sort Oldest to Newest", isOn: $settings.timelineSortDirection) Toggle("Sort Oldest to Newest", isOn: $settings.timelineSortDirection)