Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#34 Replace MaterialFilePicker dependency and fragmentActivity usage #81

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ val viewModel = getViewModel {
}

viewModel.mediaController.bind(
lifecycle,
supportFragmentManager
activity
) // permissioncController bind automatically
```

Expand Down
8 changes: 3 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
[versions]
kotlinVersion = "1.8.10"
kotlinVersion = "1.9.10"
androidAppCompatVersion = "1.6.1"
materialDesignVersion = "1.8.0"
androidLifecycleVersion = "2.3.1"
androidExifInterface = "1.3.6"
androidMediaFilePicker = "1.9.1"
coroutinesVersion = "1.6.4"
mokoMvvmVersion = "0.16.0"
mokoPermissionsVersion = "0.15.0"
mokoPermissionsVersion = "0.18.0"
mokoTestVersion = "0.6.1"
mokoMediaVersion = "0.11.0"
composeJetBrainsVersion = "1.3.1"
composeJetBrainsVersion = "1.5.1"

[libraries]
appCompat = { module = "androidx.appcompat:appcompat", version.ref = "androidAppCompatVersion" }
material = { module = "com.google.android.material:material", version.ref = "materialDesignVersion" }
lifecycle = { module = "androidx.lifecycle:lifecycle-extensions", version.ref = "androidLifecycleVersion" }
exifInterface = { module = "androidx.exifinterface:exifinterface", version.ref = "androidExifInterface" }
mediaFilePicker = { module = "com.github.icerockdev:MaterialFilePicker", version.ref = "androidMediaFilePicker" }
coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutinesVersion" }

mokoPermissions = { module = "dev.icerock.moko:permissions", version.ref = "mokoPermissionsVersion" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
package dev.icerock.moko.media.compose

import android.content.Context
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LifecycleOwner
import dev.icerock.moko.media.picker.MediaPickerController

Expand All @@ -21,8 +20,10 @@ actual fun BindMediaPickerEffect(mediaPickerController: MediaPickerController) {
val context: Context = LocalContext.current

LaunchedEffect(mediaPickerController, lifecycleOwner, context) {
val fragmentManager: FragmentManager = (context as FragmentActivity).supportFragmentManager
val activity: ComponentActivity = checkNotNull(context as? ComponentActivity) {
"$context context is not instance of ComponentActivity"
}

mediaPickerController.bind(lifecycleOwner.lifecycle, fragmentManager)
mediaPickerController.bind(activity)
}
}
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 @@ -5,10 +5,12 @@
package dev.icerock.moko.media.test

import dev.icerock.moko.media.Bitmap
import kotlinx.cinterop.ExperimentalForeignApi
import platform.CoreGraphics.CGRectMake
import platform.CoreImage.CIImage
import platform.UIKit.UIImage

@OptIn(ExperimentalForeignApi::class)
actual fun createBitmapMock(): Bitmap {
val image = CIImage.clearImage.imageByCroppingToRect(CGRectMake(0.0, 0.0, 1.0, 1.0))
return Bitmap(UIImage(image))
Expand Down
3 changes: 0 additions & 3 deletions media/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ dependencies {

androidMainImplementation(libs.appCompat)
androidMainImplementation(libs.exifInterface)

// TODO #34 remove external dependency
androidMainImplementation(libs.mediaFilePicker)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.media.picker

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContracts

internal class CameraPickerDelegate :
ImagePickerDelegate<CameraPickerDelegate.CameraPickerCallbackData, Uri, Bitmap>() {

override fun registerActivityResult(
context: Context,
activityResultRegistry: ActivityResultRegistry
): ActivityResultLauncher<Uri> = activityResultRegistry.register(
PICK_CAMERA_IMAGE_KEY,
ActivityResultContracts.TakePicture(),
) { result ->
val callbackData = callback ?: return@register
callback = null

if (!result) {
callbackData.callback.invoke(Result.failure(CanceledException()))
return@register
}

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

override fun createCallback(
callback: (Result<Bitmap>) -> Unit,
mediaOptions: MediaOptions?
): CameraPickerCallbackData {
val cameraPickerMediaOptions = mediaOptions as? CameraPickerMediaOptions
val outputUri = cameraPickerMediaOptions?.outputUri ?: Uri.EMPTY
val maxWidth = cameraPickerMediaOptions?.maxWidth ?: 0
val maxHeight = cameraPickerMediaOptions?.maxHeight ?: 0
return CameraPickerCallbackData(
callback,
outputUri,
maxWidth,
maxHeight,
)
}

override fun launchActivityResult(mediaOptions: MediaOptions?) {
val cameraPickerMediaOptions = mediaOptions as? CameraPickerMediaOptions
val outputUri = cameraPickerMediaOptions?.outputUri ?: Uri.EMPTY
pickerLauncherHolder.value?.launch(
outputUri
)
}

class CameraPickerCallbackData(
override val callback: (Result<Bitmap>) -> Unit,
val outputUri: Uri,
val maxWidth: Int,
val maxHeight: Int,
) : CallbackData<Bitmap>

class CameraPickerMediaOptions(
val outputUri: Uri,
val maxWidth: Int,
val maxHeight: Int,
) : MediaOptions

companion object {
private const val PICK_CAMERA_IMAGE_KEY = "PickCameraImageKey"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.media.picker
Alex009 marked this conversation as resolved.
Show resolved Hide resolved

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.provider.OpenableColumns
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContracts
import dev.icerock.moko.media.FileMedia
import java.io.File

internal class FilePickerDelegate :
PickerDelegate<FilePickerDelegate.FilePickerCallbackData, Array<String>, FileMedia>() {

override fun registerActivityResult(
context: Context,
activityResultRegistry: ActivityResultRegistry
): ActivityResultLauncher<Array<String>> = activityResultRegistry.register(
PICK_FILE_KEY,
ActivityResultContracts.OpenDocument(),
) { uri ->
val callbackData = callback ?: return@register
callback = null

val callback = callbackData.callback

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

val path = uri.path
if (path == null) {
callback.invoke(Result.failure(java.lang.IllegalStateException("File is null")))
return@register
}

@SuppressLint("Range")
val fileNameWithExtension = if (uri.scheme == ContentResolver.SCHEME_CONTENT) {
val cursor = context.contentResolver.query(uri, null, null, null, null)
cursor?.use {
if (!it.moveToFirst()) null
else it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
} else null

val file = File(path)
val name = file.name
val result = Result.success(
FileMedia(
fileNameWithExtension ?: name,
uri.toString(),
)
)
callback.invoke(result)
}

override fun createCallback(
callback: (Result<FileMedia>) -> Unit,
mediaOptions: MediaOptions?
): FilePickerCallbackData = FilePickerCallbackData(callback)

override fun launchActivityResult(mediaOptions: MediaOptions?) {
pickerLauncherHolder.value?.launch(
arrayOf(
"*/*",
)
)
}

class FilePickerCallbackData(
override val callback: (Result<FileMedia>) -> Unit
) : CallbackData<FileMedia>

companion object {
private const val PICK_FILE_KEY = "PickFileKey"
}
}

This file was deleted.

Loading
Loading