func fetchGlimpseData() {
if lastDocument == nil {
GLIMPSE_ALL_USERS_DATA.order(by: TIMESTAMP, descending: true).limit(to: 3)
.getDocuments { [self] (snapshot, error) in
guard let last = snapshot?.documents.last else { return }
for dictionary in snapshot!.documents {
let name = dictionary[FIRST_NAME] as? String ?? ""
let age = dictionary[AGE] as? String ?? ""
let city = dictionary[CURRENT_CITY] as? String ?? ""
let profileImageURL = dictionary[PROFILE_IMAGE_URL] as? String ?? ""
let glimpseCaption = dictionary[GLIMPSE_CAPTION] as? String ?? ""
let glimpseURL = dictionary[GLIMPSE_IMAGE_URL] as? String ?? ""
let timestamp = dictionary[TIMESTAMP] as? Double ?? 0.0
let documentID = dictionary.documentID
let glimpseInfo = Glimpse(name: name, age: age, city: city, profileImageURL: profileImageURL, glimpseCaption: glimpseCaption, glimpseURL: glimpseURL, timestamp: Date(timeIntervalSince1970: timestamp), documentID: documentID)
self.glimpse.append(glimpseInfo)
self.glimpseTableView.reloadData()
}
self.lastDocument = last
}
} else {
glimpseTableView.tableFooterView = createSpinnerFooter()
GLIMPSE_ALL_USERS_DATA.order(by: TIMESTAMP, descending: true).start(afterDocument: lastDocument!).limit(to: 3)
.getDocuments { [self] (snapshot, error ) in
DispatchQueue.main.async {
self.glimpseTableView.tableFooterView = nil
}
guard let last = snapshot?.documents.last else { return }
for dictionary in snapshot!.documents {
let name = dictionary[FIRST_NAME] as? String ?? ""
let age = dictionary[AGE] as? String ?? ""
let city = dictionary[CURRENT_CITY] as? String ?? ""
let profileImageURL = dictionary[PROFILE_IMAGE_URL] as? String ?? ""
let glimpseCaption = dictionary[GLIMPSE_CAPTION] as? String ?? ""
let glimpseURL = dictionary[GLIMPSE_IMAGE_URL] as? String ?? ""
let timestamp = dictionary[TIMESTAMP] as? Double ?? 0.0
let documentID = dictionary.documentID
let glimpseInfo = Glimpse(name: name, age: age, city: city, profileImageURL: profileImageURL, glimpseCaption: glimpseCaption, glimpseURL: glimpseURL, timestamp: Date(timeIntervalSince1970: timestamp), documentID: documentID)
self.glimpse.append(glimpseInfo)
self.glimpseTableView.reloadData()
}
self.lastDocument = last
}}
}
//This is what i use to delete the post
func optionsMenu(sendUser: String) {
let alert = UIAlertController(title: "MyApp", message: "Options", preferredStyle: .actionSheet)
alert.view.tintColor = .brick
let deleteGlimpse = UIAlertAction(title: "Delete Post", style: .default) { (action) in
GLIMPSE_USER_COLLECTION.delete() { err in
if let err = err {
print("COULD NOT REMOVE GLIMPSE...\(err.localizedDescription)")
} else {
print("IMG HAS BEEN DELETED")
}}
GLIMPSE_USER_STORAGE.delete()
self.handleRefresh()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(deleteGlimpse)
alert.addAction(cancelAction)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.backgroundColor = .green
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
self.present(alert, animated: true, completion: nil)
}