v1.3: smooth 30fps rotation (6° steps), guaranteed minimum one full spin per tap
This commit is contained in:
@@ -8,9 +8,11 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.SystemClock
|
||||
import androidx.work.*
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import me.khodak.claudeusage.data.PreferencesManager
|
||||
|
||||
class UsageUpdateWorker(
|
||||
@@ -31,6 +33,7 @@ class UsageUpdateWorker(
|
||||
prefs.saveUsageData(data)
|
||||
} catch (_: Exception) {}
|
||||
animJob.cancel()
|
||||
animJob.join() // wait for the minimum-rotation finally block to finish
|
||||
}
|
||||
|
||||
pushWidgetUpdate()
|
||||
@@ -40,10 +43,25 @@ class UsageUpdateWorker(
|
||||
private suspend fun rotateRefreshIcon() {
|
||||
val manager = AppWidgetManager.getInstance(context)
|
||||
val ids = manager.getAppWidgetIds(ComponentName(context, ClaudeUsageWidget::class.java))
|
||||
while (true) {
|
||||
ClaudeUsageWidget.currentRotation = (ClaudeUsageWidget.currentRotation + 30f) % 360f
|
||||
ids.forEach { id -> ClaudeUsageWidget.updateWidget(context, manager, id) }
|
||||
delay(83) // ~12 fps — one full rotation per second
|
||||
var totalDegrees = 0f
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
ClaudeUsageWidget.currentRotation = (ClaudeUsageWidget.currentRotation + 6f) % 360f
|
||||
totalDegrees += 6f
|
||||
ids.forEach { id -> ClaudeUsageWidget.updateWidget(context, manager, id) }
|
||||
delay(33) // 30 fps, one full rotation per second
|
||||
}
|
||||
} finally {
|
||||
// Even if the fetch finishes early, complete at least one full 360°
|
||||
withContext(NonCancellable) {
|
||||
while (totalDegrees < 360f) {
|
||||
ClaudeUsageWidget.currentRotation = (ClaudeUsageWidget.currentRotation + 6f) % 360f
|
||||
totalDegrees += 6f
|
||||
ids.forEach { id -> ClaudeUsageWidget.updateWidget(context, manager, id) }
|
||||
delay(33)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user