M O O N R I S E avatar

SwiftyConnect - Steem for iOS - Added Full Code Documentation + Account History Pagination

moonrise

Published: 14 May 2018 › Updated: 14 May 2018SwiftyConnect - Steem for iOS - Added Full Code Documentation + Account History Pagination

SwiftyConnect - Steem for iOS - Added Full Code Documentation + Account History Pagination

Repository

https://github.com/caspernikus/SwiftyConnect

New Features

What feature(s) did you add?

SwiftyConnect finally reached Version 0.2 which brings full documented code and a complete GitHub ReadMe. The next step will be building a Tutorial Series in combination with a GitHub Wiki and example project.

Nevertheless does V0.2 bring more than adding doc blocks to the code. I also worked on an account history pagination helper method, which I needed mainly on private projects. After finishing it, I decided to add it to SwiftyConnect since paginating the account history is something an app should have.

public func paginateAccountHistory(name: String, steps: Int, pageSteps: Int, maxLimit: Int?, completion:((Any?, Any?) -> Void)?) {
        var start = -1
        var page = 0
        var steps = steps
        var done = false
        var asyncRequest = false
        var allTransactions : [NSDictionary] = []
        var paginated : [[NSDictionary]] = [[]]
        
        while !done {
            guard asyncRequest == false else {
                continue
            }

            asyncRequest = true
            Steem.sharedInstance.api.getAccountHistory(name: name, from: start, limit: steps, completion: { (error, response) in
                guard error == nil else {
                    done = true
                    completion!(error, nil)
                    return
                }
                
                let response = response as! NSDictionary
                let result = response["result"] as! [NSArray]
                let length = result.count - 1
                if length == 0 {
                    done = true
                    return
                }
                
                for (index, trx) in result.enumerated() {
                    allTransactions.append(trx[1] as! NSDictionary)
                    
                    if paginated[page].count >= pageSteps {
                        page = page + 1
                        paginated.append([])
                    }
                    
                    paginated[page].append(trx[1] as! NSDictionary)

                    if index == length {
                        if start == -1 {
                            start = 0
                        }

                        start = (trx[0] as! Int) - length
                    }
                }
                
                if maxLimit != nil && allTransactions.count > maxLimit! {
                    done = true
                }
                
                if (start < steps) {
                    steps = start
                }
                
                if (start < 0) {
                    let newLimit = start + length
                    start = 0
                    steps = newLimit
                }
                
                if (start == 0 && steps == 0) {
                    done = true
                }

                asyncRequest = false
            })
        }
        
        completion!(nil, paginated)
    }

As you can see paginateAccountHistory has 4 parameters and one callback:
public func paginateAccountHistory(name: String, steps: Int, pageSteps: Int, maxLimit: Int?, completion:((Any?, Any?) -> Void)?):

  • name: Should be the account name
  • steps: In which steps should the account history be loaded (100 a time ?, or max 1000 a time?)
  • pageSteps: How many transactions should be visible per page
  • maxLimit: If you do not want to load the complete history enter a positive value, else set it to nil

The Account History is loaded descending, meaning the newest transactions gets loaded first.

How did you implement it/them?

Doc Blocks can be found here, all commits have 'Added Documentation' in it.

The Pagination itself can be found above, the hardest part was mainly to achieve quite an easy async loading of the account history in an sync and ordered way without using multiple functions.
Swift currently has a lack of async / await functions, which makes it hard to build complex async operations uncomplicated. I used a while loop and flag which stops me from firing a new request before the last async request isn't done. This simulates quite a sync way of doing it :)
The other steps are just math and creating a simple paginated array.

Installation

Carthage

github "caspernikus/SwiftyConnect" ~> 0.2
(When building SwiftyConnect the lib OAuth2 is also builded, there is no need to add OAuth2 inside your project, since SwiftyConenct contains OAuth2!)

Roadmap

  • V0.2:
    • Full Code Documentation
  • V1.0:
    • More Helper Class Functions
    • Transactions Sending / Receiving
    • Signing

How to contribute?

Create Pull Requests

Proof for GitHub

Please visit my contribution here, to see that benediktveith is also my account !

GitHub Account

https://github.com/caspernikus

Leave SwiftyConnect - Steem for iOS - Added Full Code Documentation + Account History Pagination to:

Written by

<3 Music & Programming & More <3

Read more #utopian-io posts


Best Posts From M O O N R I S E

We have not curated any of moonrise's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From M O O N R I S E