sency-ai / smkit-ui-react-native-demo

0 stars 1 forks source link

ReadableNativeMap cannot be cast to java.lang.String Error in React Native Module #23

Closed caesarkrit closed 2 months ago

caesarkrit commented 2 months ago

Description: When invoking the startCustomWorkout method, the application encounters a ReadableNativeMap cannot be cast to java.lang.String error. This error suggests that a field expected to be a String is actually an object, leading to a type mismatch during the interaction between JavaScript and Kotlin.

Steps to Reproduce:

  1. Set up a React Native project with the @sency/react-native-smkit-ui library integrated.
  2. Implement a button that triggers the startCustomWorkout method with a workout object in JavaScript.
  3. Run the application and press the button to initiate the workout.

Expected Behavior: The workout should start successfully without any errors, and the expected workout details should be logged.

Actual Behavior: The application throws a ReadableNativeMap cannot be cast to java.lang.String error, indicating a type mismatch in the native module.

Relevant Code:

JavaScript (TypeScript) Side:

export function startCustomWorkout(workout) { console.log("startCustomWorkout called with workout:", JSON.stringify(workout, null, 2)); return SMKitUIManager.startCustomWorkout(workout); }

// Example workout object const workout = new SMWorkout( "50", "Simple Workout", null, null, [ new SMExercise( "Simple Exercise", 30, 5, null, null, [UIElement.RepsCounter, UIElement.Timer], "HighKnees", true, null, new SMScoringParams(null, 0.3, 20, 10, null, null) ) ], null, null, null );

// Triggering the workout startCustomWorkout(workout);

Kotlin Side (SmkitUiLibraryModule.kt):

@ReactMethod fun startCustomWorkout(workout: ReadableMap, promise: Promise) { try { val workoutId = workout.getString("id") val workoutName = workout.getString("name") val workoutIntro = workout.getString("workoutIntro") val soundtrack = workout.getString("soundtrack")

    Log.d("SmkitUiLibraryModule", "workoutId: $workoutId")
    Log.d("SmkitUiLibraryModule", "workoutName: $workoutName")
    Log.d("SmkitUiLibraryModule", "workoutIntro: $workoutIntro")
    Log.d("SmkitUiLibraryModule", "soundtrack: $soundtrack")

    val exercises = workout.getArray("exercises")
    Log.d("SmkitUiLibraryModule", "exercises: ${exercises.toString()}")

    for (i in 0 until exercises.size()) {
        val exercise = exercises.getMap(i)
        Log.d("SmkitUiLibraryModule", "exercise $i: ${exercise.toString()}")
        val detector = exercise.getString("detector")
        val uiElements = exercise.getArray("uiElements")
        Log.d("SmkitUiLibraryModule", "detector: $detector")
        Log.d("SmkitUiLibraryModule", "uiElements: ${uiElements.toString()}")
        val repBased = exercise.getBoolean("repBased")
        Log.d("SmkitUiLibraryModule", "repBased: $repBased")

        // Log other fields similarly
    }

    // Use the extracted values to perform necessary operations

    // Returning success response
    val result = Arguments.createMap()
    result.putString("summary", "Workout started successfully")
    result.putBoolean("didFinish", true)
    promise.resolve(result)
} catch (e: Exception) {
    promise.reject("ERROR_STARTING_WORKOUT", e)
}

}

Possible Cause: The issue is likely due to a mismatch in the expected types between JavaScript and Kotlin. Specifically, a field expected to be a String in Kotlin is actually a ReadableNativeMap (object) from JavaScript. This typically occurs when nested structures are not properly handled.

Screenshot_20240731-141305_baseball_tracker

SencyCaspit commented 2 months ago

Hey @caesarkrit, Thanks for reaching out and letting us know about this issue. After investigating your comment, we actually found the problem. An hotfix will be released ASAP. Please stay tune. Thanks in advance for you patience, Sincerely, Sency Team.

ofergoldstein commented 2 months ago

Hi @caesarkrit we have pushed the hotfix to plugin version 0.1.6

you can upgrade it by running - npm install @sency/react-native-smkit-ui

caesarkrit commented 2 months ago

Hi, Library is failing to compile after moving to 0.1.6. v0.1.5 is still operational.

SencyCaspit commented 2 months ago

Hey @caesarkrit, Im sorry to hear that. I tested again 0.1.6 by recloning this repo and run npm install. I advise you to do the same. If you working on existing project, sometimes cleaning gradle's cache before updating the library's version might be useful. You can do it by navigating to gradle's global directory on your local machine and run:

rm -rf caches
rm -rf daemon
rm -rf wrapper

Please let us know what was the outcome of this process.

Sincerely, Sency Team.