Skip to content

Commit

Permalink
Add:Sync local progress to server natively #74
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Jan 27, 2022
1 parent 1a555ea commit 16da0c9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,34 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi
} else if (listeningStreamId == "download") {
// TODO: Save downloaded audiobook progress & send to server if connected
Log.d(tag, "ListeningTimer: Is listening download")

// Send sync data only for local books
var syncData: JSObject = JSObject()
var duration = playerNotificationService.getAudiobookDuration() / 1000
var currentTime = playerNotificationService.getCurrentTime() / 1000
syncData.put("totalDuration", duration)
syncData.put("currentTime", currentTime)
syncData.put("progress", if (duration > 0) (currentTime / duration) else 0)
syncData.put("isRead", false)
syncData.put("lastUpdate", System.currentTimeMillis())
syncData.put("audiobookId", listeningBookId)
sendLocalSyncData(syncData) {
Log.d(tag, "Local sync done")
}
}
}

fun sendLocalSyncData(payload:JSObject, cb: (() -> Unit)) {
var serverUrl = playerNotificationService.getServerUrl()
var token = playerNotificationService.getUserToken()

if (serverUrl == "" || token == "") {
return
}

Log.d(tag, "Sync Local $serverUrl | $token")
var url = "$serverUrl/api/syncLocal"
sendServerRequest(url, token, payload, cb)
}

fun sendStreamSyncData(payload:JSObject, cb: (() -> Unit)) {
Expand All @@ -127,7 +154,10 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi

Log.d(tag, "Sync Stream $serverUrl | $token")
var url = "$serverUrl/api/syncStream"
sendServerRequest(url, token, payload, cb)
}

fun sendServerRequest(url:String, token:String, payload:JSObject, cb: () -> Unit) {
val mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = payload.toString().toRequestBody(mediaType)
val request = Request.Builder().post(requestBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
Log.d(tag, "Playing ${getCurrentBookTitle()} | ${currentPlayer.mediaMetadata.title} | ${currentPlayer.mediaMetadata.displayTitle}")
if (player.isPlaying) {
audiobookProgressSyncer.start()
}
if (!player.isPlaying && audiobookProgressSyncer.listeningTimerRunning) {
} else {
audiobookProgressSyncer.stop()
}

Expand Down Expand Up @@ -784,6 +783,12 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return currentAudiobookStreamData?.id
}

// The duration stored on the audiobook
fun getAudiobookDuration() : Long {
if (currentAudiobookStreamData == null) return 0L
return currentAudiobookStreamData!!.duration
}

fun getServerUrl(): String {
return audiobookManager.serverUrl
}
Expand Down

0 comments on commit 16da0c9

Please sign in to comment.