|
1 | 1 | package com.materialstudies.reply.ui
|
2 | 2 |
|
3 |
| -import android.content.Context |
4 | 3 | import androidx.appcompat.app.AppCompatActivity
|
5 | 4 | import android.os.Bundle
|
6 | 5 | import android.view.MenuItem
|
7 |
| -import android.view.View |
8 |
| -import android.widget.LinearLayout |
9 |
| -import android.widget.Toast |
10 | 6 | import androidx.appcompat.app.AppCompatDelegate
|
11 |
| -import androidx.appcompat.widget.AppCompatImageView |
12 |
| -import androidx.appcompat.widget.AppCompatTextView |
13 | 7 | import androidx.appcompat.widget.Toolbar
|
14 |
| -import androidx.coordinatorlayout.widget.CoordinatorLayout |
15 |
| -import androidx.core.view.ViewCompat |
16 |
| -import androidx.core.view.WindowInsetsCompat |
17 |
| -import com.google.android.material.bottomappbar.BottomAppBar |
18 |
| -import com.google.android.material.floatingactionbutton.FloatingActionButton |
19 | 8 | import com.materialstudies.reply.R
|
| 9 | +import com.materialstudies.reply.databinding.ActivityMainBinding |
20 | 10 | import com.materialstudies.reply.ui.nav.AlphaSlideAction
|
21 |
| -import com.materialstudies.reply.ui.nav.BottomNavigationDrawer |
22 | 11 | import com.materialstudies.reply.ui.nav.ChangeSettingsMenuStateAction
|
23 | 12 | import com.materialstudies.reply.ui.nav.QuarterRotateSlideAction
|
24 | 13 | import com.materialstudies.reply.ui.nav.ShowHideFabStateAction
|
| 14 | +import com.materialstudies.reply.util.contentView |
25 | 15 |
|
26 | 16 | class MainActivity : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
|
27 | 17 |
|
28 |
| - private lateinit var coordinatorLayout: CoordinatorLayout |
29 |
| - private lateinit var bottomAppBar: BottomAppBar |
30 |
| - private lateinit var bottomAppBarContentContainer: LinearLayout |
31 |
| - private lateinit var bottomAppBarChevron: AppCompatImageView |
32 |
| - private lateinit var bottomAppBarTitleTextView: AppCompatTextView |
33 |
| - private lateinit var bottomNavigationDrawer: BottomNavigationDrawer |
34 |
| - private lateinit var fab: FloatingActionButton |
| 18 | + private val binding: ActivityMainBinding by contentView(R.layout.activity_main) |
35 | 19 |
|
36 | 20 | override fun onCreate(savedInstanceState: Bundle?) {
|
37 | 21 | super.onCreate(savedInstanceState)
|
38 |
| - setContentView(R.layout.activity_main) |
39 |
| - |
40 |
| - // Draw behind all system bars |
41 |
| - val decor = window.decorView |
42 |
| - val flags = decor.systemUiVisibility or |
43 |
| - View.SYSTEM_UI_FLAG_LAYOUT_STABLE or |
44 |
| - View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or |
45 |
| - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
46 |
| - decor.systemUiVisibility = flags |
47 |
| - |
48 |
| - coordinatorLayout = findViewById(R.id.coordinator_layout) |
49 |
| - bottomAppBar = findViewById(R.id.bottom_app_bar) |
50 |
| - bottomAppBarContentContainer = findViewById(R.id.bottom_app_bar_content_container) |
51 |
| - bottomAppBarChevron = findViewById(R.id.bottom_app_bar_chevron) |
52 |
| - bottomAppBarTitleTextView = findViewById(R.id.bottom_app_bar_title) |
53 |
| - bottomNavigationDrawer = findViewById(R.id.bottom_navigation_drawer) |
54 |
| - fab = findViewById(R.id.fab) |
55 |
| - |
56 |
| - ViewCompat.setOnApplyWindowInsetsListener(coordinatorLayout) { _, insets -> |
57 |
| - handleApplyWindowInsets(insets) |
58 |
| - } |
59 |
| - |
60 | 22 | setUpBottomNavigationAndFab()
|
61 | 23 | }
|
62 | 24 |
|
63 |
| - fun handleMenuBottomSheetItemClicked(entry: MenuBottomSheetDialogFragment.MenuEntry) { |
64 |
| - Toast.makeText(this, getString(entry.title), Toast.LENGTH_SHORT).show() |
65 |
| - } |
66 |
| - |
67 |
| - private fun handleApplyWindowInsets(insets: WindowInsetsCompat): WindowInsetsCompat { |
68 |
| - bottomAppBar.layoutParams.height = getBottomAppBarHeight(this, insets) |
69 |
| - return insets |
70 |
| - } |
71 |
| - |
72 | 25 | private fun setUpBottomNavigationAndFab() {
|
73 |
| - fab.setShowMotionSpecResource(R.animator.fab_show) |
74 |
| - fab.setHideMotionSpecResource(R.animator.fab_hide) |
| 26 | + binding.fab.setShowMotionSpecResource(R.animator.fab_show) |
| 27 | + binding.fab.setHideMotionSpecResource(R.animator.fab_hide) |
75 | 28 |
|
76 |
| - bottomNavigationDrawer.apply { |
77 |
| - addOnSlideAction(QuarterRotateSlideAction(bottomAppBarChevron)) |
78 |
| - addOnSlideAction(AlphaSlideAction(bottomAppBarTitleTextView, true)) |
79 |
| - addOnStateChangedAction(ShowHideFabStateAction(fab)) |
| 29 | + binding.bottomNavigationDrawer.apply { |
| 30 | + addOnSlideAction(QuarterRotateSlideAction(binding.bottomAppBarChevron)) |
| 31 | + addOnSlideAction(AlphaSlideAction(binding.bottomAppBarTitle, true)) |
| 32 | + addOnStateChangedAction(ShowHideFabStateAction(binding.fab)) |
80 | 33 | addOnStateChangedAction(ChangeSettingsMenuStateAction { showSettings ->
|
81 |
| - bottomAppBar.replaceMenu(if (showSettings) { |
| 34 | + binding.bottomAppBar.replaceMenu(if (showSettings) { |
82 | 35 | R.menu.bottom_app_bar_settings_menu
|
83 | 36 | } else {
|
84 | 37 | R.menu.bottom_app_bar_home_menu
|
85 | 38 | })
|
86 | 39 | })
|
87 | 40 | }
|
88 | 41 |
|
89 |
| - bottomAppBar.setNavigationOnClickListener { bottomNavigationDrawer.toggle() } |
90 |
| - bottomAppBar.setOnMenuItemClickListener(this) |
91 |
| - bottomAppBarContentContainer.setOnClickListener { bottomNavigationDrawer.toggle() } |
| 42 | + binding.bottomAppBar.setNavigationOnClickListener { |
| 43 | + binding.bottomNavigationDrawer.toggle() |
| 44 | + } |
| 45 | + binding.bottomAppBar.setOnMenuItemClickListener(this) |
| 46 | + binding.bottomAppBarContentContainer.setOnClickListener { |
| 47 | + binding.bottomNavigationDrawer.toggle() |
| 48 | + } |
92 | 49 | }
|
93 | 50 |
|
94 | 51 | override fun onMenuItemClick(item: MenuItem?): Boolean {
|
95 | 52 | when (item?.itemId) {
|
96 |
| - R.id.menu_theme -> toggleTheme() |
| 53 | + R.id.menu_theme -> { |
| 54 | + binding.bottomNavigationDrawer.close() |
| 55 | + showDarkThemeMenu() |
| 56 | + } |
97 | 57 | }
|
98 | 58 | return true
|
99 | 59 | }
|
100 | 60 |
|
101 |
| - private fun toggleTheme() { |
102 |
| - delegate.localNightMode = if (delegate.localNightMode == AppCompatDelegate.MODE_NIGHT_YES) { |
103 |
| - AppCompatDelegate.MODE_NIGHT_NO |
104 |
| - } else { |
105 |
| - AppCompatDelegate.MODE_NIGHT_YES |
106 |
| - } |
| 61 | + private fun showDarkThemeMenu() { |
| 62 | + MenuBottomSheetDialogFragment(R.menu.dark_theme_bottom_sheet_menu) { |
| 63 | + onDarkThemeMenuItemSelected(it.itemId) |
| 64 | + }.show(supportFragmentManager, null) |
107 | 65 | }
|
108 | 66 |
|
109 |
| - companion object { |
110 |
| - fun getBottomAppBarHeight(context: Context, insets: WindowInsetsCompat): Int { |
111 |
| - return context.resources.getDimensionPixelSize( |
112 |
| - R.dimen.bottom_app_bar_height |
113 |
| - ) + insets.systemWindowInsetBottom |
| 67 | + private fun onDarkThemeMenuItemSelected(itemId: Int): Boolean { |
| 68 | + val nightMode = when (itemId) { |
| 69 | + R.id.menu_light -> AppCompatDelegate.MODE_NIGHT_NO |
| 70 | + R.id.menu_dark -> AppCompatDelegate.MODE_NIGHT_YES |
| 71 | + R.id.menu_battery_saver -> AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY |
| 72 | + R.id.menu_system_default -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM |
| 73 | + else -> return false |
114 | 74 | }
|
| 75 | + |
| 76 | + delegate.localNightMode = nightMode |
| 77 | + return true |
115 | 78 | }
|
116 | 79 | }
|
0 commit comments