Unverified Commit 8f9d6f0b authored by Zach Knox's avatar Zach Knox
Browse files

Offline viewing achieved!

Remember to get things from realm from the main thread
parent 8174e608
......@@ -34,7 +34,7 @@ class MainTableViewController: UITableViewController {
SRCTNetworkController.performDownload { (facilities) in
self.facilitiesArray = Array(facilities)
self.facilitiesArray = Array(facilities!)
self.tableView.reloadData()
}
}
......
......@@ -15,33 +15,40 @@ import ObjectMapper
class SRCTNetworkController: NSObject {
//Use this for testing with the new API, might make it possible to get stuff moving pre official release
//https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json
public static func performDownload(completion: @escaping (_ result: List<Facility>) -> Void) {
public static func performDownload(completion: @escaping (_ result: List<Facility>?) -> Void) {
let requestURL: NSURL = NSURL(string: "https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json")!
let requestURL: NSURL = NSURL(string: "https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
if(error != nil) {
completion(nil)
return
}
else {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
if let dataN = data {
if let json = try? JSONSerialization.jsonObject(with: dataN, options: []) as? [[String: Any]] {
// Map function to iterate through each JSON tree
let facilities = json!.map({ (json) -> Facility in
let facility = Facility()
let map = Map(mappingType: .fromJSON, JSON: json, toObject: true, context: facility, shouldIncludeNilValues: true)
facility.mapping(map: map)
return facility
})
// This is where completion is called
// Right after the array is done mapping all facility objects
completion(List(facilities))
}
}
}
}
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
if let dataN = data {
if let json = try? JSONSerialization.jsonObject(with: dataN, options: []) as? [[String: Any]] {
// Map function to iterate through each JSON tree
let facilities = json!.map({ (json) -> Facility in
let facility = Facility()
let map = Map(mappingType: .fromJSON, JSON: json, toObject: true, context: facility, shouldIncludeNilValues: true)
facility.mapping(map: map)
return facility
})
// This is where completion is called
// Right after the array is done mapping all facility objects
completion(List(facilities))
}
}
}
}
task.resume()
......
......@@ -239,6 +239,7 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
}
else {
facilitiesArray = facilities
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(lastUpdated)
}
}
else {
......@@ -252,33 +253,54 @@ class FacilitiesListViewController: UIViewController, UICollectionViewDelegate,
func update(_ sender: Any) {
SRCTNetworkController.performDownload { (facilities) in
self.facilitiesArray = facilities
DispatchQueue.main.async {
//let defaults = UserDefaults.standard
//defaults.set(facilities, forKey: "FacilitiesList")
let date = Date()
//defaults.set(date, forKey: "lastUpdatedList")
self.LocationsList.reloadData()
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
let model = FacilitiesModel()
model.facilities = facilities
model.lastUpdated = date
let results = self.realm.objects(FacilitiesModel.self)
if results.count == 0 {
try! self.realm.write {
self.realm.add(model)
if(facilities == nil) {
DispatchQueue.main.async {
let results = self.realm.objects(FacilitiesModel.self)
if results.count > 0 {
let model = results[0]
let facilitiesFromDB = model.facilities
let lastUpdated = model.lastUpdated
self.facilitiesArray = facilitiesFromDB
self.LocationsList.reloadData()
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(lastUpdated)
}
else {
self.facilitiesArray = List<Facility>()
}
}
else {
let fromRealm = results[0]
try! self.realm.write {
fromRealm.facilities = model.facilities
fromRealm.lastUpdated = model.lastUpdated
}
else {
self.facilitiesArray = facilities!
DispatchQueue.main.async {
//let defaults = UserDefaults.standard
//defaults.set(facilities, forKey: "FacilitiesList")
let date = Date()
//defaults.set(date, forKey: "lastUpdatedList")
self.LocationsList.reloadData()
self.LastUpdatedLabel.title = "Updated: " + self.shortDateFormat(date)
let model = FacilitiesModel()
model.facilities = facilities!
model.lastUpdated = date
let results = self.realm.objects(FacilitiesModel.self)
if results.count == 0 {
try! self.realm.write {
self.realm.add(model)
}
}
else {
let fromRealm = results[0]
try! self.realm.write {
fromRealm.facilities = model.facilities
fromRealm.lastUpdated = model.lastUpdated
}
}
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment