Skip to content

Commit

Permalink
Dependencies removed
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfej94 committed Jan 7, 2022
1 parent 06c484f commit 551e6d0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 114 deletions.
4 changes: 1 addition & 3 deletions AtlasKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "AtlasKit"
spec.version = "0.1.7"
spec.version = "0.1.8"
spec.license = "MIT"
spec.summary = "A swift library for quickly integrating a location search in your app."
spec.homepage = "https://github.com/appoly/AtlasKit"
Expand All @@ -13,8 +13,6 @@ Pod::Spec.new do |spec|
spec.framework = "CoreLocation"
spec.framework = "MapKit"
spec.framework = "Contacts"

spec.dependency 'Alamofire', '~> 4.9.0'

spec.swift_versions = ["5.0", "5.1"]

Expand Down
16 changes: 0 additions & 16 deletions Package.resolved

This file was deleted.

3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ let package = Package(
name: "AtlasKit",
targets: ["AtlasKit"]),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
Expand Down
145 changes: 69 additions & 76 deletions Sources/AtlasKit/AtlasKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import Foundation
import MapKit
import Contacts
import Alamofire



Expand All @@ -20,7 +19,6 @@ public class AtlasKit {
// MARK: - Variables

private let datasource: AtlasKitDataSource
private let sessionManager = SessionManager()
private var searchTimer: Timer?
private var search: MKLocalSearch?

Expand Down Expand Up @@ -54,10 +52,6 @@ public class AtlasKit {
/// - term: Search term
/// - completion: Code to be ran when a result is received (Places, Error)
public func performSearch(_ term: String, completion: @escaping ([AtlasKitPlace]?, AtlasKitError?) -> Void) {
guard isNetworkAvailable() else {
completion(nil, .networkUnavailable)
return
}
switch datasource {
case .apple:
performMapKitSearch(term, completion: completion)
Expand Down Expand Up @@ -103,85 +97,89 @@ public class AtlasKit {


private func performGooglePlacesSearch(_ term: String, completion: @escaping ([AtlasKitPlace]?, AtlasKitError?) -> Void) {
let parameters = [
"key": datasource.apiKey!,
"inputtype": "textquery",
"locationbias": "ipbias",
"fields": "formatted_address,name,geometry",
"input": term
]
guard let apiKey = datasource.apiKey else {
completion(nil, .apiKey)
return
}

let request = sessionManager.request(URL(string: "https://maps.googleapis.com/maps/api/place/findplacefromtext/json")!,
method: .get,
parameters: parameters,
encoding: URLEncoding.methodDependent,
headers: nil)
let api: AtlasKitAPI = .googleSearch(term: term, apiKey: apiKey)
var request = URLRequest(url: api.url)
request.httpMethod = api.method

request.validate(statusCode: 200..<300).responseJSON { [weak self] (response) in
switch response.result {
case .failure(_):
completion(nil, .generic)
case .success(let value):
guard let json = value as? [String: Any] else {
completion(nil, .generic)
return
}

guard let data = json["candidates"] as? [[String: Any]] else {
completion(nil, .generic)
return
}

completion(self?.formatResults(data), nil)
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode) else {
completion(nil, .generic)
return
}

guard error == nil, let data = data else {
completion(nil, .generic)
return
}

guard let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
completion(nil, .generic)
return
}

guard let data = json["candidates"] as? [[String: Any]] else {
completion(nil, .generic)
return
}

completion(self?.formatResults(data), nil)
}
}


private func performGetAddressSearch(_ postcode: String, completion: @escaping ([AtlasKitPlace]?, AtlasKitError?) -> Void) {
let parameters = [
"api-key": datasource.apiKey!
]
guard let apiKey = datasource.apiKey else {
completion(nil, .apiKey)
return
}

guard let searchTerm = postcode.trimmingCharacters(in: .whitespacesAndNewlines).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed), let url = URL(string: "https://api.getaddress.io/find/\(searchTerm)") else {
guard let searchTerm = postcode.trimmingCharacters(in: .whitespacesAndNewlines).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
completion(nil, .generic)
return
}

let request = sessionManager.request(url,
method: .get,
parameters: parameters,
encoding: URLEncoding.methodDependent,
headers: nil)
let api: AtlasKitAPI = .getAddressSearch(term: searchTerm, apiKey: apiKey)
var request = URLRequest(url: api.url)
request.httpMethod = api.method

request.validate(statusCode: 200..<300).responseJSON { [weak self] (response) in
switch response.result {
case .failure(_):
completion(nil, .generic)
case .success(let value):
guard let json = value as? [String: Any] else {
completion(nil, .generic)
return
}

guard let data = json["addresses"] as? [String] else {
completion(nil, .generic)
return
}

guard let latitude = json["latitude"] as? Double else {
completion(nil, .generic)
return
}

guard let longitude = json["longitude"] as? Double else {
completion(nil, .generic)
return
}

completion(self?.formatResults(data, postcode: postcode.uppercased().removingAllWhitespaces, latitude: latitude, longitude: longitude), nil)
URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode) else {
completion(nil, .generic)
return
}
}

guard error == nil, let data = data else {
completion(nil, .generic)
return
}

guard let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
completion(nil, .generic)
return
}

guard let data = json["addresses"] as? [String] else {
completion(nil, .generic)
return
}

guard let latitude = json["latitude"] as? Double else {
completion(nil, .generic)
return
}

guard let longitude = json["longitude"] as? Double else {
completion(nil, .generic)
return
}

completion(self?.formatResults(data, postcode: postcode.uppercased().removingAllWhitespaces, latitude: latitude, longitude: longitude), nil)
}.resume()
}


Expand Down Expand Up @@ -229,16 +227,11 @@ public class AtlasKit {
}

return AtlasKitPlace(streetAddress: GoogleHelper.extractStreetAddress(from: address), city: GoogleHelper.extractCity(from: address), postcode: GoogleHelper.extractPostcode(from: address), state: GoogleHelper.extractState(from: address), country: GoogleHelper.extractCountry(from: address), location: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
}.filter({ $0 != nil && ($0!.formattedAddress.first) != nil }) as! [AtlasKitPlace]
}.filter({ $0 != nil && ($0!.formattedAddress.first) != nil }).map { $0! }

return addresses.sorted(by: { $0.formattedAddress.localizedStandardCompare($1.formattedAddress) == .orderedAscending })
}


private func isNetworkAvailable() -> Bool {
return NetworkReachabilityManager()!.isReachable
}

}


Expand Down
41 changes: 41 additions & 0 deletions Sources/AtlasKit/AtlasKitAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// AtlasKitAPI.swift
//
//
// Created by James Wolfe on 07/01/2022.
//



import Foundation



enum AtlasKitAPI {

// MARK: - Cases

case googleSearch(term: String, apiKey: String)
case getAddressSearch(term: String, apiKey: String)



// MARK: - Variables

var url: URL {
switch self {
case .googleSearch(let term, let apiKey):
return URL(string: "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?term=\(term)&inputtype=textquery&locationbias=ipbias&fields=formatted_address,name,geometry&key=\(apiKey)")!
case .getAddressSearch(let term, let apiKey):
return URL(string: "https://api.getaddress.io/find/\(term)?api-key=\(apiKey)")!
}
}

var method: String {
return "GET"
}

}



7 changes: 5 additions & 2 deletions Sources/AtlasKit/AtlasKitError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Foundation
public enum AtlasKitError: Error {
case generic
case networkUnavailable
case apiKey
}


Expand All @@ -23,8 +24,10 @@ extension AtlasKitError {
switch self {
case .generic:
return "Failed to lookup address"
case .networkUnavailable:
return "Please check your network connection and try again"
case .networkUnavailable:
return "Please check your network connection and try again"
case .apiKey:
return "API key is invalid"
}
}
}
16 changes: 2 additions & 14 deletions Tests/AtlasKitTests/AtlasKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@


import XCTest
@testable import PhotographyKit
@testable import AtlasKit



final class PhotographyKitTests: XCTestCase {

func testResetCamera() {
}


func testTakePhoto() {
}

static var allTests = [
("testResetCamera", testResetCamera),
("testTakePhoto", testTakePhoto),
]
final class AtlasKitTests: XCTestCase {
}

0 comments on commit 551e6d0

Please sign in to comment.