| struct CreateDividendView: View { |
| @Environment(\.presentationMode) var presentationMode |
| @Environment(\.colorScheme) var colorScheme |
| @ObservedObject var exchange: StockExchange |
| @ObservedObject var stock: Stock |
| @StateObject var dividend = Dividend() |
| @State private var showingAlert = false |
| @State private var showingDateAlert = false |
| var isNew = true |
| |
| var numberFormatter: NumberFormatter = { |
| let nf = NumberFormatter() |
| nf.locale = Locale.current |
| nf.numberStyle = .decimal |
| |
| return nf |
| }() |
| |
| var decimalFormatter: NumberFormatter = { |
| let df = NumberFormatter() |
| df.locale = Locale.current |
| df.numberStyle = .decimal |
| df.generatesDecimalNumbers = true |
| |
| return df |
| }() |
| |
| var body: some View { |
| NavigationView { |
| VStack(spacing: 0) { |
| HStack(spacing: 0) { |
| |
| Text(stock.ticker) |
| .font(.title) |
| .bold() |
| |
| Spacer() |
| } |
| .padding(.horizontal).padding(.horizontal, 5) |
| |
| HStack(spacing: 0) { |
| |
| Text(stock.name.uppercased()) |
| .font(.footnote) |
| .foregroundColor(.gray) |
| |
| Spacer() |
| } |
| .padding(.horizontal).padding(.horizontal, 5) |
| |
| HStack(spacing: 0) { |
| |
| Text("Dividend Value") |
| .padding(.leading, 10) |
| |
| Spacer() |
| |
| Text("$\(Double(dividend.quantity ?? 0) * (dividend.value ?? 0) - (dividend.fees ?? 0), specifier: "%.2f")") |
| .padding(.trailing, 10) |
| } |
| .padding(.horizontal, 10).padding(.vertical, 10) |
| .background(Color(UIColor.systemGray2)) |
| .cornerRadius(8) |
| .padding(.top, 20).padding(.horizontal, 20) |
| |
| Form { |
| HStack { |
| DatePicker("Date", selection: $dividend.date, displayedComponents: [.date]) |
| .datePickerStyle(.compact) |
| } |
| |
| HStack { |
| Text("Quantity") |
| TextField("100", value: $dividend.quantity, formatter: numberFormatter) |
| .multilineTextAlignment(.trailing) |
| .keyboardType(.numberPad) |
| } |
| |
| HStack { |
| Text("Value") |
| TextField("1.234", value: $dividend.value, formatter: decimalFormatter) |
| .multilineTextAlignment(.trailing) |
| .keyboardType(.decimalPad) |
| } |
| |
| HStack { |
| Text("Fees") |
| TextField("1.00", value: $dividend.fees, formatter: decimalFormatter) |
| .multilineTextAlignment(.trailing) |
| .keyboardType(.decimalPad) |
| } |
| } |
| |
| if !isNew { |
| Button(action: { |
| showingAlert = true |
| }) { |
| Text("\(Image(systemName: "trash.fill")) Delete Dividend") |
| .font(.title3) |
| .foregroundColor(.white) |
| .frame(width: UIScreen.main.bounds.width/1.2, height: 35, alignment: .center) |
| .padding(.horizontal, 10).padding(.vertical, 5) |
| .background(Color.red) |
| .cornerRadius(10) |
| } |
| .padding(8) |
| } |
| |
| Spacer() |
| } |
| .navigationTitle(isNew ? "Record Dividend" : "Edit Dividend") |
| .navigationBarTitleDisplayMode(.inline) |
| .background(colorScheme == .dark ? Color(UIColor.systemBackground) : Color(UIColor.secondarySystemBackground)) |
| .toolbar { |
| ToolbarItem(placement: .navigationBarLeading) { |
| Button(action: { |
| presentationMode.wrappedValue.dismiss() |
| }) { |
| Text("Cancel") |
| } |
| } |
| ToolbarItem(placement: .navigationBarTrailing) { |
| Button(action: { |
| if dividend.date > Date.now { |
| showingDateAlert = true |
| } else { |
| stock.saveDividend(dividend: dividend) |
| exchange.updateValues() |
| DataController.shared.saveData() |
| presentationMode.wrappedValue.dismiss() |
| } |
| }) { |
| Text("Save") |
| .bold() |
| } |
| } |
| } |
| .alert(isPresented: $showingDateAlert) { |
| Alert( |
| title: Text("Date is in the future"), |
| primaryButton: .default(Text("Save anyway")) { |
| stock.saveDividend(dividend: dividend) |
| DataController.shared.saveData() |
| presentationMode.wrappedValue.dismiss() |
| }, |
| secondaryButton: .cancel() |
| ) |
| } |
| .alert(isPresented: $showingAlert) { |
| Alert( |
| title: Text("Are you sure?"), |
| primaryButton: .destructive(Text("Delete")) { |
| stock.deleteDividend(dividend: dividend) |
| }, |
| secondaryButton: .cancel() |
| ) |
| } |
| } |
| } |
| } |