Latest 0.1.9
Homepage https://github.com/Mioke/SwiftArchitectureWithPOP
License MIT
Platforms ios 8.0
Dependencies Alamofire, FMDB, KMCache
Frameworks UIKit, Foundation
Authors

SwiftArchitectureWithPOP

A base architecture written in swift and protocol oriented.

Install

Using cocoapods:
pod 'bais-ios'
Manually

Download .zip package and copy the swiftAchitecture/Base folder into you project.

What to provide

Task

  • Task executor and result reciever.

    Any Object can be a task executor or a result reciever by conform the protocols of sender and reciever. A task can be any operation that you want be done in background thread. And callbacks will running in main thread again to do the things later.

  // self is any thing conform to protocol<sender, reciever>
  func someAction() -> Void {
      self.doTask({ () -> receiveDataType in
          return someCalculationNeedLongTime()
      }, identifier: "calculation")
  }

  // callback
  override func finishTaskWithReuslt(result: receiveDataType, identifier: String) {
       if identifier == "calculation" {
          Log.debugPrint(result)
      }
  }

  override func taskCancelledWithError(error: ErrorResultType, identifier: String) {
      super.taskCancelledWithError(error, identifier: identifier)
  }

Networking

  • Server

    Provide some basic functionality of a server like onlieURL, offlineURL, isOnline etc. In test Mode, offline the server.

    #if DEBUG
    Server.online = false
    #endif

You can comstomize the operation of dealing response data now, just subclass from Server and conform to protocol ServerDataProcessProtocol like:

func handle(data: Any, shouldRetry: inout Bool) throws {

    if  let dic = data as? [String: Any],
        let errorCode = dic["error_code"] as? Int,
        errorCode != 0 {

        switch errorCode {
        case 101:
            throw NSError()
        default:
            throw NSError()
        }
    }
}
  • ApiManager

    Now you can manager request with ApiManager, just sublass from BaseApiManager and conform to protocol ApiInfoProtocol. Only need to provide some infomation about the API and set where the callback is, you are already finished the configuration of an API.

    var apiVersion: String {
        get { return "v2" }
    }
    var apiName: String {
        get { return "user/login" }
    }
    var server: Server {
        get { return mainServer }
    }

The BaseApiManager provide some basic method like:

  public func loadData(with params: [String: Any]?) -> Void

Set delegate for receiving success with origin data or failure with error:

  extension ViewController: ApiCallbackProtocol {

      func ApiManager(apiManager: BaseApiManager, finishWithOriginData data: AnyObject) {

          if let apiManager = apiManager as? ApiLogin {
              print("login success: n (apiManager.originData())")
          }
      }

      func ApiManager(apimanager: BaseApiManager, failedWithError error: NSError) {

          if apiManager is ApiLogin {
              Log.debugPrint("login failed with error: (error)")
          }
      }
  }
  • Attentions
  1. The request is generated by RequestGenerator, using Alamofire Request.

Persistance

  • Database

    Like ApiManager, only need to subclass from KMPersistanceDatabase and conform to DatabaseManagerProtocol, provide path,databaseName,database, you are already create a new database in your project. e.g.

  class DefaultDatabase: KMPersistanceDatabase, DatabaseManagerProtocol {

    override init() {

        self.databaseName = "default.db"
        self.path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! + "/" + self.databaseName
        self.database = FMDatabaseQueue(path: self.path)

        super.init()
    }
  }
  • Table and Record

    Subclass from KMPersistanceTable and conform to TableProtocol let you create a new table in a database. Any objcect conform RecordProtocol can be record in the table you want. See more details in demo.

    Using this just like:

    let table = UserTable()
    let newUser = UserModel(name: "Klein", uid: 310)
    table.replaceRecord(newUser)
  • Fetch

    Fetch data with conditions using DatabaseCommandCondition:

    let table = UserTable()
    let condition = DatabaseCommandCondition()
    
    condition.whereConditions = "user_id >= 0"
    condition.orderBy = "user_name"
    
    let result = table.queryRecordWithSelect("user_name", condition: condition)
  • Advanced

    Always, database provide method of doing query or execute with sql directly, for complex database operation:

    let db = DefaultDatabase()
    db.query("select * from tableDoesntExtist", withArgumentsInArray: nil)

Tools and Kits

  • Custom extensions and categories.
  • UI relevant class for easy accessing global UI settings.
  • SystemLog can write log to files, and stored in sandbox.

Almost done >w<!

TODO

  • Networking: cache, origin data transform to Model or View’s data, priority of request.
  • Download and upload functions in API manager.
  • Persistance: transform data to model or View’s data after query.
  • Animations, Tools and Kits: TextKit like YYText, etc. (bais-ios won’t provide those utilities, because base shouldn’t have to.)
  • Refactoring, more functional and reative. Considering to use Rx or ReactiveSwift. Fully use genericity.

License

All the source code is published under the MIT license. See LICENSE file for details.

Latest podspec

{
    "name": "bais-ios",
    "version": "0.1.9",
    "summary": "Breezy architecture in Swift for building iOS applications.",
    "description": "* Breezy architecture in Swift for building iOS applications. It offers lots of functions which simple and easy to use for developer.",
    "homepage": "https://github.com/Mioke/SwiftArchitectureWithPOP",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Mioke Klein": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Mioke/SwiftArchitectureWithPOP.git",
        "tag": "0.1.9"
    },
    "platforms": {
        "ios": "8.0"
    },
    "frameworks": [
        "UIKit",
        "Foundation"
    ],
    "dependencies": {
        "Alamofire": [],
        "FMDB": [],
        "KMCache": []
    },
    "source_files": "swiftArchitecture/Base/**/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This