2020-07-03 14:31:48 +02:00
|
|
|
//
|
2020-07-03 17:43:20 +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
|
|
|
|
|
|
|
|
|
2020-07-03 17:43:20 +02:00
|
|
|
struct AddWebFeedView: View {
|
2020-07-03 14:31:48 +02:00
|
|
|
|
2020-07-09 11:36:17 +02:00
|
|
|
@StateObject private var viewModel = AddWebFeedModel()
|
2020-08-13 18:04:39 +02:00
|
|
|
@Binding var isPresented: Bool
|
2020-07-03 14:31:48 +02:00
|
|
|
|
2020-08-12 15:04:56 +02:00
|
|
|
var body: some View {
|
2020-07-03 14:31:48 +02:00
|
|
|
#if os(iOS)
|
|
|
|
iosForm
|
2020-07-04 03:07:55 +02:00
|
|
|
.onAppear {
|
|
|
|
viewModel.pasteUrlFromPasteboard()
|
|
|
|
}
|
|
|
|
.onReceive(viewModel.$shouldDismiss, perform: { dismiss in
|
|
|
|
if dismiss == true {
|
2020-08-13 18:04:39 +02:00
|
|
|
isPresented = false
|
2020-07-04 03:07:55 +02:00
|
|
|
}
|
|
|
|
})
|
2020-07-03 14:31:48 +02:00
|
|
|
#else
|
|
|
|
macForm
|
|
|
|
.onAppear {
|
2020-07-04 03:07:55 +02:00
|
|
|
viewModel.pasteUrlFromPasteboard()
|
2020-07-03 14:31:48 +02:00
|
|
|
}.alert(isPresented: $viewModel.showError) {
|
2020-07-09 11:36:17 +02:00
|
|
|
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
|
|
|
}))
|
2020-08-13 18:04:39 +02:00
|
|
|
}
|
|
|
|
.onChange(of: viewModel.shouldDismiss, perform: { dismiss in
|
2020-07-04 03:07:55 +02:00
|
|
|
if dismiss == true {
|
2020-08-13 18:04:39 +02:00
|
|
|
isPresented = false
|
2020-07-04 03:07:55 +02:00
|
|
|
}
|
|
|
|
})
|
2020-07-03 14:31:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
var macForm: some View {
|
2020-07-03 16:58:39 +02:00
|
|
|
Form {
|
2020-07-03 15:43:55 +02:00
|
|
|
HStack {
|
|
|
|
Spacer()
|
2020-07-04 16:01:01 +02:00
|
|
|
Image(rsImage: AppAssets.faviconTemplateImage)
|
2020-07-04 15:07:18 +02:00
|
|
|
.resizable()
|
|
|
|
.renderingMode(.template)
|
|
|
|
.frame(width: 30, height: 30)
|
2020-07-03 15:43:55 +02:00
|
|
|
Text("Add a Web Feed")
|
|
|
|
.font(.title)
|
|
|
|
Spacer()
|
2020-07-05 03:06:53 +02:00
|
|
|
}.padding()
|
2020-07-04 15:07:18 +02:00
|
|
|
|
|
|
|
LazyVGrid(columns: [GridItem(.fixed(75), spacing: 10, alignment: .trailing),GridItem(.fixed(400), spacing: 0, alignment: .leading) ], alignment: .leading, spacing: 10, pinnedViews: [], content:{
|
|
|
|
Text("URL:").bold()
|
|
|
|
urlTextField
|
|
|
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
|
|
|
.help("The URL of the feed you want to add.")
|
|
|
|
Text("Name:").bold()
|
|
|
|
providedNameTextField
|
|
|
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
|
|
|
.help("The name of the feed. (Optional.)")
|
|
|
|
Text("Folder:").bold()
|
|
|
|
folderPicker
|
|
|
|
.help("Pick the folder you want to add the feed to.")
|
|
|
|
})
|
2020-07-03 14:31:48 +02:00
|
|
|
buttonStack
|
|
|
|
}
|
2020-07-04 15:07:18 +02:00
|
|
|
.frame(maxWidth: 485)
|
|
|
|
.padding(12)
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if os(iOS)
|
2020-08-13 16:25:34 +02:00
|
|
|
var iosForm: some View {
|
2020-07-03 17:43:20 +02:00
|
|
|
NavigationView {
|
2020-07-05 03:06:53 +02:00
|
|
|
List {
|
2020-07-03 17:43:20 +02:00
|
|
|
urlTextField
|
|
|
|
providedNameTextField
|
|
|
|
folderPicker
|
|
|
|
}
|
|
|
|
.listStyle(InsetGroupedListStyle())
|
2020-07-04 03:31:58 +02:00
|
|
|
.navigationBarTitle("Add Web Feed")
|
2020-07-03 17:43:20 +02:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.navigationBarItems(leading:
|
|
|
|
Button("Cancel", action: {
|
2020-08-13 18:04:39 +02:00
|
|
|
isPresented = false
|
2020-07-03 17:43:20 +02:00
|
|
|
})
|
|
|
|
.help("Cancel Add Feed")
|
|
|
|
, trailing:
|
2020-07-04 17:08:12 +02:00
|
|
|
HStack(spacing: 12) {
|
|
|
|
if viewModel.showProgressIndicator == true {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
Button("Add", action: {
|
|
|
|
viewModel.addWebFeed()
|
|
|
|
})
|
|
|
|
.disabled(!viewModel.providedURL.mayBeURL)
|
|
|
|
.help("Add Feed")
|
|
|
|
}
|
2020-07-03 17:43:20 +02:00
|
|
|
)
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-05 03:06:53 +02:00
|
|
|
@ViewBuilder var urlTextField: some View {
|
|
|
|
#if os(iOS)
|
|
|
|
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 {
|
2020-07-09 11:04:04 +02:00
|
|
|
TextField("Title (Optional)", text: $viewModel.providedName)
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 15:07:18 +02:00
|
|
|
@ViewBuilder var folderPicker: some View {
|
|
|
|
#if os(iOS)
|
2020-07-05 03:06:53 +02:00
|
|
|
Picker("Folder", selection: $viewModel.selectedFolderIndex, content: {
|
2020-07-25 20:53:46 +02:00
|
|
|
ForEach(0..<viewModel.containers.count, id: \.self, content: { position in
|
|
|
|
if let containerName = (viewModel.containers[position] as? DisplayNameProvider)?.nameForDisplay {
|
|
|
|
if viewModel.containers[position] is Folder {
|
2020-07-05 03:06:53 +02:00
|
|
|
HStack(alignment: .top) {
|
2020-07-25 20:53:46 +02:00
|
|
|
if let image = viewModel.smallIconImage(for: viewModel.containers[position]) {
|
2020-07-09 11:36:17 +02:00
|
|
|
Image(rsImage: image)
|
2020-07-11 18:07:37 +02:00
|
|
|
.foregroundColor(Color("AccentColor"))
|
2020-07-05 03:06:53 +02:00
|
|
|
}
|
2020-07-09 11:36:17 +02:00
|
|
|
Text("\(containerName)")
|
2020-07-25 20:53:46 +02:00
|
|
|
.tag(position)
|
2020-07-05 03:06:53 +02:00
|
|
|
}.padding(.leading, 16)
|
2020-07-03 14:31:48 +02:00
|
|
|
} else {
|
2020-07-05 03:06:53 +02:00
|
|
|
HStack(alignment: .top) {
|
2020-07-25 20:53:46 +02:00
|
|
|
if let image = viewModel.smallIconImage(for: viewModel.containers[position]) {
|
2020-07-09 11:36:17 +02:00
|
|
|
Image(rsImage: image)
|
2020-07-11 18:07:37 +02:00
|
|
|
.foregroundColor(Color("AccentColor"))
|
2020-07-05 03:06:53 +02:00
|
|
|
}
|
2020-07-09 11:36:17 +02:00
|
|
|
Text(containerName)
|
2020-07-25 20:53:46 +02:00
|
|
|
.tag(position)
|
2020-07-05 03:06:53 +02:00
|
|
|
}
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-07-03 17:43:20 +02:00
|
|
|
})
|
2020-07-04 15:07:18 +02:00
|
|
|
#else
|
|
|
|
Picker("", selection: $viewModel.selectedFolderIndex, content: {
|
|
|
|
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 {
|
2020-07-05 03:06:53 +02:00
|
|
|
HStack {
|
|
|
|
if let image = viewModel.smallIconImage(for: viewModel.containers[index]) {
|
|
|
|
Image(rsImage: image)
|
|
|
|
}
|
|
|
|
Text("\(containerName)")
|
2020-07-09 11:36:17 +02:00
|
|
|
}
|
|
|
|
.padding(.leading, 2)
|
|
|
|
.tag(index)
|
2020-07-04 15:07:18 +02:00
|
|
|
} else {
|
2020-07-09 11:36:17 +02:00
|
|
|
Text(containerName)
|
|
|
|
.padding(.leading, 2)
|
|
|
|
.tag(index)
|
2020-07-04 15:07:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-07-09 11:36:17 +02:00
|
|
|
})
|
|
|
|
.padding(.leading, -8)
|
2020-07-04 15:07:18 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var buttonStack: some View {
|
|
|
|
HStack {
|
|
|
|
if viewModel.showProgressIndicator == true {
|
|
|
|
ProgressView()
|
|
|
|
.frame(width: 25, height: 25)
|
2020-07-03 15:43:55 +02:00
|
|
|
.help("Adding Feed")
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
Button("Cancel", action: {
|
2020-08-13 18:04:39 +02:00
|
|
|
isPresented = false
|
2020-07-03 14:31:48 +02:00
|
|
|
})
|
2020-07-03 15:43:55 +02:00
|
|
|
.help("Cancel Add Feed")
|
|
|
|
|
2020-07-03 14:31:48 +02:00
|
|
|
Button("Add", action: {
|
2020-07-04 03:07:55 +02:00
|
|
|
viewModel.addWebFeed()
|
2020-07-03 15:43:55 +02:00
|
|
|
})
|
2020-07-04 17:08:12 +02:00
|
|
|
.disabled(!viewModel.providedURL.mayBeURL)
|
2020-07-03 15:43:55 +02:00
|
|
|
.help("Add Feed")
|
2020-07-09 11:36:17 +02:00
|
|
|
}
|
|
|
|
.padding(.trailing, 2)
|
2020-07-03 14:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:04:39 +02:00
|
|
|
|