-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccountViewModel.swift
139 lines (131 loc) · 5.53 KB
/
AccountViewModel.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// AccountViewModel.swift
// Unsplah_Clone
//
// Created by JungpyoHong on 5/9/21.
//
import Firebase
import UIKit
protocol AccountViewModelDelegate: AnyObject {
func reload()
func show(error: AppError)
}
class AccountViewModel {
let db = Firestore.firestore()
private var photoDataSource = [UserPhotoData]() {
didSet {
self.delegate?.reload()
}
}
private var likeDataSource = [UserLikeData]() {
didSet {
self.delegate?.reload()
}
}
private var collectionDataSource = [UserCollectionData]() {
didSet {
self.delegate?.reload()
}
}
weak var delegate: AccountViewModelDelegate?
init(delegate: AccountViewModelDelegate) {
self.delegate = delegate
}
func logIn(emailTextField: UITextField, passwordTextField: UITextField, viewController: UIViewController) {
if let email = emailTextField.text, let password = passwordTextField.text {
emailTextField.text = ""
passwordTextField.text = ""
Auth.auth().signIn(withEmail: email, password: password) { _, error in
if let error = error {
print(error)
let alert = ReusableComponent.alertMessage(title: "Login Error", message: "Password or ID is invalid try again")
viewController.present(alert, animated: true, completion: nil)
} else {
viewController.performSegue(withIdentifier: "CompletedLogIn", sender: nil)
}
}
}
}
func numberOfPhotoLow() -> Int {
photoDataSource.count
}
func numberOfLikeRow() -> Int {
likeDataSource.count
}
func numberOfCollectionRow() -> Int {
collectionDataSource.count
}
func configureLikeCell(in tableView: UITableView, for indexPath: IndexPath) -> UITableViewCell {
guard let cell: AccountTableViewCell = tableView.dequeueReusableCell(withIdentifier: "AccountTableViewCell") as? AccountTableViewCell
else { fatalError("not find") }
let photo = likeDataSource[indexPath.row]
cell.configure(author: photo.author, photoImage: photo.url)
return cell
}
func configureCollectionCell(in tableView: UITableView, for indexPath: IndexPath) -> UITableViewCell {
guard let cell: AccountTableViewCollectionCell = tableView.dequeueReusableCell(withIdentifier: "AccountTableViewCollectionCell") as? AccountTableViewCollectionCell
else { fatalError("not find") }
let photo = collectionDataSource[indexPath.row]
cell.configure(photoImage: photo.photoUrl, author: photo.author)
return cell
}
func getUserName(nameLabel: UILabel) {
// guard let currentUser = Auth.auth().currentUser?.uid else { return }
db.collection("users").addSnapshotListener { querySnapshot, error in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let firstName = data["firstName"] as? String, let lastName = data["lastName"] as? String, let userID = data["uid"] as? String {
if userID == Auth.auth().currentUser?.uid {
DispatchQueue.main.async {
nameLabel.text = "\(firstName) \(lastName)"
}
}
}
}
}
}
}
}
func fetchLikeFromServer() {
db.collection("photoUrl").addSnapshotListener { querySnapshot, error in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let author = data["author"] as? String, let url = data["url"] as? String, let userID = data["uid"] as? String {
if userID == Auth.auth().currentUser?.uid {
self.likeDataSource.append(UserLikeData(author: author, url: url))
self.likeDataSource = self.likeDataSource.removingDuplicates()
}
}
}
}
}
}
}
func fetchCollectionFromServer() {
db.collection("collectionUrl").addSnapshotListener { querySnapshot, error in
if let error = error {
print(error.localizedDescription)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let author = data["author"] as? String, let url = data["url"] as? String, let userID = data["uid"] as? String {
if userID == Auth.auth().currentUser?.uid {
self.collectionDataSource.append(UserCollectionData(photoUrl: url, author: author))
self.collectionDataSource = self.collectionDataSource.removingDuplicates()
}
}
}
}
}
}
}
}