Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
380e4ca916 | ||
|
|
47b1197a92 | ||
|
|
c1563b8172 | ||
|
|
4a00d8adff | ||
|
|
944d2e6634 | ||
|
|
d7de09a152 | ||
|
|
e21a134d65 | ||
|
|
b9baf61264 | ||
|
|
7b63ee4377 | ||
|
|
d892427a3f | ||
|
|
628813c3f6 | ||
|
|
4f202f8c10 | ||
|
|
44ebe68f75 | ||
|
|
5de466792a | ||
|
|
b6e6bd328c | ||
|
|
5132ae66b9 | ||
|
|
5151665216 | ||
|
|
96461bd7cf |
@@ -1,16 +1,45 @@
|
|||||||
package com.example.research.core.domain.model
|
package com.example.research.core.domain.model
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Snapshot of an ongoing indexing operation.
|
* Snapshot of an ongoing indexing operation.
|
||||||
*
|
*
|
||||||
* [progress] is the authoritative [0f, 1f] completion value: the repository
|
* [progress] is the authoritative [0f, 1f] completion value and is O(1) to
|
||||||
* aggregates a file-size-weighted sum across files incrementally while
|
* compute: the repository's internal `ProgressTracker` maintains a running
|
||||||
* indexing, so reading it is O(1).
|
* sum across files incrementally and stores it in [aggregateSum], avoiding
|
||||||
|
* an O(n) walk over [perFileProgress] on every read.
|
||||||
|
* [perFileProgress] is kept for diagnostics; callers should prefer
|
||||||
|
* [progress] / [progressPercent].
|
||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
data class IndexingProgress(
|
data class IndexingProgress(
|
||||||
|
val currentFile: String = "",
|
||||||
|
val currentIndex: Int = 0,
|
||||||
|
val totalFiles: Int = 0,
|
||||||
val isIndexing: Boolean = false,
|
val isIndexing: Boolean = false,
|
||||||
val progress: Float = 0f,
|
val currentFileProgress: Float = 0f,
|
||||||
)
|
val label: String = "",
|
||||||
|
val perFileProgress: Map<String, Float> = emptyMap(),
|
||||||
|
/** Pre-aggregated sum in [0f, totalFiles] supplied by the producer; -1f = unknown. */
|
||||||
|
val aggregateSum: Float = -1f,
|
||||||
|
) {
|
||||||
|
val progress: Float
|
||||||
|
get() = if (totalFiles > 0) {
|
||||||
|
when {
|
||||||
|
aggregateSum >= 0f -> (aggregateSum / totalFiles).coerceIn(0f, 1f)
|
||||||
|
perFileProgress.isNotEmpty() -> {
|
||||||
|
val totalProgress = perFileProgress.values.sum()
|
||||||
|
(totalProgress / totalFiles).coerceIn(0f, 1f)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val completedFiles = (currentIndex - 1).coerceAtLeast(0)
|
||||||
|
((completedFiles + currentFileProgress) / totalFiles).coerceIn(0f, 1f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else 0f
|
||||||
|
|
||||||
|
val progressPercent: Int
|
||||||
|
get() = (progress * 100).roundToInt().coerceIn(0, 100)
|
||||||
|
}
|
||||||
|
|||||||
+30
-24
@@ -70,9 +70,13 @@ private class ProgressTracker(files: List<LocalDictionaryRepository.DiscoveredFi
|
|||||||
aggregateWeightedMicros.addAndGet(newMicros - prevMicros)
|
aggregateWeightedMicros.addAndGet(newMicros - prevMicros)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun progress(): Float {
|
/**
|
||||||
|
* Current weighted sum mapped back to [0f, totalFiles] so the existing
|
||||||
|
* IndexingProgress model can keep deriving progress as aggregate/total.
|
||||||
|
*/
|
||||||
|
fun aggregateSum(): Float {
|
||||||
val weightedProgress = aggregateWeightedMicros.get().toDouble() / (totalWeight.toDouble() * 1_000_000.0)
|
val weightedProgress = aggregateWeightedMicros.get().toDouble() / (totalWeight.toDouble() * 1_000_000.0)
|
||||||
return weightedProgress.coerceIn(0.0, 1.0).toFloat()
|
return (weightedProgress.coerceIn(0.0, 1.0) * totalFiles).toFloat()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reset() {
|
fun reset() {
|
||||||
@@ -188,59 +192,61 @@ class LocalDictionaryRepository(
|
|||||||
mutableIndexingProgress.emit(
|
mutableIndexingProgress.emit(
|
||||||
IndexingProgress(
|
IndexingProgress(
|
||||||
isIndexing = true,
|
isIndexing = true,
|
||||||
progress = progressTracker?.progress() ?: 0f,
|
currentFile = context.getString(R.string.indexing_label),
|
||||||
|
totalFiles = filesToScan.size,
|
||||||
|
currentIndex = filesToScan.size - filesToIndex.size,
|
||||||
|
aggregateSum = progressTracker?.aggregateSum() ?: -1f,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
val lastEmittedPercent = AtomicInteger(-1)
|
val lastEmittedPercent = AtomicInteger(-1)
|
||||||
val lastProgressEmitMs = AtomicLong(0L)
|
val lastProgressEmitMs = AtomicLong(0L)
|
||||||
|
|
||||||
val publishCurrentProgress = {
|
|
||||||
val tracker = progressTracker
|
|
||||||
if (tracker != null) {
|
|
||||||
val progress = tracker.progress()
|
|
||||||
lastEmittedPercent.set((progress * 100f).toInt().coerceIn(0, 100))
|
|
||||||
lastProgressEmitMs.set(SystemClock.elapsedRealtime())
|
|
||||||
mutableIndexingProgress.update { p ->
|
|
||||||
p.copy(progress = progress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
supervisorScope {
|
supervisorScope {
|
||||||
filesToIndex.map { file ->
|
filesToIndex.mapIndexed { index, file ->
|
||||||
async {
|
async {
|
||||||
indexingSemaphore.withPermit {
|
indexingSemaphore.withPermit {
|
||||||
coroutineContext.ensureActive()
|
coroutineContext.ensureActive()
|
||||||
yield()
|
yield()
|
||||||
|
|
||||||
val res = indexDictionary(file) { _, _, prog ->
|
val res = indexDictionary(file) { _, op, prog ->
|
||||||
if (prog >= 0f) {
|
if (prog >= 0f) {
|
||||||
progressTracker?.updateFileProgress(file.name, prog)
|
progressTracker?.updateFileProgress(file.name, prog)
|
||||||
}
|
}
|
||||||
|
|
||||||
val progress = progressTracker?.progress() ?: 0f
|
val tracker = progressTracker
|
||||||
val newPercent = (progress * 100f).toInt().coerceIn(0, 100)
|
val aggregate = tracker?.aggregateSum() ?: -1f
|
||||||
|
val newPercent = if (filesToScan.isNotEmpty() && aggregate >= 0f) {
|
||||||
|
((aggregate / filesToScan.size) * 100f).toInt().coerceIn(0, 100)
|
||||||
|
} else -1
|
||||||
|
|
||||||
|
val currentIdx = filesToScan.size - filesToIndex.size + index + 1
|
||||||
|
val previous = mutableIndexingProgress.value
|
||||||
val now = SystemClock.elapsedRealtime()
|
val now = SystemClock.elapsedRealtime()
|
||||||
val percentChanged = newPercent != lastEmittedPercent.get()
|
val percentChanged = newPercent >= 0 && newPercent != lastEmittedPercent.get()
|
||||||
|
val fileChanged = previous.currentFile != file.name
|
||||||
val completed = newPercent >= 100 || prog >= 1f
|
val completed = newPercent >= 100 || prog >= 1f
|
||||||
val intervalElapsed =
|
val intervalElapsed =
|
||||||
now - lastProgressEmitMs.get() >= MIN_PROGRESS_EMIT_INTERVAL_MS
|
now - lastProgressEmitMs.get() >= MIN_PROGRESS_EMIT_INTERVAL_MS
|
||||||
val shouldEmit = completed || (percentChanged && intervalElapsed)
|
val shouldEmit = fileChanged || completed || (percentChanged && intervalElapsed)
|
||||||
|
|
||||||
if (shouldEmit) {
|
if (shouldEmit) {
|
||||||
lastEmittedPercent.set(newPercent)
|
if (newPercent >= 0) lastEmittedPercent.set(newPercent)
|
||||||
lastProgressEmitMs.set(now)
|
lastProgressEmitMs.set(now)
|
||||||
mutableIndexingProgress.update { p ->
|
mutableIndexingProgress.update { p ->
|
||||||
p.copy(progress = progress)
|
p.copy(
|
||||||
|
currentFile = file.name,
|
||||||
|
currentIndex = currentIdx,
|
||||||
|
currentFileProgress = prog,
|
||||||
|
label = op.ifEmpty { p.label },
|
||||||
|
aggregateSum = aggregate,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res is OperationResult.Success) {
|
if (res is OperationResult.Success) {
|
||||||
progressTracker?.updateFileProgress(file.name, 1.0f)
|
progressTracker?.updateFileProgress(file.name, 1.0f)
|
||||||
publishCurrentProgress()
|
|
||||||
totalArticlesIndexed.addAndGet(res.data.articleCount)
|
totalArticlesIndexed.addAndGet(res.data.articleCount)
|
||||||
val dictionary = res.data
|
val dictionary = res.data
|
||||||
addDictionary(dictionary)
|
addDictionary(dictionary)
|
||||||
|
|||||||
+7
-21
@@ -1,8 +1,8 @@
|
|||||||
package com.example.research.ui.settings.components
|
package com.example.research.ui.settings.components
|
||||||
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.core.Animatable
|
import androidx.compose.animation.core.FastOutSlowInEasing
|
||||||
import androidx.compose.animation.core.LinearEasing
|
import androidx.compose.animation.core.animateFloatAsState
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.core.tween
|
||||||
import androidx.compose.animation.expandVertically
|
import androidx.compose.animation.expandVertically
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.animation.fadeIn
|
||||||
@@ -19,10 +19,6 @@ import androidx.compose.ui.res.stringResource
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.example.research.R
|
import com.example.research.R
|
||||||
|
|
||||||
private const val MILLIS_PER_PROGRESS_UNIT = 10_000
|
|
||||||
private const val MIN_PROGRESS_MOTION_MS = 50
|
|
||||||
private const val MAX_PROGRESS_MOTION_MS = 650
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DictionaryProgressSection(
|
fun DictionaryProgressSection(
|
||||||
visible: Boolean,
|
visible: Boolean,
|
||||||
@@ -62,21 +58,11 @@ fun DictionaryProgressSection(
|
|||||||
displayedTestTag = testTag
|
displayedTestTag = testTag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val animatedState = remember { Animatable(0f) }
|
val animatedProgress by animateFloatAsState(
|
||||||
LaunchedEffect(monotonicTargetProgress) {
|
targetValue = monotonicTargetProgress,
|
||||||
val delta = monotonicTargetProgress - animatedState.value
|
animationSpec = tween(durationMillis = 650, easing = FastOutSlowInEasing),
|
||||||
if (delta > 0f) {
|
label = "dictionary-progress"
|
||||||
animatedState.animateTo(
|
)
|
||||||
targetValue = monotonicTargetProgress,
|
|
||||||
animationSpec = tween(
|
|
||||||
durationMillis = (delta * MILLIS_PER_PROGRESS_UNIT).toInt()
|
|
||||||
.coerceIn(MIN_PROGRESS_MOTION_MS, MAX_PROGRESS_MOTION_MS),
|
|
||||||
easing = LinearEasing,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val animatedProgress = animatedState.value
|
|
||||||
|
|
||||||
Column(modifier = Modifier.testTag(displayedTestTag)) {
|
Column(modifier = Modifier.testTag(displayedTestTag)) {
|
||||||
Row(
|
Row(
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<string name="language_english">Английский</string>
|
<string name="language_english">Английский</string>
|
||||||
<string name="language_russian">Русский</string>
|
<string name="language_russian">Русский</string>
|
||||||
<string name="language_system">Системный</string>
|
<string name="language_system">Системный</string>
|
||||||
|
<string name="indexing_label">Индексация словарей</string>
|
||||||
<string name="clear_search">Очистить поиск</string>
|
<string name="clear_search">Очистить поиск</string>
|
||||||
<string name="article_no_selected">Статья не выбрана</string>
|
<string name="article_no_selected">Статья не выбрана</string>
|
||||||
<string name="article_return_to_search">Вернуться к поиску</string>
|
<string name="article_return_to_search">Вернуться к поиску</string>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<string name="language_english">English</string>
|
<string name="language_english">English</string>
|
||||||
<string name="language_russian">Russian</string>
|
<string name="language_russian">Russian</string>
|
||||||
<string name="language_system">System</string>
|
<string name="language_system">System</string>
|
||||||
|
<string name="indexing_label">Indexing dictionaries</string>
|
||||||
<string name="clear_search">Clear search</string>
|
<string name="clear_search">Clear search</string>
|
||||||
<string name="article_no_selected">No article selected</string>
|
<string name="article_no_selected">No article selected</string>
|
||||||
<string name="article_return_to_search">Return to search</string>
|
<string name="article_return_to_search">Return to search</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user