1. Home
  2. Docs
  3. Finance App – Documentation
  4. Integrate Your Own Data Sources

Integrate Your Own Data Sources

All the financial data that is displayed in the tables and charts of our finance app is being provided via our data source pattern. Assuming you’ve followed the steps we outlined in the “Getting Started” section, you’ve probably already read about our generic data source. If not, please take a few moments to truly understand our data source pattern, especially the ATCGenericCollectionViewControllerDataSource section.

We’ve built the generic data source so that you can integrate ANY type of backend extremely easily.

However, you will still require some basic Swift programming knowledge, since implementing a data source is literally an implementation – in most cases you’ll probably need to make a few http requests (if your backend is a REST endpoint), or use an SDK if your backend offers one (e.g. AWS Amplify, Firebase, etc).

All the data sources are being specified in FinanceDataSourceProvider. This is the main object that covers the data sources for ALL the screens that you can see in our finance app. The code is extremely well modularized, so that you only need to change this file alone in order to reroute the entire app to your own backend.

As you can see, all the methods that exist in this provider have clear names, self-explaining what they are used for. For example, fetchBankAccountChart returns the data feeding the chart on the bank account screen. fetchCryptosChart feeds the data for the chart on the crypto screen. stocksHomeDataSource provides the data source used by the stocks section on the home screen.

All data sources are simply classes that conform to the ATCGenericCollectionViewControllerDataSource protocol. You only need to write your own data source that conforms to this protocol and the app will start using your own backend.

By default, our app only serves static hardcoded data, as presented on the product page. For example, the data source for the screens showing all the stocks looks like this in the provider:

var allStocksListDataSource: ATCGenericCollectionViewControllerDataSource {
    return ATCGenericLocalHeteroDataSource(items:
        FinanceStaticDataProvider.stocks
    )
}

To achieve this, we’ve built our own data source, that’s returning mock data. As you can see, it’s called ATCGenericLocalHeteroDataSource. You can find its extremely simple implementation in Core/Helpers.

You’ll need to create a similar class, that instead of returning mock data, will return data from your own backend. You new class needs to conform to the ATCGenericCollectionViewControllerDataSource protocol, so you need to implement 3 different methods:

protocol ATCGenericCollectionViewControllerDataSource: class {
    var delegate: ATCGenericCollectionViewControllerDataSourceDelegate? {get set}

    func object(at index: Int) -> ATCGenericBaseModel?
    func numberOfObjects() -> Int

    func loadFirst()
    func loadBottom() // optional
    func loadTop() // optional
}

Keep in mind that the implementation of these methods decides what data gets displayed on the screen, in the UI. It’s up to you to fetch the data from your own backend, which usually happens in the loadFirst method.

To make it even easier for you, here’s a skeleton for how your data sources should look like for the stocks:

class MyOwnStocksDataSource: ATCGenericCollectionViewControllerDataSource {

    private var stocks: [ATCFinanceAsset] = [ATCFinanceAsset]()

    func object(at index: Int) -> ATCGenericBaseModel? {
        if (index < stocks.count) {
            return stocks[index]
        }
        return nil
    }

    func numberOfObjects() -> Int {
        return stocks.count
    }

    func loadFirst() {
       // add your backend call here
       // once you fetch the data from your backend, assign it to self.stocks
       // self.stocks = results
       // then call
       // self.delegate?.genericCollectionViewControllerDataSource(self, didLoadFirst: results)
    }

    func loadBottom() {} // this is optional, only if you do pagination
    func loadTop() {} // this is optional, only if you want pull to refresh

}

As you notice, loadFirst() is the most important method that you need to implement. It fetches the data from your backend.

Once you finished the implementation of MyOwnStocksDataSource, you can then use it in  FinanceDataSourceProvider for the stocks data source. So change the implementation of the stocks data source from:

var allStocksListDataSource: ATCGenericCollectionViewControllerDataSource {
    return ATCGenericLocalHeteroDataSource(items:
        FinanceStaticDataProvider.stocks
    )
}

to:

var allStocksListDataSource: ATCGenericCollectionViewControllerDataSource {
    return MyStockDataSource()
}

That’s it. Now, instead of displaying our mock hardcoded data, the app will start displaying the stock values from your own backend.