-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
59 lines (53 loc) · 1.89 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ru.netology.nework
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import dagger.hilt.android.AndroidEntryPoint
import ru.netology.nework.auth.AppAuth
import ru.netology.nework.viewmodel.AuthViewModel
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var appAuth: AppAuth
private val authViewModel: AuthViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
authViewModel.state.observe(this) {
invalidateOptionsMenu()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_auth, menu)
menu.let {
it.setGroupVisible(R.id.authorized, authViewModel.authorized)
it.setGroupVisible(R.id.unauthorized, !authViewModel.authorized)
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.logOut -> {
if (findNavController(R.id.fragment_container).currentDestination?.id == R.id.newPostFragment) {
false
} else {
appAuth.removeAuth()
true
}
}
R.id.signIn -> {
findNavController(R.id.fragment_container).navigate(R.id.action_global_fragment_sing_in)
true
}
R.id.signUp -> {
findNavController(R.id.fragment_container).navigate(R.id.action_global_signUpFragment)
true
}
else -> super.onOptionsItemSelected(item)
}
}
}