Vivek Modi
07/21/2022, 4:42 PMprivate const val NEAREST_RESULT_JSON = "nearestResultJson"
@Composable
internal fun NavigationGraph() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = ScreenRoute.Home.route) {
composable(ScreenRoute.Home.route) {
SetupMainActivityView { nearestResult ->
val nearestResultJson = Uri.encode(Gson().toJson(nearestResult))
navController.navigate(ScreenRoute.Result.route + "/{$nearestResultJson}")
}
}
composable(
ScreenRoute.Result.route + "/{$NEAREST_RESULT_JSON}",
arguments = listOf(
navArgument(NEAREST_RESULT_JSON) { type = NearestResultParamType() }
)
) { backStackEntry ->
ResultScreen(backStackEntry.arguments?.getParcelableArrayList(NEAREST_RESULT_JSON))
}
}
}
Vivek Modi
07/21/2022, 4:42 PMclass NearestResultParamType : NavType<ArrayList<NearestResult>>(isNullableAllowed = false) {
override fun get(bundle: Bundle, key: String): ArrayList<NearestResult>? {
return bundle.getParcelable(key)
}
override fun parseValue(value: String): ArrayList<NearestResult> {
Log.e("value", ">>$value")
return Gson().fromJson<ArrayList<NearestResult>>(value, ArrayList::class.java)
}
override fun put(bundle: Bundle, key: String, value: ArrayList<NearestResult>) {
bundle.putParcelableArrayList(key, value)
}
}
Vivek Modi
07/21/2022, 4:42 PM@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SetupMainActivityView(
viewModel: MainActivityViewModel = koinViewModel(),
navigateToNext: (nearestResult: ArrayList<NearestResult>) -> Unit,
) {
Scaffold(topBar = {
TopAppBar(
title = { Text(text = stringResource(id = R.string.app_name)) },
backgroundColor = getBackgroundColor(),
elevation = 0.dp
)
}, content = { padding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.background(getBackgroundColor()),
contentAlignment = Center
) {
when (val state = viewModel.stateResultFetchState.collectAsState().value) {
is ResultFetchState.OnSuccess -> {
LaunchedEffect(Unit) {
navigateToNext(state.nearestResult)
}
}
is ResultFetchState.IsLoading -> {
LoadingFunction()
}
is ResultFetchState.OnError,
is ResultFetchState.OnEmpty -> {
ActivityContent(viewModel)
}
}
}
})
}
Vivek Modi
07/21/2022, 4:43 PMparseValue
which drops the value if day
attribute
E/value: >>{[{"day":{},"event":"Roland Garros: Rafael Nadal wins against Schwartzman in 3 sets"},{"day":{},"event":"Lewis Hamilton wins Silverstone Grand Prix by 5.856 seconds"},{"day":{},"event":"Roland Garros: Novak Djokovic wins against Stefanos Tsitsipas in 3 sets"},{"day":{},"event":"Lebron James leads Lakers to game 6 win in the NBA playoffs"}]}
I am getting this error and crash the application
2022-07-21 17:25:13.821 537-537/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vivek.sportsresult, PID: 537
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:1003)
at com.google.gson.Gson.fromJson(Gson.java:956)
at com.google.gson.Gson.fromJson(Gson.java:905)
at com.vivek.sportsresult.ui.screen.navigation.NearestResultParamType.parseValue(NearestResultParamType.kt:16)
at com.vivek.sportsresult.ui.screen.navigation.NearestResultParamType.parseValue(NearestResultParamType.kt:9)
at androidx.navigation.NavType.parseAndPut(NavType.kt:80)
at androidx.navigation.NavDeepLink.parseArgument(NavDeepLink.kt:254)
at androidx.navigation.NavDeepLink.getMatchingArguments(NavDeepLink.kt:175)
at androidx.navigation.NavDestination.matchDeepLink(NavDestination.kt:347)
at androidx.navigation.NavGraph.matchDeepLink(NavGraph.kt:72)
at androidx.navigation.NavController.navigate(NavController.kt:1652)
at androidx.navigation.NavController.navigate(NavController.kt:1984)
at androidx.navigation.NavController.navigate$default(NavController.kt:1979)
at com.vivek.sportsresult.ui.screen.navigation.NavigationGraphKt$NavigationGraph$1$1$1.invoke(NavigationGraph.kt:22)
at com.vivek.sportsresult.ui.screen.navigation.NavigationGraphKt$NavigationGraph$1$1$1.invoke(NavigationGraph.kt:20)
at com.vivek.sportsresult.ui.screen.MainActivityKt$SetupMainActivityView$1$1$1.invokeSuspend(MainActivity.kt:101)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7697)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@f221268, androidx.compose.runtime.BroadcastFrameClock@78db681, StandaloneCoroutine{Cancelling}@db83326, AndroidUiDispatcher@c678b67]
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:991)
... 27 more
Vivek Modi
07/21/2022, 4:44 PMVivek Modi
07/21/2022, 4:44 PMChrimaeon
07/21/2022, 5:04 PMVivek Modi
07/21/2022, 5:05 PMVivek Modi
07/21/2022, 5:05 PMChrimaeon
07/21/2022, 5:11 PM>>{[
This is not valid json. You need something like
{βlistβ:[]}
Chrimaeon
07/21/2022, 5:11 PMVivek Modi
07/21/2022, 5:12 PMVivek Modi
07/21/2022, 5:46 PMVivek Modi
07/21/2022, 5:47 PMVivek Modi
07/21/2022, 5:50 PMChrimaeon
07/21/2022, 5:54 PM{
"cars": [
"Ford",
"BMW",
"Fiat"
]
}
what you are creating is not a valid JSON.Chrimaeon
07/21/2022, 5:54 PMVivek Modi
07/21/2022, 5:58 PMVivek Modi
07/21/2022, 6:02 PM[NearestResult(day=2020-05-09T23:15:15+01:00[Europe/Dublin], event=Roland Garros: Rafael Nadal wins against Schwartzman in 3 sets), NearestResult(day=2020-05-09T20:09:03+01:00[Europe/Dublin], event=Lewis Hamilton wins Silverstone Grand Prix by 5.856 seconds), NearestResult(day=2020-05-09T14:00:40+01:00[Europe/Dublin], event=Roland Garros: Novak Djokovic wins against Stefanos Tsitsipas in 3 sets), NearestResult(day=2020-05-09T09:15:15+01:00[Europe/Dublin], event=Lebron James leads Lakers to game 6 win in the NBA playoffs)]
Vivek Modi
07/21/2022, 6:02 PMIan Lake
07/21/2022, 6:03 PMUri.encode
Vivek Modi
07/21/2022, 6:12 PMval nearestResultJson = Uri.encode(Gson().toJson(nearestResult))
Chrimaeon
07/21/2022, 6:14 PM{}
navController.navigate(ScreenRoute.Result.route + "/{$nearestResultJson}")
just remove the {}
Chrimaeon
07/21/2022, 6:16 PMScreenRoute.Result.route + "/{$NEAREST_RESULT_JSON}",
so the path that you create is result/{nearestResultJson}
and not just result/nearestResultJson
Vivek Modi
07/21/2022, 6:20 PMVivek Modi
07/21/2022, 6:21 PMIan Lake
07/21/2022, 6:30 PM{nearestResultJson}
is the placeholder pattern that you replace with the actual argument. You don't put braces in the final string you navigate toVivek Modi
07/21/2022, 7:06 PMday
attritube is dropping the value.Vivek Modi
07/21/2022, 7:27 PMSetupMainActivityView { nearestResult ->
Log.e("nearestResult ","$nearestResult")
val nearestResultJson = Uri.encode(Gson().toJson(nearestResult))
navController.navigate(ScreenRoute.Result.route + "/$nearestResultJson")
}
it giving me this correct outputVivek Modi
07/21/2022, 7:28 PMVivek Modi
07/21/2022, 8:03 PMVivek Modi
07/21/2022, 8:04 PMColton Idle
07/21/2022, 9:55 PMIan Lake
07/21/2022, 11:05 PMTransactionTooLargeException
), but clearly you're already deep down that bad behavior holeVivek Modi
07/22/2022, 9:37 AM