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

in utopian-io •  7 years ago 

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for your contribution. Though the development effort in this contribution is too low and mainly it is about documenting your codebase. At first we thought it should belong to Documentation category but after going through the commits we feel it is not fitted in any of the category.

We want you to continue working on the project and hopefully, next contribution will be more of a development effort.

Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post,Click here


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]