Skip to content

Commit

Permalink
[Presentation] Show the version details dialog when tapping on the el…
Browse files Browse the repository at this point in the history
…ement chart
  • Loading branch information
Wolfteam committed May 6, 2022
1 parent 4ebcf24 commit 6ed9dd4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
7 changes: 7 additions & 0 deletions lib/application/charts/charts_bloc.dart
Expand Up @@ -25,6 +25,13 @@ class ChartsBloc extends Bloc<ChartsEvent, ChartsState> {
yield s;
}

//TODO: MAYBE REMOVE THIS FUNCTION FROM HERE
//Some versions were skipped (e.g: 1.7, 1.8, 1.9), that's why we use this function
//to determine if the version can be skipped or no
static bool isValidVersion(double value) {
return value + 1 < 1.7 || value + 1 >= 2;
}

Future<ChartsState> _init() async {
await _telemetryService.trackChartsOpened();
final tops = [
Expand Down
11 changes: 11 additions & 0 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -1017,9 +1017,20 @@ class GenshinServiceImpl implements GenshinService {
..sort((x, y) => x.version.compareTo(y.version));
final charts = <ChartElementItemModel>[];
final characters = getCharactersForCard();
final usedChars = <double, List<String>>{};

for (final banner in banners) {
for (final key in banner.itemKeys) {
final bannerHasAlreadyBeenAdded = usedChars.containsKey(banner.version);
final characterAlreadyAppearedInThisBanner = usedChars.entries.any((el) => el.key == banner.version && el.value.contains(key));
if (!bannerHasAlreadyBeenAdded) {
usedChars.putIfAbsent(banner.version, () => [key]);
} else if (characterAlreadyAppearedInThisBanner) {
continue;
} else {
usedChars.update(banner.version, (value) => [...value, key]);
}

final char = characters.firstWhere((el) => el.key == key);
final existing = charts.firstWhereOrNull((el) => el.type == char.elementType);
final points = existing?.points ?? [];
Expand Down
Expand Up @@ -17,11 +17,16 @@ const _dateFormat = 'yyyy/MM/dd';

class VersionDetailsDialog extends StatelessWidget {
final double version;
final bool showWeapons;
final bool showCharacters;

const VersionDetailsDialog({
Key? key,
required this.version,
}) : super(key: key);
this.showCharacters = true,
this.showWeapons = true,
}) : assert(!(showCharacters == false && showWeapons == false), 'You must show either characters, weapons or both'),
super(key: key);

@override
Widget build(BuildContext context) {
Expand All @@ -48,6 +53,8 @@ class VersionDetailsDialog extends StatelessWidget {
return _VersionDetailPeriod(
from: group.from,
until: group.until,
showCharacters: showCharacters,
showWeapons: showWeapons,
items: e.expand((el) => el.items).toList(),
);
},
Expand All @@ -73,12 +80,16 @@ class _VersionDetailPeriod extends StatelessWidget {
final DateTime from;
final DateTime until;
final List<ItemCommonWithRarityAndType> items;
final bool showWeapons;
final bool showCharacters;

const _VersionDetailPeriod({
Key? key,
required this.from,
required this.until,
required this.items,
required this.showCharacters,
required this.showWeapons,
}) : super(key: key);

@override
Expand All @@ -90,6 +101,10 @@ class _VersionDetailPeriod extends StatelessWidget {
final characters = items.where((el) => el.type == ItemType.character).toList()..sort((x, y) => y.rarity.compareTo(x.rarity));
final weapons = items.where((el) => el.type == ItemType.weapon).toList()..sort((x, y) => y.rarity.compareTo(x.rarity));

if (characters.isEmpty && !showWeapons || weapons.isEmpty && !showCharacters) {
return const SizedBox.shrink();
}

return Container(
margin: const EdgeInsets.only(bottom: 20),
child: Column(
Expand All @@ -103,14 +118,14 @@ class _VersionDetailPeriod extends StatelessWidget {
],
),
Divider(color: theme.colorScheme.primary),
if (characters.isNotEmpty) Text(s.characters, style: theme.textTheme.subtitle1),
if (characters.isNotEmpty)
if (characters.isNotEmpty && showCharacters) Text(s.characters, style: theme.textTheme.subtitle1),
if (characters.isNotEmpty && showCharacters)
_Items(
type: BannerHistoryItemType.character,
items: characters,
),
if (weapons.isNotEmpty) Text(s.weapons, style: theme.textTheme.subtitle1),
if (weapons.isNotEmpty)
if (weapons.isNotEmpty && showWeapons) Text(s.weapons, style: theme.textTheme.subtitle1),
if (weapons.isNotEmpty && showWeapons)
_Items(
type: BannerHistoryItemType.weapon,
items: weapons,
Expand Down
13 changes: 12 additions & 1 deletion lib/presentation/charts/charts_page.dart
Expand Up @@ -6,6 +6,7 @@ import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/extensions/iterable_extensions.dart';
import 'package:shiori/generated/l10n.dart';
import 'package:shiori/injection.dart';
import 'package:shiori/presentation/banner_history/widgets/version_details_dialog.dart';
import 'package:shiori/presentation/character/character_page.dart';
import 'package:shiori/presentation/charts/widgets/chart_card.dart';
import 'package:shiori/presentation/charts/widgets/chart_legend.dart';
Expand Down Expand Up @@ -182,7 +183,17 @@ class ChartsPage extends StatelessWidget {
)
.toList(),
),
child: HorizontalBarChart(items: state.filteredElements),
child: HorizontalBarChart(
items: state.filteredElements,
canValueBeRendered: ChartsBloc.isValidVersion,
onPointTap: (version) => showDialog(
context: context,
builder: (_) => VersionDetailsDialog(
version: version,
showWeapons: false,
),
),
),
),
Text(s.birthdays, style: theme.textTheme.headline5),
ChartCard(
Expand Down
15 changes: 14 additions & 1 deletion lib/presentation/charts/widgets/horizontal_bar_chart.dart
Expand Up @@ -6,12 +6,19 @@ import 'package:shiori/domain/models/models.dart';
import 'package:shiori/presentation/shared/extensions/element_type_extensions.dart';
import 'package:shiori/presentation/shared/styles.dart';

typedef CanValueBeRendered = bool Function(double);
typedef OnPointTap = void Function(double);

class HorizontalBarChart extends StatelessWidget {
final List<ChartElementItemModel> items;
final CanValueBeRendered canValueBeRendered;
final OnPointTap? onPointTap;

const HorizontalBarChart({
Key? key,
required this.items,
required this.canValueBeRendered,
this.onPointTap,
}) : super(key: key);

@override
Expand All @@ -33,6 +40,12 @@ class HorizontalBarChart extends StatelessWidget {
maxY: maxY,
lineTouchData: LineTouchData(
handleBuiltInTouches: true,
touchCallback: (event, response) {
if (event is FlTapUpEvent && response?.lineBarSpots != null && response!.lineBarSpots!.isNotEmpty) {
final version = response.lineBarSpots!.first.x + 1;
onPointTap?.call(version);
}
},
touchTooltipData: LineTouchTooltipData(
tooltipBgColor: theme.backgroundColor,
),
Expand All @@ -45,7 +58,7 @@ class HorizontalBarChart extends StatelessWidget {
reservedSize: 32,
interval: xIntervals,
//TODO: TOOLTIPS
getTitlesWidget: (value, meta) => value + 1 >= 1.7 && value + 1 < 2
getTitlesWidget: (value, meta) => !canValueBeRendered(value)
? Container()
: Padding(
padding: const EdgeInsets.only(top: 10.0),
Expand Down

0 comments on commit 6ed9dd4

Please sign in to comment.