Commit 9d58a1ee authored by tybrandt's avatar tybrandt
Browse files

Can now delete individual transactions

parent 8e9c360b
......@@ -104,6 +104,10 @@ class AddTransactionViewController: UIViewController, UITextFieldDelegate, UINav
addButton.isEnabled = false
return
}
if Double(text) == 0.0 {
addButton.isEnabled = false
return
}
addButton.isEnabled = true
}
......
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="8jZ-aG-Xu9">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="8jZ-aG-Xu9">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
......@@ -52,7 +52,7 @@
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="CategoryTableViewCell" rowHeight="40" id="way-ii-Wwy" customClass="CategoryTableViewCell" customModule="Budget_Tracker" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="374" height="40"/>
<rect key="frame" x="0.0" y="24.5" width="374" height="40"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="way-ii-Wwy" id="9Fx-Di-qVx">
<rect key="frame" x="0.0" y="0.0" width="374" height="40"/>
......@@ -147,7 +147,7 @@
<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="44"/>
<rect key="frame" x="0.0" y="24.5" 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="44"/>
......@@ -218,9 +218,16 @@
<outlet property="delegate" destination="G1A-Zz-7cZ" id="v5d-PW-7nE"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="All Transactions" id="xym-6q-kXb"/>
<navigationItem key="navigationItem" title="All Transactions" id="xym-6q-kXb">
<barButtonItem key="leftBarButtonItem" title="Back" id="ZC9-3v-N7P">
<connections>
<segue destination="GSr-eX-ZSF" kind="unwind" unwindAction="deleteTransactionWithSender:" id="aW2-JV-nVR"/>
</connections>
</barButtonItem>
</navigationItem>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Vnp-er-RtW" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
<exit id="GSr-eX-ZSF" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="1848" y="165"/>
</scene>
......
......@@ -56,6 +56,26 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
}
}
@IBAction func deleteTransaction(sender: UIStoryboardSegue) {
if let sourceView = sender.source as? TransactionTableViewController {
if sourceView.deletedTransaction {
categories = sourceView.categories
if sourceView.transactions[0].name != "No Transactions" {
transactions = sourceView.transactions
} else {
transactions = [Transaction]()
}
saveData()
categoryTable.reloadData()
var total = 0.0
for category in categories {
total += category.price
}
totalLabel.text = String(format: "Total: $%.2f", total)
}
}
}
@IBAction func deleteData(sender: UIStoryboardSegue) {
if let sourceView = sender.source as? DeletionViewController {
if sourceView.confirmed {
......@@ -79,6 +99,7 @@ class CategoryViewController: UIViewController, UITableViewDelegate, UITableView
case "showTransactions":
guard let showView = segue.destination as? TransactionTableViewController else {return}
showView.transactions = transactions
showView.categories = categories
default:
return
}
......
......@@ -9,6 +9,8 @@ import UIKit
class TransactionTableViewController: UITableViewController {
var transactions = [Transaction]()
var categories = [Category]()
var deletedTransaction = false
override func viewDidLoad() {
super.viewDidLoad()
......@@ -44,4 +46,33 @@ class TransactionTableViewController: UITableViewController {
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
deletedTransaction = true
let transaction = transactions[indexPath.row]
var i = 0
for category in categories {
if category.name == transaction.category {
category.price -= transaction.price
if category.price <= 0.0 {
categories.remove(at: i)
}
break
}
i += 1
}
transactions.remove(at: indexPath.row)
if transactions.isEmpty {
let noTransaction = Transaction(name: "No Transactions")
transactions.append(noTransaction!)
}
tableView.reloadData()
}
}
}
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