Skip to content

Instantly share code, notes, and snippets.

View mhijack's full-sized avatar
🎯
Focusing

Jianyuan Chen mhijack

🎯
Focusing
View GitHub Profile
@mhijack
mhijack / alamofire-evolution-before.swift
Last active April 7, 2021 05:59
Evolution of handling API response with Alamofire
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
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 {
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)
func handleGetUsers() {
getUsers { (success, users, error) in
if success,
let users = users {
/// Display users
} else if let error = error {
/// Handle error
}
}
}
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
}
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
}
}
}
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 {
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,
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
extension EmptyTableView {
func showEmptyCell() {
guard self.emptyDataSource !== self.dataSource else {
reloadData()
return
}
self.delegate = self.emptyTableViewDelegate
self.dataSource = self.emptyDataSource
self.reloadData()