This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func signup() { | |
let headers: HTTPHeaders? = ["Content-Type": "application/x-www-form-urlencoded", | |
"Access-Token": "Bearer abcdefgasmyaccesstoken"] | |
AF.request("http://myserver.com/", | |
method: .post, | |
parameters: nil, | |
encoding: URLEncoding.default, | |
headers: headers, | |
interceptor: nil) | |
.response(completionHandler: { dataResponse in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getUsers(complete: @escaping (_ success: Bool, _ users: [User]?, _ error: Error?) -> ()) -> Request { | |
return alamoFireManager | |
.request("https://myserver.com/", | |
method: .get, | |
parameters: nil, | |
encoding: JSONEncoding.default, | |
headers: nil) | |
.validate(statusCode: 200..<500) | |
.responseJSON(completionHandler: { (response) in | |
switch response.result { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let manager = Session.default | |
manager.session.configuration.timeoutIntervalForRequest = 30 | |
manager.session.configuration.timeoutIntervalForResource = 30 | |
let alamoFireManager = Session(configuration: manager.session.configuration, | |
delegate: SessionDelegate(), | |
startRequestsImmediately: true, | |
interceptor: requestInterceptor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handleGetUsers() { | |
getUsers { (success, users, error) in | |
if success, | |
let users = users { | |
/// Display users | |
} else if let error = error { | |
/// Handle error | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct APIError { | |
let code: Int | |
let message: String | |
} | |
struct UserListAPIResponseData { | |
let users: [User] | |
/// This is a list of users, we might have pagination information, thus struct comes in handy in holding that information | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handleGetUsers() { | |
getUsers { (userListResponse) in | |
switch userListResponse { | |
case .Success(let userData): | |
/// Handle user data with userData.users | |
case .Fail(let error): | |
/// Handle failure with error.message or error.code | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
switch data.count == 0 { | |
case true: | |
return 1 | |
case false: | |
return data.count | |
} | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmptyTableView: UITableView { | |
var emptyDataSource: EmptyTableViewDataSource | |
var emptyTableViewDelegate: EmptyTableViewDelegate? | |
var normalDataSource: UITableViewDataSource | |
var normalTableViewDelegate: UITableViewDelegate? | |
/// If we don't pass in empty and normal data source, this table view can be used as a regular table view. | |
init(emptyDataSource: EmptyTableViewDataSource, | |
normalDataSource: UITableViewDataSource, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmptyTableViewDataSource: NSObject, UITableViewDataSource { | |
let identifier: String | |
let emptyModel: EmptyTableViewCellModel | |
let cellConfigurator: EmptyTableViewCellConfigurator | |
init(identifier: String = "empty-table-view-cell", | |
emptyModel: EmptyTableViewCellModel = EmptyTableViewCellModel(), | |
cellConfigurator: EmptyTableViewCellConfigurator = PlainEmptyTableViewCellConfigurator()) { | |
self.identifier = identifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension EmptyTableView { | |
func showEmptyCell() { | |
guard self.emptyDataSource !== self.dataSource else { | |
reloadData() | |
return | |
} | |
self.delegate = self.emptyTableViewDelegate | |
self.dataSource = self.emptyDataSource | |
self.reloadData() |
OlderNewer