Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codelab instructions leads to bug in app #27

Open
hmrcodenz opened this issue Jan 9, 2025 · 0 comments
Open

Codelab instructions leads to bug in app #27

hmrcodenz opened this issue Jan 9, 2025 · 0 comments

Comments

@hmrcodenz
Copy link

URL of codelab:
https://developer.android.com/codelabs/basic-android-kotlin-compose-datastore?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-6-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-datastore#6

Specify the language of the codelab if it is not English:
english

In which task and step of the codelab can this issue be found?
task 6 and step 6

Describe the problem

I have followed the codelab and written the code as instructed.
When I load the app when the preference IS_LINEAR_LAYOUT is set to false, the app displays the linear layout for a second and then the grid.
This is fixed in the solution code but the steps for the codelab code instructions need updating.

The differing code is in DessertReseaseViewModel.kt
Codelab code with issue:

// UI states access for various [DessertReleaseUiState]
    val uiState: StateFlow<DessertReleaseUiState> =
        userPreferencesRepository.isLinearLayout.map { isLinearLayout ->
            DessertReleaseUiState(isLinearLayout)
        }
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000L),
            initialValue = DessertReleaseUiState()
        )

Solution code:

val uiState: StateFlow<DessertReleaseUiState> =
        userPreferencesRepository.isLinearLayout.map { isLinearLayout ->
            DessertReleaseUiState(isLinearLayout)
        }.stateIn(
            scope = viewModelScope,
            // Flow is set to emits value for when app is on the foreground
            // 5 seconds stop delay is added to ensure it flows continuously
            // for cases such as configuration change
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = runBlocking {
                DessertReleaseUiState(
                    isLinearLayout = userPreferencesRepository.isLinearLayout.first()
                )
            }
        )

I can see it's the initialValue code that needs to be changed.

Steps to reproduce?

  1. Take the starter code and write the code that the codelab tells you to write.
  2. Open the app, change the app to Grid mode
  3. close the app and reopen
  4. you can see it loads linear and then grid mode

Versions
Android Studio version:
Android Studio Ladybug | 2024.2.1 Patch 3

API version of the emulator: Using my phone to run it, Android 14

Additional information
Include screenshots if they would be useful in clarifying the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant