Created
January 25, 2022 06:35
-
-
Save allfinlir/cd2d3a6c26acff93f4eb92531be07536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct StartView: View { | |
@State var monthlySaving: String = "" | |
@State var yearlyReturn: String = "" | |
@State var savings: String = "" | |
@State var amountToReach: String = "" | |
@State var yearsSaving: String = "" | |
@State private var tapped = false | |
@State private var savingsChoice = 0 | |
var yearsSavingCalculation: String { | |
// guard monthlySaving.isEmpty == false, yearlyReturn.isEmpty == false, savings.isEmpty == false else { return "Please fill in all fields" } do I even need this code? the app works the same without it, for now. | |
guard let mS = Double(monthlySaving), let yR = Double(yearlyReturn), let sav = Double(savings), let yS = Double(yearsSaving) else { return "Error" } | |
let interest = (yR/100.0) | |
let interestOnExistingCapital = 1 + (yR/100.0) | |
let monthlyInterest = 1 + (interest/12.0) | |
let monthlyInterestDecimal = interest/12.0 | |
let totalMonthsSaving = yS * 12.0 | |
let yearlyInterestMonthCompound = (pow(monthlyInterest, totalMonthsSaving) - 1.0) | |
let yearlyInterestCapital = pow(interestOnExistingCapital, yS) // I think that calculating the existing capital on a yearly basis is more fair. See monthly compunding in the comment code below | |
// let yearlyInterestCapital = pow(monthlyInterest, totalMonthsSaving) // compunded monthly | |
let finalCalculation = (sav * yearlyInterestCapital) + (mS * yearlyInterestMonthCompound/monthlyInterestDecimal) | |
return String(format: "%.2f", finalCalculation) | |
} | |
// var reachAmountCalculation: String { | |
// guard monthlySaving.isEmpty == false, yearlyReturn.isEmpty == false, savings.isEmpty == false else { return "" } same comment as above, do I need this code? | |
// guard let mS = Double(monthlySaving), let yR = Double(yearlyReturn), let sav = Double(savings), let yS = Double(yearsSaving), let amount = Double(amountToReach) else { return "Error" } | |
// let interest = (yR/100.0) | |
// let interestOnExistingCapital = 1 + interest | |
// let amountDivided = amount/sav | |
// let amountFinalCalc = (log(amountDivided)) / (log(interestOnExistingCapital)) | |
// return String(format: "%.2f", amountFinalCalc) | |
// } This calculation is still ongoing for calculating the amount to reach, havent figured out the math yet :) | |
let gradientColors = Gradient(colors: [Color.green, Color.green.opacity(0.5)]) | |
var body: some View { | |
NavigationView { | |
ZStack { | |
LinearGradient(gradient: gradientColors, startPoint: .top, endPoint: .bottom) | |
.ignoresSafeArea() | |
ScrollView { | |
VStack { | |
VStack(alignment: .leading) { | |
Text("Monthly savings:") | |
.padding(.horizontal) | |
.padding(.top, 30) | |
TextField("Enter value", text: $monthlySaving) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding(.horizontal) | |
Text("Yearly return:") | |
.padding(.horizontal) | |
TextField("Enter return", text: $yearlyReturn) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding(.horizontal) | |
Text("Current savings:") | |
.padding(.horizontal) | |
TextField("Enter savings", text: $savings) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding(.horizontal) | |
} | |
Picker("Savings", selection: $savingsChoice) { | |
Text("Years") | |
.tag(0) | |
Text("Amount") | |
.tag(1) | |
} | |
.pickerStyle(SegmentedPickerStyle()) | |
.padding() | |
GeometryReader { proxy in | |
ZStack { | |
VStack(alignment: .leading) { | |
HStack { | |
Text("Number of years saving:") | |
Spacer() | |
Image(systemName: "number.circle") | |
} | |
TextField("Number of years", text: $yearsSaving) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding(.bottom) | |
HStack { | |
Spacer() | |
Button(action: { | |
tapped.toggle() | |
}, label: { | |
Text("Get Result") | |
.frame(width: 200, height: 50) | |
.background(Color.green) | |
.foregroundColor(.white) | |
.cornerRadius(12) | |
.shadow(color: .black.opacity(0.3), radius: 5, x: 5, y: 5) | |
}) | |
Spacer() | |
} | |
if tapped == true { | |
Text("After \(yearsSaving) years you will have saved a total of \(yearsSavingCalculation)") | |
.padding(.top) | |
.font(.title) | |
} | |
} | |
.padding() | |
.background(LinearGradient(gradient: gradientColors, startPoint: .leading, endPoint: .trailing)) | |
.cornerRadius(20) | |
.offset(x: savingsChoice == 0 ? 0 : -proxy.size.width, y: 0) | |
.animation(.default, value: savingsChoice) | |
.padding() | |
VStack(alignment: .leading) { | |
HStack { | |
Text("Amount to reach:") | |
Spacer() | |
Image(systemName: "dollarsign.circle") | |
} | |
.foregroundColor(yearsSaving.isEmpty == false ? .gray : .black) | |
TextField("Enter amount", text: $amountToReach) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
// .disabled(yearsSaving.isEmpty == false ? true : false) | |
HStack { | |
Spacer() | |
Button(action: { | |
tapped.toggle() | |
}, label: { | |
Text("Get Result") | |
.frame(width: 200, height: 50) | |
.background(Color.green) | |
.foregroundColor(.white) | |
.cornerRadius(12) | |
.shadow(color: .black.opacity(0.3), radius: 5, x: 5, y: 5) | |
}) | |
Spacer() | |
if tapped == true { | |
Text("After \(yearsSaving) years you will have saved a total of \(yearsSavingCalculation)") | |
.padding(.top) | |
.font(.title) | |
} | |
} | |
} | |
.padding() | |
.background(Color.gray) | |
.cornerRadius(20) | |
.foregroundColor(.orange) | |
.offset(x: savingsChoice == 1 ? 0 : proxy.size.width, y: 0) | |
.animation(.default, value: savingsChoice) | |
.padding() | |
} | |
} | |
} | |
} | |
} | |
.navigationTitle("Return Calculator") | |
} | |
} | |
} | |
struct StartView_Previews: PreviewProvider { | |
static var previews: some View { | |
StartView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment