forked from icerockdev/moko-media
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
icerockdev#34 add MediaPickerDelegate
- Loading branch information
Alexey Nesterov
committed
May 2, 2024
1 parent
d358b7f
commit 39c2649
Showing
2 changed files
with
78 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
media/src/androidMain/kotlin/dev/icerock/moko/media/picker/MediaPickerDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dev.icerock.moko.media.picker | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.ActivityResultRegistryOwner | ||
import androidx.activity.result.PickVisualMediaRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import dev.icerock.moko.media.Media | ||
import dev.icerock.moko.media.MediaFactory | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
internal class MediaPickerDelegate { | ||
|
||
private var callback: CallbackData? = null | ||
|
||
private val mediaPickerLauncherHolder = | ||
MutableStateFlow<ActivityResultLauncher<PickVisualMediaRequest>?>(null) | ||
|
||
fun bind(activity: ComponentActivity) { | ||
val activityResultRegistryOwner = activity as ActivityResultRegistryOwner | ||
val activityResultRegistry = activityResultRegistryOwner.activityResultRegistry | ||
|
||
mediaPickerLauncherHolder.value = activityResultRegistry.register( | ||
PICK_MEDIA_KEY, | ||
ActivityResultContracts.PickVisualMedia() | ||
) { uri -> | ||
val callbackData = callback ?: return@register | ||
callback = null | ||
|
||
val callback = callbackData.callback | ||
|
||
if (uri == null) { | ||
callback.invoke(Result.failure(CanceledException())) | ||
return@register | ||
} | ||
|
||
val result = kotlin.runCatching { | ||
MediaFactory.create(activity, uri) | ||
} | ||
callback.invoke(result) | ||
} | ||
|
||
val observer = object : LifecycleEventObserver { | ||
|
||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { | ||
if (event == Lifecycle.Event.ON_DESTROY) { | ||
mediaPickerLauncherHolder.value = null | ||
source.lifecycle.removeObserver(this) | ||
} | ||
} | ||
} | ||
activity.lifecycle.addObserver(observer) | ||
} | ||
|
||
fun pickMedia(callback: (Result<Media>) -> Unit) { | ||
this.callback?.let { | ||
it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) | ||
this.callback = null | ||
} | ||
this.callback = CallbackData(callback) | ||
|
||
mediaPickerLauncherHolder.value?.launch( | ||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo) | ||
) | ||
} | ||
|
||
class CallbackData(val callback: (Result<Media>) -> Unit) | ||
|
||
companion object { | ||
private const val PICK_MEDIA_KEY = "PickMediaKey" | ||
} | ||
} |