NetNewsWire/Multiplatform/Shared/Add/AddWebFeedView.swift

164 lines
3.8 KiB
Swift
Raw Normal View History

2020-07-03 14:31:48 +02:00
//
// AddWebFeedView.swift
2020-07-03 14:31:48 +02:00
// NetNewsWire
//
// Created by Stuart Breckenridge on 3/7/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
import RSCore
struct AddWebFeedView: View {
2020-07-03 14:31:48 +02:00
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private var viewModel = AddWebFeedModel()
2020-07-03 14:31:48 +02:00
@ViewBuilder var body: some View {
#if os(iOS)
iosForm
.onAppear {
viewModel.pasteUrlFromPasteboard()
}
.onReceive(viewModel.$shouldDismiss, perform: { dismiss in
if dismiss == true {
presentationMode.wrappedValue.dismiss()
}
})
2020-07-03 14:31:48 +02:00
#else
macForm
.onAppear {
viewModel.pasteUrlFromPasteboard()
2020-07-03 14:31:48 +02:00
}.alert(isPresented: $viewModel.showError) {
Alert(title: Text("Oops"), message: Text(viewModel.addFeedError!.localizedDescription), dismissButton: Alert.Button.cancel({
viewModel.addFeedError = AddWebFeedError.none
2020-07-03 14:31:48 +02:00
}))
}.onReceive(viewModel.$shouldDismiss, perform: { dismiss in
if dismiss == true {
presentationMode.wrappedValue.dismiss()
}
})
2020-07-03 14:31:48 +02:00
#endif
}
#if os(macOS)
var macForm: some View {
Form {
HStack {
Spacer()
Image(systemName: "globe").foregroundColor(.accentColor).font(.title)
Text("Add a Web Feed")
.font(.title)
Spacer()
}
2020-07-03 14:31:48 +02:00
urlTextField
.textFieldStyle(RoundedBorderTextFieldStyle())
.help("The URL of the feed you want to add.")
2020-07-03 14:31:48 +02:00
providedNameTextField
.textFieldStyle(RoundedBorderTextFieldStyle())
.help("The name of the feed. (Optional.)")
2020-07-03 14:31:48 +02:00
folderPicker
.help("Pick the folder you want to add the feed to.")
2020-07-03 14:31:48 +02:00
buttonStack
}
.padding()
.frame(minWidth: 450)
2020-07-03 14:31:48 +02:00
}
#endif
#if os(iOS)
@ViewBuilder var iosForm: some View {
NavigationView {
Form {
urlTextField
providedNameTextField
folderPicker
}
.listStyle(InsetGroupedListStyle())
.navigationBarTitle("Add Web Feed")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(leading:
Button("Cancel", action: {
presentationMode.wrappedValue.dismiss()
})
.help("Cancel Add Feed")
, trailing:
Button("Add", action: {
viewModel.addWebFeed()
})
.disabled(!viewModel.providedURL.isValidURL)
.help("Add Feed")
)
2020-07-03 14:31:48 +02:00
}
}
#endif
var urlTextField: some View {
HStack {
Text("Feed:")
#if os(iOS)
2020-07-03 14:31:48 +02:00
TextField("URL", text: $viewModel.providedURL)
.disableAutocorrection(true)
.autocapitalization(UITextAutocapitalizationType.none)
#else
TextField("URL", text: $viewModel.providedURL)
.disableAutocorrection(true)
#endif
2020-07-03 14:31:48 +02:00
}
}
var providedNameTextField: some View {
HStack(alignment: .lastTextBaseline) {
Text("Name:")
2020-07-03 14:31:48 +02:00
TextField("Optional", text: $viewModel.providedName)
}
}
var folderPicker: some View {
Picker("Folder:", selection: $viewModel.selectedFolderIndex, content: {
2020-07-03 14:31:48 +02:00
ForEach(0..<viewModel.containers.count, id: \.self, content: { index in
if let containerName = (viewModel.containers[index] as? DisplayNameProvider)?.nameForDisplay {
if viewModel.containers[index] is Folder {
Text("\(viewModel.containers[index].account?.nameForDisplay ?? "") / \(containerName)").tag(index)
} else {
Text(containerName).tag(index)
}
}
})
})
2020-07-03 14:31:48 +02:00
}
var buttonStack: some View {
HStack {
if viewModel.showProgressIndicator == true {
ProgressView()
.frame(width: 25, height: 25)
.help("Adding Feed")
2020-07-03 14:31:48 +02:00
}
Spacer()
Button("Cancel", action: {
presentationMode.wrappedValue.dismiss()
})
.help("Cancel Add Feed")
2020-07-03 14:31:48 +02:00
Button("Add", action: {
viewModel.addWebFeed()
})
.disabled(!viewModel.providedURL.isValidURL)
.help("Add Feed")
2020-07-03 14:31:48 +02:00
}
}
}
struct AddFeedView_Previews: PreviewProvider {
static var previews: some View {
AddWebFeedView()
2020-07-03 14:31:48 +02:00
}
}