Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.material.studies.rally |
| 18 | |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 19 | import android.annotation.SuppressLint |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 20 | import androidx.compose.Composable |
Adam Powell | 5f07ae8 | 2020-03-30 14:36:42 -0700 | [diff] [blame] | 21 | import androidx.compose.getValue |
| 22 | import androidx.compose.setValue |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 23 | import androidx.compose.state |
Mihai Popa | 5b7a6bb | 2020-04-08 20:11:03 +0100 | [diff] [blame] | 24 | import androidx.ui.core.Alignment |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 25 | import androidx.ui.core.Modifier |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 26 | import androidx.compose.foundation.ScrollableColumn |
| 27 | import androidx.compose.foundation.Text |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 28 | import androidx.compose.foundation.layout.Arrangement |
| 29 | import androidx.compose.foundation.layout.Column |
| 30 | import androidx.compose.foundation.layout.InnerPadding |
| 31 | import androidx.compose.foundation.layout.Row |
| 32 | import androidx.compose.foundation.layout.Spacer |
| 33 | import androidx.compose.foundation.layout.fillMaxWidth |
| 34 | import androidx.compose.foundation.layout.padding |
| 35 | import androidx.compose.foundation.layout.preferredHeight |
Louis Pullen-Freilich | 0d04438 | 2020-03-17 22:43:35 +0000 | [diff] [blame] | 36 | import androidx.ui.material.Card |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 37 | import androidx.ui.material.Divider |
Louis Pullen-Freilich | 0d04438 | 2020-03-17 22:43:35 +0000 | [diff] [blame] | 38 | import androidx.ui.material.IconButton |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 39 | import androidx.ui.material.MaterialTheme |
| 40 | import androidx.ui.material.TextButton |
| 41 | import androidx.ui.material.icons.Icons |
Louis Pullen-Freilich | a7eeb10 | 2020-07-22 17:54:24 +0100 | [diff] [blame^] | 42 | import androidx.compose.ui.unit.dp |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 43 | import java.util.Locale |
| 44 | |
| 45 | @Composable |
| 46 | fun OverviewBody() { |
Matvei Malkov | 235b4fa | 2020-07-07 21:14:49 +0100 | [diff] [blame] | 47 | ScrollableColumn(contentPadding = InnerPadding(16.dp)) { |
| 48 | AlertCard() |
| 49 | Spacer(Modifier.preferredHeight(RallyDefaultPadding)) |
| 50 | AccountsCard() |
| 51 | Spacer(Modifier.preferredHeight(RallyDefaultPadding)) |
| 52 | BillsCard() |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * The Alerts card within the Rally Overview screen. |
| 58 | */ |
| 59 | @Composable |
| 60 | private fun AlertCard() { |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 61 | var openDialog by state { false } |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 62 | val alertMessage = "Heads up, you've used up 90% of your Shopping budget for this month." |
| 63 | |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 64 | if (openDialog) { |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 65 | RallyAlertDialog( |
| 66 | onDismiss = { |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 67 | openDialog = false |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 68 | }, |
| 69 | bodyText = alertMessage, |
| 70 | buttonText = "Dismiss".toUpperCase(Locale.getDefault()) |
| 71 | ) |
| 72 | } |
| 73 | Card { |
| 74 | Column { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 75 | AlertHeader { openDialog = true } |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 76 | RallyDivider( |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 77 | modifier = Modifier.padding(start = RallyDefaultPadding, end = RallyDefaultPadding) |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 78 | ) |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 79 | AlertItem(alertMessage) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Composable |
| 85 | private fun AlertHeader(onClickSeeAll: () -> Unit) { |
| 86 | Row( |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 87 | modifier = Modifier.padding(RallyDefaultPadding).fillMaxWidth(), |
Mihai Popa | 09a9eb8 | 2020-04-08 23:16:46 +0100 | [diff] [blame] | 88 | horizontalArrangement = Arrangement.SpaceBetween |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 89 | ) { |
| 90 | Text( |
| 91 | text = "Alerts", |
Louis Pullen-Freilich | b259185 | 2020-03-23 19:00:09 +0000 | [diff] [blame] | 92 | style = MaterialTheme.typography.subtitle2, |
Mihai Popa | 5b7a6bb | 2020-04-08 20:11:03 +0100 | [diff] [blame] | 93 | modifier = Modifier.gravity(Alignment.CenterVertically) |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 94 | ) |
| 95 | TextButton( |
| 96 | onClick = onClickSeeAll, |
Mihai Popa | 5ec932a | 2020-04-08 15:35:26 +0100 | [diff] [blame] | 97 | padding = InnerPadding(0.dp), |
Mihai Popa | 5b7a6bb | 2020-04-08 20:11:03 +0100 | [diff] [blame] | 98 | modifier = Modifier.gravity(Alignment.CenterVertically) |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 99 | ) { |
| 100 | Text("SEE ALL") |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Composable |
| 106 | private fun AlertItem(message: String) { |
| 107 | // TODO: Make alerts into a data structure |
| 108 | Row( |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 109 | modifier = Modifier.padding(RallyDefaultPadding), |
Mihai Popa | 09a9eb8 | 2020-04-08 23:16:46 +0100 | [diff] [blame] | 110 | horizontalArrangement = Arrangement.SpaceBetween |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 111 | ) { |
| 112 | Text( |
Louis Pullen-Freilich | b259185 | 2020-03-23 19:00:09 +0000 | [diff] [blame] | 113 | style = MaterialTheme.typography.h3, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 114 | modifier = Modifier.weight(1f), |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 115 | text = message |
| 116 | ) |
| 117 | IconButton( |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 118 | onClick = {}, |
Mihai Popa | 5b7a6bb | 2020-04-08 20:11:03 +0100 | [diff] [blame] | 119 | modifier = Modifier.gravity(Alignment.Top) |
Louis Pullen-Freilich | a5a9e15 | 2020-03-12 23:19:53 +0000 | [diff] [blame] | 120 | ) { |
| 121 | Icon(Icons.Filled.Sort) |
| 122 | } |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Base structure for cards in the Overview screen. |
| 128 | */ |
| 129 | @SuppressLint("UnnecessaryLambdaCreation") |
| 130 | @Composable |
| 131 | private fun <T> OverviewScreenCard( |
| 132 | title: String, |
| 133 | amount: Float, |
| 134 | onClickSeeAll: () -> Unit, |
| 135 | data: List<T>, |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 136 | row: @Composable (T) -> Unit |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 137 | ) { |
| 138 | Card { |
| 139 | Column { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 140 | Column(Modifier.padding(RallyDefaultPadding)) { |
Louis Pullen-Freilich | b259185 | 2020-03-23 19:00:09 +0000 | [diff] [blame] | 141 | Text(text = title, style = MaterialTheme.typography.subtitle2) |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 142 | val amountText = "$" + formatAmount(amount) |
Louis Pullen-Freilich | b259185 | 2020-03-23 19:00:09 +0000 | [diff] [blame] | 143 | Text(text = amountText, style = MaterialTheme.typography.h2) |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 144 | } |
Anastasia Soboleva | cc1d166 | 2020-03-30 19:38:06 +0100 | [diff] [blame] | 145 | Divider(color = rallyGreen, thickness = 1.dp) |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 146 | Column(Modifier.padding(start = 16.dp, top = 4.dp, end = 8.dp)) { |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 147 | data.take(3).forEach { row(it) } |
| 148 | SeeAllButton(onClick = onClickSeeAll) |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * The Accounts card within the Rally Overview screen. |
| 156 | */ |
| 157 | @Composable |
| 158 | private fun AccountsCard() { |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 159 | 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 Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * The Bills card within the Rally Overview screen. |
| 179 | */ |
| 180 | @Composable |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 181 | private 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 Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 197 | } |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Composable |
| 201 | private fun SeeAllButton(onClick: () -> Unit) { |
| 202 | TextButton( |
| 203 | onClick = onClick, |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 204 | modifier = Modifier.preferredHeight(44.dp).fillMaxWidth() |
Clara Bayarri | 7372573 | 2020-02-12 18:27:38 +0000 | [diff] [blame] | 205 | ) { |
| 206 | Text("SEE ALL") |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private val RallyDefaultPadding = 12.dp |