This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
/
MainViewController.swift
112 lines (94 loc) · 3.54 KB
/
MainViewController.swift
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// MainViewController.swift
// PulsePartner
//
// Created by yannik grotkop on 23.04.19.
// Copyright © 2019 PulsePartner. All rights reserved.
//
import UIKit
import CoreLocation
import Firebase
import CoreData
class MainViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var profilePicture: UIButton!
var allMatches = [MatchWithImage]()
override func viewDidLoad() {
super.viewDidLoad()
if let user = UserManager.sharedInstance.user {
updateImage(user: user)
}
UserManager.sharedInstance.addObserver(self)
self.tableView.delegate = self
self.tableView.dataSource = self
MatchManager.sharedInstance.addObserver(self)
let img = UIImage()
self.navigationController?.navigationBar.shadowImage = img
self.navigationController?.navigationBar.setBackgroundImage(img, for: UIBarMetrics.default)
// self.navigationController?.isNavigationBarHidden = true
LocationManager.sharedInstance.startUpdatingLocation()
}
@IBAction func onLogout(_ sender: Any) {
LocationManager.sharedInstance.stopUpdatingLocation()
UserManager.sharedInstance.logout()
}
func updateImage(user: FullUser) {
var placeholder = self.profilePicture.image(for: .normal)
if placeholder == nil {
placeholder = user.gender == "m" ?
UIImage(named: "PlaceholderImageMale") :
UIImage(named: "PlaceholderImageFemale")
}
self.profilePicture.kf.setImage(with: URL(string: user.image), for: .normal, placeholder: placeholder)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let indexPath = sender as? IndexPath else {
return
}
guard let destinationVC = segue.destination as? ChatViewController else {
return
}
destinationVC.user = self.allMatches[indexPath.row]
guard let cell = self.tableView.cellForRow(at: indexPath) as? MatchCell else {
return
}
destinationVC.messageCounter = cell.messageCounter
}
}
extension MainViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allMatches.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.rowHeight = 110
let cell = ( self.tableView.dequeueReusableCell(withIdentifier: "MatchCell", for: indexPath) as? MatchCell )!
let user = self.allMatches[indexPath.row]
cell.insertContent(match: user)
// cell.profilePicture.image = resizedImage
return cell
}
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
// Pass the indexPath as sender
// let user = self.allMatches[indexPath.row]
self.performSegue(withIdentifier: "ChatSegue", sender: indexPath)
}
}
extension MainViewController: UserObserver {
func userData(didUpdate user: FullUser?) {
guard let user = user else {
return
}
updateImage(user: user)
}
}
extension MainViewController: MatchObserver {
func matchData(didUpdate matches: [MatchWithImage]?) {
if let matches = matches {
self.allMatches = matches
self.tableView.reloadData()
}
}
}