Created
December 28, 2015 20:20
-
-
Save alonecuzzo/f8ca685547cba25dba78 to your computer and use it in GitHub Desktop.
so lost
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
// | |
// StopService.swift | |
// MetroBurb | |
// | |
// Created by Jabari Bell on 10/26/15. | |
// Copyright © 2015 Code Mitten. All rights reserved. | |
// | |
import Foundation | |
import RxSwift | |
import SQLite | |
//1. closest stop service really, since that's all it's getting for us - doesn't matter where it comes from | |
//2. the db stop service can handle all interfacing with the stops, we'll still need to convert them into stops, but this should be returning a stop model objcect | |
//3. or do we have a stop service that allows us to do what we want to the stops? | |
//what are things that we want to do with the stops | |
//get stop by id | |
//get stop by location | |
//get stop by name | |
struct Location { | |
let latitude: Double | |
let longitude: Double | |
} | |
//there's a trip | |
struct Trip { | |
let tripID: String | |
let tripHeadSign: String //"Penn Station", "Bablyon" etc | |
let directionID: Int //1 means going out, 0 means coming in | |
} | |
//1. can we get all of the trips for a stop | |
//2. can we then get all of the trips at a stop with a departure time within an hour | |
//3. then out of those just show the arrival times w/ the matching stopid | |
//we want to select a stop, and see the trips that terminate at the other stop | |
//going to be async and possibly failable - that's why we have observables instead of just returning values | |
protocol StopServiceProtocol { | |
func getStop(id: Int) -> Observable<Stop> | |
func getStop(location: Location) -> Observable<Stop> | |
func getStop(name: String) -> Observable<Stop> | |
} | |
//should we make this configd w/ a line yes | |
class SQLiteStopService: StopServiceProtocol { | |
//MARK: Property | |
let line: Line | |
var db: Connection! | |
let stops = Table("stops") | |
//MARK: Method | |
init(line: Line) { | |
self.line = line | |
setupDB() | |
} | |
func setupDB() -> Void { | |
let filePath = NSBundle.mainBundle().pathForResource("gtfs", ofType: "db", inDirectory: line.localDBDirectory) | |
do { | |
db = try Connection(filePath!) | |
} catch { | |
print("couldnt connect to db") | |
return //some error or something | |
} | |
} | |
func getStop(id: Int) -> Observable<Stop> { | |
//should these be responsible for transformations? | |
//really we just return Stop.decode() -> so we need something that can decode these | |
// let stop | |
//select * from stop_times where departure_time (is within 5 minutes) AND stop_id == 1 | |
let stopTimes = Table("stop_times") | |
let alias = stopTimes.alias("myAlias") | |
let tripID = Expression<String>("trip_id") | |
let stopID = Expression<String>("stop_id") | |
// let departureTime = Expression<NSDate>("departure_time") | |
let startStopAlias = stopTimes.alias("startStopAlias") | |
let endStopAlias = stopTimes.alias("endStopAlias") | |
let startStopQuery = startStopAlias | |
.filter(startStopAlias[stopID] == String(id)) | |
let endStopQuery = endStopAlias | |
.filter(endStopAlias[stopID] == "25") | |
// let tripIDAlias = endStopQuery.alias("stop_times") | |
let query = startStopQuery.select(startStopQuery[tripID]).join(endStopAlias, on: endStopAlias[tripID] == startStopQuery[tripID]) | |
//great neck = 25 | |
print("startStopQuery[tripID]: \(startStopQuery[stopID])") | |
print("endStopQuery[tripID]: \(endStopQuery[stopID])") | |
print("tripID: \(tripID)") | |
print("startStopQuery[tripID]: \(startStopQuery[tripID])") | |
print("endStopQuery[tripID]: \(endStopQuery[tripID])") | |
print("startStopAlias[tripID]: \(startStopAlias[tripID])") | |
print("endStopAlias[tripID]: \(endStopAlias[tripID])") | |
print("alias[tripID]: \(alias[tripID])") | |
print("query \(query.asSQL())") | |
//that's the starting query, then we need to check from these trips, which end in the station that we want | |
// .filter(NSDate(timeIntervalSinceNow: departureTime) < (60 * 60)) //1 hour | |
return create { [weak self] observer in | |
// let idToCompare = Expression<Int64>(value: Int64(id)) | |
// let stopQuery = self?.stops.filter(queryId == 1) | |
// let s = self?.stops.filter(queryId == String(id)) | |
// for stop in (self?.db.prepare((self?.stops)!))! { | |
// print("stop: \(stop[queryId])") | |
// } | |
for stop in (self?.db.prepare(query))! { | |
print("id: \(stop)") | |
} | |
return NopDisposable.instance | |
} | |
} | |
func getStop(location: Location) -> Observable<Stop> { | |
return create { observer in | |
return NopDisposable.instance | |
} | |
} | |
func getStop(name: String) -> Observable<Stop> { | |
return create { observer in | |
return NopDisposable.instance | |
} | |
} | |
} | |
//protocol ClosestStopService { | |
// func closestStop | |
//} | |
/** | |
* Base protocol for Stop retrieval services. | |
*/ | |
protocol StopService { | |
/// Line type | |
var line: Line { get } | |
/** | |
Requests list of stops for a particular line. | |
- returns: An observable signal. | |
*/ | |
func requestForStopsStringSignal() -> Observable<String> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment