Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import Foundation
  2. import CoreLocation
  3.  
  4. class Location: Codable {
  5. static let dateFormatter: DateFormatter = {
  6. let formatter = DateFormatter()
  7. formatter.dateStyle = .medium
  8. formatter.timeStyle = .medium
  9. return formatter
  10. }()
  11.  
  12. var coordinates: CLLocationCoordinate2D {
  13. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  14. }
  15.  
  16. let latitude: Double
  17. let longitude: Double
  18. let date: Date
  19. let dateString: String
  20. let description: String
  21.  
  22. init(_ location: CLLocationCoordinate2D, date: Date, descriptionString: String) {
  23. latitude = location.latitude
  24. longitude = location.longitude
  25. self.date = date
  26. dateString = Location.dateFormatter.string(from: date)
  27. description = descriptionString
  28. }
  29.  
  30. convenience init(visit: CLVisit, descriptionString: String) {
  31. self.init(visit.coordinate, date: visit.arrivalDate, descriptionString: descriptionString)
  32. }
  33. }
  34.  
  35. import Foundation
  36. import CoreLocation
  37.  
  38. class LocationsStorage {
  39. static let shared = LocationsStorage()
  40.  
  41. var locations: [Location]
  42. private let fileManager: FileManager
  43. private let documentsURL: URL
  44.  
  45. init() {
  46. let fileManager = FileManager.default
  47. documentsURL = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
  48. self.fileManager = fileManager
  49.  
  50. let jsonDecoder = JSONDecoder()
  51.  
  52. let locationFilesURLs = try! fileManager.contentsOfDirectory(at: documentsURL,
  53. includingPropertiesForKeys: nil)
  54. locations = locationFilesURLs.compactMap { url -> Location? in
  55. guard !url.absoluteString.contains(".DS_Store") else {
  56. return nil
  57. }
  58. guard let data = try? Data(contentsOf: url) else {
  59. return nil
  60. }
  61. return try? jsonDecoder.decode(Location.self, from: data)
  62. }.sorted(by: { $0.date < $1.date })
  63. }
  64.  
  65. func saveLocationOnDisk(_ location: Location) {
  66. let encoder = JSONEncoder()
  67. let timestamp = location.date.timeIntervalSinceNow
  68. let fileURL = documentsURL.appendingPathComponent("(timestamp)")
  69.  
  70. let data = try! encoder.encode(location)
  71. try! data.write(to: fileURL)
  72.  
  73. locations.append(location)
  74.  
  75. NotificationCenter.default.post(name: .newLocationSaved, object: self, userInfo: ["location": location])
  76. }
  77.  
  78. func saveCLLocationToDisk(_ clLocation: CLLocation) {
  79. let currentDate = Date()
  80. AppDelegate.geoCoder.reverseGeocodeLocation(clLocation) { placemarks, _ in
  81. if let place = placemarks?.first {
  82. let location = Location(clLocation.coordinate, date: currentDate, descriptionString: "(place)")
  83. self.saveLocationOnDisk(location)
  84. }
  85. }
  86. }
  87. }
  88.  
  89. extension Notification.Name {
  90. static let newLocationSaved = Notification.Name("newLocationSaved")
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement