-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionStore.swift
More file actions
96 lines (67 loc) · 2.63 KB
/
QuestionStore.swift
File metadata and controls
96 lines (67 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// QuestionStore.swift
// Kapıştır
//
// Created by Evren Yortuçboylu on 13/03/16.
// Copyright © 2016 Evren Yortuçboylu. All rights reserved.
//
import Foundation
struct QuestionStore{
typealias Batch = [Question]
typealias Callback = (Batch)-> Void
private static var currentQuestion: Question?
static var currentQuestionIndex = 0
static var questionCount: Int {
return self._questions.count
}
private static var _questions = [Question]()
private static var _callbacks = [Callback]()
private static func publishUpdate(){
for block in self._callbacks {
block(self._questions)
}
}
static func registerUpdateCallback(block: Callback) {
self._callbacks.append(block)
}
static var hasNextQuestion: Bool {
return _questions.count > currentQuestionIndex+1
}
static func getBatch() {
// sorular indirilecek
Api.getBatch({ () -> Void in
// error
},
successCallback: {(questions) -> Void in
print("\(questions.count) questions fetched")
let questionsWillAdd = questions.filter({ q -> Bool in
return self._questions.indexOf({ question -> Bool in
return question.id == q.id
}) == nil
})
self._questions = self._questions + questionsWillAdd
print("\(questionsWillAdd.count) questions added")
self.publishUpdate()
})
}
static func setCurrentQuestion(question: Question) {
self.currentQuestion = question
self.currentQuestionIndex = self._questions.indexOf { q -> Bool in
return q.id == question.id
}!
print("index \(self.currentQuestionIndex), question count: \(self._questions.count)")
if self._questions.count - self.currentQuestionIndex < 7 && self._questions.count > 7 {
print("get next batch")
self.getBatch()
}
}
static func insertCurrentQuestion(question: Question){
// to be fixed.
// if kapistir count = 0 and user tries to create kapistir
// kapistir count must be checked
let questionIndex = QuestionStore.questionCount > 0 ?
self.currentQuestionIndex + 1 : 0
self._questions.insert(question, atIndex: questionIndex)
self.publishUpdate()
}
}