Playback: - Persist the playhead in the playback snapshot and resume from it; restored longform progress is no longer overwritten with 0 on first play. - Previous compares elapsed playtime since the track start, so resuming at 1:00:00 and pressing previous goes back instead of restarting the book. - Auto-advance no longer overwrites completed=true with false, longform PUTs are serialized, and playing rows/search results resume saved positions. - Pause cancels the 400 ms poll; seek errors are reported instead of crashing. Sync and state: - First-sync snapshots keep the first page revision; cached revisions are only trusted for the same server/user. - Refresh re-reads /me, merges local and server longform progress by timestamp, replays newer local positions, and keeps cached longform after deltas. - Logout cancels refresh jobs, suppresses late progress writes, clears after playback teardown, and closes the HTTP client on release. - Failed/racing mood opens clear the previous mood; stale responses cannot replace a newer selection. Mood drafts survive background refreshes. Downloads and storage: - Stream downloads to a .part file and atomically rename on Android/desktop, with a download-specific timeout and no whole-file buffering. - Cache writes are best-effort and atomic; blocked/full web storage no longer leaves the app on the splash screen or blocks playback. - Artwork failures do not fail audio downloads; failed downloads refresh the download index. - Android queues sleep-timer commands before the controller connects. UI: - Music search is wired, debounced and kept separate from mood search. - Player bar respects navigation-bar insets and hides music-only mood actions for longform. Mood gradient handles Int.MIN_VALUE.
This commit is contained in:
1 parent
14bebced10
commit
43bd9629d3
17 files changed
+766
-145
No files matched your search
+16
-7
@@ -21,7 +21,7 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
private var controller: MediaController? = null
|
||||
private var pending: ((MediaController) -> Unit)? = null
|
||||
private val pending = ArrayDeque<(MediaController) -> Unit>()
|
||||
private var connected = false
|
||||
|
||||
@Volatile private var finished = false
|
||||
@@ -45,8 +45,10 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
controller = sessionController
|
||||
sessionController.addListener(listener)
|
||||
connected = true
|
||||
pending?.invoke(sessionController)
|
||||
pending = null
|
||||
val queued = synchronized(pending) {
|
||||
pending.toList().also { pending.clear() }
|
||||
}
|
||||
queued.forEach { action -> runCatching { action(sessionController) } }
|
||||
}
|
||||
},
|
||||
ContextCompat.getMainExecutor(appContext),
|
||||
@@ -125,9 +127,14 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
}
|
||||
|
||||
override fun scheduleSleepTimer(seconds: Long?): Boolean {
|
||||
val current = controller ?: return false
|
||||
val args = Bundle().apply { putLong(SLEEP_TIMER_SECONDS, seconds ?: 0L) }
|
||||
return runCatching { current.sendCustomCommand(SLEEP_TIMER_COMMAND, args) }.isSuccess
|
||||
val send: (MediaController) -> Boolean = { current ->
|
||||
runCatching { current.sendCustomCommand(SLEEP_TIMER_COMMAND, args) }.isSuccess
|
||||
}
|
||||
val current = controller
|
||||
if (current != null && connected) return send(current)
|
||||
synchronized(pending) { pending.addLast { controller -> send(controller) } }
|
||||
return true
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
@@ -135,7 +142,7 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
MediaController.releaseFuture(future)
|
||||
controller = null
|
||||
connected = false
|
||||
pending = null
|
||||
synchronized(pending) { pending.clear() }
|
||||
}
|
||||
|
||||
private fun withController(block: (MediaController) -> Unit) {
|
||||
@@ -143,7 +150,9 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
if (current != null && connected) {
|
||||
block(current)
|
||||
} else {
|
||||
pending = block
|
||||
// The first playback request can be followed immediately by a seek
|
||||
// (long-form resume). Keep every action, not only the last one.
|
||||
synchronized(pending) { pending.addLast(block) }
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
-3
@@ -6,6 +6,7 @@ import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.utils.io.readAvailable
|
||||
import java.io.File
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
@@ -32,7 +33,7 @@ class AndroidStorage(context: Context) : AppStorage {
|
||||
|
||||
override fun write(name: String, text: String) {
|
||||
root.mkdirs()
|
||||
File(root, name).writeText(text)
|
||||
atomicWrite(File(root, name)) { it.writeText(text) }
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
@@ -42,10 +43,39 @@ class AndroidStorage(context: Context) : AppStorage {
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? {
|
||||
downloads.mkdirs()
|
||||
val file = File(downloads, "${safeName(songId)}.mp3")
|
||||
file.writeBytes(bytes)
|
||||
atomicWrite(file) { it.writeBytes(bytes) }
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
override suspend fun saveDownloadStream(
|
||||
songId: String,
|
||||
source: io.ktor.utils.io.ByteReadChannel,
|
||||
): String? {
|
||||
downloads.mkdirs()
|
||||
val target = File(downloads, "${safeName(songId)}.mp3")
|
||||
val temp = File(downloads, "${safeName(songId)}.mp3.part")
|
||||
try {
|
||||
temp.outputStream().use { output ->
|
||||
val buffer = ByteArray(64 * 1024)
|
||||
while (true) {
|
||||
val read = source.readAvailable(buffer)
|
||||
if (read < 0) break
|
||||
if (read > 0) output.write(buffer, 0, read)
|
||||
}
|
||||
output.flush()
|
||||
}
|
||||
if (temp.length() == 0L) error("empty download")
|
||||
if (!temp.renameTo(target)) {
|
||||
temp.copyTo(target, overwrite = true)
|
||||
temp.delete()
|
||||
}
|
||||
return target.absolutePath
|
||||
} catch (t: Throwable) {
|
||||
temp.delete()
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
override fun downloadPath(songId: String): String? =
|
||||
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath
|
||||
|
||||
@@ -53,7 +83,7 @@ class AndroidStorage(context: Context) : AppStorage {
|
||||
if (bytes.isEmpty()) return null
|
||||
downloads.mkdirs()
|
||||
val file = File(downloads, "${safeName(songId)}.jpg")
|
||||
file.writeBytes(bytes)
|
||||
atomicWrite(file) { it.writeBytes(bytes) }
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
@@ -74,6 +104,20 @@ class AndroidStorage(context: Context) : AppStorage {
|
||||
override fun storageUsedBytes(): Long =
|
||||
downloads.listFiles()?.sumOf { it.length() } ?: 0L
|
||||
|
||||
private fun atomicWrite(target: File, write: (File) -> Unit) {
|
||||
val temp = File(target.parentFile, "${target.name}.part")
|
||||
try {
|
||||
write(temp)
|
||||
if (!temp.renameTo(target)) {
|
||||
temp.copyTo(target, overwrite = true)
|
||||
temp.delete()
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
temp.delete()
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
private fun safeName(songId: String): String =
|
||||
songId.map { if (it.isLetterOrDigit() || it == '-' || it == '_') it else '_' }.joinToString("")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user