Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
aydenp committed Dec 6, 2017
1 parent 8860f76 commit a32e1d3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Bank/AccountViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class AccountViewController: UITableViewController, AccountHeaderViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()

// Add refresh control
refreshControl = UIRefreshControl()
refreshControl!.addTarget(self, action: #selector(shouldRefresh), for: .valueChanged)
// Listen for when refreshes finish
NotificationCenter.default.addObserver(self, selector: #selector(refreshDidFinish), name: ViewController.finishedRefreshingDataNotification, object: nil)
// Setup header view
headerView.account = account
headerView.accountIndex = index
Expand Down Expand Up @@ -61,6 +66,18 @@ class AccountViewController: UITableViewController, AccountHeaderViewDelegate {
}
}

// MARK: - Refresh Control Handling

@objc func shouldRefresh() {
NotificationCenter.default.post(name: ViewController.shouldRefreshDataNotification, object: nil)
}

@objc func refreshDidFinish() {
DispatchQueue.main.async {
self.refreshControl!.endRefreshing()
}
}

// MARK: - Tap Handling

@objc func tapped(with gestureRecognizer: UITapGestureRecognizer) {
Expand Down
20 changes: 17 additions & 3 deletions Bank/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import UIKit

class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, AccountViewControllerDelegate, NoAccountsViewControllerDelegate {
static let shouldRefreshDataNotification = Notification.Name(rawValue: "ViewControllerShouldRefreshDataNotificationName")
static let finishedRefreshingDataNotification = Notification.Name(rawValue: "ViewControllerFinishedRefreshingDataNotificationName")
var statusViewController: StatusViewController?
var statusBarOverlayView: StatusBarOverlayView!
var isExchanging = false
Expand Down Expand Up @@ -38,18 +40,30 @@ class ViewController: UIPageViewController, UIPageViewControllerDataSource, UIPa
// Listen for and reload on fetched bank account changes
NotificationCenter.default.addObserver(self, selector: #selector(reloadViewControllers), name: SessionDataStorage.accountsChangedNotification, object: nil)

// Listen for others wanting data refresh
NotificationCenter.default.addObserver(self, selector: #selector(startLoading), name: ViewController.shouldRefreshDataNotification, object: nil)

setupStatus()
}

/// Called when we're ready to start finding out about the linked accounts.
func startLoading() {
@objc func startLoading() {
PlaidManager.shared.api.getTransactions { (transactions, accounts, error) in
let alreadyHasData = SessionDataStorage.shared.accounts != nil
if alreadyHasData {
// Notify others we finished refreshing
NotificationCenter.default.post(name: ViewController.finishedRefreshingDataNotification, object: nil)
}
guard error == nil, let transactions = transactions, let accounts = accounts else {
print("Couldn't load bank accounts:", error?.localizedDescription ?? "no error")
(self.statusViewController ?? self).showAlert(title: "Couldn't Load Bank Accounts", message: error?.localizedDescription ?? "An unknown error occurred while attempting to load your bank accounts.", actions: [.cancel("Retry") { _ in self.startLoading() }, .normal("Unlink Bank") { _ in
func handleRetry(_ action: UIAlertAction) {
self.startLoading()
}
let actions: [UIAlertAction] = alreadyHasData ? [.normal("Retry", handler: handleRetry), .cancel] : [.cancel("Retry", handler: handleRetry), .normal("Unlink Bank") { _ in
PlaidManager.shared.accessToken = nil
self.determineStatus()
}])
}]
(self.statusViewController ?? self).showAlert(title: "Couldn't Load Bank Accounts", message: error?.localizedDescription ?? "An unknown error occurred while attempting to load your bank accounts.", actions: actions)
return
}
print("Got accounts from server")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This app uses the [Plaid API](https://www.plaid.com) to fetch information about

## IMPORTANT: Production Usage

**This project is meant for personal use.**. If you plan to ship any of the code, [please read this](/production-usage.md) to **ensure you distribute it safely.**
**This project is meant for personal use.** If you plan to ship any of the code, [please read this](/production-usage.md) to **ensure you distribute it safely.**

## Reporting Issues

Expand Down

0 comments on commit a32e1d3

Please sign in to comment.