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 ...@@ -14,6 +14,7 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav
@IBOutlet weak var costTextField: UITextField! @IBOutlet weak var costTextField: UITextField!
var doneButton: UIBarButtonItem! var doneButton: UIBarButtonItem!
var categories = [Category]() var categories = [Category]()
var transactions = [Transaction]()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
...@@ -61,6 +62,13 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav ...@@ -61,6 +62,13 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav
super.prepare(for: segue, sender: sender) super.prepare(for: segue, sender: sender)
guard let button = sender as? UIBarButtonItem, button === addButton else {return} guard let button = sender as? UIBarButtonItem, button === addButton else {return}
let price = Double(costTextField.text ?? "0.0") 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 { for category in categories {
if categoryTextField.text == category.name { if categoryTextField.text == category.name {
category.addToTotal(price: price ?? 0.0) category.addToTotal(price: price ?? 0.0)
......
...@@ -147,28 +147,69 @@ ...@@ -147,28 +147,69 @@
<color key="backgroundColor" systemColor="systemBackgroundColor"/> <color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes> <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"> <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"/> <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"> <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"/> <autoresizingMask key="autoresizingMask"/>
<subviews> <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"> <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"/> <rect key="frame" x="20" y="14.5" width="57" height="29.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/> <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="textColor"/>
<nil key="highlightedColor"/> <nil key="highlightedColor"/>
</label> </label>
</subviews> </subviews>
<constraints> <constraints>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="top" secondItem="Q12-f8-IYE" secondAttribute="topMargin" id="3HX-yH-7bM"/> <constraint firstItem="TY2-fe-Hf6" firstAttribute="top" secondItem="Q12-f8-IYE" secondAttribute="top" id="78u-8N-jkW"/>
<constraint firstItem="ZQh-dN-pAc" firstAttribute="leading" secondItem="Q12-f8-IYE" secondAttribute="leadingMargin" id="Ze3-CE-NrL"/> <constraint firstItem="TY2-fe-Hf6" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="ieZ-9F-Ud3" secondAttribute="trailing" constant="1" id="7uk-Eu-hzl"/>
<constraint firstAttribute="bottomMargin" secondItem="ZQh-dN-pAc" secondAttribute="bottom" constant="0.5" id="pvp-fP-BeH"/> <constraint firstItem="mCQ-Pi-90k" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="ZQh-dN-pAc" secondAttribute="trailing" constant="1" id="FtQ-HG-DIR"/>
<constraint firstAttribute="trailingMargin" relation="lessThanOrEqual" secondItem="ZQh-dN-pAc" secondAttribute="trailing" constant="332" id="yMY-ib-KtU"/> <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> </constraints>
</tableViewCellContentView> </tableViewCellContentView>
<connections> <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="name" destination="ZQh-dN-pAc" id="BAX-R7-zkd"/>
<outlet property="price" destination="mCQ-Pi-90k" id="M0O-bG-CnA"/>
</connections> </connections>
</tableViewCell> </tableViewCell>
</prototypes> </prototypes>
...@@ -187,7 +228,7 @@ ...@@ -187,7 +228,7 @@
</tableViewController> </tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Vnp-er-RtW" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="Vnp-er-RtW" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects> </objects>
<point key="canvasLocation" x="2812" y="165"/> <point key="canvasLocation" x="2811.594202898551" y="164.73214285714286"/>
</scene> </scene>
<!--Delete All Confirmation--> <!--Delete All Confirmation-->
<scene sceneID="a5z-6h-cFL"> <scene sceneID="a5z-6h-cFL">
......
...@@ -9,6 +9,7 @@ import UIKit ...@@ -9,6 +9,7 @@ import UIKit
class CategoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { class CategoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var categories = [Category]() var categories = [Category]()
var transactions = [Transaction]()
@IBOutlet weak var categoryTable: UITableView! @IBOutlet weak var categoryTable: UITableView!
@IBOutlet weak var totalLabel: UILabel! @IBOutlet weak var totalLabel: UILabel!
...@@ -22,6 +23,9 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView ...@@ -22,6 +23,9 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
} }
totalLabel.text = String(format: "Total: $%.2f", total) totalLabel.text = String(format: "Total: $%.2f", total)
} }
if let savedTransactions = loadTransactions() {
transactions += savedTransactions
}
categoryTable.delegate = self categoryTable.delegate = self
categoryTable.dataSource = self categoryTable.dataSource = self
} }
...@@ -41,7 +45,8 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView ...@@ -41,7 +45,8 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
@IBAction func displayData(sender: UIStoryboardSegue) { @IBAction func displayData(sender: UIStoryboardSegue) {
if let sourceView = sender.source as? AddTransactionViewController { if let sourceView = sender.source as? AddTransactionViewController {
categories = sourceView.categories categories = sourceView.categories
saveCategories() transactions = sourceView.transactions
saveData()
categoryTable.reloadData() categoryTable.reloadData()
var total = 0.0 var total = 0.0
for category in categories { for category in categories {
...@@ -55,10 +60,11 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView ...@@ -55,10 +60,11 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
if let sourceView = sender.source as? DeletionViewController { if let sourceView = sender.source as? DeletionViewController {
if sourceView.confirmed { if sourceView.confirmed {
categories = [Category]() 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 ...@@ -69,6 +75,11 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
guard let addViewNav = segue.destination as? UINavigationController else {return} guard let addViewNav = segue.destination as? UINavigationController else {return}
guard let addView = addViewNav.viewControllers.first as? AddTransactionViewController else {return} guard let addView = addViewNav.viewControllers.first as? AddTransactionViewController else {return}
addView.categories = categories 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: default:
return return
} }
...@@ -94,13 +105,16 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView ...@@ -94,13 +105,16 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
return cell return cell
} }
private func saveCategories() { private func saveData() {
do { 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) requiringSecureCoding: false)
try data.write(to: Category.ArchiveURL) try categoryData.write(to: Category.ArchiveURL)
} catch { } catch {
return print("failed with error: \(error)")
} }
} }
...@@ -109,6 +123,17 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView ...@@ -109,6 +123,17 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData( return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(
Data(contentsOf: Category.ArchiveURL)) as? [Category] Data(contentsOf: Category.ArchiveURL)) as? [Category]
} catch { } 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 return nil
} }
} }
......
...@@ -11,27 +11,45 @@ class Transaction: NSObject, NSCoding { ...@@ -11,27 +11,45 @@ class Transaction: NSObject, NSCoding {
var name: String var name: String
var category: String var category: String
var price: Double var price: Double
var date: String
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("transactions") static let ArchiveURL = DocumentsDirectory.appendingPathComponent("transactions")
struct PropertyKey { struct PropertyKey {
static let name = "name" 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) { init?(name: String) {
self.name = name self.name = name
self.category = "" self.category = ""
self.price = 0.0 self.price = -1.0
self.date = ""
} }
func encode(with coder: NSCoder) { func encode(with coder: NSCoder) {
coder.encode(name, forKey: PropertyKey.name) 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) { required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: PropertyKey.name) as? String else {return nil} 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 ...@@ -9,6 +9,9 @@ import UIKit
class TransactionTableViewCell: UITableViewCell { class TransactionTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel! @IBOutlet weak var name: UILabel!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var date: UILabel!
override func awakeFromNib() { override func awakeFromNib() {
super.awakeFromNib() super.awakeFromNib()
......
...@@ -12,8 +12,10 @@ class TransactionTableViewController: UITableViewController { ...@@ -12,8 +12,10 @@ class TransactionTableViewController: UITableViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let transaction = Transaction(name: "No Transactions") if transactions.isEmpty {
transactions.append(transaction!) let transaction = Transaction(name: "No Transactions")
transactions.append(transaction!)
}
} }
override func numberOfSections(in tableView: UITableView) -> Int { override func numberOfSections(in tableView: UITableView) -> Int {
...@@ -32,6 +34,13 @@ class TransactionTableViewController: UITableViewController { ...@@ -32,6 +34,13 @@ class TransactionTableViewController: UITableViewController {
} }
let transaction = transactions[indexPath.row] let transaction = transactions[indexPath.row]
cell.name.text = transaction.name 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 return cell
} }
......
Supports Markdown
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