Fix IllegalStateException when inflating menu
This commit is contained in:
@@ -19,8 +19,7 @@ import timber.log.Timber
|
|||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
private lateinit var binding: MainActivityBinding
|
private lateinit var binding: MainActivityBinding
|
||||||
private val authViewModel by viewModels<AuthenticationViewModel>()
|
private val authViewModel by viewModels<AuthenticationViewModel>()
|
||||||
private val authenticationState: AuthenticationState
|
private var lastAuthenticationState: AuthenticationState? = null
|
||||||
get() = authViewModel.currentAuthenticationState
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -57,14 +56,15 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private fun onAuthStateUpdate(authState: AuthenticationState) {
|
private fun onAuthStateUpdate(authState: AuthenticationState) {
|
||||||
Timber.v("onAuthStateUpdate() called with: it = $authState")
|
Timber.v("onAuthStateUpdate() called with: it = $authState")
|
||||||
|
lastAuthenticationState = authState
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
Timber.v("onCreateOptionsMenu() called with: menu = $menu")
|
Timber.v("onCreateOptionsMenu() called with: menu = $menu")
|
||||||
menuInflater.inflate(R.menu.main_toolbar, menu)
|
menuInflater.inflate(R.menu.main_toolbar, menu)
|
||||||
menu.findItem(R.id.logout).isVisible = authenticationState == AUTHORIZED
|
menu.findItem(R.id.logout).isVisible = lastAuthenticationState == AUTHORIZED
|
||||||
menu.findItem(R.id.login).isVisible = authenticationState == UNAUTHORIZED
|
menu.findItem(R.id.login).isVisible = lastAuthenticationState == UNAUTHORIZED
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.login -> {
|
R.id.login -> {
|
||||||
authViewModel.login()
|
authViewModel.enableLoginRequest()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
else -> super.onOptionsItemSelected(item)
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ class AuthenticationViewModel @Inject constructor(
|
|||||||
flow = authRepo.isAuthorizedFlow,
|
flow = authRepo.isAuthorizedFlow,
|
||||||
transform = AuthenticationState::determineState
|
transform = AuthenticationState::determineState
|
||||||
).asLiveData()
|
).asLiveData()
|
||||||
val currentAuthenticationState: AuthenticationState
|
|
||||||
get() = checkNotNull(authenticationState.value) { "Auth state flow mustn't be null" }
|
|
||||||
|
|
||||||
fun authenticate(username: String, password: String): LiveData<Result<Unit>> {
|
fun authenticate(username: String, password: String): LiveData<Result<Unit>> {
|
||||||
Timber.v("authenticate() called with: username = $username, password = $password")
|
Timber.v("authenticate() called with: username = $username, password = $password")
|
||||||
|
|||||||
Reference in New Issue
Block a user