Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
whats-open-ios
Commits
8f9d6f0b
Unverified
Commit
8f9d6f0b
authored
Dec 14, 2017
by
Zach Knox
Browse files
Offline viewing achieved!
Remember to get things from realm from the main thread
parent
8174e608
Changes
3
Hide whitespace changes
Inline
Side-by-side
WhatsOpen/WhatsOpen/MainTableViewController.swift
View file @
8f9d6f0b
...
...
@@ -34,7 +34,7 @@ class MainTableViewController: UITableViewController {
SRCTNetworkController
.
performDownload
{
(
facilities
)
in
self
.
facilitiesArray
=
Array
(
facilities
)
self
.
facilitiesArray
=
Array
(
facilities
!
)
self
.
tableView
.
reloadData
()
}
}
...
...
WhatsOpen/WhatsOpen/SRCTNetworkController.swift
View file @
8f9d6f0b
...
...
@@ -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
()
...
...
WhatsOpen/WhatsOpen/Views/FacilitiesListViewController.swift
View file @
8f9d6f0b
...
...
@@ -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
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment