Skip to content

Commit

Permalink
#34 Fix pass image max size params
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Nesterov committed May 2, 2024
1 parent 0cd5051 commit 425c22f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package dev.icerock.moko.media.test

import androidx.activity.ComponentActivity
import dev.icerock.moko.media.Bitmap
import dev.icerock.moko.media.FileMedia
import dev.icerock.moko.media.Media
Expand Down Expand Up @@ -35,8 +36,7 @@ actual open class MediaPickerControllerMock actual constructor(
}

override fun bind(
lifecycle: androidx.lifecycle.Lifecycle,
fragmentManager: androidx.fragment.app.FragmentManager
activity: ComponentActivity
) {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,27 @@ internal class ImagePickerDelegate {
val callbackData = callback ?: return@register
callback = null

val callback = callbackData.callback
if (callbackData !is CallbackData.Gallery) {
callbackData.callback.invoke(
Result.failure(
IllegalStateException("Callback type should be Gallery")
)
)
return@register
}

if (uri == null) {
callback.invoke(Result.failure(CanceledException()))
callbackData.callback.invoke(Result.failure(CanceledException()))
return@register
}

processResult(activity, callback, uri)
processResult(
context = activity,
callback = callbackData.callback,
uri = uri,
maxImageWidth = callbackData.maxWidth,
maxImageHeight = callbackData.maxHeight,
)
}

takePictureLauncherHolder.value = activityResultRegistry.register(
Expand All @@ -63,7 +76,13 @@ internal class ImagePickerDelegate {
return@register
}

processResult(activity, callbackData.callback, callbackData.outputUri)
processResult(
context = activity,
callback = callbackData.callback,
uri = callbackData.outputUri,
maxImageWidth = callbackData.maxWidth,
maxImageHeight = callbackData.maxHeight,
)
}

val observer = object : LifecycleEventObserver {
Expand All @@ -89,7 +108,11 @@ internal class ImagePickerDelegate {
this.callback = null
}

this.callback = CallbackData.Gallery(callback)
this.callback = CallbackData.Gallery(
callback,
maxWidth,
maxHeight,
)

pickVisualMediaLauncherHolder.value?.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
Expand All @@ -107,7 +130,12 @@ internal class ImagePickerDelegate {
this.callback = null
}

this.callback = CallbackData.Camera(callback, outputUri)
this.callback = CallbackData.Camera(
callback,
outputUri,
maxWidth,
maxHeight,
)

takePictureLauncherHolder.value?.launch(
outputUri
Expand Down Expand Up @@ -152,12 +180,17 @@ internal class ImagePickerDelegate {
}

sealed class CallbackData(val callback: (Result<android.graphics.Bitmap>) -> Unit) {
class Gallery(callback: (Result<android.graphics.Bitmap>) -> Unit) :
CallbackData(callback)
class Gallery(
callback: (Result<android.graphics.Bitmap>) -> Unit,
val maxWidth: Int,
val maxHeight: Int,
) : CallbackData(callback)

class Camera(
callback: (Result<android.graphics.Bitmap>) -> Unit,
val outputUri: Uri
val outputUri: Uri,
val maxWidth: Int,
val maxHeight: Int,
) : CallbackData(callback)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import android.os.Environment
import androidx.activity.ComponentActivity
import androidx.core.content.FileProvider
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import dev.icerock.moko.media.Bitmap
import dev.icerock.moko.media.FileMedia
import dev.icerock.moko.media.Media
Expand Down Expand Up @@ -45,12 +44,13 @@ internal class MediaPickerControllerImpl(
mediaPickerDelegate.bind(activity)
filePickerDelegate.bind(activity)

val observer = object : LifecycleObserver {
val observer = object : LifecycleEventObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroyed(source: LifecycleOwner) {
this@MediaPickerControllerImpl.activityHolder.value = null
source.lifecycle.removeObserver(this)
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == Lifecycle.Event.ON_DESTROY) {
this@MediaPickerControllerImpl.activityHolder.value = null
source.lifecycle.removeObserver(this)
}
}
}
activity.lifecycle.addObserver(observer)
Expand Down

0 comments on commit 425c22f

Please sign in to comment.