ComposePreference icon indicating copy to clipboard operation
ComposePreference copied to clipboard

Add support for Long type

Open grote opened this issue 2 months ago • 3 comments

I am storing a Long in the same shared preferences as your MutableStateFlow is using. When trying to turn your preferences into ShredPreferences things crash:

                                                                                                java.lang.IllegalArgumentException: Unsupported type for value 1761326652892
                                                                                                	at me.zhanghai.compose.preference.PreferenceFlow_androidKt.setPreferences(PreferenceFlow.android.kt:66)
                                                                                                	at me.zhanghai.compose.preference.PreferenceFlow_androidKt.access$setPreferences(PreferenceFlow.android.kt:1)
                                                                                                	at me.zhanghai.compose.preference.PreferenceFlow_androidKt$createPreferenceFlow$1$1$1.emit(PreferenceFlow.android.kt:49)
                                                                                                	at me.zhanghai.compose.preference.PreferenceFlow_androidKt$createPreferenceFlow$1$1$1.emit(PreferenceFlow.android.kt:49)
                                                                                                	at kotlinx.coroutines.flow.FlowKt__LimitKt$drop$2$1.emit(Limit.kt:22)
                                                                                                	at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:401)
                                                                                                	at kotlinx.coroutines.flow.StateFlowImpl$collect$1.invokeSuspend(Unknown Source:15)
                                                                                                	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:34)
                                                                                                	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:233)
                                                                                                	at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:175)
                                                                                                	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:147)
                                                                                                	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:470)
                                                                                                	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$kotlinx_coroutines_core(CancellableContinuationImpl.kt:504)
                                                                                                	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$kotlinx_coroutines_core$default(CancellableContinuationImpl.kt:493)
                                                                                                	at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:359)
                                                                                                	at kotlinx.coroutines.flow.StateFlowSlot.makePending(StateFlow.kt:290)
                                                                                                	at kotlinx.coroutines.flow.StateFlowImpl.updateState(StateFlow.kt:354)
                                                                                                	at kotlinx.coroutines.flow.StateFlowImpl.setValue(StateFlow.kt:321)
                                                                                                	at me.zhanghai.compose.preference.PreferenceStateKt.rememberPreferenceState$lambda$4$lambda$3(PreferenceState.kt:37)
                                                                                                	at me.zhanghai.compose.preference.PreferenceStateKt.$r8$lambda$ogaDIDsMh9kalF6kpsNVDvOimho(Unknown Source:0)
                                                                                                	at me.zhanghai.compose.preference.PreferenceStateKt$$ExternalSyntheticLambda0.invoke(D8$$SyntheticClass:0)
                                                                                                	at me.zhanghai.compose.preference.AsMutableState.setValue(PreferenceState.kt:50)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt.ListPreference$lambda$3(ListPreference.kt:350)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt.ListPreference$lambda$5$lambda$4(ListPreference.kt:112)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt.$r8$lambda$1HSQOvL0_3k0mkbBFod9nL6QvJ4(Unknown Source:0)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt$$ExternalSyntheticLambda8.invoke(D8$$SyntheticClass:0)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt$ListPreference$11$1$1$1$1$1.invoke(ListPreference.kt:161)
                                                                                                	at me.zhanghai.compose.preference.ListPreferenceKt$ListPreference$11$1$1$1$1$1.invoke(ListPreference.kt:160)

grote avatar Oct 24 '25 20:10 grote

Yes, unfortunately long isn't supported right now due to this library being multiplatform and long being much harder to support on Apple and web platforms. Maybe you can use your own preference flow impl?

zhanghai avatar Oct 24 '25 22:10 zhanghai

Or maybe I can add Long support only for Android platform?

zhanghai avatar Oct 24 '25 23:10 zhanghai

If that's possible for you without too much hassle, that may be a sufficient solution. The crash happens in PreferenceFlow_androidKt, so if there's already Android specific code, just adding a line for Long there may fix things already:

when (mapValue) {
    is Boolean -> putBoolean(key, mapValue)
    is Int -> putInt(key, mapValue)
    is Long -> putLong(key, mapValue)
    is Float -> putFloat(key, mapValue)
    is String -> putString(key, mapValue)
    is Set<*> ->
    @Suppress("UNCHECKED_CAST") putStringSet(key, mapValue as Set<String>)
    else -> throw IllegalArgumentException("Unsupported type for value $mapValue")
}

I am not sure I understand why it needs to re-put all values it finds in shared preferences. Depending on the explanation for that it may also be sufficient to just leave values it doesn't know (or can't handle) alone.

grote avatar Oct 27 '25 12:10 grote