This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathMainActivity.kt
226 lines (205 loc) · 9 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.materialstudies.reply.ui
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.annotation.MenuRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import com.materialstudies.reply.R
import com.materialstudies.reply.databinding.ActivityMainBinding
import com.materialstudies.reply.ui.compose.ComposeFragmentDirections
import com.materialstudies.reply.ui.email.EmailFragmentArgs
import com.materialstudies.reply.ui.nav.AlphaSlideAction
import com.materialstudies.reply.ui.nav.BottomNavDrawerFragment
import com.materialstudies.reply.ui.nav.ChangeSettingsMenuStateAction
import com.materialstudies.reply.ui.nav.HalfClockwiseRotateSlideAction
import com.materialstudies.reply.ui.nav.HalfCounterClockwiseRotateSlideAction
import com.materialstudies.reply.ui.nav.ShowHideFabStateAction
import com.materialstudies.reply.util.contentView
import kotlin.LazyThreadSafetyMode.NONE
class MainActivity : AppCompatActivity(),
Toolbar.OnMenuItemClickListener,
NavController.OnDestinationChangedListener {
private val binding: ActivityMainBinding by contentView(R.layout.activity_main)
private val bottomNavDrawer: BottomNavDrawerFragment? by lazy(NONE) {
supportFragmentManager.findFragmentById(R.id.bottom_nav_drawer) as BottomNavDrawerFragment?
}
// Keep track of the current Email being viewed, if any, in order to pass the correct email id
// to ComposeFragment when this Activity's FAB is clicked.
private var currentEmailId = -1L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setUpBottomNavigationAndFab()
}
private fun setUpBottomNavigationAndFab() {
// Wrap binding.run to ensure ContentViewBindingDelegate is calling this Activity's
// setContentView before accessing views
binding.run {
findNavController(R.id.nav_host_fragment).addOnDestinationChangedListener(
this@MainActivity
)
}
// Set a custom animation for showing and hiding the FAB
binding.fab?.let { fab ->
fab.apply {
setShowMotionSpecResource(R.animator.fab_show)
setHideMotionSpecResource(R.animator.fab_hide)
setOnClickListener {
findNavController(R.id.nav_host_fragment)
.navigate(ComposeFragmentDirections.actionGlobalComposeFragment(currentEmailId))
}
}
}
bottomNavDrawer?.let { bottomNavBar ->
bottomNavBar.apply {
addOnSlideAction(HalfClockwiseRotateSlideAction(binding.bottomAppBarChevron!!))
addOnSlideAction(AlphaSlideAction(binding.bottomAppBarTitle!!, true))
addOnStateChangedAction(ShowHideFabStateAction(binding.fab!!))
addOnStateChangedAction(ChangeSettingsMenuStateAction { showSettings ->
// Toggle between the current destination's BAB menu and the menu which should
// be displayed when the BottomNavigationDrawer is open.
binding.bottomAppBar?.replaceMenu(if (showSettings) {
R.menu.bottom_app_bar_settings_menu
} else {
getBottomAppBarMenuForDestination()
})
})
addOnSandwichSlideAction(HalfCounterClockwiseRotateSlideAction(binding.bottomAppBarChevron!!))
}
}
// Set up the BottomAppBar menu
binding.bottomAppBar?.let { bottomAppBar ->
bottomAppBar.apply {
setNavigationOnClickListener {
bottomNavDrawer?.toggle()
}
setOnMenuItemClickListener(this@MainActivity)
}
}
// Set up the BottomNavigationDrawer's open/close affordance
binding.bottomAppBarContentContainer?.setOnClickListener {
bottomNavDrawer?.toggle()
}
}
override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?
) {
// Set the currentEmail being viewed so when the FAB is pressed, the correct email
// reply is created. In a real app, this should be done in a ViewModel but is done
// here to keep things simple. Here we're also setting the configuration of the
// BottomAppBar and FAB based on the current destination.
when (destination.id) {
R.id.homeFragment -> {
currentEmailId = -1
setBottomAppBarForHome(getBottomAppBarMenuForDestination(destination))
}
R.id.emailFragment -> {
currentEmailId =
if (arguments == null) -1 else EmailFragmentArgs.fromBundle(arguments).emailId
setBottomAppBarForEmail(getBottomAppBarMenuForDestination(destination))
}
R.id.composeFragment -> {
currentEmailId = -1
setBottomAppBarForCompose()
}
}
}
/**
* Helper function which returns the menu which should be displayed for the current
* destination.
*
* Used both when the destination has changed, centralizing destination-to-menu mapping, as
* well as switching between the alternate menu used when the BottomNavigationDrawer is
* open and closed.
*/
@MenuRes
private fun getBottomAppBarMenuForDestination(destination: NavDestination? = null): Int {
val dest = destination ?: findNavController(R.id.nav_host_fragment).currentDestination
return when (dest?.id) {
R.id.homeFragment -> R.menu.bottom_app_bar_home_menu
R.id.emailFragment -> R.menu.bottom_app_bar_email_menu
else -> R.menu.bottom_app_bar_home_menu
}
}
private fun setBottomAppBarForHome(@MenuRes menuRes: Int) {
binding.run {
fab?.setImageState(intArrayOf(-android.R.attr.state_activated), true)
bottomAppBar?.visibility = View.VISIBLE
bottomAppBar?.replaceMenu(menuRes)
fab?.contentDescription = getString(R.string.fab_compose_email_content_description)
bottomAppBarTitle?.visibility = View.VISIBLE
bottomAppBar?.performShow()
fab?.show()
}
}
private fun setBottomAppBarForEmail(@MenuRes menuRes: Int) {
binding.run {
fab?.setImageState(intArrayOf(android.R.attr.state_activated), true)
bottomAppBar?.visibility = View.VISIBLE
bottomAppBar?.replaceMenu(menuRes)
fab?.contentDescription = getString(R.string.fab_reply_email_content_description)
bottomAppBarTitle?.visibility = View.INVISIBLE
bottomAppBar?.performShow()
fab?.show()
}
}
private fun setBottomAppBarForCompose() {
binding.run {
bottomAppBar?.performHide()
fab?.hide()
// Hide the BottomAppBar to avoid it showing above the keyboard
// when composing a new email.
bottomAppBar?.visibility = View.GONE
}
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_settings -> {
bottomNavDrawer?.close()
showDarkThemeMenu()
}
}
return true
}
private fun showDarkThemeMenu() {
MenuBottomSheetDialogFragment(R.menu.dark_theme_bottom_sheet_menu) {
onDarkThemeMenuItemSelected(it.itemId)
}.show(supportFragmentManager, null)
}
/**
* Set this Activity's night mode based on a user's in-app selection.
*/
private fun onDarkThemeMenuItemSelected(itemId: Int): Boolean {
val nightMode = when (itemId) {
R.id.menu_light -> AppCompatDelegate.MODE_NIGHT_NO
R.id.menu_dark -> AppCompatDelegate.MODE_NIGHT_YES
R.id.menu_battery_saver -> AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
R.id.menu_system_default -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> return false
}
delegate.localNightMode = nightMode
return true
}
}