(draft) `Day 80` - Bug Fixes ◦ *issue (solved)...
# 100daysofkotlin-2021
b
(draft)
Day 80
• Bug Fixes ◦ issue (solved) - When logging in, the current note was not being cached correctly. ▪︎ Added a LaunchedEffect here so whenever the user data changes, the current note also updates
Copy code
val username by userVm.currentUsername.observeAsState()    
    LaunchedEffect(username) {
        vm.checkForCurrentNote()
    }
issue (discovered) - App crashes when moving to the background (only when a user is logged in)issue (?) - If logged into another account immediately after signing out of previous, it will load 0-all notes from previous account. Coroutine issue here. See thread for fix Difficult to pinpoint, and wasn't able to replicate it after applying fix in thread • Added Progress Bar to NoteListScreen to show while note list is loading
1
issue (discovered) - If logged into another account immediately after signing out of previous, it will load 0-all notes from previous account. Coroutine issue here (change below did not solve the bug) Changed:
Copy code
curNotesResponse?.body()?.let { notes ->
    noteDao.deleteAllNotes()
    insertNotes(notes.onEach { note -> note.isSynced = true } )
To:
Copy code
curNotesResponse?.body()?.let { notes ->
    withContext(Dispatchers.Default) {
        noteDao.deleteAllNotes()
    }
    insertNotes(notes.onEach { note -> note.isSynced = true } )
}
Still had issues, after some trial and error I've left the change by adding LaunchedEffect in NoteListScreen. A big issue here is due to not using fragments/activites, so I'm trying to update the screen in the background while logging in/out inside of the drawerstate. I have quite a few LaunchedEffects going on, I don't believe it's intended to be used this way and I'm completely abusing it. lol.. Code could use refactoring to work on Single Source of Truth.. Until I fix the coroutine to make sure the user data loads correctly before making this check, I set a delay Added:
Copy code
val user by userVm.currentUsername.observeAsState(userVm.currentUsername.value ?: "")
LaunchedEffect(user) {
    delay(100)
    if (user.isNullOrEmpty()) {
        vm.clearLocalDatabase()
    }
}
1
issue (discovered) - App crashes when moving to the background (only when a user is logged in) Error:
Copy code
2021-06-17 13:09:30.488 3081-3081/com.lid.dailydoc E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lid.dailydoc, PID: 3081
    java.lang.RuntimeException: Parcel: unable to marshal value Note(
date=18795, noteId=80a9499d-c1d8-4029-aedf-fac20c810c52, summary=some text5, content=111, survey1=, survey2=, survey3=, owner=test, users=[], isSynced=true)
        at android.os.Parcel.writeValue(Parcel.java:1782)
        at android.os.Parcel.writeList(Parcel.java:1045)
        at android.os.Parcel.writeValue(Parcel.java:1729)
        at androidx.compose.runtime.ParcelableSnapshotMutableState.writeToParcel(ParcelableSnapshotMutableState.kt:30)
....
Changed:
Copy code
val currentNotes = rememberSaveable { mutableStateOf<List<Note>>(emptyList()) }
To:
Copy code
val currentNotes = remember { mutableStateOf<List<Note>>(emptyList()) }
1