-
Notifications
You must be signed in to change notification settings - Fork 54
/
ReviewItem.swift
107 lines (94 loc) · 3.35 KB
/
ReviewItem.swift
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
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2022 David Sansome
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import WaniKaniAPI
enum TaskType {
case reading
case meaning
static func random() -> TaskType {
arc4random_uniform(2) == 0 ? .reading : .meaning
}
}
class ReviewItem: NSObject {
private class func filterReadyItems(assignments: [TKMAssignment],
localCachingClient: LocalCachingClient,
_ isIncluded: (TKMAssignment) -> Bool) -> [ReviewItem] {
var ret = [ReviewItem]()
guard let userInfo = localCachingClient.getUserInfo() else {
return ret
}
for assignment in assignments {
if !localCachingClient.isValid(subjectId: assignment.subjectID) ||
!isIncluded(assignment) {
continue
}
ret.append(ReviewItem(assignment: assignment))
}
return ret
}
class func readyForReview(assignments: [TKMAssignment],
localCachingClient: LocalCachingClient) -> [ReviewItem] {
filterReadyItems(assignments: assignments,
localCachingClient: localCachingClient) { (assignment) -> Bool in
assignment.isReviewStage && assignment.availableAtDate.timeIntervalSinceNow < 0
}
}
class func readyForLessons(assignments: [TKMAssignment],
localCachingClient: LocalCachingClient) -> [ReviewItem] {
filterReadyItems(assignments: assignments,
localCachingClient: localCachingClient) { (assignment) -> Bool in
assignment.isLessonStage
}
}
let assignment: TKMAssignment
var answeredReading = false
var answeredMeaning = false
var answer = TKMProgress()
init(assignment: TKMAssignment) {
self.assignment = assignment
answer.assignment = assignment
answer.isLesson = assignment.isLessonStage
}
private func getSubjectTypeIndex(_ subjectType: TKMSubject.TypeEnum) -> Int {
for (idx, typeValue) in Settings.lessonOrder.enumerated() {
if typeValue == subjectType {
return idx
}
}
return 0
}
func compareForLessons(other: ReviewItem) -> Bool {
if assignment.level < other.assignment.level {
return Settings.prioritizeCurrentLevel ? false : true
} else if assignment.level > other.assignment.level {
return Settings.prioritizeCurrentLevel ? true : false
}
let myIndex = getSubjectTypeIndex(assignment.subjectType)
let otherIndex = getSubjectTypeIndex(other.assignment.subjectType)
if myIndex < otherIndex {
return true
} else if myIndex > otherIndex {
return false
}
return assignment.subjectID <= other.assignment.subjectID
}
func reset() {
answer.clearMeaningWrong()
answer.clearReadingWrong()
answer.clearMeaningWrongCount()
answer.clearReadingWrongCount()
answeredMeaning = false
answeredReading = false
}
}