feat: upload collected run data to api
This commit is contained in:
@@ -8,6 +8,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import at.lockstep.jukebox.api.LockstepApiException
|
||||
import at.lockstep.jukebox.db.TrackRow
|
||||
import at.lockstep.player.data.UserPreferencesRepository
|
||||
import at.lockstep.player.data.db.FileMetadataEntity
|
||||
import at.lockstep.player.data.db.TrackPairingEntity
|
||||
import at.lockstep.player.util.AudioUriValidator
|
||||
import at.lockstep.player.playback.TrackBoundaryEvent
|
||||
@@ -28,6 +29,8 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@@ -38,6 +41,7 @@ class LockstepViewModel(
|
||||
|
||||
companion object {
|
||||
private const val TAG = "LockstepPairing"
|
||||
private const val METADATA_TAG = "LockstepMetadata"
|
||||
|
||||
/** Serialize on-demand playlist detail fetch so PairingScreen + folder flow do not double-hit the API. */
|
||||
private val playlistDetailMutexes = ConcurrentHashMap<String, Mutex>()
|
||||
@@ -46,6 +50,7 @@ class LockstepViewModel(
|
||||
}
|
||||
private val prefs = UserPreferencesRepository(application)
|
||||
private val pairingDao get() = app.database.pairingDao()
|
||||
private val fileMetadataDao get() = app.database.fileMetadataDao()
|
||||
|
||||
val onboardingComplete: StateFlow<Boolean> =
|
||||
prefs.onboardingComplete.stateIn(
|
||||
@@ -207,11 +212,11 @@ class LockstepViewModel(
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val pairing = pairingDao.findForTrack(playlistId, event.trackId)
|
||||
val meta = pairing?.localUri?.takeIf { it.isNotBlank() } ?: return@launch
|
||||
RunDataStorage.writeRunDataFile(
|
||||
context = getApplication(),
|
||||
writeRunDataAndRecordMetadata(
|
||||
runSessionFolder = runSessionFolder,
|
||||
playlistDisplayName = playlistDisplayName,
|
||||
trackQueueIndex0Based = event.queueIndex,
|
||||
trackId = event.trackId,
|
||||
metaContentUri = meta,
|
||||
title = event.title,
|
||||
artist = event.artist,
|
||||
@@ -237,11 +242,11 @@ class LockstepViewModel(
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val pairing = pairingDao.findForTrack(playlistId, trackId)
|
||||
val meta = pairing?.localUri?.takeIf { it.isNotBlank() } ?: return@launch
|
||||
RunDataStorage.writeRunDataFile(
|
||||
context = getApplication(),
|
||||
writeRunDataAndRecordMetadata(
|
||||
runSessionFolder = runSessionFolder,
|
||||
playlistDisplayName = playlistDisplayName,
|
||||
trackQueueIndex0Based = queueIndex,
|
||||
trackId = trackId,
|
||||
metaContentUri = meta,
|
||||
title = title,
|
||||
artist = artist,
|
||||
@@ -250,6 +255,99 @@ class LockstepViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun writeRunDataAndRecordMetadata(
|
||||
runSessionFolder: String,
|
||||
playlistDisplayName: String,
|
||||
trackQueueIndex0Based: Int,
|
||||
trackId: String,
|
||||
metaContentUri: String,
|
||||
title: String,
|
||||
artist: String,
|
||||
snapshot: RunTrackDataSnapshot,
|
||||
) {
|
||||
val uri =
|
||||
RunDataStorage.writeRunDataFile(
|
||||
context = getApplication(),
|
||||
runSessionFolder = runSessionFolder,
|
||||
playlistDisplayName = playlistDisplayName,
|
||||
trackQueueIndex0Based = trackQueueIndex0Based,
|
||||
metaContentUri = metaContentUri,
|
||||
title = title,
|
||||
artist = artist,
|
||||
snapshot = snapshot,
|
||||
) ?: return
|
||||
val entry =
|
||||
FileMetadataEntity(
|
||||
fileUri = uri.toString(),
|
||||
trackId = trackId,
|
||||
type = FileMetadataEntity.TYPE_COLLECTION,
|
||||
version = BuildConfig.VERSION_CODE,
|
||||
)
|
||||
val id = fileMetadataDao.insert(entry)
|
||||
syncMetadataEntry(entry.copy(id = id))
|
||||
}
|
||||
|
||||
suspend fun syncPendingMetadata(): String? {
|
||||
val token = spotifyAccessToken.value
|
||||
if (token.isNullOrBlank()) {
|
||||
return null
|
||||
}
|
||||
return withContext(Dispatchers.IO) {
|
||||
val pending = fileMetadataDao.listUnsynced()
|
||||
if (pending.isEmpty()) {
|
||||
return@withContext null
|
||||
}
|
||||
val failures = pending.count { entry -> !syncMetadataEntry(entry) }
|
||||
if (failures > 0) {
|
||||
"Failed to sync $failures collection(s)"
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun syncMetadataEntry(entry: FileMetadataEntity): Boolean {
|
||||
val token = spotifyAccessToken.value?.takeIf { it.isNotBlank() } ?: return false
|
||||
val collection =
|
||||
withContext(Dispatchers.IO) {
|
||||
readCollectionJson(entry.fileUri)
|
||||
} ?: return false
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
app.metadataSyncClient.uploadCollection(
|
||||
accessToken = token,
|
||||
trackId = entry.trackId,
|
||||
type = entry.type,
|
||||
version = entry.version,
|
||||
collection = collection,
|
||||
)
|
||||
fileMetadataDao.markSynced(entry.id)
|
||||
true
|
||||
} catch (e: IOException) {
|
||||
Log.w(METADATA_TAG, "metadata sync failed id=${entry.id}", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readCollectionJson(fileUri: String): JSONObject? {
|
||||
val uri = Uri.parse(fileUri)
|
||||
try {
|
||||
context.contentResolver.openInputStream(uri)?.use { stream ->
|
||||
return JSONObject(stream.bufferedReader().readText())
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(METADATA_TAG, "readCollectionJson contentResolver failed uri=$fileUri", e)
|
||||
}
|
||||
return try {
|
||||
val path = uri.path ?: return null
|
||||
JSONObject(File(path).readText())
|
||||
} catch (e: Exception) {
|
||||
Log.w(METADATA_TAG, "readCollectionJson file path failed uri=$fileUri", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun syncJukeboxIfToken(): String? {
|
||||
val token = spotifyAccessToken.value
|
||||
if (token.isNullOrBlank()) {
|
||||
|
||||
Reference in New Issue
Block a user