Skip to content

Commit

Permalink
Merge pull request #26 from Roslund/LocationLogging
Browse files Browse the repository at this point in the history
Network implementation
  • Loading branch information
Roslund authored Nov 26, 2019
2 parents 147c1cb + b3c6a73 commit 5fa0220
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Sequor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ADA166EE238430E700C53998 /* ActivityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADA166ED238430E700C53998 /* ActivityView.swift */; };
ADBEB00923743CB9001534CD /* DashboardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADBEB00823743CB9001534CD /* DashboardView.swift */; };
ADBEB00D2374423D001534CD /* PurchaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADBEB00C2374423D001534CD /* PurchaseView.swift */; };
ADC2A22B238714B10096F1ED /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADC2A22A238714B10096F1ED /* Network.swift */; };
ADD8B71D237B0C9F00D8DD55 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADD8B71C237B0C9F00D8DD55 /* AppState.swift */; };
ADD8B71F237B0DDD00D8DD55 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADD8B71E237B0DDD00D8DD55 /* Profile.swift */; };
ADD8B723237B4ECF00D8DD55 /* RelativeTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADD8B722237B4ECF00D8DD55 /* RelativeTime.swift */; };
Expand Down Expand Up @@ -73,6 +74,7 @@
ADBEB00823743CB9001534CD /* DashboardView.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = DashboardView.swift; sourceTree = "<group>"; tabWidth = 2; };
ADBEB00A23743E12001534CD /* CouponsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CouponsView.swift; sourceTree = "<group>"; };
ADBEB00C2374423D001534CD /* PurchaseView.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = PurchaseView.swift; sourceTree = "<group>"; tabWidth = 2; };
ADC2A22A238714B10096F1ED /* Network.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
ADD8B71C237B0C9F00D8DD55 /* AppState.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; tabWidth = 2; };
ADD8B71E237B0DDD00D8DD55 /* Profile.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = "<group>"; tabWidth = 2; };
ADD8B722237B4ECF00D8DD55 /* RelativeTime.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = RelativeTime.swift; sourceTree = "<group>"; tabWidth = 2; };
Expand Down Expand Up @@ -151,6 +153,7 @@
AD1F064A236761C800F74D9A /* Views */,
AD1F0619236760CC00F74D9A /* AppDelegate.swift */,
AD1F061B236760CC00F74D9A /* SceneDelegate.swift */,
ADC2A22A238714B10096F1ED /* Network.swift */,
AD1F061F236760CD00F74D9A /* Assets.xcassets */,
AD1F0624236760CD00F74D9A /* LaunchScreen.storyboard */,
AD1F0627236760CD00F74D9A /* Info.plist */,
Expand Down Expand Up @@ -395,6 +398,7 @@
files = (
ADECEB142375AAD70097B726 /* Ticket.swift in Sources */,
ADD8B723237B4ECF00D8DD55 /* RelativeTime.swift in Sources */,
ADC2A22B238714B10096F1ED /* Network.swift in Sources */,
ADD8B71D237B0C9F00D8DD55 /* AppState.swift in Sources */,
ADBEB00D2374423D001534CD /* PurchaseView.swift in Sources */,
AD1F061A236760CC00F74D9A /* AppDelegate.swift in Sources */,
Expand Down
9 changes: 5 additions & 4 deletions Sequor/Models/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ final class AppState: ObservableObject {
/// Valid coupons the user has erned.
@Published var coupons: [Coupon] = []

@Published var lastTrip: Trip?

var locationLogger: LocationLogger?

init() {
Expand All @@ -34,11 +32,14 @@ final class AppState: ObservableObject {
locationLogger = LocationLogger(trip: Trip())
}

/// Sends a request to the server to invalidate the ticket but changing experation date.
// Should send a request to the server to invalidate the ticket.
/// Currently invalidates the ticket client side and send the trip to the server.
func invalidateTicket() {
// Temp for testing. Should make request to server.
activeTicket = nil
lastTrip = locationLogger?.end()
if let trip = locationLogger?.end() {
HTTP.post(asJSON: trip, to: URL(string: "http://10.3.10.102:8000/trip")!)
}
}

}
1 change: 1 addition & 0 deletions Sequor/Models/LocationLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class LocationLogger: NSObject, ObservableObject, CLLocationManagerDelegat

func end() -> Trip {
locationManager.stopUpdatingLocation()
trip.endDate = Date()
return trip
}
}
81 changes: 81 additions & 0 deletions Sequor/Network.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Foundation

enum HTTP {
static func get(url: URL,
headders: [AnyHashable: Any]? = nil,
completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpAdditionalHeaders = headders

let defaultSession = URLSession(configuration: configuration)
let dataTask = defaultSession.dataTask(with: url, completionHandler: completionHandler)
dataTask.resume()
}

static func get(url: URL,
headders: [AnyHashable: Any]? = nil,
completionHandler: @escaping (Data) -> Void) {
get(url: url) { data, response, _ in
if let data = data,
let response = response as? HTTPURLResponse,
response.statusCode == 200 {
completionHandler(data)
}
}
}

static func downloadTask(url: URL,
headders: [AnyHashable: Any]? = nil,
completionHandler: @escaping (Data) -> Void) {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpAdditionalHeaders = headders
configuration.shouldUseExtendedBackgroundIdleMode = true

let defaultSession = URLSession(configuration: configuration)
var dataTask: URLSessionDownloadTask

dataTask = defaultSession
.downloadTask(with: url) { url, response, _ in
if let url = url,
let response = response as? HTTPURLResponse,
response.statusCode == 200 {
guard let data = try? Data(contentsOf: url) else {
return
}

completionHandler(data)
}
}
dataTask.resume()
}

static func post(data body: Data, to url: URL) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = body
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let task = URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()
}

static func post(asJSON data: Trip, to url: URL) {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .secondsSince1970
// swiftlint:disable:next force_try
let json = try! encoder.encode(data)
print(String(data: json, encoding: .utf8)!)
post(data: json, to: url)

[1,2].firstIndex(of: 4)
}
}

0 comments on commit 5fa0220

Please sign in to comment.