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

How usage moko-media and moko-permissions in now update compose multiplatform. Resolve Fragment Activity bugs #75

Open
maeceloacm1998 opened this issue Jan 3, 2024 · 2 comments

Comments

@maeceloacm1998
Copy link

maeceloacm1998 commented Jan 3, 2024

I was having trouble launching the gallery in cross-platform composition on the Android platform. My problem is that the fragment activity was not initialized when entering the screen with the BindMediaPickerEffect(mediaPicker) method. Solve this problem when change in ComposableActivity() to FragmentActivity() in MainActivity.kt

MainActivity.kt in android package

image

How to usage moko-media and moko-permissions in compose multiplatform 2024

To use moko-media and moko-permissions in cross-platform composition, follow the steps below:

  • 1 - Install dependencys in build.gradle commons packeage
commonMain.dependencies {
            // Media File Picker
            implementation("com.darkrockstudios:mpfilepicker:3.1.0")

           // Moko Permissions
            implementation("dev.icerock.moko:permissions:0.17.0")

            // Moko Permissions Compose
            implementation("dev.icerock.moko:permissions-compose:0.17.0")

            // Moko Media Compose
            implementation("dev.icerock.moko:media-compose:0.11.0")
}
  • 2 - Invoke moko permissions and moko media factory and usage to invoke controller. This controllers method usage in BindEffect( permissions controller) and BindMediaPickerEffect(media controller). Follow this example:

⚠️⚠️⚠️ ATTENTION: not remove remember to instancy controller, because crash app. ⚠️⚠️⚠️

@Composable
fun Content() {
        val coroutineScope = rememberCoroutineScope()

        val mediaFactory = rememberMediaPickerControllerFactory()
        val mediaPicker = remember(mediaFactory) { mediaFactory.createMediaPickerController() }

        val permissionFactory: PermissionsControllerFactory = rememberPermissionsControllerFactory()
        val permissionController: PermissionsController = remember(permissionFactory) {
            permissionFactory.createPermissionsController()
        }

        BindEffect(permissionController)
        BindMediaPickerEffect(mediaPicker)

        Button(
            onClick = {
                ViewModel.openPhoto(
                    coroutineScope = coroutineScope,
                    mediaPickerController = mediaPicker,
                    permissionsController = permissionController
                )
          }
       ) {
            Text(text = "Click on me")
       }
}

in ViewModel, this function usage to open gallary and manipulate show image selected in your screen

class RegisterPhotoScreenModel : ViewModel() {
    fun openPhoto(
        coroutineScope: CoroutineScope,
        mediaPickerController: MediaPickerController,
        permissionsController: PermissionsController
    ) {
        coroutineScope.launch {
            try {
                permissionsController.providePermission(Permission.GALLERY)
                val result = mediaPickerController.pickImage(MediaSource.GALLERY)
                val imageBitmap = result.toImageBitmap()
                // Update your livedata or mutableStateFlow with imageBitmap and colect in screen.
            } catch (deniedAlways: DeniedAlwaysException) {

                // TODO add snackbar with action button redirect user to config app.

            } catch (denied: DeniedException) {

                // TODO add snackbar with action button redirect user to config app.

            }
        }
    }
}
@orelvis15
Copy link

orelvis15 commented May 6, 2024

I am having the same problem when I use ComponentActivity it gives me this error

AppActivity cannot be cast to androidx.fragment.app.FragmentActivity

and when I use FragmentActivity it throws me this other one

No interface method bind(Landroidx/lifecycle/Lifecycle;Landroidx/fragment/app/FragmentManager;)V in class Ldev/icerock/moko/permissions/PermissionsController; or its super classes (declaration of 'dev.icerock.moko.permissions.PermissionsController'

I am trying the same code from the documentation

@Composable
fun Sample() {

    val mediaFactory = rememberMediaPickerControllerFactory()
    val mediaPicker = remember(mediaFactory) { mediaFactory.createMediaPickerController() }

    val factory: PermissionsControllerFactory = rememberPermissionsControllerFactory()
    val controller: PermissionsController = remember(factory) { factory.createPermissionsController() }
    val coroutineScope: CoroutineScope = rememberCoroutineScope()

    BindEffect(controller)
    BindMediaPickerEffect(mediaPicker)

    Button(
        onClick = {
            coroutineScope.launch {
                controller.providePermission(Permission.GALLERY)
                val bitmap = mediaPicker.pickImage(MediaSource.GALLERY)
            }
        }
    ) {
        Text(text = "Test")
    }
}

@melinamorch
Copy link

I'm getting the same error as @orelvis15, is there a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants