Code Block | var countDadJokes = Int() |
| var countAssistantJokes = Int() |
| var countKnockKnockJokes = Int() |
| var countRandomJokes = Int() |
| func getDadJokes() { |
| let collectionRef = self.db.collection("jokes") |
| let documentRef = collectionRef.document("Dad Jokes") |
| documentRef.getDocument(completion: { documentSnapshot, error in |
| if let error = error { |
| print(error.localizedDescription) |
| return |
| } |
|
| countDadJokes = (documentSnapshot?.data()!.count)! + 1 |
| //print("count = \(count)") |
| }) |
| db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in |
| |
| //check for error |
| if error == nil { |
| //check if document exists |
| if document != nil && document!.exists { |
| |
| if let Joke = document!.get("\(Int.random(in: 0...countDadJokes))") as? String { |
| self.DadJokes = Joke |
| print("Joke: \(self.DadJokes)") |
| } |
| } |
| } |
| } |
| } |
| func getAssistantJokes() { |
| if countAssistantJokes == 0 { |
| let collectionRef = self.db.collection("jokes") |
| let documentRef = collectionRef.document("Assistant Jokes") |
| documentRef.getDocument(completion: { documentSnapshot, error in |
| if let error = error { |
| print(error.localizedDescription) |
| return |
| } |
|
| countAssistantJokes = (documentSnapshot?.data()!.count)! + 1 |
| //print("count = \(count)") |
| }) |
| db.collection("jokes").document("Assistant Jokes").addSnapshotListener { document, error in |
| |
| //check for error |
| if error == nil { |
| //check if document exists |
| if document != nil && document!.exists { |
| |
| if let Joke = document!.get("\(Int.random(in: 0...countAssistantJokes))") as? String { |
| self.AssistantJokes = Joke |
| print("Joke: \(self.AssistantJokes)") |
| } |
| } |
| } |
| } |
| } |
| } |
| func getKnockKnockJokes() { |
| if countDadJokes == 0 { |
| let collectionRef = self.db.collection("jokes") |
| let documentRef = collectionRef.document("Knock Knock Jokes") |
| documentRef.getDocument(completion: { documentSnapshot, error in |
| if let error = error { |
| print(error.localizedDescription) |
| return |
| } |
|
| countKnockKnockJokes = (documentSnapshot?.data()!.count)! + 1 |
| //print("count = \(count)") |
| }) |
| db.collection("jokes").document("Knock Knock Jokes").addSnapshotListener { document, error in |
| |
| //check for error |
| if error == nil { |
| //check if document exists |
| if document != nil && document!.exists { |
| |
| if let Joke = document!.get("\(Int.random(in: 0...countKnockKnockJokes))") as? String { |
| self.KnockKnockJokes = Joke |
| print("Joke: \(self.KnockKnockJokes)") |
| } |
| } |
| } |
| } |
| } |
| } |
| func getRandomJokes() { |
| if countDadJokes == 0 { |
| let collectionRef = self.db.collection("jokes") |
| let documentRef = collectionRef.document("Random Jokes") |
| documentRef.getDocument(completion: { documentSnapshot, error in |
| if let error = error { |
| print(error.localizedDescription) |
| return |
| } |
|
| countRandomJokes = (documentSnapshot?.data()!.count)! + 1 |
| //print("count = \(count)") |
| }) |
| db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in |
| |
| //check for error |
| if error == nil { |
| //check if document exists |
| if document != nil && document!.exists { |
| |
| if let Joke = document!.get("\(Int.random(in: 0...countRandomJokes))") as? String { |
| self.RandomJokes = Joke |
| print("Joke: \(self.RandomJokes)") |
| } |
| } |
| } |
| } |
| } |
| } |
| let pickedCat = Int.random(in: 1...4) |
| //print("picked cat: \(pickedCat)") |
| if pickedCat == 1 { |
| getRandomJokes() |
| randomJoke.text = "Random Joke: \(self.DadJokes)" |
| //print("Random Joke: \(self.DadJokes)") |
| }else if pickedCat == 2 { |
| getAssistantJokes() |
| randomJoke.text = "Random Joke: \(self.AssistantJokes)" |
| //print("Random Joke: \(self.AssistantJokes)") |
| }else if pickedCat == 3 { |
| getKnockKnockJokes() |
| randomJoke.text = "Random Joke: \(self.KnockKnockJokes)" |
| //print("Random Joke: \(self.KnockKnockJokes)") |
| }else if pickedCat == 4 { |
| getRandomJokes() |
| randomJoke.text = "Random Joke: \(self.RandomJokes)" |
| //print("Random Joke: \(self.RandomJokes)") |
| } |
| if randomJoke.text == "Random Joke: " { |
| randomJoke.text = "Random Joke: Failed to connect to server" |
| } |
| } |
|