63 lines
2.3 KiB
Swift
63 lines
2.3 KiB
Swift
import SwiftUI
|
|
|
|
struct AddSubscriptionView: View {
|
|
@Environment(\.presentationMode) var presentationMode
|
|
@Binding var subs: [Subscription]
|
|
|
|
@State private var name: String = ""
|
|
@State private var payments: [Payment] = [Payment(amount: 0, intervall: .monthly)]
|
|
|
|
var body: some View {
|
|
NavigationView {
|
|
Form {
|
|
Section(header: Text("Subscription Name")) {
|
|
TextField("Name", text: $name)
|
|
}
|
|
|
|
ForEach($payments) { $payment in
|
|
Section(header: Text("Payment")) {
|
|
HStack {
|
|
TextField("Amount", value: $payment.amount, formatter: NumberFormatter())
|
|
Spacer()
|
|
Text("\(Currency.euro.description)")
|
|
}
|
|
Picker("Intervall", selection: $payment.intervall) {
|
|
Text("Weekly").tag(PaymentIntervall.weekly)
|
|
Text("Monthly").tag(PaymentIntervall.monthly)
|
|
Text("Quarter").tag(PaymentIntervall.quarter)
|
|
Text("Yearly").tag(PaymentIntervall.yearly)
|
|
}
|
|
.pickerStyle(SegmentedPickerStyle())
|
|
}
|
|
}
|
|
.onDelete(perform: deletePayment)
|
|
|
|
Section {
|
|
Button("Add Payment") {
|
|
let newPayment = Payment(amount: 0, intervall: .monthly)
|
|
payments.append(newPayment)
|
|
}
|
|
}
|
|
|
|
Section {
|
|
Button("Add Subscription") {
|
|
let newSubscription = Subscription(name: name, payments: payments)
|
|
subs.append(newSubscription)
|
|
presentationMode.wrappedValue.dismiss()
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Add Subscription")
|
|
#if os(iOS)
|
|
.navigationBarItems(trailing: Button("Cancel") {
|
|
presentationMode.wrappedValue.dismiss()
|
|
})
|
|
#endif
|
|
}
|
|
}
|
|
|
|
func deletePayment(at offsets: IndexSet) {
|
|
payments.remove(atOffsets: offsets)
|
|
}
|
|
}
|