Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Settings UI #46

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
192 changes: 125 additions & 67 deletions ishare/Views/Settings/UploaderSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,98 +193,128 @@ struct AddCustomUploaderView: View {

@State private var uploaderName = ""
@State private var requestUrl = ""
@State private var headers: [String: String] = [:]
@State private var formData: [String: String] = [:]
@State private var fileFormName = ""
@State private var responseProp = ""
@State private var header: [CustomEntryModel] = []
@State private var formData: [CustomEntryModel] = []

var body: some View {
VStack {
HStack {
Text("Name:")
TextField("Name", text: $uploaderName)
}
.padding()

HStack {
Text("Request URL:")
TextField("Request URL", text: $requestUrl)
}
.padding()

HStack {
Text("Response Property:")
TextField("Response Property", text: $responseProp)
}
.padding()

Section(header: Text("Headers (optional)")) {
ForEach(Array(headers.keys), id: \.self) { key in
HStack {
Text("Header Name:")
TextField("Header Name", text: Binding(
get: { key },
set: { newKey in headers[newKey] = headers.removeValue(forKey: key) }
))

Text("Header Value:")
TextField("Header Value", text: Binding(
get: { headers[key] ?? "" },
set: { headers[key] = $0 }
))
}
.padding(.horizontal)
ScrollView {
VStack {
HStack {
Text("Name:")
TextField("Name", text: $uploaderName)
}

.padding()

HStack {
Text("Request URL:")
TextField("Request URL", text: $requestUrl)
}
.padding()

HStack {
Text("Response Property:")
TextField("Response Property", text: $responseProp)
}
.padding()

HeaderView()

FormDataView()

Button(action: {
headers[""] = ""
saveCustomUploader()
}) {
Text("Add Header")
Text("Save")
}
.padding()
}

Section(header: Text("Form Data (optional)")) {
ForEach(Array(formData.keys), id: \.self) { key in
HStack {
Text("Field Name:")
TextField("Field Name", text: Binding(
get: { key },
set: { newKey in formData[newKey] = formData.removeValue(forKey: key) }
))

Text("Field Value:")
TextField("Field Value", text: Binding(
get: { formData[key] ?? "" },
set: { formData[key] = $0 }
))
}
}

private func HeaderView() -> some View {
Section(header: Text("Header (optional)")) {
ForEach(header.indices, id: \.self) { index in
HStack(spacing: 10) {
Text("Header Name: \(header[index].key)")
Text("Header Value: \(header[index].value)")

Button(action: {
header.remove(at: index)
}) {
Image(systemName: "minus.circle")
}
.padding(.horizontal)
}

Button(action: {
formData[""] = ""
}) {
Text("Add Field")

}

ForEach(header) { entry in
if (entry == header.last) {
CustomEntryView(entry: $header[header.firstIndex(of: entry)!])
.padding(.horizontal)
}
.padding()
}


Button(action: {
header.append(CustomEntryModel(key: "", value: ""))
}) {
Text("Add Header")
}
.padding()
}
}

private func FormDataView() -> some View {
Section(header: Text("Form Data (optional)")) {
ForEach(formData.indices, id: \.self) { index in
HStack(spacing: 10) {
Text("Form Data Name: \(formData[index].key)")
Text("Form Data Value: \(formData[index].value)")

Button(action: {
formData.remove(at: index)
}) {
Image(systemName: "minus.circle")
}
}

}

ForEach(formData) { entry in
if (entry == formData.last) {
CustomEntryView(entry: $formData[formData.firstIndex(of: entry)!])
.padding(.horizontal)
}
}

Button(action: {
saveCustomUploader()
formData.append(CustomEntryModel(key: "", value: ""))
}) {
Text("Save")
Text("Add FormData")
}
.padding()
}
}

private func saveCustomUploader() {
var headerData: [String: String] {
return header.reduce(into: [String: String]()) { result, entry in
result[entry.key] = entry.value
}
}

var formDataModel: [String: String] {
return formData.reduce(into: [String: String]()) { result, entry in
result[entry.key] = entry.value
}
}

let uploader = CustomUploader(
name: uploaderName,
requestUrl: requestUrl,
headers: headers.isEmpty ? nil : headers,
formData: formData.isEmpty ? nil : formData,
headers: header.count == 0 ? nil : headerData,
formData: formData.count == 0 ? nil : formDataModel,
fileFormName: fileFormName.isEmpty ? nil : fileFormName,
responseProp: responseProp
)
Expand All @@ -299,6 +329,23 @@ struct AddCustomUploaderView: View {

presentationMode.wrappedValue.dismiss()
}

private struct CustomEntryView: View {
@Binding var entry: CustomEntryModel

var body: some View {
HStack {
VStack(alignment: .leading) {
Text("Name")
TextField("Name", text: $entry.key)
}
VStack(alignment: .leading) {
Text("Value")
TextField("Value", text: $entry.value)
}
}
}
}
}

struct ImportCustomUploaderView: View {
Expand Down Expand Up @@ -399,3 +446,14 @@ struct ImportError: Identifiable {
error.localizedDescription
}
}

struct CustomEntryModel: Identifiable, Equatable {
let id = UUID()
var key: String
var value: String
}


#Preview {
AddCustomUploaderView()
}