Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Getting rid of Alamofire and ObjectMapper :)
  • Loading branch information
tomarra committed Apr 24, 2019
1 parent b87742d commit 218e022
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 101 deletions.
15 changes: 0 additions & 15 deletions Bike Buddy.xcodeproj/project.pbxproj
Expand Up @@ -64,9 +64,6 @@
D1B6DD1D1C9EC3E3008543A3 /* BikeBuddySnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B6DD1C1C9EC3E3008543A3 /* BikeBuddySnapshotTests.swift */; };
D1B6DD261C9EC436008543A3 /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B6DD251C9EC436008543A3 /* SnapshotHelper.swift */; };
D1C4D76A1CEFC163001982F3 /* StationListSectionHeaderCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C4D7691CEFC163001982F3 /* StationListSectionHeaderCell.swift */; };
D1D230FE225050CF00644DB7 /* AlamofireObjectMapper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1D230F9225050CF00644DB7 /* AlamofireObjectMapper.framework */; };
D1D230FF225050CF00644DB7 /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1D230FA225050CF00644DB7 /* Alamofire.framework */; };
D1D23100225050CF00644DB7 /* ObjectMapper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1D230FB225050CF00644DB7 /* ObjectMapper.framework */; };
D1D23101225050CF00644DB7 /* SVProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1D230FC225050CF00644DB7 /* SVProgressHUD.framework */; };
D1D23102225050CF00644DB7 /* DZNEmptyDataSet.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1D230FD225050CF00644DB7 /* DZNEmptyDataSet.framework */; };
D1D98EFB1DE9C789000A226E /* CityBikes_Networks_API_Response.json in Resources */ = {isa = PBXBuildFile; fileRef = D1D98EFA1DE9C789000A226E /* CityBikes_Networks_API_Response.json */; };
Expand Down Expand Up @@ -205,9 +202,6 @@
D1B6DD1E1C9EC3E3008543A3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D1B6DD251C9EC436008543A3 /* SnapshotHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SnapshotHelper.swift; path = fastlane/SnapshotHelper.swift; sourceTree = SOURCE_ROOT; };
D1C4D7691CEFC163001982F3 /* StationListSectionHeaderCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StationListSectionHeaderCell.swift; sourceTree = "<group>"; };
D1D230F9225050CF00644DB7 /* AlamofireObjectMapper.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AlamofireObjectMapper.framework; path = Carthage/Build/iOS/AlamofireObjectMapper.framework; sourceTree = "<group>"; };
D1D230FA225050CF00644DB7 /* Alamofire.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Alamofire.framework; path = Carthage/Build/iOS/Alamofire.framework; sourceTree = "<group>"; };
D1D230FB225050CF00644DB7 /* ObjectMapper.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjectMapper.framework; path = Carthage/Build/iOS/ObjectMapper.framework; sourceTree = "<group>"; };
D1D230FC225050CF00644DB7 /* SVProgressHUD.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SVProgressHUD.framework; path = Carthage/Build/iOS/SVProgressHUD.framework; sourceTree = "<group>"; };
D1D230FD225050CF00644DB7 /* DZNEmptyDataSet.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DZNEmptyDataSet.framework; path = Carthage/Build/iOS/DZNEmptyDataSet.framework; sourceTree = "<group>"; };
D1D98EFA1DE9C789000A226E /* CityBikes_Networks_API_Response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = CityBikes_Networks_API_Response.json; sourceTree = "<group>"; };
Expand All @@ -223,10 +217,7 @@
D1D23101225050CF00644DB7 /* SVProgressHUD.framework in Frameworks */,
C516F01B1B0DAA7C00CF4468 /* MapKit.framework in Frameworks */,
D1D23102225050CF00644DB7 /* DZNEmptyDataSet.framework in Frameworks */,
D1D230FE225050CF00644DB7 /* AlamofireObjectMapper.framework in Frameworks */,
D1D23100225050CF00644DB7 /* ObjectMapper.framework in Frameworks */,
D1DD8BC41DA327C400DED913 /* BikeBuddyKit.framework in Frameworks */,
D1D230FF225050CF00644DB7 /* Alamofire.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -461,10 +452,7 @@
E9678157F4E75F6588D67B18 /* Frameworks */ = {
isa = PBXGroup;
children = (
D1D230FA225050CF00644DB7 /* Alamofire.framework */,
D1D230F9225050CF00644DB7 /* AlamofireObjectMapper.framework */,
D1D230FD225050CF00644DB7 /* DZNEmptyDataSet.framework */,
D1D230FB225050CF00644DB7 /* ObjectMapper.framework */,
D1D230FC225050CF00644DB7 /* SVProgressHUD.framework */,
D149580B1DB317AE0038A615 /* CoreLocation.framework */,
D1F7B7FB1BACE2B600D6732C /* SystemConfiguration.framework */,
Expand Down Expand Up @@ -740,9 +728,6 @@
);
inputPaths = (
"$(SRCROOT)/Carthage/Build/iOS/SVProgressHUD.framework",
"$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework",
"$(SRCROOT)/Carthage/Build/iOS/AlamofireObjectMapper.framework",
"$(SRCROOT)/Carthage/Build/iOS/ObjectMapper.framework",
"$(SRCROOT)/Carthage/Build/iOS/DZNEmptyDataSet.framework",
);
name = "Copy in Carthage Frameworks";
Expand Down
28 changes: 21 additions & 7 deletions BikeBuddyKit/CityBikesNetworkDetailResponse.swift
Expand Up @@ -7,20 +7,34 @@
//
import Foundation
import ObjectMapper

/**
Represents a response from the CityBikes API call for details about a network.
:Implements: Mappable - Allows easy mapping from JSON to object via ObjectMapper
*/
public class CityBikesNetworkDetailResponse: Mappable {
public class CityBikesNetworkDetailResponse: Codable {

// MARK: - Variables
public var network: Network?

required public init?(map: Map) {
enum CodingKeys: String, CodingKey {
case network
}

// MARK: - Initalizers
required public init?() {
}

required public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
network = try values.decode(Network.self, forKey: .network)
}

public func mapping(map: Map) {
network <- map["network"]
// MARK: - Public Functions
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(network, forKey: .network)
}
}
28 changes: 21 additions & 7 deletions BikeBuddyKit/CityBikesNetworksResponse.swift
Expand Up @@ -7,20 +7,34 @@
//
import Foundation
import ObjectMapper

/**
Represents a response from the CityBikes API call for a list of available networks
:Implements: Mappable - Allows easy mapping from JSON to object via ObjectMapper
*/
public class CityBikesNetworksResponse: Mappable {
public class CityBikesNetworksResponse: Codable {

// MARK: - Variables
public var networks: [Network]?

required public init?(map: Map) {
enum CodingKeys: String, CodingKey {
case networks
}

// MARK: - Initalizers
required public init?() {
}

required public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
networks = try values.decode([Network].self, forKey: .networks)
}

public func mapping(map: Map) {
networks <- map["networks"]
// MARK: - Public Functions
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(networks, forKey: .networks)
}
}
2 changes: 1 addition & 1 deletion BikeBuddyKit/Constants.swift
Expand Up @@ -97,7 +97,7 @@ public struct Constants {

public struct CityBikes {
public static let BaseAPIURL = "https://api.citybik.es"
public static let NetworksAPI = CityBikes.BaseAPIURL + "/v2/networks"
public static let NetworksAPI = CityBikes.BaseAPIURL + "/v2/networks?fields=id,name,href,location"
}

// MARK: - Privacy Policy File
Expand Down
40 changes: 30 additions & 10 deletions BikeBuddyKit/Location.swift
Expand Up @@ -7,26 +7,46 @@
//
import Foundation
import ObjectMapper

/**
Represents a Location from the CityBikes API. This is usually found under a Network object.
:Implements: Mappable - Allows easy mapping from JSON to object via ObjectMapper
*/
public class Location: Mappable {
public class Location: Codable {

// MARK: - Variables
public var city: String?
public var country: String?
public var latitude: Double?
public var longitude: Double?

required public init?(map: Map) {
enum CodingKeys: String, CodingKey {
case city
case country
case latitude
case longitude
}

// MARK: - Initalizers
required public init?() {
}

required public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
city = try values.decode(String.self, forKey: .city)
country = try values.decode(String.self, forKey: .country)
latitude = try values.decode(Double.self, forKey: .latitude)
longitude = try values.decode(Double.self, forKey: .longitude)
}

public func mapping(map: Map) {
city <- map["city"]
country <- map["country"]
latitude <- map["latitude"]
longitude <- map["longitude"]
// MARK: - Public Functions
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(city, forKey: .city)
try container.encode(country, forKey: .country)
try container.encode(latitude, forKey: .latitude)
try container.encode(longitude, forKey: .longitude)
}
}
52 changes: 39 additions & 13 deletions BikeBuddyKit/Network.swift
Expand Up @@ -7,14 +7,14 @@
//
import Foundation
import ObjectMapper

/**
Represents a Network from the CityBikes API.
:Implements: Mappable - Allows easy mapping from JSON to object via ObjectMapper
*/
public class Network: Mappable {
public class Network: Codable {

// MARK: - Variables
public var company: [String]?
public var gbfsHref: String?
public var href: String?
Expand All @@ -23,16 +23,42 @@ public class Network: Mappable {
public var name: String?
public var stations: [Station]?

required public init?(map: Map) {
enum CodingKeys: String, CodingKey {
case company
case gbfsHref = "gbfs_href"
case href
case id
case location
case name
case stations
}

// MARK: - Initalizers
required public init?() {
}

public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

self.gbfsHref = try values.decodeIfPresent(String.self, forKey: .gbfsHref)
self.href = try values.decodeIfPresent(String.self, forKey: .href)
self.id = try values.decodeIfPresent(String.self, forKey: .id)
self.location = try values.decodeIfPresent(Location.self, forKey: .location)
self.name = try values.decodeIfPresent(String.self, forKey: .name)
self.stations = try values.decodeIfPresent([Station].self, forKey: .stations)
}

public func mapping(map: Map) {
company <- map["company"]
gbfsHref <- map["gbfs_href"]
href <- map["href"]
id <- map["id"]
location <- map["location"]
name <- map["name"]
stations <- map["stations"]
// MARK: - Public Functions
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(company, forKey: .company)
try container.encode(gbfsHref, forKey: .gbfsHref)
try container.encode(href, forKey: .href)
try container.encode(id, forKey: .id)
try container.encode(location, forKey: .location)
try container.encode(name, forKey: .name)
try container.encode(stations, forKey: .stations)
}
}
46 changes: 38 additions & 8 deletions BikeBuddyKit/NetworksDataService.swift
Expand Up @@ -7,8 +7,6 @@
//
import Foundation
import Alamofire
import AlamofireObjectMapper

public class NetworksDataService {

Expand All @@ -33,17 +31,49 @@ public class NetworksDataService {
- parameter apiUrl: The URL to the API to call. The API should return data in the format found in Supporting Files/CityBikes_Networks_API_Response.json
- parameter completionHandler: The closure to call when the response is received. Takes 2 parameters, responseObject as an Array of Network objects and an error as NSError
*/
*/
public func getAllNetworkData(apiUrl: String, completionHandler: @escaping (_ responseObject: [Network], _ error: NSError?) -> Void) {
var returnNetworks = [Network]()

AF.request(apiUrl, method: .get, parameters: nil)
.responseObject { (response: DataResponse<CityBikesNetworksResponse>) in
if let testResponseResult = response.result.value?.networks {
returnNetworks = testResponseResult
let session = URLSession.shared
if let url = URL(string: apiUrl) {
let task = session.dataTask(with: url, completionHandler: { data, response, error in

if error != nil || data == nil {
print("Client error!")
return
}

guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else {
print("Server error!")
return
}

completionHandler(returnNetworks, NSError(domain: Constants.NSErrorInfo.DomainString, code: Constants.NSErrorInfo.NetworkErrorCode))
guard let mime = response.mimeType, mime == "application/json" else {
print("Wrong MIME type!")
return
}

do {
if let safeData = data {
let decoder = JSONDecoder()
let model = try decoder.decode(CityBikesNetworksResponse.self, from: safeData)

if let testResponseResult = model.networks {
returnNetworks = testResponseResult
}

DispatchQueue.main.async {
completionHandler(returnNetworks, NSError(domain: Constants.NSErrorInfo.DomainString, code: Constants.NSErrorInfo.NetworkErrorCode))
}
}
} catch {
print("JSON error: \(error.localizedDescription)")
}
})

task.resume()
}

}
}

0 comments on commit 218e022

Please sign in to comment.