From 621ce23fc79c054d3ce155bf5cad11f4c55e5ca9 Mon Sep 17 00:00:00 2001 From: k3y0708 Date: Fri, 28 Jun 2024 13:36:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Initial=20Commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AboTracker.xcodeproj/project.pbxproj | 4 ++ AboTracker/ContentView.swift | 64 ++++++++++++++++++++++------ AboTracker/SubView.swift | 21 +++++++++ AboTracker/Subscription.swift | 50 ++++++++++++++++++++++ 4 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 AboTracker/SubView.swift create mode 100644 AboTracker/Subscription.swift diff --git a/AboTracker.xcodeproj/project.pbxproj b/AboTracker.xcodeproj/project.pbxproj index aa3b834..d199d93 100644 --- a/AboTracker.xcodeproj/project.pbxproj +++ b/AboTracker.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ D40CCAE22C2DC5A9007C4A9F /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40CCAE12C2DC5A9007C4A9F /* ContentView.swift */; }; D40CCAE42C2DC5AA007C4A9F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D40CCAE32C2DC5AA007C4A9F /* Assets.xcassets */; }; D40CCAE82C2DC5AA007C4A9F /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D40CCAE72C2DC5AA007C4A9F /* Preview Assets.xcassets */; }; + D40CCAEF2C2DC5D8007C4A9F /* Subscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40CCAEE2C2DC5D8007C4A9F /* Subscription.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -20,6 +21,7 @@ D40CCAE32C2DC5AA007C4A9F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; D40CCAE52C2DC5AA007C4A9F /* AboTracker.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AboTracker.entitlements; sourceTree = ""; }; D40CCAE72C2DC5AA007C4A9F /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + D40CCAEE2C2DC5D8007C4A9F /* Subscription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Subscription.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,6 +59,7 @@ D40CCAE32C2DC5AA007C4A9F /* Assets.xcassets */, D40CCAE52C2DC5AA007C4A9F /* AboTracker.entitlements */, D40CCAE62C2DC5AA007C4A9F /* Preview Content */, + D40CCAEE2C2DC5D8007C4A9F /* Subscription.swift */, ); path = AboTracker; sourceTree = ""; @@ -139,6 +142,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + D40CCAEF2C2DC5D8007C4A9F /* Subscription.swift in Sources */, D40CCAE22C2DC5A9007C4A9F /* ContentView.swift in Sources */, D40CCAE02C2DC5A9007C4A9F /* AboTrackerApp.swift in Sources */, ); diff --git a/AboTracker/ContentView.swift b/AboTracker/ContentView.swift index 7ad4a26..7e03c64 100644 --- a/AboTracker/ContentView.swift +++ b/AboTracker/ContentView.swift @@ -1,21 +1,59 @@ -// -// ContentView.swift -// AboTracker -// -// Created by Keyvan Atashfaraz on 27.06.24. -// - import SwiftUI struct ContentView: View { + var subs: [Subscription] = [ + Subscription(name: "Test", amount: 9.99, intervall: .monthly), + Subscription(name: "Fitness First", amount: 8, intervall: .weekly) + ] + var body: some View { - VStack { - Image(systemName: "globe") - .imageScale(.large) - .foregroundStyle(.tint) - Text("Hello, world!") + NavigationView { + List { + ForEach(subs) { sub in + HStack { + VStack(alignment: .leading, spacing: 8) { + HStack { + Image(systemName: "arrow.left") + Text(sub.name) + Spacer() + Text("\(sub.amount, specifier: "%.2f")€/\(sub.intervall.description)") + } + } + .frame(maxWidth: .infinity, alignment: .leading) + .padding(7) + .background(Color.gray.opacity(0.15)) + .cornerRadius(10) + .padding(5) + } + .frame(maxWidth: .infinity) + .padding(.vertical, 4) + } + .listRowInsets(EdgeInsets()) + .listRowSeparator(.hidden) + + HStack { + Text("Monthly Total: ") + Spacer() + Text("\(getMonthlyTotal(subs: subs), specifier: "%.2f€")") + } + .padding(.vertical, 4) + Button("Add a Subscription", action: addSub) + } + .listStyle(PlainListStyle()) + .navigationTitle("Subscriptions") } - .padding() + } + + func getMonthlyTotal(subs: [Subscription]) -> Float { + var monthlyTotal: Float = 0.0 + for sub in subs { + monthlyTotal += sub.getMonthlyAmount() + } + return monthlyTotal + } + + func addSub() -> Void { + } } diff --git a/AboTracker/SubView.swift b/AboTracker/SubView.swift new file mode 100644 index 0000000..ed9801b --- /dev/null +++ b/AboTracker/SubView.swift @@ -0,0 +1,21 @@ +import SwiftUI + +struct SubView: View { + var subs: [Subscription] = [ + Subscription(name: "Test", amount: 9.99), + Subscription(name: "Test 2", amount: 9.99) + ] + + var body: some View { + VStack { + ForEach(subs) {sub in + Text(sub.name) + } + } + .padding() + } +} + +#Preview { + SubView() +} diff --git a/AboTracker/Subscription.swift b/AboTracker/Subscription.swift new file mode 100644 index 0000000..2c1c60a --- /dev/null +++ b/AboTracker/Subscription.swift @@ -0,0 +1,50 @@ +// +// Subscription.swift +// AboTracker +// +// Created by Keyvan Atashfaraz on 27.06.24. +// + +import Foundation +import SwiftUI + +final class Subscription: Identifiable { + public let id = UUID() + var name: String + var amount: Float + var intervall: PaymentIntervall + + init(name: String, amount: Float, intervall: PaymentIntervall) { + self.name = name + self.amount = amount + self.intervall = intervall + } + + func getMonthlyAmount() -> Float { + switch intervall { + case .weekly: + return amount/7*30; + case .monthly: + return amount; + case .yearly: + return amount/12; + } + } +} + +enum PaymentIntervall: CustomStringConvertible { + case weekly + case monthly + case yearly + + var description: String { + switch self { + case .weekly: + return "week" + case .monthly: + return "month" + case .yearly: + return "year" + } + } +}