func getDataFromFireStore() {
print("getting data")
let activityView = activityIndicatorView()
activityView.startAnimating()
let db = Firestore.firestore()
db.collection("Groups").getDocuments() { (querySnapshot, error) in
if error != nil {
print("Error getting documents: \(error!)")
} else {
self.mentalHealthArray.removeAll()
self.circumstanceArray.removeAll()
self.identityArray.removeAll()
for document in querySnapshot!.documents {
print("for loop")
let group = JoinGroupModel()
let data = document.data()
group.name = data["name"] as! String
group.description = data["description"] as! String
group.image_url = data["image_url"] as! String
let groupType = data["type"] as! String
print("Group Type = \(groupType)")
if groupType == "Mental Health" {
self.mentalHealthArray.append(group)
} else if groupType == "Circumstance" {
self.circumstanceArray.append(group)
} else {
self.identityArray.append(group)
}
}
self.mentalHealthCollectionView.reloadData()
self.circumstanceCollectionView.reloadData()
self.identityCollectionView.reloadData()
activityView.stopAnimating()
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 1 {
let cell = mentalHealthCollectionView.dequeueReusableCell(withReuseIdentifier: "MentalHealthCollectionViewCell", for: indexPath) as! MentalHealthCollectionViewCell
cell.groupNameLabel.text = mentalHealthArray[indexPath.row].name
let imageUrl = mentalHealthArray[indexPath.row].image_url
cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
cell.cellButton.tag = indexPath.row
cell.cellButton.addTarget(self, action: #selector(metalHealthCellPressed(sender:)), for: UIControl.Event.touchUpInside)
return cell
} else if collectionView.tag == 2 {
let cell = circumstanceCollectionView.dequeueReusableCell(withReuseIdentifier: "CircumstancesCollectionViewCell", for: indexPath) as! CircumstancesCollectionViewCell
cell.groupNameLabel.text = circumstanceArray[indexPath.row].name
let imageUrl = circumstanceArray[indexPath.row].image_url
cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
cell.cellButton.tag = indexPath.row
cell.cellButton.addTarget(self, action: #selector(circumstanceCellPressed(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
else {
let cell = identityCollectionView.dequeueReusableCell(withReuseIdentifier: "IdentityCollectionViewCell", for: indexPath) as! IdentityCollectionViewCell
cell.groupNameLabel.text = identityArray[indexPath.row].name
let imageUrl = identityArray[indexPath.row].image_url
cell.groupImageView.sd_setImage(with: URL(string: imageUrl.replacingOccurrences(of: " ", with: "%20")))
cell.cellButton.tag = indexPath.row
cell.cellButton.addTarget(self, action: #selector(identityCellPressed(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
}