Commit f693c40d authored by tybrandt's avatar tybrandt
Browse files

TransactionTableView now displays the appropriate data

parent 94fc4dc6
......@@ -14,6 +14,7 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav
@IBOutlet weak var costTextField: UITextField!
var doneButton: UIBarButtonItem!
var categories = [Category]()
var transactions = [Transaction]()
override func viewDidLoad() {
super.viewDidLoad()
......@@ -61,6 +62,13 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav
super.prepare(for: segue, sender: sender)
guard let button = sender as? UIBarButtonItem, button === addButton else {return}
let price = Double(costTextField.text ?? "0.0")
let dateObject = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy h:mma"
let date = dateFormatter.string(from: dateObject)
let transaction = Transaction(name: nameTextField.text ?? "", category: categoryTextField.text ?? "",
price: price ?? 0.0, date: date)
transactions.insert(transaction!, at: 0)
for category in categories {
if categoryTextField.text == category.name {
category.addToTotal(price: price ?? 0.0)
......
......@@ -147,28 +147,69 @@
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="allTransactions" id="e68-D8-BFK" customClass="TransactionTableViewCell" customModule="Budget_Tracker" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="414" height="43.5"/>
<rect key="frame" x="0.0" y="28" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="e68-D8-BFK" id="Q12-f8-IYE">
<rect key="frame" x="0.0" y="0.0" width="414" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ZQh-dN-pAc">
<rect key="frame" x="20" y="11" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<rect key="frame" x="20" y="14.5" width="57" height="29.5"/>
<constraints>
<constraint firstAttribute="height" constant="29" id="Zlr-Gn-agb"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ieZ-9F-Ud3">
<rect key="frame" x="20" y="0.0" width="31" height="14.5"/>
<constraints>
<constraint firstAttribute="height" constant="14.5" id="YAr-zG-SjO"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="TY2-fe-Hf6">
<rect key="frame" x="363" y="0.0" width="31" height="15"/>
<constraints>
<constraint firstAttribute="height" constant="15" id="HAR-xr-XIi"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="mCQ-Pi-90k">
<rect key="frame" x="337" y="15" width="57" height="29"/>
<constraints>
<constraint firstAttribute="height" constant="29" id="4I1-Rr-8be"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="top" secondItem="Q12-f8-IYE" secondAttribute="topMargin" id="3HX-yH-7bM"/>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="leading" secondItem="Q12-f8-IYE" secondAttribute="leadingMargin" id="Ze3-CE-NrL"/>
<constraint firstAttribute="bottomMargin" secondItem="ZQh-dN-pAc" secondAttribute="bottom" constant="0.5" id="pvp-fP-BeH"/>
<constraint firstAttribute="trailingMargin" relation="lessThanOrEqual" secondItem="ZQh-dN-pAc" secondAttribute="trailing" constant="332" id="yMY-ib-KtU"/>
<constraint firstItem="TY2-fe-Hf6" firstAttribute="top" secondItem="Q12-f8-IYE" secondAttribute="top" id="78u-8N-jkW"/>
<constraint firstItem="TY2-fe-Hf6" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="ieZ-9F-Ud3" secondAttribute="trailing" constant="1" id="7uk-Eu-hzl"/>
<constraint firstItem="mCQ-Pi-90k" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="ZQh-dN-pAc" secondAttribute="trailing" constant="1" id="FtQ-HG-DIR"/>
<constraint firstAttribute="bottom" secondItem="ZQh-dN-pAc" secondAttribute="bottom" id="Hhh-aV-Hc2"/>
<constraint firstAttribute="bottom" secondItem="mCQ-Pi-90k" secondAttribute="bottom" id="MQX-Ng-N8E"/>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="leading" secondItem="Q12-f8-IYE" secondAttribute="leading" constant="20" symbolic="YES" id="Orz-Fd-KUG"/>
<constraint firstAttribute="trailing" secondItem="mCQ-Pi-90k" secondAttribute="trailing" constant="20" symbolic="YES" id="XHa-Te-W2P"/>
<constraint firstItem="ieZ-9F-Ud3" firstAttribute="leading" secondItem="Q12-f8-IYE" secondAttribute="leading" constant="20" symbolic="YES" id="bAa-OE-MP3"/>
<constraint firstAttribute="trailing" secondItem="TY2-fe-Hf6" secondAttribute="trailing" constant="20" symbolic="YES" id="cnh-Dm-zjm"/>
<constraint firstItem="mCQ-Pi-90k" firstAttribute="top" secondItem="TY2-fe-Hf6" secondAttribute="bottom" id="gIl-4b-xAD"/>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="top" secondItem="ieZ-9F-Ud3" secondAttribute="bottom" id="izU-Yw-ENe"/>
<constraint firstItem="ieZ-9F-Ud3" firstAttribute="top" secondItem="Q12-f8-IYE" secondAttribute="top" id="rK2-rt-Gxf"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="category" destination="ieZ-9F-Ud3" id="Xme-GH-swl"/>
<outlet property="date" destination="TY2-fe-Hf6" id="luq-n8-Pjj"/>
<outlet property="name" destination="ZQh-dN-pAc" id="BAX-R7-zkd"/>
<outlet property="price" destination="mCQ-Pi-90k" id="M0O-bG-CnA"/>
</connections>
</tableViewCell>
</prototypes>
......@@ -187,7 +228,7 @@
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Vnp-er-RtW" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2812" y="165"/>
<point key="canvasLocation" x="2811.594202898551" y="164.73214285714286"/>
</scene>
<!--Delete All Confirmation-->
<scene sceneID="a5z-6h-cFL">
......
......@@ -9,6 +9,7 @@ import UIKit
class CategoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var categories = [Category]()
var transactions = [Transaction]()
@IBOutlet weak var categoryTable: UITableView!
@IBOutlet weak var totalLabel: UILabel!
......@@ -22,6 +23,9 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
}
totalLabel.text = String(format: "Total: $%.2f", total)
}
if let savedTransactions = loadTransactions() {
transactions += savedTransactions
}
categoryTable.delegate = self
categoryTable.dataSource = self
}
......@@ -41,7 +45,8 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
@IBAction func displayData(sender: UIStoryboardSegue) {
if let sourceView = sender.source as? AddTransactionViewController {
categories = sourceView.categories
saveCategories()
transactions = sourceView.transactions
saveData()
categoryTable.reloadData()
var total = 0.0
for category in categories {
......@@ -55,10 +60,11 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
if let sourceView = sender.source as? DeletionViewController {
if sourceView.confirmed {
categories = [Category]()
transactions = [Transaction]()
saveData()
categoryTable.reloadData()
totalLabel.text = "Total: $0.00"
}
saveCategories()
categoryTable.reloadData()
totalLabel.text = "Total: $0.00"
}
}
......@@ -69,6 +75,11 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
guard let addViewNav = segue.destination as? UINavigationController else {return}
guard let addView = addViewNav.viewControllers.first as? AddTransactionViewController else {return}
addView.categories = categories
addView.transactions = transactions
case "showTransactions":
guard let showViewNav = segue.destination as? UINavigationController else {return}
guard let showView = showViewNav.viewControllers.first as? TransactionTableViewController else {return}
showView.transactions = transactions
default:
return
}
......@@ -94,13 +105,16 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
return cell
}
private func saveCategories() {
private func saveData() {
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: categories,
let transactionData = try NSKeyedArchiver.archivedData(withRootObject: transactions,
requiringSecureCoding: false)
try transactionData.write(to: Transaction.ArchiveURL)
let categoryData = try NSKeyedArchiver.archivedData(withRootObject: categories,
requiringSecureCoding: false)
try data.write(to: Category.ArchiveURL)
try categoryData.write(to: Category.ArchiveURL)
} catch {
return
print("failed with error: \(error)")
}
}
......@@ -109,6 +123,17 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(
Data(contentsOf: Category.ArchiveURL)) as? [Category]
} catch {
print("failed with error: \(error)")
return nil
}
}
private func loadTransactions() -> [Transaction]? {
do {
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(Data(contentsOf: Transaction.ArchiveURL))
as? [Transaction]
} catch {
print("failed with error: \(error)")
return nil
}
}
......
......@@ -11,27 +11,45 @@ class Transaction: NSObject, NSCoding {
var name: String
var category: String
var price: Double
var date: String
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("transactions")
struct PropertyKey {
static let name = "name"
static let category = "category"
static let price = "price"
static let date = "date"
}
init?(name: String, category: String, price: Double, date: String) {
self.name = name
self.category = category
self.price = price
self.date = date
}
init?(name: String) {
self.name = name
self.category = ""
self.price = 0.0
self.price = -1.0
self.date = ""
}
func encode(with coder: NSCoder) {
coder.encode(name, forKey: PropertyKey.name)
coder.encode(category, forKey: PropertyKey.category)
coder.encode(price, forKey: PropertyKey.price)
coder.encode(date, forKey: PropertyKey.date)
}
required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: PropertyKey.name) as? String else {return nil}
self.init(name: name)
guard let category = coder.decodeObject(forKey: PropertyKey.category) as? String else {return nil}
let price = coder.decodeDouble(forKey: PropertyKey.price)
guard let date = coder.decodeObject(forKey: PropertyKey.date) as? String else {return nil}
self.init(name: name, category: category, price: price, date: date)
}
}
......@@ -9,6 +9,9 @@ import UIKit
class TransactionTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var date: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
......
......@@ -12,8 +12,10 @@ class TransactionTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let transaction = Transaction(name: "No Transactions")
transactions.append(transaction!)
if transactions.isEmpty {
let transaction = Transaction(name: "No Transactions")
transactions.append(transaction!)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
......@@ -32,6 +34,13 @@ class TransactionTableViewController: UITableViewController {
}
let transaction = transactions[indexPath.row]
cell.name.text = transaction.name
cell.category.text = transaction.category
if transaction.price < 0 {
cell.price.text = ""
} else {
cell.price.text = String(format: "$%.2f", transaction.price)
}
cell.date.text = transaction.date
return cell
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment