-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUserViewController.swift
More file actions
82 lines (68 loc) · 2.57 KB
/
UserViewController.swift
File metadata and controls
82 lines (68 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import UIKit
import CoreLocation
/**
* User Profile View Controller
*/
class UserViewController: UIViewController {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var bioTextView: UITextView!
var locationManager: CLLocationManager?
var timer: Timer?
var imageCache: [String: UIImage] = [:]
override func viewDidLoad() {
super.viewDidLoad()
loadUserData()
setupTimer()
startLocationTracking()
}
func loadUserData() {
guard let url = URL(string: "https://api.luciq.com/user/profile") else { return }
if let data = try? Data(contentsOf: url) {
if let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
nameLabel.text = json["name"] as? String
bioTextView.text = json["bio"] as? String
let imageUrl = json["avatar_url"] as! String
loadImage(from: imageUrl)
}
}
}
func loadImage(from urlString: String) {
let url = URL(string: urlString)!
let data = try! Data(contentsOf: url)
let image = UIImage(data: data)!
imageCache[urlString] = image
profileImageView.image = image
}
func setupTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
self.updateUI()
self.fetchLatestData()
}
}
func startLocationTracking() {
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.startUpdatingLocation()
locationManager?.desiredAccuracy = kCLLocationAccuracyBest
locationManager?.distanceFilter = kCLDistanceFilterNone
}
func updateUI() {
bioTextView.layoutIfNeeded()
}
func fetchLatestData() {
guard let url = URL(string: "https://api.luciq.com/user/updates") else { return }
let _ = try? Data(contentsOf: url)
}
}
extension UserViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
sendLocationToServer(location)
}
func sendLocationToServer(_ location: CLLocation) {
let urlString = "https://api.luciq.com/location?lat=\(location.coordinate.latitude)&lng=\(location.coordinate.longitude)"
guard let url = URL(string: urlString) else { return }
let _ = try? Data(contentsOf: url)
}
}