Abhishek Agarwal
09/07/2023, 4:40 AMProperties
tab, it lags a lot in iOS, not sure what’s the reason. In my compose view I am just collecting the flow and showing it. Also there is no network call in this screen and in my repository there is already the data in the flow. Please refer the video .
See 🧵 for codeAbhishek Agarwal
09/07/2023, 4:41 AM@Composable
internal fun SharedPrefDataScreen(
modifier: Modifier = Modifier,
) {
val models by NoobRepository.sharedPrefData.collectAsStateWithLifecycleOrCollectAsState()
LazyColumn(
modifier = modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 16.dp, horizontal = 4.dp)
) {
items(models) {model ->
Text(
text = model.prefName,
style = MaterialTheme.typography.titleMedium,
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold
)
for((key,value) in model.data) {
SharedPrefRow(
key = "key",
value = "value.toString()"
) {newValue ->
//
}
}
Divider(thickness = 1.dp, modifier = Modifier.padding(bottom = 16.dp))
}
}
}
Abhishek Agarwal
09/07/2023, 4:41 AM@Composable
fun <T : R, R> StateFlow<T>.collectAsStateWithLifecycleOrCollectAsState(
context: CoroutineContext = EmptyCoroutineContext,
): State<R> {
if(isAndroid())
return collectAsStateWithLifecycle(initial = this.value, context = context)
return collectAsState(context)
}
Abhishek Agarwal
09/07/2023, 4:41 AMprivate val _sharedPrefData = MutableStateFlow<List<SharedPrefModel>>(listOf())
val sharedPrefData = _sharedPrefData.asStateFlow()
Pablichjenkov
09/07/2023, 4:57 AMPablichjenkov
09/07/2023, 5:02 AMAbhishek Agarwal
09/07/2023, 5:55 AMstart
function which is called from the App Delegate or the application class. Whenever the start
function is called the library fetches all the shared Pref data (user default in this case for ios) and save it in repository. One thing to note here is that till this point, this compose view is not shown to user yet. Now whenever user shake the device, then I start this activity (or push view controller in case of ios) consisting of the bottom bar . So whenever user taps on Properties
tab, the repository already have all the data, the view just has to show it but it is lagging in ios but in android its working fine.Abhishek Agarwal
09/07/2023, 5:58 AMfun start() {
NoobHelper.registerProtocol()
NoobRepository.getAllPrefData()
}
Repository function
fun getAllPrefData() {
print("NooberT entered getAllPrefData")
GlobalScope.launch {
_sharedPrefData.emit(prefManager.getAllValues())
}
}
override fun getAllValues(): List<SharedPrefModel> {
val userDefaultData = userDefaults.dictionaryRepresentation()
val mappedData = userDefaultData.mapKeys { it.key.toString() }.mapValues { it.value.toString() }
return listOf(SharedPrefModel("", mappedData))
}
Pablichjenkov
09/07/2023, 6:06 AMprefManager.getAllValues()
just to see what's going on. Log timestamps to try to identify in what lines the lagging comes from.Abhishek Agarwal
09/07/2023, 7:39 AMColton Idle
09/07/2023, 5:18 PMAbhishek Agarwal
09/08/2023, 10:00 AMitems(models) {model ->
Text(
text = model.prefName,
style = MaterialTheme.typography.titleMedium,
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Bold
)
for((key,value) in model.data) {
SharedPrefRow(
key = "key",
value = "value.toString()"
) {newValue ->
//
}
}
the forLoop inside item was causing the lag