Skip to content
Snippets Groups Projects
Commit d57361e8 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Improve / clean some code

parent f09c06f8
No related branches found
No related tags found
No related merge requests found
Pipeline #4748 passed
......@@ -3,9 +3,10 @@ import 'package:fl_chart/fl_chart.dart';
import 'package:scrobbles/config/app_colors.dart';
import 'package:scrobbles/models/topartists.dart';
import 'package:scrobbles/ui/widgets/abstracts/custom_chart.dart';
import 'package:scrobbles/utils/color_extensions.dart';
class ChartTopArtists extends StatelessWidget {
class ChartTopArtists extends CustomChart {
final TopArtistsData chartData;
const ChartTopArtists({super.key, required this.chartData});
......@@ -15,6 +16,8 @@ class ChartTopArtists extends StatelessWidget {
return AspectRatio(
aspectRatio: 2.1,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: AspectRatio(
......@@ -37,36 +40,22 @@ class ChartTopArtists extends StatelessWidget {
);
}
Color getColorIndex(int index) {
const List<int> hexValues = [
0x8dd3c7,
0xffffb3,
0xbebada,
0xfb8072,
0x80b1d3,
0xfdb462,
0xb3de69,
0xfccde5,
0xd9d9d9,
0xbc80bd,
0xccebc5,
0xffed6f,
];
return Color(hexValues[index % hexValues.length] + 0xff000000);
}
List<PieChartSectionData> getPieChartData() {
List<PieChartSectionData> items = [];
final radius = 40.0;
final fontSize = 11.0;
const shadows = [Shadow(color: Colors.black, blurRadius: 2)];
const radius = 40.0;
const fontSize = 11.0;
const shadows = [
Shadow(
color: Colors.black,
blurRadius: 2,
)
];
int index = 0;
this.chartData.topArtists.forEach((element) {
Color baseColor = getColorIndex(index++);
final Color baseColor = getColorFromIndex(index++);
final PieChartSectionData item = PieChartSectionData(
value: element.count.toDouble(),
......@@ -77,7 +66,7 @@ class ChartTopArtists extends StatelessWidget {
width: 1,
),
radius: radius,
titleStyle: TextStyle(
titleStyle: const TextStyle(
fontSize: fontSize,
color: AppColors.mainTextColor1,
shadows: shadows,
......@@ -97,7 +86,7 @@ class ChartTopArtists extends StatelessWidget {
int index = 0;
this.chartData.topArtists.forEach((element) {
Color baseColor = getColorIndex(index++);
final Color baseColor = getColorFromIndex(index++);
final Widget item = Row(
children: <Widget>[
......@@ -113,9 +102,7 @@ class ChartTopArtists extends StatelessWidget {
),
),
),
const SizedBox(
width: 4,
),
const SizedBox(width: 4),
Text(
element.artistName + ' (' + element.count.toString() + ')',
style: TextStyle(
......
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:scrobbles/config/app_colors.dart';
import 'package:scrobbles/models/topartists.dart';
import 'package:scrobbles/ui/widgets/abstracts/custom_line_chart.dart';
import 'package:scrobbles/utils/color_extensions.dart';
......@@ -16,22 +14,22 @@ class ChartTopArtistsStream extends CustomLineChart {
@override
Widget build(BuildContext context) {
final horizontalScale = getHorizontalScale();
if (this.chartData.topArtistsStream.keys.isEmpty) {
return SizedBox(
height: this.chartHeight,
);
}
final horizontalScale = getHorizontalScaleFromDates(this.chartData.topArtistsStream.keys);
return Container(
height: this.chartHeight,
child: LineChart(
LineChartData(
lineBarsData: getDataStreamLine(),
betweenBarsData: getBetweenBarsData(),
borderData: getBorderData(),
gridData: getGridData(),
borderData: noBorderData,
gridData: horizontalGridData,
titlesData: getTitlesData(),
lineTouchData: const LineTouchData(enabled: false),
minX: horizontalScale['min'],
......@@ -44,24 +42,12 @@ class ChartTopArtistsStream extends CustomLineChart {
);
}
double getNextRoundNumber(double number, double scale) {
return scale * ((number ~/ scale).toInt() + 1);
}
FlGridData getGridData() {
return const FlGridData(
show: true,
drawHorizontalLine: true,
drawVerticalLine: false,
);
}
double getMaxVerticalValue() {
double maxValue = 0;
this.chartData.topArtistsStream.keys.forEach((dateAsString) {
double totalValue = 0.0;
List<TopArtistsStreamDataValue> artists =
final List<TopArtistsStreamDataValue> artists =
this.chartData.topArtistsStream[dateAsString] ?? [];
artists.forEach((artist) {
......@@ -77,55 +63,15 @@ class ChartTopArtistsStream extends CustomLineChart {
return maxValue;
}
Map<String, double> getHorizontalScale() {
double minDateAsDouble = double.maxFinite;
double maxDateAsDouble = -double.maxFinite;
this.chartData.topArtistsStream.keys.forEach((dateAsString) {
final double date = DateTime.parse(dateAsString).millisecondsSinceEpoch.toDouble();
if (date < minDateAsDouble) {
minDateAsDouble = date;
}
if (date > maxDateAsDouble) {
maxDateAsDouble = date;
}
});
return {
'min': minDateAsDouble,
'max': maxDateAsDouble,
};
}
Color getColorIndex(int index) {
const List<int> hexValues = [
0x8dd3c7,
0xffffb3,
0xbebada,
0xfb8072,
0x80b1d3,
0xfdb462,
0xb3de69,
0xfccde5,
0xd9d9d9,
0xbc80bd,
0xccebc5,
0xffed6f,
];
return Color(hexValues[index % hexValues.length] + 0xff000000);
}
List<LineChartBarData> getDataStreamLine() {
int artistsCount =
final int artistsCount =
this.chartData.topArtistsStream[this.chartData.topArtistsStream.keys.first]?.length ??
0;
List<LineChartBarData> lines = [];
LineChartBarData getZeroHorizontalLine() {
final baseColor = getColorIndex(0);
final baseColor = getColorFromIndex(0);
final borderColor = baseColor.darken(20);
List<FlSpot> spots = [];
......@@ -146,7 +92,7 @@ class ChartTopArtistsStream extends CustomLineChart {
lines.add(getZeroHorizontalLine());
LineChartBarData getLinesFromIndex(int index) {
final baseColor = getColorIndex(index);
final baseColor = getColorFromIndex(index);
final borderColor = baseColor.darken(20);
List<FlSpot> spots = [];
......@@ -183,7 +129,7 @@ class ChartTopArtistsStream extends CustomLineChart {
}
List<BetweenBarsData> getBetweenBarsData() {
int artistsCount =
final int artistsCount =
this.chartData.topArtistsStream[this.chartData.topArtistsStream.keys.first]?.length ??
0;
......@@ -192,33 +138,12 @@ class ChartTopArtistsStream extends CustomLineChart {
.map((index) => BetweenBarsData(
fromIndex: index,
toIndex: index + 1,
color: getColorIndex(index),
color: getColorFromIndex(index),
))
.toList();
}
Widget getHorizontalTitlesWidget(double value, TitleMeta meta) {
final DateFormat formatter = DateFormat('dd/MM');
final DateTime date = DateTime.fromMillisecondsSinceEpoch(value.toInt());
final String text = formatter.format(date);
return SideTitleWidget(
axisSide: meta.axisSide,
space: 4,
child: Padding(
padding: EdgeInsets.only(right: 10),
child: RotationTransition(
turns: new AlwaysStoppedAnimation(-30 / 360),
child: Text(
text,
style: TextStyle(
color: AppColors.mainTextColor1,
fontSize: this.titleFontSize,
),
),
),
),
);
return getHorizontalTitlesWidgetWithDate(value, meta);
}
}
......@@ -148,10 +148,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
sha256: d4872660c46d929f6b8a9ef4e7a7eff7e49bbf0c4ec3f385ee32df5119175139
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.1.2"
http_parser:
dependency: transitive
description:
......@@ -417,10 +417,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: "7c99c0e1e2fa190b48d25c81ca5e42036d5cac81430ef249027d97b0935c553f"
sha256: b0f37db61ba2f2e9b7a78a1caece0052564d1bc70668156cf3a29d676fe4e574
url: "https://pub.dev"
source: hosted
version: "5.1.0"
version: "5.1.1"
xdg_directories:
dependency: transitive
description:
......
......@@ -3,7 +3,7 @@ description: Display scrobbles data and charts
publish_to: 'none'
version: 0.0.49+49
version: 0.0.50+50
environment:
sdk: '^3.0.0'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment