Skip to content

Commit

Permalink
some horribleness encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
daytime-em committed Jun 12, 2024
1 parent 92ea4e0 commit f950e88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.10'
kotlinCompilerExtensionVersion '1.5.14'
}
packagingOptions {
resources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class UploadNotificationService : Service() {
val kbUploaded = (upload.currentProgress.bytesUploaded / 1024).toInt()
val kbTotal = (upload.currentProgress.totalBytes / 1024).toInt()

Log.d(TAG, "upload state: ${upload.uploadStatus}")

builder.setProgress(kbTotal, kbUploaded, false)
builder.setContentTitle(
resources.getQuantityString(
Expand Down Expand Up @@ -168,11 +170,11 @@ 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())
}
// this.uploadsByFile.values.forEach { it.clearListeners() }
// uploads.forEach {
// this.uploadsByFile[it.videoFile.path] = it
// it.setStatusListener(UploadStatusListener())
// }
}

private inner class UploadListListener : UploadEventListener<List<MuxUpload>> {
Expand Down
33 changes: 27 additions & 6 deletions library/src/main/java/com/mux/video/upload/api/MuxUpload.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mux.video.upload.api

import android.net.Uri
import android.util.Log
import androidx.annotation.MainThread
import com.mux.video.upload.MuxUploadSdk
import com.mux.video.upload.api.MuxUpload.Builder
import com.mux.video.upload.internal.UploadInfo
import com.mux.video.upload.internal.update
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.distinctUntilChangedBy
import java.io.File

/**
Expand Down Expand Up @@ -83,7 +85,9 @@ class MuxUpload private constructor(
private var progressListener: UploadEventListener<Progress>? = null
private var statusListener: UploadEventListener<UploadStatus>? = null
private var observerJob: Job? = null
private var currentStatus: UploadStatus = UploadStatus.Ready
private val currentStatus: UploadStatus get() =
uploadInfo.statusFlow?.value ?: lastKnownStatus ?: UploadStatus.Ready
private var lastKnownStatus: UploadStatus? = null
private val lastKnownProgress: Progress? get() = currentStatus.getProgress()

private val callbackScope: CoroutineScope = MainScope()
Expand Down Expand Up @@ -208,11 +212,18 @@ class MuxUpload private constructor(
* @see setStatusListener
*/
@MainThread
fun setResultListener(listener: UploadEventListener<Result<Progress>>) {
fun setResultListener(listener: UploadEventListener<Result<Progress>>?) {
if (listener == null) {
observerJob?.cancel("clearing listeners")
observerJob = null
} else {
observeUpload(uploadInfo)
}

resultListener = listener
lastKnownProgress?.let {
if (it.bytesUploaded >= it.totalBytes) {
listener.onEvent(Result.success(it))
listener?.onEvent(Result.success(it))
}
}
}
Expand All @@ -226,6 +237,12 @@ class MuxUpload private constructor(
fun setStatusListener(listener: UploadEventListener<UploadStatus>?) {
statusListener = listener
listener?.onEvent(currentStatus)
if (listener == null) {
observerJob?.cancel("clearing listeners")
observerJob = null
} else {
observeUpload(uploadInfo)
}
}

/**
Expand All @@ -234,6 +251,7 @@ class MuxUpload private constructor(
@Suppress("unused")
@MainThread
fun clearListeners() {
observerJob?.cancel("clearing listeners")
resultListener = null
progressListener = null
statusListener = null
Expand All @@ -244,10 +262,11 @@ class MuxUpload private constructor(
return callbackScope.launch {
upload.statusFlow?.let { flow ->
launch {
flow.collect { status ->
flow
.collect { status ->
// Update the status of our upload
currentStatus = status
statusListener?.onEvent(status)
lastKnownStatus = status
Log.d("Upload", "status $status")

// Notify the old listeners
when (status) {
Expand All @@ -267,6 +286,8 @@ class MuxUpload private constructor(
}
else -> { } // no relevant info
}

statusListener?.onEvent(status)
}
}
}
Expand Down

0 comments on commit f950e88

Please sign in to comment.