Skip to content

Commit

Permalink
Merge pull request #13 from ArcBlock/feature/ch405/migrate-data-sourc…
Browse files Browse the repository at this point in the history
…es-to-depend-on-absdkclient

migrate data sources to depend on ABSDKClient
  • Loading branch information
jonathanlu813 authored Jul 9, 2018
2 parents b157127 + b792351 commit feee43f
Show file tree
Hide file tree
Showing 52 changed files with 576 additions and 3,100 deletions.
7 changes: 2 additions & 5 deletions .jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

output: docs
clean: true
objc: true
umbrella_header: ArcBlockSDK.h
framework_root: .
sdk: iphonesimulator
author: ArcBlock
author_url: https://arcblock.io
module: ArcBlockSDK
module: ABSDKCoreKit
github_url: https://github.com/ArcBlock/arcblock-ios-sdk
github_file_prefix: https://github.com/ArcBlock/arcblock-ios-sdk/tree/master
module_version: 0.4.8
module_version: 0.4.9
27 changes: 8 additions & 19 deletions ABSDKCoreKit/ABSDKClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ public class ABSDKClient: NetworkConnectionNotification {
}
}

func onNetworkAvailabilityStatusChanged(isEndpointReachable: Bool) {
var accessState: ClientNetworkAccessState = .offline
if isEndpointReachable {
accessState = .offline
}
self.connectionStateChangeHandler?.stateChanged(networkState: accessState)
}

/// Fetches a query from the server or from the local cache, depending on the current contents of the cache and the specified cache policy.
///
/// - Parameters:
Expand All @@ -238,20 +246,9 @@ public class ABSDKClient: NetworkConnectionNotification {
/// - error: An error that indicates why the fetch failed, or `nil` if the fetch was succesful.
/// - Returns: A query watcher object that can be used to control the watching behavior.
@discardableResult public func watch<Query: GraphQLQuery>(query: Query, cachePolicy: CachePolicy = .returnCacheDataElseFetch, queue: DispatchQueue = DispatchQueue.main, resultHandler: @escaping OperationResultHandler<Query>) -> GraphQLQueryWatcher<Query> {

return apolloClient!.watch(query: query, cachePolicy: cachePolicy, queue: queue, resultHandler: resultHandler)
}

// public func subscribe<Subscription: GraphQLSubscription>(subscription: Subscription, queue: DispatchQueue = DispatchQueue.main, resultHandler: @escaping SubscriptionResultHandler<Subscription>) throws -> AWSAppSyncSubscriptionWatcher<Subscription>? {
//
// return AWSAppSyncSubscriptionWatcher(client: self.appSyncMQTTClient,
// httpClient: self.httpTransport!,
// store: self.store!,
// subscription: subscription,
// handlerQueue: queue,
// resultHandler: resultHandler)
// }

/// Performs a mutation by sending it to the server.
///
/// - Parameters:
Expand All @@ -277,12 +274,4 @@ public class ABSDKClient: NetworkConnectionNotification {

return apolloClient!.perform(mutation: mutation, queue: queue, resultHandler: resultHandler)
}

func onNetworkAvailabilityStatusChanged(isEndpointReachable: Bool) {
var accessState: ClientNetworkAccessState = .offline
if isEndpointReachable {
accessState = .offline
}
self.connectionStateChangeHandler?.stateChanged(networkState: accessState)
}
}
30 changes: 0 additions & 30 deletions ABSDKCoreKit/ABSDKCoreKit.h

This file was deleted.

186 changes: 186 additions & 0 deletions ABSDKCoreKit/ABSDKDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// ABSDKDataSource.swift
//
// Copyright (c) 2017-present ArcBlock Foundation Ltd <https://www.arcblock.io/>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Apollo
import UIKit

public typealias ArrayDataSourceMapper<Query: GraphQLQuery, Data: GraphQLSelectionSet> = (_ data: Query.Data) -> [Data?]?

public typealias ObjectDataSourceMapper<Query: GraphQLQuery, Data: GraphQLSelectionSet> = (_ data: Query.Data) -> Data?

public typealias ViewUpdateHandler<Data: GraphQLSelectionSet> = (_ view: UIView, _ data: Data) -> Void

protocol ABSDKDataSource {
associatedtype Query: GraphQLQuery
associatedtype Data: GraphQLSelectionSet

var client: ABSDKClient { get }
var query: Query { get }
var viewUpdateHandler: ViewUpdateHandler<Data> { get }
var watcher: GraphQLQueryWatcher<Query>? { get }

init(client: ABSDKClient, query: Query, viewUpdateHandler: @escaping ViewUpdateHandler<Data>)
}

final public class ABSDKObjectDataSource<Query: GraphQLQuery, Data: GraphQLSelectionSet>: ABSDKDataSource {
public weak var view: UIView?

var object: Data? = nil {
didSet {
viewUpdateHandler(view!, object!)
}
}

var dataSourceMapper: ObjectDataSourceMapper<Query, Data>? = nil
var watcher: GraphQLQueryWatcher<Query>? = nil

let client: ABSDKClient
let query: Query
let viewUpdateHandler: ViewUpdateHandler<Data>

init(client: ABSDKClient, query: Query, viewUpdateHandler: @escaping ViewUpdateHandler<Data>) {
self.client = client
self.query = query
self.viewUpdateHandler = viewUpdateHandler
}

public convenience init(client: ABSDKClient, query: Query, dataSourceMapper: @escaping ObjectDataSourceMapper<Query, Data>, viewUpdateHandler: @escaping ViewUpdateHandler<Data>) {
self.init(client: client, query: query, viewUpdateHandler: viewUpdateHandler)
self.dataSourceMapper = dataSourceMapper

self.watcher = self.client.watch(query: self.query, cachePolicy: .returnCacheDataAndFetch, resultHandler: { (result, err) in
self.object = self.dataSourceMapper!((result?.data)!)
})
}
}

final public class ABSDKTableViewDataSource<Query: GraphQLQuery, Data: GraphQLSelectionSet>: NSObject, UITableViewDataSource, ABSDKDataSource {
public weak var tableView: UITableView? {
didSet {
tableView?.dataSource = self
}
}

public var reuseIdentifier: String! = "Cell"

var array: [Data?]? = [] {
didSet {
tableView?.reloadData()
}
}

var dataSourceMapper: ArrayDataSourceMapper<Query, Data>? = nil
var watcher: GraphQLQueryWatcher<Query>? = nil

let client: ABSDKClient
let query: Query
let viewUpdateHandler: ViewUpdateHandler<Data>

init(client: ABSDKClient, query: Query, viewUpdateHandler: @escaping ViewUpdateHandler<Data>) {
self.client = client
self.query = query
self.viewUpdateHandler = viewUpdateHandler
super.init()
}

public convenience init(client: ABSDKClient, query: Query, dataSourceMapper: @escaping (Query.Data) -> [Data?]?, viewUpdateHandler: @escaping (UIView, Data) -> Void) {
self.init(client: client, query: query, viewUpdateHandler: viewUpdateHandler)
self.dataSourceMapper = dataSourceMapper

self.watcher = self.client.watch(query: self.query, cachePolicy: .returnCacheDataAndFetch, resultHandler: { (result, err) in
self.array = self.dataSourceMapper!((result?.data)!)
})
}

public func dataForIndexPath(indexPath: IndexPath) -> Data? {
return array![indexPath.row]
}

public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array?.count ?? 0
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
let data = self.dataForIndexPath(indexPath: indexPath)
viewUpdateHandler(cell, data!)
return cell
}
}

final public class ABSDKCollectionViewDataSource<Query: GraphQLQuery, Data: GraphQLSelectionSet>: NSObject, UICollectionViewDataSource, ABSDKDataSource {
public weak var collectionView: UICollectionView? {
didSet {
collectionView?.dataSource = self
}
}

public var reuseIdentifier: String! = "Cell"

var array: [Data?]? = [] {
didSet {
collectionView?.reloadData()
}
}

var dataSourceMapper: ArrayDataSourceMapper<Query, Data>? = nil
var watcher: GraphQLQueryWatcher<Query>? = nil

let client: ABSDKClient
let query: Query
let viewUpdateHandler: ViewUpdateHandler<Data>

init(client: ABSDKClient, query: Query, viewUpdateHandler: @escaping ViewUpdateHandler<Data>) {
self.client = client
self.query = query
self.viewUpdateHandler = viewUpdateHandler
super.init()
}

public convenience init(client: ABSDKClient, query: Query, dataSourceMapper: @escaping (Query.Data) -> [Data?]?, viewUpdateHandler: @escaping (UIView, Data) -> Void) {
self.init(client: client, query: query, viewUpdateHandler: viewUpdateHandler)
self.dataSourceMapper = dataSourceMapper

self.watcher = self.client.watch(query: self.query, cachePolicy: .returnCacheDataAndFetch, resultHandler: { (result, err) in
self.array = self.dataSourceMapper!((result?.data)!)
})
}

public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array?.count ?? 0
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
let data = array![indexPath.row]!
viewUpdateHandler(cell, data)
return cell
}
}
Loading

0 comments on commit feee43f

Please sign in to comment.