-
Notifications
You must be signed in to change notification settings - Fork 58
/
MainActivity.kt
319 lines (288 loc) · 12.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2020 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.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.annotation.MenuRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import com.google.android.material.transition.MaterialElevationScale
import com.google.android.material.transition.MaterialFadeThrough
import com.google.android.material.transition.MaterialSharedAxis
import com.materialstudies.reply.R
import com.materialstudies.reply.data.EmailStore
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.home.HomeFragmentDirections
import com.materialstudies.reply.ui.home.Mailbox
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.NavigationAdapter
import com.materialstudies.reply.ui.nav.NavigationModelItem
import com.materialstudies.reply.ui.nav.ShowHideFabStateAction
import com.materialstudies.reply.ui.search.SearchFragmentDirections
import com.materialstudies.reply.util.contentView
import kotlin.LazyThreadSafetyMode.NONE
class MainActivity : AppCompatActivity(),
Toolbar.OnMenuItemClickListener,
NavController.OnDestinationChangedListener,
NavigationAdapter.NavigationAdapterListener {
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
val currentNavigationFragment: Fragment?
get() = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
?.childFragmentManager
?.fragments
?.first()
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.apply {
setShowMotionSpecResource(R.animator.fab_show)
setHideMotionSpecResource(R.animator.fab_hide)
setOnClickListener {
navigateToCompose()
}
}
bottomNavDrawer.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))
addNavigationListener(this@MainActivity)
}
// Set up the BottomAppBar menu
binding.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()
}
R.id.searchFragment -> {
currentEmailId = -1
setBottomAppBarForSearch()
}
}
}
/**
* 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() {
hideBottomAppBar()
}
private fun setBottomAppBarForSearch() {
hideBottomAppBar()
binding.fab.hide()
}
private fun hideBottomAppBar() {
binding.run {
bottomAppBar.performHide()
// Get a handle on the animator that hides the bottom app bar so we can wait to hide
// the fab and bottom app bar until after it's exit animation finishes.
bottomAppBar.animate().setListener(object : AnimatorListenerAdapter() {
var isCanceled = false
override fun onAnimationEnd(animation: Animator?) {
if (isCanceled) return
// Hide the BottomAppBar to avoid it showing above the keyboard
// when composing a new email.
bottomAppBar.visibility = View.GONE
fab.visibility = View.INVISIBLE
}
override fun onAnimationCancel(animation: Animator?) {
isCanceled = true
}
})
}
}
override fun onNavMenuItemClicked(item: NavigationModelItem.NavMenuItem) {
// Swap the list of emails for the given mailbox
navigateToHome(item.titleRes, item.mailbox)
}
override fun onNavEmailFolderClicked(folder: NavigationModelItem.NavEmailFolder) {
// Do nothing
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_settings -> {
bottomNavDrawer.close()
showDarkThemeMenu()
}
R.id.menu_search -> navigateToSearch()
R.id.menu_email_star -> {
EmailStore.update(currentEmailId) { isStarred = !isStarred }
}
R.id.menu_email_delete -> {
EmailStore.delete(currentEmailId)
findNavController(R.id.nav_host_fragment).popBackStack()
}
}
return true
}
private fun showDarkThemeMenu() {
MenuBottomSheetDialogFragment
.newInstance(R.menu.dark_theme_bottom_sheet_menu)
.show(supportFragmentManager, null)
}
fun navigateToHome(@StringRes titleRes: Int, mailbox: Mailbox) {
binding.bottomAppBarTitle.text = getString(titleRes)
currentNavigationFragment?.apply {
exitTransition = MaterialFadeThrough().apply {
duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
}
val directions = HomeFragmentDirections.actionGlobalHomeFragment(mailbox)
findNavController(R.id.nav_host_fragment).navigate(directions)
}
private fun navigateToCompose() {
currentNavigationFragment?.apply {
exitTransition = MaterialElevationScale(false).apply {
duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
reenterTransition = MaterialElevationScale(true).apply {
duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
}
val directions = ComposeFragmentDirections.actionGlobalComposeFragment(currentEmailId)
findNavController(R.id.nav_host_fragment).navigate(directions)
}
private fun navigateToSearch() {
currentNavigationFragment?.apply {
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true).apply {
duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false).apply {
duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
}
val directions = SearchFragmentDirections.actionGlobalSearchFragment()
findNavController(R.id.nav_host_fragment).navigate(directions)
}
/**
* 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
}
}