1 /*
<lambda>null2  * Copyright (C) 2023 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 com.android.settingslib.spa.gallery.chart
18 
19 import android.os.Bundle
20 import androidx.compose.runtime.Composable
21 import androidx.compose.ui.tooling.preview.Preview
22 import com.android.settingslib.spa.framework.common.SettingsEntry
23 import com.android.settingslib.spa.framework.common.SettingsEntryBuilder
24 import com.android.settingslib.spa.framework.common.SettingsPageProvider
25 import com.android.settingslib.spa.framework.common.createSettingsPage
26 import com.android.settingslib.spa.framework.compose.navigator
27 import com.android.settingslib.spa.framework.theme.SettingsTheme
28 import com.android.settingslib.spa.widget.chart.LineChart
29 import com.android.settingslib.spa.widget.chart.LineChartData
30 import com.android.settingslib.spa.widget.chart.LineChartModel
31 import com.android.settingslib.spa.widget.chart.PieChart
32 import com.android.settingslib.spa.widget.chart.PieChartData
33 import com.android.settingslib.spa.widget.chart.PieChartModel
34 import com.android.settingslib.spa.widget.preference.Preference
35 import com.android.settingslib.spa.widget.preference.PreferenceModel
36 import com.github.mikephil.charting.formatter.IAxisValueFormatter
37 import java.text.NumberFormat
38 
39 private enum class WeekDay(val num: Int) {
40     Sun(0), Mon(1), Tue(2), Wed(3), Thu(4), Fri(5), Sat(6),
41 }
42 
43 private const val TITLE = "Sample Chart"
44 
45 object ChartPageProvider : SettingsPageProvider {
46     override val name = "Chart"
47     private val owner = createSettingsPage()
48 
getTitlenull49     override fun getTitle(arguments: Bundle?): String {
50         return TITLE
51     }
52 
buildEntrynull53     override fun buildEntry(arguments: Bundle?): List<SettingsEntry> {
54         val entryList = mutableListOf<SettingsEntry>()
55         entryList.add(
56             SettingsEntryBuilder.create("Line Chart", owner)
57                 .setUiLayoutFn {
58                     Preference(object : PreferenceModel {
59                         override val title = "Line Chart"
60                     })
61                     LineChart(
62                         lineChartModel = object : LineChartModel {
63                             override val chartDataList = listOf(
64                                 LineChartData(x = 0f, y = 0f),
65                                 LineChartData(x = 1f, y = 0.1f),
66                                 LineChartData(x = 2f, y = 0.2f),
67                                 LineChartData(x = 3f, y = 0.6f),
68                                 LineChartData(x = 4f, y = 0.9f),
69                                 LineChartData(x = 5f, y = 1.0f),
70                                 LineChartData(x = 6f, y = 0.8f),
71                             )
72                             override val xValueFormatter =
73                                 IAxisValueFormatter { value, _ ->
74                                     "${WeekDay.values()[value.toInt()]}"
75                                 }
76                             override val yValueFormatter =
77                                 IAxisValueFormatter { value, _ ->
78                                     NumberFormat.getPercentInstance().format(value)
79                                 }
80                         }
81                     )
82                 }.build()
83         )
84         entryList.add(createBarChartEntry(owner))
85         entryList.add(
86             SettingsEntryBuilder.create("Pie Chart", owner)
87                 .setUiLayoutFn {
88                     Preference(object : PreferenceModel {
89                         override val title = "Pie Chart"
90                     })
91                     PieChart(
92                         pieChartModel = object : PieChartModel {
93                             override val chartDataList = listOf(
94                                 PieChartData(label = "Settings", value = 20f),
95                                 PieChartData(label = "Chrome", value = 5f),
96                                 PieChartData(label = "Gmail", value = 3f),
97                             )
98                             override val centerText = "Today"
99                         }
100                     )
101                 }.build()
102         )
103 
104         return entryList
105     }
106 
107     @Composable
Entrynull108     fun Entry() {
109         Preference(object : PreferenceModel {
110             override val title = TITLE
111             override val onClick = navigator(name)
112         })
113     }
114 }
115 
116 @Preview(showBackground = true)
117 @Composable
ChartPagePreviewnull118 private fun ChartPagePreview() {
119     SettingsTheme {
120         ChartPageProvider.Page(null)
121     }
122 }
123