-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathAnkiStatsTaskHandler.kt
169 lines (163 loc) · 7.45 KB
/
AnkiStatsTaskHandler.kt
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************************************************
* Copyright (c) 2014 Michael Goldbach <michael@m-goldbach.net> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
package com.ichi2.anki.stats
import android.R
import android.view.View
import android.webkit.WebView
import android.widget.ProgressBar
import android.widget.TextView
import com.ichi2.libanki.Collection
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.stats.Stats
import com.ichi2.libanki.stats.Stats.AxisType
import com.ichi2.libanki.stats.Stats.ChartType
import com.ichi2.themes.Themes.getColorFromAttr
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.net.URLEncoder
import kotlin.math.roundToInt
class AnkiStatsTaskHandler private constructor(
private val collectionData: Collection,
private val mainDispatcher: CoroutineDispatcher,
private val defaultDispatcher: CoroutineDispatcher
) {
var standardTextSize = 10f
var statType = AxisType.TYPE_MONTH
private var mDeckId: DeckId = 0
fun setDeckId(deckId: DeckId) {
mDeckId = deckId
}
suspend fun createChart(
chartType: ChartType,
progressBar: ProgressBar,
chartView: ChartView,
) = withContext(defaultDispatcher) {
mutex.withLock {
val plotSheet = if (!this.isActive) {
Timber.d("Quitting CreateChartTask (%s) before execution", chartType.name)
null
} else {
Timber.d("Starting CreateChartTask, type: %s", chartType.name)
val chartBuilder = ChartBuilder(
chartView, collectionData,
mDeckId, chartType
)
chartBuilder.renderChart(statType)
}
plotSheet?.let {
withContext(mainDispatcher) {
chartView.setData(plotSheet)
progressBar.visibility = View.GONE
chartView.visibility = View.VISIBLE
chartView.invalidate()
}
}
}
}
suspend fun createStatisticsOverview(webView: WebView, progressBar: ProgressBar) =
withContext(defaultDispatcher) {
mutex.withLock {
val html = if (!isActive) {
Timber.d("Quitting CreateStatisticsOverview before execution")
null
} else {
Timber.d("Starting CreateStatisticsOverview")
val overviewStatsBuilder =
OverviewStatsBuilder(webView, collectionData, mDeckId, statType)
overviewStatsBuilder.createInfoHtmlString()
}
html?.let {
withContext(mainDispatcher) {
runCatching {
webView.loadData(
URLEncoder.encode(html, "UTF-8").replace("\\+".toRegex(), " "),
"text/html; charset=utf-8",
"utf-8"
)
}.getOrElse {
Timber.w(it)
}
progressBar.visibility = View.GONE
val backgroundColor = getColorFromAttr(webView.context, R.attr.colorBackground)
webView.setBackgroundColor(backgroundColor)
webView.visibility = View.VISIBLE
webView.invalidate()
}
}
}
}
companion object {
var instance: AnkiStatsTaskHandler? = null
private set
private val mutex = Mutex()
@Synchronized
fun getInstance(
collection: Collection,
mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
): AnkiStatsTaskHandler {
if (instance == null || instance!!.collectionData !== collection) {
instance = AnkiStatsTaskHandler(collection, mainDispatcher, defaultDispatcher)
}
return instance!!
}
suspend fun createReviewSummaryStatistics(
col: Collection,
view: TextView,
mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
defaultDispatcher: CoroutineDispatcher = Dispatchers.IO
): Unit = mutex.withLock {
withContext(defaultDispatcher) {
val todayStatString = if (!isActive || col.dbClosed) {
Timber.d("Quitting DeckPreviewStatistics before execution")
null
} else {
Timber.d("Starting DeckPreviewStatistics")
// eventually put this in Stats (in desktop it is not though)
var cards: Int
var minutes: Int
/* cards, excludes rescheduled cards https://github.com/ankidroid/Anki-Android/issues/8592 */
val query = "select sum(case when ease > 0 then 1 else 0 end), " +
"sum(time)/1000 from revlog where id > " + (col.sched.dayCutoff - Stats.SECONDS_PER_DAY) * 1000
Timber.d("DeckPreviewStatistics query: %s", query)
col.db
.query(query).use { cur ->
cur.moveToFirst()
cards = cur.getInt(0)
minutes = (cur.getInt(1) / 60.0).roundToInt()
}
val res = view.resources
val span = res.getQuantityString(com.ichi2.anki.R.plurals.in_minutes, minutes, minutes)
res.getQuantityString(com.ichi2.anki.R.plurals.studied_cards_today, cards, cards, span)
}
todayStatString?.let {
withContext(mainDispatcher) {
view.apply {
text = it
visibility = View.VISIBLE
invalidate()
}
}
}
}
}
}
}