NetNewsWire/iOS/Settings/SettingsView.swift

189 lines
5.6 KiB
Swift
Raw Normal View History

2019-06-11 23:59:16 +02:00
//
// SettingsView.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 6/11/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import SwiftUI
2019-06-13 21:30:56 +02:00
import Combine
2019-06-11 23:59:16 +02:00
import Account
2019-06-11 23:59:16 +02:00
struct SettingsView : View {
2019-06-13 21:30:56 +02:00
@ObjectBinding var viewModel: ViewModel
2019-06-16 17:54:18 +02:00
@State var importSubscriptionsAccounts: ActionSheet? = nil
@State var importSubscriptionsDocumentPicker: Modal? = nil
2019-06-16 01:19:20 +02:00
@State var showExportSubscriptions = false
2019-06-11 23:59:16 +02:00
var body: some View {
NavigationView {
List {
Section(header: Text("ACCOUNTS")) {
ForEach(viewModel.accounts.identified(by: \.self)) { account in
2019-06-13 21:30:56 +02:00
NavigationButton(destination: SettingsDetailAccountView(viewModel: SettingsDetailAccountView.ViewModel(account)), isDetail: false) {
Text(verbatim: account.nameForDisplay)
}
2019-06-11 23:59:16 +02:00
}
NavigationButton(destination: SettingsAddAccountView(), isDetail: false) {
Text("Add Account")
}
}
Section(header: Text("ABOUT")) {
Text("About NetNewsWire")
PresentationButton(Text("Website"), destination: SafariView(url: URL(string: "https://ranchero.com/netnewswire/")!))
PresentationButton(Text("Github Repository"), destination: SafariView(url: URL(string: "https://github.com/brentsimmons/NetNewsWire")!))
PresentationButton(Text("Bug Tracker"), destination: SafariView(url: URL(string: "https://github.com/brentsimmons/NetNewsWire/issues")!))
PresentationButton(Text("Technotes"), destination: SafariView(url: URL(string: "https://github.com/brentsimmons/NetNewsWire/tree/master/Technotes")!))
PresentationButton(Text("How to Support NetNewsWire"), destination: SafariView(url: URL(string: "https://github.com/brentsimmons/NetNewsWire/blob/master/Technotes/HowToSupportNetNewsWire.markdown")!))
2019-06-11 23:59:16 +02:00
Text("Add NetNewsWire News Feed")
}
.foregroundColor(.primary)
Section(header: Text("TIMELINE")) {
Toggle(isOn: $viewModel.sortOldestToNewest) {
Text("Sort Oldest to Newest")
}
Stepper(value: $viewModel.timelineNumberOfLines, in: 2...6) {
Text("Number of Text Lines: \(viewModel.timelineNumberOfLines)")
}
}
Section(header: Text("DATABASE")) {
Picker(selection: $viewModel.refreshInterval, label: Text("Refresh Interval")) {
ForEach(RefreshInterval.allCases.identified(by: \.self)) { interval in
Text(interval.description()).tag(interval)
}
}
2019-06-16 01:19:20 +02:00
Button(action: {
2019-06-16 17:54:18 +02:00
self.importSubscriptionsAccounts = self.createImportSubscriptionsAccounts
2019-06-16 01:19:20 +02:00
}) {
Text("Import Subscriptions...")
}
2019-06-16 17:54:18 +02:00
.presentation(importSubscriptionsAccounts)
.presentation(importSubscriptionsDocumentPicker)
2019-06-16 01:19:20 +02:00
Button(action: {
self.showExportSubscriptions = true
}) {
Text("Export Subscriptions...")
}
2019-06-16 17:54:18 +02:00
.presentation(showExportSubscriptions ? exportSubscriptionsAccounts : nil)
2019-06-11 23:59:16 +02:00
}
2019-06-16 01:19:20 +02:00
.foregroundColor(.primary)
2019-06-11 23:59:16 +02:00
}
.listStyle(.grouped)
.navigationBarTitle(Text("Settings"), displayMode: .inline)
}
}
2019-06-13 21:30:56 +02:00
2019-06-16 17:54:18 +02:00
var createImportSubscriptionsAccounts: ActionSheet {
2019-06-16 01:19:20 +02:00
var buttons = [ActionSheet.Button]()
for account in viewModel.accounts {
let button = ActionSheet.Button.default(Text(verbatim: account.nameForDisplay)) {
2019-06-16 17:54:18 +02:00
self.importSubscriptionsAccounts = nil
self.importSubscriptionsDocumentPicker = Modal(SettingsImportSubscriptionsDocumentPickerView(account: account))
2019-06-16 01:19:20 +02:00
}
buttons.append(button)
}
2019-06-16 17:54:18 +02:00
buttons.append(.cancel { self.importSubscriptionsAccounts = nil })
2019-06-16 01:19:20 +02:00
return ActionSheet(title: Text("Import Subscriptions..."), message: Text("Select the account to import your OPML file into."), buttons: buttons)
}
2019-06-16 17:54:18 +02:00
var exportSubscriptionsAccounts: ActionSheet {
2019-06-16 01:19:20 +02:00
var buttons = [ActionSheet.Button]()
for account in viewModel.accounts {
let button = ActionSheet.Button.default(Text(verbatim: account.nameForDisplay)) {
self.showExportSubscriptions = false
// Call doc picker here...
}
buttons.append(button)
}
2019-06-16 17:54:18 +02:00
buttons.append(.cancel { self.showExportSubscriptions = false })
2019-06-16 01:19:20 +02:00
return ActionSheet(title: Text("Export Subscriptions..."), message: Text("Select the account to export out of."), buttons: buttons)
}
2019-06-13 21:30:56 +02:00
class ViewModel: BindableObject {
let didChange = PassthroughSubject<ViewModel, Never>()
init() {
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange(_:)), name: .AccountsDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
}
var accounts: [Account] {
get {
return AccountManager.shared.sortedAccounts
}
set {
}
}
var sortOldestToNewest: Bool {
get {
return AppDefaults.timelineSortDirection == .orderedDescending
}
set {
if newValue == true {
AppDefaults.timelineSortDirection = .orderedDescending
} else {
AppDefaults.timelineSortDirection = .orderedAscending
}
didChange.send(self)
}
}
var timelineNumberOfLines: Int {
get {
return AppDefaults.timelineNumberOfLines
}
set {
AppDefaults.timelineNumberOfLines = newValue
didChange.send(self)
}
}
var refreshInterval: RefreshInterval {
get {
return AppDefaults.refreshInterval
}
set {
AppDefaults.refreshInterval = newValue
didChange.send(self)
}
}
@objc func accountsDidChange(_ notification: Notification) {
didChange.send(self)
}
@objc func displayNameDidChange(_ notification: Notification) {
didChange.send(self)
}
}
2019-06-11 23:59:16 +02:00
}
#if DEBUG
struct SettingsView_Previews : PreviewProvider {
static var previews: some View {
2019-06-13 21:30:56 +02:00
SettingsView(viewModel: SettingsView.ViewModel())
2019-06-11 23:59:16 +02:00
}
}
#endif