Skip to content

Commit f0d4da7

Browse files
committed
Add reusable home fragment which accepts a list type parameter for the list of emails to be displayed
1 parent d21ffe7 commit f0d4da7

File tree

9 files changed

+142
-16
lines changed

9 files changed

+142
-16
lines changed

Reply/app/src/main/java/com/materialstudies/reply/data/Email.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.materialstudies.reply.data
1818

1919
import androidx.recyclerview.widget.DiffUtil
20+
import com.materialstudies.reply.ui.home.Mailbox
2021

2122
/**
2223
* A simple data class to represent an Email.
@@ -29,7 +30,8 @@ data class Email(
2930
val body: String = "",
3031
val attachments: List<EmailAttachment> = emptyList(),
3132
var isImportant: Boolean = false,
32-
var isStarred: Boolean = false
33+
var isStarred: Boolean = false,
34+
var mailbox: Mailbox = Mailbox.INBOX
3335
) {
3436
val senderPreview: String = "${sender.fullName} - 4 hrs ago"
3537
val hasBody: Boolean = body.isNotBlank()

Reply/app/src/main/java/com/materialstudies/reply/data/EmailStore.kt

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package com.materialstudies.reply.data
1818

1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
21+
import androidx.lifecycle.Transformations
2122
import com.materialstudies.reply.R
23+
import com.materialstudies.reply.ui.home.Mailbox
2224

2325
/**
2426
* A static data store of [Email]s.
@@ -80,7 +82,8 @@ object EmailStore {
8082
I was at the grocery store on Sunday night.. when I ran into Genie Williams! I almost didn't recognize her afer 20 years!
8183
8284
Anyway, it turns out she is on the organizing committee for the high school reunion this fall. I don't know if you were planning on going or not, but she could definitely use our help in trying to track down lots of missing alums. If you can make it, we're doing a little phone-tree party at her place next Saturday, hoping that if we can find one person, thee more will...
83-
""".trimIndent()
85+
""".trimIndent(),
86+
mailbox = Mailbox.SENT
8487
),
8588
Email(
8689
4L,
@@ -113,25 +116,111 @@ object EmailStore {
113116
listOf(AccountStore.getDefaultUserAccount()),
114117
"Recipe to try",
115118
"Raspberry Pie: We should make this pie recipe tonight! The filling is " +
116-
"very quick to put together."
119+
"very quick to put together.",
120+
mailbox = Mailbox.SENT
117121
),
118122
Email(
119123
7L,
120124
AccountStore.getContactAccountById(9L),
121125
listOf(AccountStore.getDefaultUserAccount()),
122126
"Delivered",
123127
"Your shoes should be waiting for you at home!"
128+
),
129+
Email(
130+
8L,
131+
AccountStore.getContactAccountById(13L),
132+
listOf(AccountStore.getDefaultUserAccount()),
133+
"Your update on Google Play Store is live!",
134+
"""
135+
Your update, 0.1.1, is now live on the Play Store and available for your alpha users to start testing.
136+
137+
Your alpha testers will be automatically notified. If you'd rather send them a link directly, go to your Google Play Console and follow the instructions for obtaining an open alpha testing link.
138+
""".trimIndent(),
139+
mailbox = Mailbox.TRASH
140+
),
141+
Email(
142+
9L,
143+
AccountStore.getContactAccountById(10L),
144+
listOf(AccountStore.getDefaultUserAccount()),
145+
"(No subject)",
146+
"""
147+
Hey,
148+
149+
Wanted to email and see what you thought of
150+
""".trimIndent(),
151+
mailbox = Mailbox.DRAFTS
152+
),
153+
Email(
154+
10L,
155+
AccountStore.getContactAccountById(5L),
156+
listOf(AccountStore.getDefaultUserAccount()),
157+
"Try a free TrailGo account",
158+
"""
159+
Looking for the best hiking trails in your area? TrailGo gets you on the path to the outdoors faster than you can pack a sandwich.
160+
161+
Whether you're an experienced hiker or just looking to get outside for the afternoon, there's a segment that suits you.
162+
""".trimIndent(),
163+
mailbox = Mailbox.TRASH
164+
),
165+
Email(
166+
10L,
167+
AccountStore.getContactAccountById(5L),
168+
listOf(AccountStore.getDefaultUserAccount()),
169+
"Free money",
170+
"""
171+
You've been selected as a winner in our latest raffle! To claim your prize, click on the link.
172+
""".trimIndent(),
173+
mailbox = Mailbox.SPAM
124174
)
125175
)
126176

127177
private val _emails: MutableLiveData<List<Email>> = MutableLiveData()
128-
val emails: LiveData<List<Email>>
129-
get() = _emails
178+
179+
private val inbox: LiveData<List<Email>>
180+
get() = Transformations.map(_emails) { emails ->
181+
emails.filter { it.mailbox == Mailbox.INBOX }
182+
}
183+
184+
private val starred: LiveData<List<Email>>
185+
get() = Transformations.map(_emails) { emails ->
186+
emails.filter { it.isStarred }
187+
}
188+
189+
private val sent: LiveData<List<Email>>
190+
get() = Transformations.map(_emails) { emails ->
191+
emails.filter { it.mailbox == Mailbox.SENT }
192+
}
193+
194+
private val trash: LiveData<List<Email>>
195+
get() = Transformations.map(_emails) { emails ->
196+
emails.filter { it.mailbox == Mailbox.TRASH }
197+
}
198+
199+
private val spam: LiveData<List<Email>>
200+
get() = Transformations.map(_emails) { emails ->
201+
emails.filter { it.mailbox == Mailbox.SPAM }
202+
}
203+
204+
private val drafts: LiveData<List<Email>>
205+
get() = Transformations.map(_emails) { emails ->
206+
emails.filter { it.mailbox == Mailbox.DRAFTS }
207+
}
130208

131209
init {
132210
_emails.value = allEmails
133211
}
134212

213+
fun getEmails(mailbox: Mailbox): LiveData<List<Email>> {
214+
return when (mailbox) {
215+
Mailbox.INBOX -> inbox
216+
Mailbox.STARRED -> starred
217+
Mailbox.SENT -> sent
218+
Mailbox.TRASH -> trash
219+
Mailbox.SPAM -> spam
220+
Mailbox.DRAFTS -> drafts
221+
}
222+
}
223+
135224
/**
136225
* Get an [Email] with the given [id].
137226
*/

Reply/app/src/main/java/com/materialstudies/reply/ui/MainActivity.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.materialstudies.reply.R
3434
import com.materialstudies.reply.databinding.ActivityMainBinding
3535
import com.materialstudies.reply.ui.compose.ComposeFragmentDirections
3636
import com.materialstudies.reply.ui.email.EmailFragmentArgs
37+
import com.materialstudies.reply.ui.home.HomeFragmentDirections
3738
import com.materialstudies.reply.ui.nav.AlphaSlideAction
3839
import com.materialstudies.reply.ui.nav.BottomNavDrawerFragment
3940
import com.materialstudies.reply.ui.nav.ChangeSettingsMenuStateAction
@@ -222,8 +223,7 @@ class MainActivity : AppCompatActivity(),
222223

223224
override fun onNavMenuItemClicked(item: NavigationModelItem.NavMenuItem) {
224225
// Swap the list of emails showing
225-
binding.bottomAppBarTitle.text = getString(item.titleRes)
226-
// TODO:
226+
navigateToMailbox(item)
227227
}
228228

229229
override fun onNavEmailFolderClicked(folder: NavigationModelItem.NavEmailFolder) {
@@ -247,6 +247,12 @@ class MainActivity : AppCompatActivity(),
247247
}.show(supportFragmentManager, null)
248248
}
249249

250+
private fun navigateToMailbox(item: NavigationModelItem.NavMenuItem) {
251+
binding.bottomAppBarTitle.text = getString(item.titleRes)
252+
findNavController(R.id.nav_host_fragment)
253+
.navigate(HomeFragmentDirections.actionHomeFragmentToHomeFragment(item.mailbox))
254+
}
255+
250256
private fun navigateToCompose() {
251257
supportFragmentManager.currentNavigationFragment?.setOutgoingTransitions(
252258
exitTransition = Hold().apply {

Reply/app/src/main/java/com/materialstudies/reply/ui/home/HomeFragment.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import androidx.fragment.app.Fragment
2525
import androidx.lifecycle.observe
2626
import androidx.navigation.fragment.FragmentNavigatorExtras
2727
import androidx.navigation.fragment.findNavController
28+
import androidx.navigation.fragment.navArgs
2829
import androidx.recyclerview.widget.ItemTouchHelper
2930
import com.google.android.material.transition.Hold
3031
import com.materialstudies.reply.R
@@ -39,6 +40,8 @@ import com.materialstudies.reply.util.setOutgoingTransitions
3940
*/
4041
class HomeFragment : Fragment(), EmailAdapter.EmailAdapterListener {
4142

43+
private val args: HomeFragmentArgs by navArgs()
44+
4245
private lateinit var binding: FragmentHomeBinding
4346

4447
private val emailAdapter = EmailAdapter(this)
@@ -66,7 +69,7 @@ class HomeFragment : Fragment(), EmailAdapter.EmailAdapterListener {
6669
}
6770
binding.recyclerView.adapter = emailAdapter
6871

69-
EmailStore.emails.observe(viewLifecycleOwner) {
72+
EmailStore.getEmails(args.listType).observe(viewLifecycleOwner) {
7073
emailAdapter.submitList(it)
7174
}
7275
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.materialstudies.reply.ui.home
2+
3+
/**
4+
* An enumeration of mailboxes into which emails can be placed.
5+
*/
6+
enum class Mailbox {
7+
INBOX, STARRED, SENT, TRASH, SPAM, DRAFTS
8+
}

Reply/app/src/main/java/com/materialstudies/reply/ui/nav/BottomNavDrawerFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ class BottomNavDrawerFragment :
277277
}
278278

279279
override fun onNavMenuItemClicked(item: NavigationModelItem.NavMenuItem) {
280-
if (NavigationModel.setNavigationMenuItemChecked(item.id)) close()
280+
NavigationModel.setNavigationMenuItemChecked(item.id)
281+
close()
281282
navigationListeners.forEach { it.onNavMenuItemClicked(item) }
282283
}
283284

Reply/app/src/main/java/com/materialstudies/reply/ui/nav/NavigationModel.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
2121
import com.materialstudies.reply.R
2222
import com.materialstudies.reply.data.EmailStore
23+
import com.materialstudies.reply.ui.home.Mailbox
2324

2425
/**
2526
* A class which maintains and generates a navigation list to be displayed by [NavigationAdapter].
@@ -31,37 +32,43 @@ object NavigationModel {
3132
id = 0,
3233
icon = R.drawable.ic_twotone_inbox,
3334
titleRes = R.string.navigation_inbox,
34-
checked = false
35+
checked = false,
36+
mailbox = Mailbox.INBOX
3537
),
3638
NavigationModelItem.NavMenuItem(
3739
id = 1,
3840
icon = R.drawable.ic_twotone_stars,
3941
titleRes = R.string.navigation_starred,
40-
checked = false
42+
checked = false,
43+
mailbox = Mailbox.STARRED
4144
),
4245
NavigationModelItem.NavMenuItem(
4346
id = 2,
4447
icon = R.drawable.ic_twotone_send,
4548
titleRes = R.string.navigation_sent,
46-
checked = false
49+
checked = false,
50+
mailbox = Mailbox.SENT
4751
),
4852
NavigationModelItem.NavMenuItem(
4953
id = 3,
5054
icon = R.drawable.ic_twotone_delete,
5155
titleRes = R.string.navigation_trash,
52-
checked = false
56+
checked = false,
57+
mailbox = Mailbox.TRASH
5358
),
5459
NavigationModelItem.NavMenuItem(
5560
id = 4,
5661
icon = R.drawable.ic_twotone_error,
5762
titleRes = R.string.navigation_spam,
58-
checked = false
63+
checked = false,
64+
mailbox = Mailbox.SPAM
5965
),
6066
NavigationModelItem.NavMenuItem(
6167
id = 5,
6268
icon = R.drawable.ic_twotone_drafts,
6369
titleRes = R.string.navigation_drafts,
64-
checked = false
70+
checked = false,
71+
mailbox = Mailbox.DRAFTS
6572
)
6673
)
6774

Reply/app/src/main/java/com/materialstudies/reply/ui/nav/NavigationModelItem.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.annotation.StringRes
2121
import androidx.recyclerview.widget.DiffUtil
2222
import com.materialstudies.reply.data.EmailFolder
2323
import com.materialstudies.reply.data.EmailFolderDiff
24+
import com.materialstudies.reply.ui.home.Mailbox
2425

2526
/**
2627
* A sealed class which encapsulates all objects [NavigationAdapter] is able to display.
@@ -34,6 +35,7 @@ sealed class NavigationModelItem {
3435
val id: Int,
3536
@DrawableRes val icon: Int,
3637
@StringRes val titleRes: Int,
38+
val mailbox: Mailbox,
3739
var checked: Boolean
3840
) : NavigationModelItem()
3941

Reply/app/src/main/res/navigation/navigation_graph.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@
1919
<fragment
2020
android:id="@+id/homeFragment"
2121
android:name="com.materialstudies.reply.ui.home.HomeFragment"
22-
android:label="HomeFragment" >
22+
android:label="HomeFragment">
23+
<argument
24+
android:name="listType"
25+
app:argType="com.materialstudies.reply.ui.home.EmailListType"
26+
android:defaultValue="INBOX"/>
2327
<action
2428
android:id="@+id/action_homeFragment_to_emailFragment"
2529
app:destination="@id/emailFragment" />
30+
<action
31+
android:id="@+id/action_homeFragment_to_homeFragment"
32+
app:destination="@+id/homeFragment"
33+
app:launchSingleTop="true"/>
2634
</fragment>
2735
<fragment
2836
android:id="@+id/emailFragment"

0 commit comments

Comments
 (0)