Learn Courses My Dashboard

Updating app data with calculated values

Hey team,

From the code below I am trying to get my $data.incomeAnnual amount to auto calculate using the computedIncomeAmount calculation.

Can anyone help how I do this?

Below is the code for the edit view and then for the data structure.

Thanks

import SwiftUI

struct IncomeEditView: View {
    @Binding var data: IncomeDetails.Data
    
    private var computedIncomeAmount: Int {
        switch data.incomeFrequency {
        case .Daily:
            return data.incomeAmount * 365
        case .Weekly:
            return data.incomeAmount * 52
        case .Fortnightly:
            return data.incomeAmount * 26
        case .Monthly:
            return data.incomeAmount * 12
        case .Quarterly:
            return data.incomeAmount * 4
        case .Annually:
            return data.incomeAmount
        }
    }
    
    var body: some View {
        Form {
            Section(header: Text("Details")) {
                TextField("Details", text: $data.incomeDetails)
            }
            Section(header: Text("Frequency")) {
                PeriodPicker(selection: $data.incomeFrequency)
            }
            Section(header: Text("Amount")) {
                TextField("Amount", value: $data.incomeAmount, format: .number)
                    .keyboardType(.numberPad)
            }
            Section(header: Text("Annual Amount")) {
                TextField("", value: $data.incomeAnnual, format: .number)
                    .disabled(true)
            }
        }
        .environment(\.colorScheme, .dark)
    }
}

struct IncomeEditView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            IncomeEditView(data: .constant(IncomeDetails.sampledata[0].data))
    }
}
}

import Foundation

struct IncomeDetails: Identifiable {
    let id: UUID
    var incomeDetails: String
    var incomeFrequency: Period
    var incomeAmount: Int
    var incomeAnnual: Int
    
    init(id: UUID = UUID(), incomeDetails: String, incomeFrequency: Period, incomeAmount: Int, incomeAnnual: Int) {
        self.id = id
        self.incomeDetails = incomeDetails
        self.incomeFrequency = incomeFrequency
        self.incomeAmount = incomeAmount
        self.incomeAnnual = incomeAnnual
    }
}

extension IncomeDetails {

struct Data {
    var incomeDetails: String = ""
    var incomeFrequency: Period = .Weekly
    var incomeAmount: Int = 0
    var incomeAnnual: Int = 0
    }

var data: Data {
    Data(incomeDetails: incomeDetails, incomeFrequency: incomeFrequency, incomeAmount: incomeAmount, incomeAnnual: incomeAnnual)
}
    
    mutating func update(from data: Data) {
        incomeDetails = data.incomeDetails
        incomeFrequency = data.incomeFrequency
        incomeAmount = Int(data.incomeAmount)
        incomeAnnual = Int(data.incomeAnnual)
    }
    
    init(data: Data) {
        id = UUID()
        incomeDetails = data.incomeDetails
        incomeFrequency = data.incomeFrequency
        incomeAmount = Int(data.incomeAmount)
        incomeAnnual = Int(data.incomeAnnual)
    }
}

extension IncomeDetails {
    static let sampledata: [IncomeDetails] =
    [
        IncomeDetails(incomeDetails: "Wages", incomeFrequency: .Weekly, incomeAmount: 1000, incomeAnnual: 52000),
        IncomeDetails(incomeDetails: "Dividends", incomeFrequency: .Quarterly, incomeAmount: 500, incomeAnnual: 2000)
    ]
}