Skip to content

Commit

Permalink
Fixes some progress-reporting bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
daytime-em committed Jun 12, 2024
1 parent f950e88 commit 704768f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class UploadNotificationService : Service() {

// can be commanded to start arbitrary number of times
if (uploadListListener == null) {
notify(MuxUploadManager.allUploadJobs())

val lis = UploadListListener()
this.uploadListListener = lis
MuxUploadManager.addUploadsUpdatedListener(lis)
Expand Down Expand Up @@ -169,12 +171,14 @@ class UploadNotificationService : Service() {
}
}

private fun updateCurrentUploads(uploads: List<MuxUpload>) {
// this.uploadsByFile.values.forEach { it.clearListeners() }
// uploads.forEach {
// this.uploadsByFile[it.videoFile.path] = it
// it.setStatusListener(UploadStatusListener())
// }
private fun updateCurrentUploads(incomingUploads: List<MuxUpload>) {
// listen to status of new uploads
incomingUploads
.filter { !this.uploadsByFile.containsKey(it.videoFile.path) }
.forEach {
this.uploadsByFile[it.videoFile.path] = it
it.setStatusListener(UploadStatusListener())
}
}

private inner class UploadListListener : UploadEventListener<List<MuxUpload>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mux.video.vod.demo.upload.viewmodel

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand All @@ -22,9 +23,8 @@ class UploadListViewModel(app: Application) : AndroidViewModel(app) {

private val listUpdateListener: UploadEventListener<List<MuxUpload>> by lazy {
UploadEventListener { newUploads ->
uploadMap.forEach { entry -> entry.value.clearListeners() }

newUploads.forEach { uploadMap[it.videoFile] = it }
//uploadMap.forEach { entry -> entry.value.clearListeners() }
observeUploads(newUploads)
updateUiData(uploadMap.values.toList())
}
}
Expand All @@ -50,11 +50,13 @@ class UploadListViewModel(app: Application) : AndroidViewModel(app) {
}

private fun observeUploads(recentUploads: List<MuxUpload>) {
recentUploads.forEach { upload ->
upload.setProgressListener {
recentUploads
.filter { !this.uploadMap.containsKey(it.videoFile) }
.forEach { upload ->
upload.setStatusListener {
updateUiData(uploadMap.values.toList())
}
uploadMap[upload.videoFile] = upload
updateUiData(uploadMap.values.toList())
}
} // recentUploads.forEach
}

Expand Down
70 changes: 38 additions & 32 deletions library/src/main/java/com/mux/video/upload/api/MuxUpload.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ class MuxUpload private constructor(
private val logger get() = MuxUploadSdk.logger

init {
// Catch Events if an upload was already in progress
observeUpload(uploadInfo)
// Catch state if an upload was already in progress
// no need to observe: the Flow will have the most-recent values when queried
uploadInfo.statusFlow?.value?.let { status -> this.lastKnownStatus = status }
}

/**
Expand Down Expand Up @@ -202,8 +203,16 @@ class MuxUpload private constructor(
*/
@MainThread
fun setProgressListener(listener: UploadEventListener<Progress>?) {
if (listener == null) {
observerJob?.cancel("clearing listeners")
observerJob = null
} else {
observeUpload(uploadInfo)
}

progressListener = listener
lastKnownProgress?.let { listener?.onEvent(it) }
observeUpload(uploadInfo)
}

/**
Expand Down Expand Up @@ -235,14 +244,15 @@ class MuxUpload private constructor(
*/
@MainThread
fun setStatusListener(listener: UploadEventListener<UploadStatus>?) {
statusListener = listener
listener?.onEvent(currentStatus)
if (listener == null) {
observerJob?.cancel("clearing listeners")
observerJob = null
} else {
observeUpload(uploadInfo)
}

statusListener = listener
listener?.onEvent(currentStatus)
}

/**
Expand All @@ -257,38 +267,34 @@ class MuxUpload private constructor(
statusListener = null
}

private fun newObserveProgressJob(upload: UploadInfo): Job {
private fun newObserveProgressJob(upload: UploadInfo): Job? {
// Job that collects and notifies state updates on the main thread (suspending on main is safe)
return callbackScope.launch {
upload.statusFlow?.let { flow ->
launch {
flow
.collect { status ->
// Update the status of our upload
lastKnownStatus = status
Log.d("Upload", "status $status")

// Notify the old listeners
when (status) {
is UploadStatus.Uploading -> { progressListener?.onEvent(status.uploadProgress) }
is UploadStatus.UploadPaused -> { progressListener?.onEvent(status.uploadProgress) }
is UploadStatus.UploadSuccess -> {
_successful = true
progressListener?.onEvent(status.uploadProgress)
resultListener?.onEvent(Result.success(status.uploadProgress))
}
is UploadStatus.UploadFailed -> {
progressListener?.onEvent(status.uploadProgress) // Make sure we're most up-to-date
if (status.exception !is CancellationException) {
_error = status.exception
resultListener?.onEvent(Result.failure(status.exception))
}
return upload.statusFlow?.let { flow ->
callbackScope.launch {
flow.collect { status ->
// Update the status of our upload
lastKnownStatus = status

// Notify the old listeners
when (status) {
is UploadStatus.Uploading -> { progressListener?.onEvent(status.uploadProgress) }
is UploadStatus.UploadPaused -> { progressListener?.onEvent(status.uploadProgress) }
is UploadStatus.UploadSuccess -> {
_successful = true
progressListener?.onEvent(status.uploadProgress)
resultListener?.onEvent(Result.success(status.uploadProgress))
}
is UploadStatus.UploadFailed -> {
progressListener?.onEvent(status.uploadProgress) // Make sure we're most up-to-date
if (status.exception !is CancellationException) {
_error = status.exception
resultListener?.onEvent(Result.failure(status.exception))
}
else -> { } // no relevant info
}

statusListener?.onEvent(status)
else -> { } // no relevant info
}

statusListener?.onEvent(status)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ internal data class UploadInfo(
@JvmSynthetic internal val uploadJob: Deferred<Result<UploadStatus>>?,
@JvmSynthetic internal val statusFlow: StateFlow<UploadStatus>?,
) {
fun isRunning(): Boolean = uploadJob?.isActive ?: false
fun isRunning(): Boolean = /*uploadJob?.isActive*/
statusFlow?.value?.let {
it is UploadStatus.Uploading || it is UploadStatus.Started || it is UploadStatus.Preparing
} ?: false
}

/**
Expand Down

0 comments on commit 704768f

Please sign in to comment.