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 {
@Published var showingImportActionSheet = false
@Published var showingExportActionSheet = false
@Published var exportingFilePath = ""
@Published var feedsSettingsError: FeedsSettingsError? {
didSet {
@ -54,25 +52,12 @@ class FeedsSettingsModel: ObservableObject {
}
@Published var showError: Bool = false
func onTapExportOPML(action: ((Account?) -> Void)) {
if AccountManager.shared.accounts.count == 1 {
action(AccountManager.shared.accounts.first)
}
else {
showingExportActionSheet = true
}
}
func onTapImportOPML(action: ((Account?) -> Void)) {
switch AccountManager.shared.activeAccounts.count {
case 0:
func checkForActiveAccount() -> Bool {
if AccountManager.shared.activeAccounts.count == 0 {
feedsSettingsError = .noActiveAccount
return
case 1:
action(AccountManager.shared.activeAccounts.first)
default:
showingImportActionSheet = true
return false
}
return true
}
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
init() {

View File

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