Skip to content

Commit 7032c82

Browse files
committed
Compatible with old version
1 parent 910063a commit 7032c82

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

yotpoKit/MyQuestion.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ open class MyQuestion: RequestYotpo {
1717
public typealias CompletionDefault = (_ code: Int, _ msg: String) -> Void
1818
public typealias CompletionQuestion = (_ code: Int, _ title: String, _ msg: String) -> Void
1919
public typealias CompleteNextPageQuestions = (_ msg: String, _ newQuestions: [Question], _ productQuestio: ProductQuestion) -> Void
20+
public typealias CompletionWithQuestionsAndExhibition = (_ code: Int, _ msg: String, _ productQuestion: ProductQuestion, _ questionsExhibition: [QuestionExhibition]) -> Void
2021

2122
open func getQuestions(productId: String, completion: @escaping CompletionWithProductQuestions) {
2223
let endPoint = Endpoint.MyQuestion().getQuestions(productId: productId, appKey: appKey)
@@ -184,6 +185,43 @@ open class MyQuestion: RequestYotpo {
184185
completion("There aren't questions to download", [], currentQuestion)
185186
}
186187
}
188+
189+
open func getNextQuestionsWithExhibition(productQuestion: ProductQuestion, oldQuestionsExhibition: [QuestionExhibition],
190+
productId: String, completion: @escaping CompletionWithQuestionsAndExhibition) {
191+
let myOldProductQuestion = productQuestion.questions.filter { (question) -> Bool in
192+
return question.id != 0
193+
}
194+
var newProductQuestion = productQuestion
195+
196+
if productQuestion.totalQuestions > myOldProductQuestion.count {
197+
let currentPage = myOldProductQuestion.count/5
198+
var questionsExhibition = [QuestionExhibition]()
199+
getQuestionPerPage(productId: productId, page: currentPage+1, completion: { (code, msg, result) in
200+
newProductQuestion.questions += result
201+
202+
for question in result {
203+
let questExhibition = QuestionExhibition(withQuestion: question)
204+
205+
let isContain = oldQuestionsExhibition.contains(where: { (questionEx) -> Bool in
206+
return questionEx.idQuestion == questExhibition.idQuestion &&
207+
questionEx.idAnswer == questExhibition.idAnswer &&
208+
questionEx.typeQuestion == questExhibition.typeQuestion
209+
})
210+
if !isContain {
211+
questionsExhibition.append(questExhibition)
212+
for answer in question.answers {
213+
let answerExhibition = QuestionExhibition(withAnswer: answer, idQuest: question.id)
214+
questionsExhibition.append(answerExhibition)
215+
}
216+
}
217+
}
218+
219+
completion(code, msg, newProductQuestion, questionsExhibition)
220+
})
221+
} else {
222+
completion(2, "There aren't questions to download", newProductQuestion, [])
223+
}
224+
}
187225

188226
open func convertProductQuestionToUnicArray(productQuestion: ProductQuestion) -> [QuestionExhibition] {
189227
var questionsExhibition = [QuestionExhibition]()

0 commit comments

Comments
 (0)