parse-community / Parse-SDK-Android

The Android SDK for Parse Platform
https://parseplatform.org/
Other
1.88k stars 739 forks source link

Compose Preview crashes because of Parse init #1205

Open FlorinCusmereanu opened 6 months ago

FlorinCusmereanu commented 6 months ago

New Issue Checklist

Issue Description

Cannot run Preview

Steps to reproduce

Create a Preview page

Actual Outcome

Error

Expected Outcome

Component rendered in preview pane

Environment

Android

Parse Android SDK

Server

Database

Logs

java.lang.NullPointerException: Cannot invoke "java.io.File.getAbsolutePath()" because the return value of "com.parse.ParseCacheDirMigrationUtils.getOldParseDir(android.content.Context)" is null

I am trying with this code to preview my signup component I have tried initializing Parse in my preview, but it still doesn't work

@OptIn(ExperimentalMaterial3Api::class)
@Preview(showBackground = true)
@Composable
fun SignUpPagePreview() {

    val configuration = Parse.Configuration
        .Builder(LocalContext.current)
        .applicationId("****")
        .server("****")
        .build()
    Parse.initialize(configuration)
    Parse.enableLocalDatastore(LocalContext.current)

    CompositionLocalProvider(
        LocalUserState provides remember { UserStateViewModel() },
    ) {
        val navController = rememberNavController()
        SignUpPage(navController = navController)
    }

}
parse-github-assistant[bot] commented 6 months ago

Thanks for opening this issue!

mtrezza commented 6 months ago

Not sure whether this is a bug or actually an incorrect setup of the preview context.

FlorinCusmereanu commented 6 months ago

hello @mtrezza and thanks for the reply. that context object is the only context reference I have in the Preview component. do you or anyone else know how to do it better? or know what is wrong? or what am I doing wrong, if any?

Now I have a new one with

@Preview(showBackground = true)
@Composable
fun PostViewPreview() {

    val configuration = Parse.Configuration
        .Builder(LocalContext.current)
        .applicationId("****")
        .server("****")
        .build()
    Parse.initialize(configuration)
    Parse.enableLocalDatastore(LocalContext.current)

    ParseObject.registerSubclass(User::class.java)
    ParseObject.registerSubclass(UserInspiration::class.java)
    val user = User()
    user.uniqueDisplayName = "John Doe"
    val userInspiration = UserInspiration()
    userInspiration.user = user
    userInspiration.title = "Something to test #test"
    PostView(userInspiration = userInspiration)
}

java.lang.NullPointerException: Cannot invoke "java.io.File.getAbsolutePath()" because the return value of "com.parse.ParseCacheDirMigrationUtils.getOldParseDir(android.content.Context)" is null   at com.parse.ParseCacheDirMigrationUtils.runSilentMigration(ParseCacheDirMigrationUtils.java:30)

thanks!