[go: nahoru, domu]

blob: aff913df42eb6630bac3c27c8b6ca2f540aa496c [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
21import androidx.compose.state
22import androidx.ui.core.Text
Clara Bayarri255d76a2020-02-12 12:23:09 +000023import androidx.ui.foundation.VerticalScroller
Clara Bayarri255d76a2020-02-12 12:23:09 +000024import androidx.ui.layout.Arrangement
25import androidx.ui.layout.Column
Clara Bayarri73725732020-02-12 18:27:38 +000026import androidx.ui.layout.EdgeInsets
Clara Bayarri255d76a2020-02-12 12:23:09 +000027import androidx.ui.layout.LayoutGravity
28import androidx.ui.layout.LayoutHeight
29import androidx.ui.layout.LayoutPadding
30import androidx.ui.layout.LayoutWidth
31import androidx.ui.layout.Row
32import androidx.ui.layout.Spacer
33import androidx.ui.material.Divider
34import androidx.ui.material.MaterialTheme
35import androidx.ui.material.TextButton
36import androidx.ui.material.icons.Icons
Andrey Kulikov602ef012020-03-05 18:12:54 +000037import androidx.ui.material.Card
Louis Pullen-Freilicha5a9e152020-03-12 23:19:53 +000038import androidx.ui.material.IconButton
Clara Bayarri255d76a2020-02-12 12:23:09 +000039import androidx.ui.unit.dp
40import java.util.Locale
41
42@Composable
43fun OverviewBody() {
44 VerticalScroller {
45 Column(modifier = LayoutPadding(16.dp)) {
46 AlertCard()
Clara Bayarri73725732020-02-12 18:27:38 +000047 Spacer(LayoutHeight(RallyDefaultPadding))
Clara Bayarri255d76a2020-02-12 12:23:09 +000048 AccountsCard()
Clara Bayarri73725732020-02-12 18:27:38 +000049 Spacer(LayoutHeight(RallyDefaultPadding))
Clara Bayarri255d76a2020-02-12 12:23:09 +000050 BillsCard()
51 }
52 }
53}
54
55/**
56 * The Alerts card within the Rally Overview screen.
57 */
58@Composable
59private fun AlertCard() {
Clara Bayarri73725732020-02-12 18:27:38 +000060 var openDialog by state { false }
Clara Bayarri255d76a2020-02-12 12:23:09 +000061 val alertMessage = "Heads up, you've used up 90% of your Shopping budget for this month."
62
Clara Bayarri73725732020-02-12 18:27:38 +000063 if (openDialog) {
Clara Bayarri255d76a2020-02-12 12:23:09 +000064 RallyAlertDialog(
65 onDismiss = {
Clara Bayarri73725732020-02-12 18:27:38 +000066 openDialog = false
Clara Bayarri255d76a2020-02-12 12:23:09 +000067 },
68 bodyText = alertMessage,
69 buttonText = "Dismiss".toUpperCase(Locale.getDefault())
70 )
71 }
72 Card {
73 Column {
Clara Bayarri73725732020-02-12 18:27:38 +000074 AlertHeader({ openDialog = true })
75 RallyDivider(
76 modifier = LayoutPadding(start = RallyDefaultPadding, end = RallyDefaultPadding)
Clara Bayarri255d76a2020-02-12 12:23:09 +000077 )
Clara Bayarri73725732020-02-12 18:27:38 +000078 AlertItem(alertMessage)
79 }
80 }
81}
82
83@Composable
84private fun AlertHeader(onClickSeeAll: () -> Unit) {
85 Row(
86 modifier = LayoutPadding(RallyDefaultPadding) + LayoutWidth.Fill,
87 arrangement = Arrangement.SpaceBetween
88 ) {
89 Text(
90 text = "Alerts",
91 style = MaterialTheme.typography().subtitle2,
92 modifier = LayoutGravity.Center
93 )
94 TextButton(
95 onClick = onClickSeeAll,
Andrey Kulikov96938ff2020-03-04 16:39:10 +000096 innerPadding = EdgeInsets(0.dp),
Clara Bayarri73725732020-02-12 18:27:38 +000097 modifier = LayoutGravity.Center
98 ) {
99 Text("SEE ALL")
100 }
101 }
102}
103
104@Composable
105private fun AlertItem(message: String) {
106 // TODO: Make alerts into a data structure
107 Row(
108 modifier = LayoutPadding(RallyDefaultPadding),
109 arrangement = Arrangement.SpaceBetween
110 ) {
111 Text(
112 style = MaterialTheme.typography().h3,
Mihai Popa9fcfaeb2020-03-09 17:50:28 +0000113 modifier = LayoutWeight(1f),
Clara Bayarri73725732020-02-12 18:27:38 +0000114 text = message
115 )
116 IconButton(
Clara Bayarri73725732020-02-12 18:27:38 +0000117 onClick = {},
118 modifier = LayoutGravity.Top
Louis Pullen-Freilicha5a9e152020-03-12 23:19:53 +0000119 ) {
120 Icon(Icons.Filled.Sort)
121 }
Clara Bayarri73725732020-02-12 18:27:38 +0000122 }
123}
124
125/**
126 * Base structure for cards in the Overview screen.
127 */
128@SuppressLint("UnnecessaryLambdaCreation")
129@Composable
130private fun <T> OverviewScreenCard(
131 title: String,
132 amount: Float,
133 onClickSeeAll: () -> Unit,
134 data: List<T>,
135 row: @Composable() (T) -> Unit
136) {
137 Card {
138 Column {
139 Column(modifier = LayoutPadding(RallyDefaultPadding)) {
140 Text(text = title, style = MaterialTheme.typography().subtitle2)
141 val amountText = "$" + formatAmount(amount)
142 Text(text = amountText, style = MaterialTheme.typography().h2)
143 }
144 Divider(color = rallyGreen, height = 1.dp)
145 Column(LayoutPadding(start = 16.dp, top = 4.dp, end = 8.dp)) {
146 data.take(3).forEach { row(it) }
147 SeeAllButton(onClick = onClickSeeAll)
Clara Bayarri255d76a2020-02-12 12:23:09 +0000148 }
149 }
150 }
151}
152
153/**
154 * The Accounts card within the Rally Overview screen.
155 */
156@Composable
157private fun AccountsCard() {
Clara Bayarri73725732020-02-12 18:27:38 +0000158 val amount = UserData.accounts.map { account -> account.balance }.sum()
159 OverviewScreenCard(
160 title = "Accounts",
161 amount = amount,
162 onClickSeeAll = {
163 // TODO: Figure out navigation
164 },
165 data = UserData.accounts
166 ) { account ->
167 AccountRow(
168 name = account.name,
169 number = account.number,
170 amount = account.balance,
171 color = account.color
172 )
Clara Bayarri255d76a2020-02-12 12:23:09 +0000173 }
174}
175
176/**
177 * The Bills card within the Rally Overview screen.
178 */
179@Composable
Clara Bayarri73725732020-02-12 18:27:38 +0000180private fun BillsCard() {
181 val amount = UserData.bills.map { bill -> bill.amount }.sum()
182 OverviewScreenCard(
183 title = "Bills",
184 amount = amount,
185 onClickSeeAll = {
186 // TODO: Figure out navigation
187 },
188 data = UserData.bills
189 ) { bill ->
190 BillRow(
191 name = bill.name,
192 due = bill.due,
193 amount = bill.amount,
194 color = bill.color
195 )
Clara Bayarri255d76a2020-02-12 12:23:09 +0000196 }
Clara Bayarri73725732020-02-12 18:27:38 +0000197}
198
199@Composable
200private fun SeeAllButton(onClick: () -> Unit) {
201 TextButton(
202 onClick = onClick,
203 modifier = LayoutHeight(44.dp) + LayoutWidth.Fill
204 ) {
205 Text("SEE ALL")
206 }
207}
208
209private val RallyDefaultPadding = 12.dp