Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit e577afe

Browse files
committed
Sandwiching account picker
Change-Id: I04fd17cc1718f9d096e9ea7896890e7918fef25c
1 parent 313374d commit e577afe

19 files changed

+576
-103
lines changed

app/src/main/java/com/materialstudies/reply/data/Account.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,32 @@
1717
package com.materialstudies.reply.data
1818

1919
import androidx.annotation.DrawableRes
20+
import androidx.recyclerview.widget.DiffUtil
21+
import com.materialstudies.reply.R
2022

2123
/**
22-
* An object which represents a person/account.
24+
* An object which represents an account which can belong to a user. A single user can have
25+
* multiple accounts.
2326
*/
2427
data class Account(
2528
val id: Long,
29+
val uid: Long,
2630
val firstName: String,
2731
val lastName: String,
2832
val email: String,
29-
@DrawableRes val avatar: Int
33+
@DrawableRes val avatar: Int,
34+
var isCurrentAccount: Boolean = false
3035
) {
3136
val fullName: String = "$firstName $lastName"
32-
}
37+
@DrawableRes val checkedIcon: Int = if (isCurrentAccount) R.drawable.ic_done_on_branded else 0
38+
}
39+
40+
object AccountDiffCallback : DiffUtil.ItemCallback<Account>() {
41+
override fun areItemsTheSame(oldItem: Account, newItem: Account) =
42+
oldItem.email == newItem.email
43+
override fun areContentsTheSame(oldItem: Account, newItem: Account) =
44+
oldItem.firstName == newItem.firstName
45+
&& oldItem.lastName == newItem.lastName
46+
&& oldItem.avatar == newItem.avatar
47+
&& oldItem.isCurrentAccount == newItem.isCurrentAccount
48+
}

app/src/main/java/com/materialstudies/reply/data/AccountStore.kt

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.materialstudies.reply.data
1818

19+
import androidx.lifecycle.LiveData
20+
import androidx.lifecycle.MutableLiveData
1921
import com.materialstudies.reply.R
2022

2123
/**
@@ -24,115 +26,162 @@ import com.materialstudies.reply.R
2426
*/
2527
object AccountStore {
2628

27-
private val userAccounts = listOf(
29+
private val allUserAccounts = mutableListOf(
2830
Account(
31+
1L,
2932
0L,
3033
"Jeff",
3134
"Hansen",
3235
33-
R.drawable.avatar_10
36+
R.drawable.avatar_10,
37+
true
3438
),
3539
Account(
40+
2L,
3641
0L,
3742
"Jeff",
3843
"H",
39-
40-
R.drawable.avatar_10
44+
45+
R.drawable.avatar_2
46+
),
47+
Account(
48+
3L,
49+
0L,
50+
"Jeff",
51+
"Hansen",
52+
53+
R.drawable.avatar_9
4154
)
4255
)
4356

44-
private val userContactAccounts = listOf(
57+
private val allUserContactAccounts = listOf(
4558
Account(
59+
4L,
4660
1L,
4761
"Tracy",
4862
"Alvarez",
4963
50-
R.drawable.avatar_4
64+
R.drawable.avatar_1
5165
),
5266
Account(
67+
5L,
5368
2L,
5469
"Allison",
5570
"Trabucco",
5671
57-
R.drawable.avatar_7
72+
R.drawable.avatar_3
5873
),
5974
Account(
75+
6L,
6076
3L,
6177
"Ali",
6278
"Connors",
6379
6480
R.drawable.avatar_5
6581
),
6682
Account(
83+
7L,
6784
4L,
6885
"Alberto",
6986
"Williams",
7087
71-
R.drawable.avatar_8
88+
R.drawable.avatar_0
7289
),
7390
Account(
91+
8L,
7492
5L,
7593
"Kim",
7694
"Alen",
7795
78-
R.drawable.avatar_9
96+
R.drawable.avatar_7
7997
),
8098
Account(
99+
9L,
81100
6L,
82101
"Google",
83102
"Express",
84103
85104
R.drawable.avatar_express
86105
),
87106
Account(
107+
10L,
88108
7L,
89109
"Sandra",
90110
"Adams",
91111
92112
R.drawable.avatar_2
93113
),
94114
Account(
115+
11L,
95116
8L,
96117
"Trevor",
97118
"Hansen",
98119
99-
R.drawable.avatar_3
120+
R.drawable.avatar_8
100121
),
101122
Account(
123+
12L,
102124
9L,
103-
"Britta",
125+
"Sean",
104126
"Holt",
105-
"bholt@gmail.com",
106-
R.drawable.avatar_4
127+
"sholt@gmail.com",
128+
R.drawable.avatar_6
107129
),
108130
Account(
131+
13L,
109132
10L,
110133
"Frank",
111134
"Hawkins",
112135
113-
R.drawable.avatar_6
136+
R.drawable.avatar_4
114137
)
115138
)
116139

140+
private val _userAccounts: MutableLiveData<List<Account>> = MutableLiveData()
141+
val userAccounts: LiveData<List<Account>>
142+
get() = _userAccounts
143+
144+
init {
145+
postUpdateUserAccountsList()
146+
}
147+
117148
/**
118149
* Get the current user's default account.
119150
*/
120-
fun getDefaultUserAccount() = userAccounts.first()
151+
fun getDefaultUserAccount() = allUserAccounts.first()
121152

122153
/**
123154
* Get all [Account]s owned by the current user.
124155
*/
125-
fun getUserAccounts() = userAccounts
156+
fun getAllUserAccounts() = allUserAccounts
126157

127158
/**
128159
* Whether or not the given [accountId] is an account owned by the current user.
129160
*/
130-
fun isUserAccount(accountId: Long): Boolean = userAccounts.any { it.id == accountId }
161+
fun isUserAccount(uid: Long): Boolean = allUserAccounts.any { it.uid == uid }
162+
163+
fun setCurrentUserAccount(accountId: Long): Boolean {
164+
var updated = false
165+
allUserAccounts.forEachIndexed { index, account ->
166+
val shouldCheck = account.id == accountId
167+
if (account.isCurrentAccount != shouldCheck) {
168+
allUserAccounts[index] = account.copy(isCurrentAccount = shouldCheck)
169+
updated = true
170+
}
171+
}
172+
if (updated) postUpdateUserAccountsList()
173+
return updated
174+
}
175+
176+
private fun postUpdateUserAccountsList() {
177+
val newList = allUserAccounts.toList()
178+
_userAccounts.value = newList
179+
}
131180

132181
/**
133182
* Get the contact of the current user with the given [accountId].
134183
*/
135184
fun getContactAccountById(accountId: Long): Account {
136-
return userContactAccounts.firstOrNull { it.id == accountId } ?: userContactAccounts.first()
185+
return allUserContactAccounts.firstOrNull { it.id == accountId } ?: allUserContactAccounts.first()
137186
}
138187
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data class Email(
3939
.map { it.firstName }
4040
.fold("") { name, acc -> "$acc, $name" }
4141
val nonUserAccountRecipients = recipients
42-
.filterNot { AccountStore.isUserAccount(it.id) }
42+
.filterNot { AccountStore.isUserAccount(it.uid) }
4343
}
4444

4545
object EmailDiffCallback : DiffUtil.ItemCallback<Email>() {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object EmailStore {
2828
private val allEmails = mutableListOf(
2929
Email(
3030
0L,
31-
AccountStore.getContactAccountById(6L),
31+
AccountStore.getContactAccountById(9L),
3232
listOf(AccountStore.getDefaultUserAccount()),
3333
"Package shipped!",
3434
"""
@@ -42,7 +42,7 @@ object EmailStore {
4242
),
4343
Email(
4444
1L,
45-
AccountStore.getContactAccountById(3L),
45+
AccountStore.getContactAccountById(6L),
4646
listOf(AccountStore.getDefaultUserAccount()),
4747
"Brunch this weekend?",
4848
"""
@@ -57,7 +57,7 @@ object EmailStore {
5757
),
5858
Email(
5959
2L,
60-
AccountStore.getContactAccountById(7L),
60+
AccountStore.getContactAccountById(5L),
6161
listOf(AccountStore.getDefaultUserAccount()),
6262
"Bonjour from Paris",
6363
"Here are some great shots from my trip...",
@@ -84,11 +84,11 @@ object EmailStore {
8484
),
8585
Email(
8686
4L,
87-
AccountStore.getContactAccountById(9L),
87+
AccountStore.getContactAccountById(11L),
8888
listOf(
8989
AccountStore.getDefaultUserAccount(),
9090
AccountStore.getContactAccountById(8L),
91-
AccountStore.getContactAccountById(3L)
91+
AccountStore.getContactAccountById(5L)
9292
),
9393
"Brazil trip",
9494
"""
@@ -104,22 +104,22 @@ object EmailStore {
104104
),
105105
Email(
106106
5L,
107-
AccountStore.getContactAccountById(10L),
107+
AccountStore.getContactAccountById(13L),
108108
listOf(AccountStore.getDefaultUserAccount()),
109109
"Update to Your Itinerary",
110110
""
111111
),
112112
Email(
113113
6L,
114-
AccountStore.getContactAccountById(9L),
114+
AccountStore.getContactAccountById(10L),
115115
listOf(AccountStore.getDefaultUserAccount()),
116116
"Recipe to try",
117117
"Raspberry Pie: We should make this pie recipe tonight! The filling is " +
118118
"very quick to put together."
119119
),
120120
Email(
121121
7L,
122-
AccountStore.getContactAccountById(6L),
122+
AccountStore.getContactAccountById(9L),
123123
listOf(AccountStore.getDefaultUserAccount()),
124124
"Delivered",
125125
"Your shoes should be waiting for you at home!"

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
package com.materialstudies.reply.ui
1818

1919
import android.graphics.drawable.AnimatedVectorDrawable
20-
import androidx.appcompat.app.AppCompatActivity
2120
import android.os.Bundle
2221
import android.view.MenuItem
2322
import android.view.View
2423
import androidx.annotation.MenuRes
24+
import androidx.appcompat.app.AppCompatActivity
2525
import androidx.appcompat.app.AppCompatDelegate
2626
import androidx.appcompat.widget.Toolbar
2727
import androidx.core.content.ContextCompat
@@ -35,7 +35,8 @@ import com.materialstudies.reply.ui.email.EmailFragmentArgs
3535
import com.materialstudies.reply.ui.nav.AlphaSlideAction
3636
import com.materialstudies.reply.ui.nav.BottomNavDrawerFragment
3737
import com.materialstudies.reply.ui.nav.ChangeSettingsMenuStateAction
38-
import com.materialstudies.reply.ui.nav.QuarterRotateSlideAction
38+
import com.materialstudies.reply.ui.nav.HalfClockwiseRotateSlideAction
39+
import com.materialstudies.reply.ui.nav.HalfCounterClockwiseRotateSlideAction
3940
import com.materialstudies.reply.ui.nav.ShowHideFabStateAction
4041
import com.materialstudies.reply.util.contentView
4142
import kotlin.LazyThreadSafetyMode.NONE
@@ -78,7 +79,7 @@ class MainActivity : AppCompatActivity(),
7879
}
7980

8081
bottomNavDrawer.apply {
81-
addOnSlideAction(QuarterRotateSlideAction(binding.bottomAppBarChevron))
82+
addOnSlideAction(HalfClockwiseRotateSlideAction(binding.bottomAppBarChevron))
8283
addOnSlideAction(AlphaSlideAction(binding.bottomAppBarTitle, true))
8384
addOnStateChangedAction(ShowHideFabStateAction(binding.fab))
8485
addOnStateChangedAction(ChangeSettingsMenuStateAction { showSettings ->
@@ -90,6 +91,8 @@ class MainActivity : AppCompatActivity(),
9091
getBottomAppBarMenuForDestination()
9192
})
9293
})
94+
95+
addOnSandwichSlideAction(HalfCounterClockwiseRotateSlideAction(binding.bottomAppBarChevron))
9396
}
9497

9598
// Set up the BottomAppBar menu

app/src/main/java/com/materialstudies/reply/ui/common/EmailAttachmentViewHolder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EmailAttachmentViewHolder(
3131

3232
fun bind(attachment: EmailAttachment) {
3333
binding.run {
34-
binding.setVariable(BR.emailAttachment, attachment)
34+
setVariable(BR.emailAttachment, attachment)
3535
executePendingBindings()
3636
}
3737
}

app/src/main/java/com/materialstudies/reply/ui/compose/ComposeFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ComposeFragment : Fragment() {
7979
senderSpinner.adapter = ArrayAdapter(
8080
senderSpinner.context,
8181
R.layout.spinner_item_layout,
82-
AccountStore.getUserAccounts().map { it.email }
82+
AccountStore.getAllUserAccounts().map { it.email }
8383
)
8484
}
8585
}

0 commit comments

Comments
 (0)