[go: nahoru, domu]

blob: 8c65ff44bf62bff65c546b5d2fe91a845a395cdc [file] [log] [blame]
Clara Bayarri255d76a2020-02-12 12:23:09 +00001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.ui.material.studies.rally
18
Clara Bayarri73725732020-02-12 18:27:38 +000019import android.annotation.SuppressLint
Clara Bayarri255d76a2020-02-12 12:23:09 +000020import androidx.compose.Composable
Adam Powell5f07ae82020-03-30 14:36:42 -070021import androidx.compose.getValue
22import androidx.compose.setValue
Clara Bayarri255d76a2020-02-12 12:23:09 +000023import androidx.compose.state
Mihai Popa5b7a6bb2020-04-08 20:11:03 +010024import androidx.ui.core.Alignment
Adam Powell999a89b2020-03-11 09:08:07 -070025import androidx.ui.core.Modifier
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010026import androidx.compose.foundation.ScrollableColumn
27import androidx.compose.foundation.Text
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010028import androidx.compose.foundation.layout.Arrangement
29import androidx.compose.foundation.layout.Column
30import androidx.compose.foundation.layout.InnerPadding
31import androidx.compose.foundation.layout.Row
32import androidx.compose.foundation.layout.Spacer
33import androidx.compose.foundation.layout.fillMaxWidth
34import androidx.compose.foundation.layout.padding
35import androidx.compose.foundation.layout.preferredHeight
Louis Pullen-Freilich0d044382020-03-17 22:43:35 +000036import androidx.ui.material.Card
Clara Bayarri255d76a2020-02-12 12:23:09 +000037import androidx.ui.material.Divider
Louis Pullen-Freilich0d044382020-03-17 22:43:35 +000038import androidx.ui.material.IconButton
Clara Bayarri255d76a2020-02-12 12:23:09 +000039import androidx.ui.material.MaterialTheme
40import androidx.ui.material.TextButton
41import androidx.ui.material.icons.Icons
Louis Pullen-Freilicha7eeb102020-07-22 17:54:24 +010042import androidx.compose.ui.unit.dp
Clara Bayarri255d76a2020-02-12 12:23:09 +000043import java.util.Locale
44
45@Composable
46fun OverviewBody() {
Matvei Malkov235b4fa2020-07-07 21:14:49 +010047 ScrollableColumn(contentPadding = InnerPadding(16.dp)) {
48 AlertCard()
49 Spacer(Modifier.preferredHeight(RallyDefaultPadding))
50 AccountsCard()
51 Spacer(Modifier.preferredHeight(RallyDefaultPadding))
52 BillsCard()
Clara Bayarri255d76a2020-02-12 12:23:09 +000053 }
54}
55
56/**
57 * The Alerts card within the Rally Overview screen.
58 */
59@Composable
60private fun AlertCard() {
Clara Bayarri73725732020-02-12 18:27:38 +000061 var openDialog by state { false }
Clara Bayarri255d76a2020-02-12 12:23:09 +000062 val alertMessage = "Heads up, you've used up 90% of your Shopping budget for this month."
63
Clara Bayarri73725732020-02-12 18:27:38 +000064 if (openDialog) {
Clara Bayarri255d76a2020-02-12 12:23:09 +000065 RallyAlertDialog(
66 onDismiss = {
Clara Bayarri73725732020-02-12 18:27:38 +000067 openDialog = false
Clara Bayarri255d76a2020-02-12 12:23:09 +000068 },
69 bodyText = alertMessage,
70 buttonText = "Dismiss".toUpperCase(Locale.getDefault())
71 )
72 }
73 Card {
74 Column {
Adam Powell999a89b2020-03-11 09:08:07 -070075 AlertHeader { openDialog = true }
Clara Bayarri73725732020-02-12 18:27:38 +000076 RallyDivider(
Adam Powell999a89b2020-03-11 09:08:07 -070077 modifier = Modifier.padding(start = RallyDefaultPadding, end = RallyDefaultPadding)
Clara Bayarri255d76a2020-02-12 12:23:09 +000078 )
Clara Bayarri73725732020-02-12 18:27:38 +000079 AlertItem(alertMessage)
80 }
81 }
82}
83
84@Composable
85private fun AlertHeader(onClickSeeAll: () -> Unit) {
86 Row(
Adam Powell999a89b2020-03-11 09:08:07 -070087 modifier = Modifier.padding(RallyDefaultPadding).fillMaxWidth(),
Mihai Popa09a9eb82020-04-08 23:16:46 +010088 horizontalArrangement = Arrangement.SpaceBetween
Clara Bayarri73725732020-02-12 18:27:38 +000089 ) {
90 Text(
91 text = "Alerts",
Louis Pullen-Freilichb2591852020-03-23 19:00:09 +000092 style = MaterialTheme.typography.subtitle2,
Mihai Popa5b7a6bb2020-04-08 20:11:03 +010093 modifier = Modifier.gravity(Alignment.CenterVertically)
Clara Bayarri73725732020-02-12 18:27:38 +000094 )
95 TextButton(
96 onClick = onClickSeeAll,
Mihai Popa5ec932a2020-04-08 15:35:26 +010097 padding = InnerPadding(0.dp),
Mihai Popa5b7a6bb2020-04-08 20:11:03 +010098 modifier = Modifier.gravity(Alignment.CenterVertically)
Clara Bayarri73725732020-02-12 18:27:38 +000099 ) {
100 Text("SEE ALL")
101 }
102 }
103}
104
105@Composable
106private fun AlertItem(message: String) {
107 // TODO: Make alerts into a data structure
108 Row(
Adam Powell999a89b2020-03-11 09:08:07 -0700109 modifier = Modifier.padding(RallyDefaultPadding),
Mihai Popa09a9eb82020-04-08 23:16:46 +0100110 horizontalArrangement = Arrangement.SpaceBetween
Clara Bayarri73725732020-02-12 18:27:38 +0000111 ) {
112 Text(
Louis Pullen-Freilichb2591852020-03-23 19:00:09 +0000113 style = MaterialTheme.typography.h3,
Adam Powell999a89b2020-03-11 09:08:07 -0700114 modifier = Modifier.weight(1f),
Clara Bayarri73725732020-02-12 18:27:38 +0000115 text = message
116 )
117 IconButton(
Clara Bayarri73725732020-02-12 18:27:38 +0000118 onClick = {},
Mihai Popa5b7a6bb2020-04-08 20:11:03 +0100119 modifier = Modifier.gravity(Alignment.Top)
Louis Pullen-Freilicha5a9e152020-03-12 23:19:53 +0000120 ) {
121 Icon(Icons.Filled.Sort)
122 }
Clara Bayarri73725732020-02-12 18:27:38 +0000123 }
124}
125
126/**
127 * Base structure for cards in the Overview screen.
128 */
129@SuppressLint("UnnecessaryLambdaCreation")
130@Composable
131private fun <T> OverviewScreenCard(
132 title: String,
133 amount: Float,
134 onClickSeeAll: () -> Unit,
135 data: List<T>,
Louis Pullen-Freilich3a54b942020-05-07 13:23:03 +0100136 row: @Composable (T) -> Unit
Clara Bayarri73725732020-02-12 18:27:38 +0000137) {
138 Card {
139 Column {
Adam Powell999a89b2020-03-11 09:08:07 -0700140 Column(Modifier.padding(RallyDefaultPadding)) {
Louis Pullen-Freilichb2591852020-03-23 19:00:09 +0000141 Text(text = title, style = MaterialTheme.typography.subtitle2)
Clara Bayarri73725732020-02-12 18:27:38 +0000142 val amountText = "$" + formatAmount(amount)
Louis Pullen-Freilichb2591852020-03-23 19:00:09 +0000143 Text(text = amountText, style = MaterialTheme.typography.h2)
Clara Bayarri73725732020-02-12 18:27:38 +0000144 }
Anastasia Sobolevacc1d1662020-03-30 19:38:06 +0100145 Divider(color = rallyGreen, thickness = 1.dp)
Adam Powell999a89b2020-03-11 09:08:07 -0700146 Column(Modifier.padding(start = 16.dp, top = 4.dp, end = 8.dp)) {
Clara Bayarri73725732020-02-12 18:27:38 +0000147 data.take(3).forEach { row(it) }
148 SeeAllButton(onClick = onClickSeeAll)
Clara Bayarri255d76a2020-02-12 12:23:09 +0000149 }
150 }
151 }
152}
153
154/**
155 * The Accounts card within the Rally Overview screen.
156 */
157@Composable
158private fun AccountsCard() {
Clara Bayarri73725732020-02-12 18:27:38 +0000159 val amount = UserData.accounts.map { account -> account.balance }.sum()
160 OverviewScreenCard(
161 title = "Accounts",
162 amount = amount,
163 onClickSeeAll = {
164 // TODO: Figure out navigation
165 },
166 data = UserData.accounts
167 ) { account ->
168 AccountRow(
169 name = account.name,
170 number = account.number,
171 amount = account.balance,
172 color = account.color
173 )
Clara Bayarri255d76a2020-02-12 12:23:09 +0000174 }
175}
176
177/**
178 * The Bills card within the Rally Overview screen.
179 */
180@Composable
Clara Bayarri73725732020-02-12 18:27:38 +0000181private fun BillsCard() {
182 val amount = UserData.bills.map { bill -> bill.amount }.sum()
183 OverviewScreenCard(
184 title = "Bills",
185 amount = amount,
186 onClickSeeAll = {
187 // TODO: Figure out navigation
188 },
189 data = UserData.bills
190 ) { bill ->
191 BillRow(
192 name = bill.name,
193 due = bill.due,
194 amount = bill.amount,
195 color = bill.color
196 )
Clara Bayarri255d76a2020-02-12 12:23:09 +0000197 }
Clara Bayarri73725732020-02-12 18:27:38 +0000198}
199
200@Composable
201private fun SeeAllButton(onClick: () -> Unit) {
202 TextButton(
203 onClick = onClick,
Adam Powell999a89b2020-03-11 09:08:07 -0700204 modifier = Modifier.preferredHeight(44.dp).fillMaxWidth()
Clara Bayarri73725732020-02-12 18:27:38 +0000205 ) {
206 Text("SEE ALL")
207 }
208}
209
210private val RallyDefaultPadding = 12.dp