-
Notifications
You must be signed in to change notification settings - Fork 58
/
HomeFragment.kt
118 lines (99 loc) · 4.21 KB
/
HomeFragment.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
/*
* 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.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.lifecycle.observe
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.ItemTouchHelper
import com.materialstudies.reply.R
import com.materialstudies.reply.data.Email
import com.materialstudies.reply.data.EmailStore
import com.materialstudies.reply.databinding.FragmentHomeBinding
import com.materialstudies.reply.ui.MainActivity
import com.materialstudies.reply.ui.MenuBottomSheetDialogFragment
import com.materialstudies.reply.ui.nav.NavigationModel
/**
* A [Fragment] that displays a list of emails.
*/
class HomeFragment : Fragment(), EmailAdapter.EmailAdapterListener {
private val args: HomeFragmentArgs by navArgs()
private lateinit var binding: FragmentHomeBinding
private val emailAdapter = EmailAdapter(this)
// An on back pressed callback that handles replacing any non-Inbox HomeFragment with inbox
// on back pressed.
private val nonInboxOnBackCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
NavigationModel.setNavigationMenuItemChecked(NavigationModel.INBOX_ID)
(requireActivity() as MainActivity)
.navigateToHome(R.string.navigation_inbox, Mailbox.INBOX);
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// TODO: Set up MaterialFadeThrough enterTransition.
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// TODO: Set up postponed enter transition.
// Only enable the on back callback if this home fragment is a mailbox other than Inbox.
// This is to make sure we always navigate back to Inbox before exiting the app.
nonInboxOnBackCallback.isEnabled = args.mailbox != Mailbox.INBOX
requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
nonInboxOnBackCallback
)
binding.recyclerView.apply {
val itemTouchHelper = ItemTouchHelper(ReboundingSwipeActionCallback())
itemTouchHelper.attachToRecyclerView(this)
adapter = emailAdapter
}
binding.recyclerView.adapter = emailAdapter
EmailStore.getEmails(args.mailbox).observe(viewLifecycleOwner) {
emailAdapter.submitList(it)
}
}
override fun onEmailClicked(cardView: View, email: Email) {
// TODO: Set up MaterialElevationScale transition as exit and reenter transitions.
val directions = HomeFragmentDirections.actionHomeFragmentToEmailFragment(email.id)
findNavController().navigate(directions)
}
override fun onEmailLongPressed(email: Email): Boolean {
MenuBottomSheetDialogFragment
.newInstance(R.menu.email_bottom_sheet_menu)
.show(parentFragmentManager, null)
return true
}
override fun onEmailStarChanged(email: Email, newValue: Boolean) {
EmailStore.update(email.id) { isStarred = newValue }
}
override fun onEmailArchived(email: Email) {
EmailStore.delete(email.id)
}
}