111 lines
4.2 KiB
Swift
111 lines
4.2 KiB
Swift
import SwiftUI
|
|
|
|
struct HomeView: View {
|
|
@Binding var subs: [Subscription]
|
|
@State private var showAddSubscriptionSheet = false
|
|
|
|
var body: some View {
|
|
NavigationView {
|
|
VStack {
|
|
List {
|
|
ForEach(subs) { sub in
|
|
Section {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text(sub.name)
|
|
.font(.headline)
|
|
ForEach(sub.payments) { payment in
|
|
HStack {
|
|
Text("\(payment.amount, specifier: "%.2f")\(Currency.euro.description)")
|
|
Spacer()
|
|
Text("/\(payment.intervall.description)")
|
|
.foregroundColor(.gray)
|
|
}
|
|
.font(.subheadline)
|
|
}
|
|
}
|
|
.padding(.vertical, 8)
|
|
}
|
|
.listRowBackground(sub.color.opacity(0.2))
|
|
.cornerRadius(8)
|
|
}
|
|
.onDelete(perform: deleteSubscription)
|
|
|
|
Section(header: Text("Totals")) {
|
|
HStack {
|
|
Text("Monthly Total")
|
|
Spacer()
|
|
Text("\(getMonthlyTotal(subs: subs), specifier: "%.2f")\(Currency.euro.description)")
|
|
.foregroundColor(.gray)
|
|
}
|
|
|
|
HStack {
|
|
Text("Remaining for Current Month")
|
|
Spacer()
|
|
Text("\(getRemainingForCurrentMonth(subs: subs), specifier: "%.2f")\(Currency.euro.description)")
|
|
.foregroundColor(.gray)
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Subscriptions")
|
|
.toolbar {
|
|
#if os(iOS)
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
EditButton()
|
|
}
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button(action: {
|
|
showAddSubscriptionSheet.toggle()
|
|
}) {
|
|
Image(systemName: "plus")
|
|
}
|
|
}
|
|
#elseif os(macOS)
|
|
ToolbarItem(placement: .primaryAction) {
|
|
Button(action: {
|
|
showAddSubscriptionSheet.toggle()
|
|
}) {
|
|
Image(systemName: "plus")
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
.sheet(isPresented: $showAddSubscriptionSheet) {
|
|
AddSubscriptionView(subs: $subs)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func deleteSubscription(at offsets: IndexSet) {
|
|
subs.remove(atOffsets: offsets)
|
|
}
|
|
|
|
func getMonthlyTotal(subs: [Subscription]) -> Float {
|
|
var monthlyTotal: Float = 0.0
|
|
for sub in subs {
|
|
monthlyTotal += sub.getMonthlyAmount()
|
|
}
|
|
return monthlyTotal
|
|
}
|
|
|
|
func getRemainingForCurrentMonth(subs: [Subscription]) -> Float {
|
|
var remainingTotal: Float = 0.0
|
|
for sub in subs {
|
|
remainingTotal += sub.getRemainingForCurrentMonth()
|
|
}
|
|
return remainingTotal
|
|
}
|
|
}
|
|
|
|
struct HomeView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
HomeView(subs: .constant([
|
|
Subscription(name: "Test", payments: [Payment(amount: 9.99, intervall: .monthly, startDate: Date())], color: .blue),
|
|
Subscription(name: "Fitness First", payments: [
|
|
Payment(amount: 7.9, intervall: .weekly, startDate: Date()),
|
|
Payment(amount: 29, intervall: .quarter, startDate: Date())
|
|
], color: .red)
|
|
]))
|
|
}
|
|
}
|