Skip to content

Commit

Permalink
Merge pull request #4 from amarofashion/Compatible-with-old-version
Browse files Browse the repository at this point in the history
Compatible with old version
  • Loading branch information
lilianeblima authored Dec 11, 2017
2 parents 78a7cee + 7032c82 commit 8a09dea
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions yotpoKit/MyQuestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ open class MyQuestion: RequestYotpo {
public typealias CompletionDefault = (_ code: Int, _ msg: String) -> Void
public typealias CompletionQuestion = (_ code: Int, _ title: String, _ msg: String) -> Void
public typealias CompleteNextPageQuestions = (_ msg: String, _ newQuestions: [Question], _ productQuestio: ProductQuestion) -> Void
public typealias CompletionWithQuestionsAndExhibition = (_ code: Int, _ msg: String, _ productQuestion: ProductQuestion, _ questionsExhibition: [QuestionExhibition]) -> Void

open func getQuestions(productId: String, completion: @escaping CompletionWithProductQuestions) {
let endPoint = Endpoint.MyQuestion().getQuestions(productId: productId, appKey: appKey)
Expand Down Expand Up @@ -184,6 +185,43 @@ open class MyQuestion: RequestYotpo {
completion("There aren't questions to download", [], currentQuestion)
}
}

open func getNextQuestionsWithExhibition(productQuestion: ProductQuestion, oldQuestionsExhibition: [QuestionExhibition],
productId: String, completion: @escaping CompletionWithQuestionsAndExhibition) {
let myOldProductQuestion = productQuestion.questions.filter { (question) -> Bool in
return question.id != 0
}
var newProductQuestion = productQuestion

if productQuestion.totalQuestions > myOldProductQuestion.count {
let currentPage = myOldProductQuestion.count/5
var questionsExhibition = [QuestionExhibition]()
getQuestionPerPage(productId: productId, page: currentPage+1, completion: { (code, msg, result) in
newProductQuestion.questions += result

for question in result {
let questExhibition = QuestionExhibition(withQuestion: question)

let isContain = oldQuestionsExhibition.contains(where: { (questionEx) -> Bool in
return questionEx.idQuestion == questExhibition.idQuestion &&
questionEx.idAnswer == questExhibition.idAnswer &&
questionEx.typeQuestion == questExhibition.typeQuestion
})
if !isContain {
questionsExhibition.append(questExhibition)
for answer in question.answers {
let answerExhibition = QuestionExhibition(withAnswer: answer, idQuest: question.id)
questionsExhibition.append(answerExhibition)
}
}
}

completion(code, msg, newProductQuestion, questionsExhibition)
})
} else {
completion(2, "There aren't questions to download", newProductQuestion, [])
}
}

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

0 comments on commit 8a09dea

Please sign in to comment.