android / codelab-android-dagger

Apache License 2.0
181 stars 157 forks source link

Strange sample code? #21

Closed tatsuyafujisaki closed 4 years ago

tatsuyafujisaki commented 4 years ago

The following code from the codelab puzzled me because the private function userJustLoggedIn() was not called from anywhere.

@Singleton
class UserManager @Inject constructor(...) {
    //Remove line
    var userDataRepository: UserDataRepository? = null

    var userComponent: UserComponent? = null
          private set

    fun isUserLoggedIn() = userComponent != null

    fun logout() {
        userComponent = null
    }

    private fun userJustLoggedIn() {
        userComponent = userComponentFactory.create()
    }
}

After checking the actual code, I realized that the above code was slightly oversimplified. It might be nicer if simplification was implied in the explanation (e.g. use an ellipsis // …).