-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBudgetController.swift
More file actions
148 lines (117 loc) · 4.87 KB
/
BudgetController.swift
File metadata and controls
148 lines (117 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// BudgetController.swift
// WalletWatcher
//
// Created by Mario Cordova on 6/18/17.
// Copyright © 2017 Mario Cordova. All rights reserved.
//
import Foundation
import UIKit
final class BudgetController {
private var totalIncome:Income?
private var budgets: [Budget] = []
private init() {
fetchIncome()
fetchBudgets()
}
static let sharedInstance = BudgetController()
private func fetchIncome() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
if try (context.fetch(Income.fetchRequest())).count == 0 {
print("No Income stored yet. Preparing to store...")
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let income = Income(context: context)
income.total = Decimal(string: "0.00") as NSDecimalNumber?
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
totalIncome = try (context.fetch(Income.fetchRequest()))[0]
} catch {
print("Error fetching Income")
}
}
private func fetchBudgets() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
budgets = try context.fetch(Budget.fetchRequest())
} catch {
print("Error fetching Budgets")
}
}
func getTotalIncome() -> NSDecimalNumber {
return totalIncome!.total!
}
func getBudget(at index: Int) -> Budget {
return budgets[index]
}
func getBudgetCount() -> Int {
return budgets.count
}
func getBudgetTitle(at index: Int) -> String {
return budgets[index].title!
}
func getBudgetPercent(at index: Int) -> String {
let percent = budgets[index].totalIncome!.dividing(by: totalIncome!.total!)
let formatter = NumberFormatter()
formatter.numberStyle = .percent
return formatter.string(from: percent)!
}
func addIncome(amount: NSDecimalNumber) {
totalIncome?.total = totalIncome?.total?.adding(amount)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
printIncome()
}
func addBudgetIncome(amount: NSDecimalNumber, budget: Budget) {
let wallet: Budget?
if let i = budgets.index(where: {$0 == budget}) {
wallet = budgets[i]
wallet?.totalIncome = wallet?.totalIncome?.adding(amount)
}
addIncome(amount: amount)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
func addExpense(amount: NSDecimalNumber, store: String, description: String, budget: Budget) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let expense = Expense(context: context)
let date = Date()
expense.cost = amount as NSDecimalNumber
expense.store = store
expense.desc = description
expense.date = date as NSDate
if let i = budgets.index(where: { $0 == budget }) {
budgets[i].addToExpenses(expense)
budgets[i].totalIncome = budgets[i].totalIncome?.subtracting(amount)
totalIncome?.total = totalIncome?.total?.subtracting(amount)
} else {
print("Error cannot find Budget in the Budget array")
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
func deleteExpense(budget: Budget, expense: Expense) {
let amountToAddBack = expense.cost! as NSDecimalNumber
budget.removeFromExpenses(expense)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
addBudgetIncome(amount: amountToAddBack, budget: budget)
fetchBudgets()
}
func deleteBudget(budget: Budget) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let amountToSubtract = budget.totalIncome?.multiplying(by: NSDecimalNumber(decimal: -1.0))
context.delete(budget)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
addIncome(amount: amountToSubtract!)
fetchBudgets()
}
func createBudget(title: String, amount: String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let budget = Budget(context: context)
budget.title = title
budget.totalIncome = NSDecimalNumber(string: amount)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
addIncome(amount: (budget.totalIncome)!)
fetchBudgets()
}
private func printIncome() {
print("Total income: \(totalIncome!.total!.decimalValue)")
}
}