Skip to content

Commit

Permalink
[Presentation] Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Apr 20, 2022
1 parent 1fc468d commit 20bf94b
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 76 deletions.
16 changes: 8 additions & 8 deletions lib/application/banner_history/banner_history_bloc.dart
Expand Up @@ -28,13 +28,12 @@ class BannerHistoryBloc extends Bloc<BannerHistoryEvent, BannerHistoryState> {

@override
Stream<BannerHistoryState> mapEventToState(BannerHistoryEvent event) async* {
await _telemetryService.trackBannerHistoryOpened();
final s = event.map(
init: (e) => _init(),
typeChanged: (e) => _typeChanged(e.type),
sortTypeChanged: (e) => _sortTypeChanged(e.type),
versionSelected: (e) => _versionSelected(e.version),
itemsSelected: (e) => _itemsSelected(e.keys),
final s = await event.map(
init: (e) async => _init(),
typeChanged: (e) async => _typeChanged(e.type),
sortTypeChanged: (e) async => _sortTypeChanged(e.type),
versionSelected: (e) async => _versionSelected(e.version),
itemsSelected: (e) async => _itemsSelected(e.keys),
);

yield s;
Expand All @@ -45,7 +44,8 @@ class BannerHistoryBloc extends Bloc<BannerHistoryEvent, BannerHistoryState> {
return banners.map((e) => ItemCommonWithName(e.key, e.image, e.name)).toSet().toList();
}

BannerHistoryState _init() {
Future<BannerHistoryState> _init() async {
await _telemetryService.trackBannerHistoryOpened();
_characterBanners.addAll(_genshinService.getBannerHistory(BannerHistoryItemType.character));
_weaponBanners.addAll(_genshinService.getBannerHistory(BannerHistoryItemType.weapon));

Expand Down
92 changes: 52 additions & 40 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -883,40 +883,48 @@ class GenshinServiceImpl implements GenshinService {
}

@override
List<BannerHistoryPeriodModel> getBanners(double version) => _bannerHistoryFile.banners
.where((el) => el.version == version)
.map(
(e) => BannerHistoryPeriodModel(
from: e.from,
until: e.until,
type: e.type,
version: e.version,
items: e.itemKeys.map((key) {
String? imagePath;
int? rarity;
ItemType? type;
switch (e.type) {
case BannerHistoryItemType.character:
final character = getCharacter(key);
rarity = character.rarity;
imagePath = character.fullImagePath;
type = ItemType.character;
break;
case BannerHistoryItemType.weapon:
final weapon = getWeapon(key);
rarity = weapon.rarity;
imagePath = weapon.fullImagePath;
type = ItemType.weapon;
break;
default:
throw Exception('Banner history item type = ${e.type} is not valid');
}
return ItemCommonWithRarityAndType(key, imagePath, rarity, type);
}).toList(),
),
)
.toList()
..sort((x, y) => x.from.compareTo(y.from));
List<BannerHistoryPeriodModel> getBanners(double version) {
final banners = _bannerHistoryFile.banners
.where((el) => el.version == version)
.map(
(e) => BannerHistoryPeriodModel(
from: e.from,
until: e.until,
type: e.type,
version: e.version,
items: e.itemKeys.map((key) {
String? imagePath;
int? rarity;
ItemType? type;
switch (e.type) {
case BannerHistoryItemType.character:
final character = getCharacter(key);
rarity = character.rarity;
imagePath = character.fullImagePath;
type = ItemType.character;
break;
case BannerHistoryItemType.weapon:
final weapon = getWeapon(key);
rarity = weapon.rarity;
imagePath = weapon.fullImagePath;
type = ItemType.weapon;
break;
default:
throw Exception('Banner history item type = ${e.type} is not valid');
}
return ItemCommonWithRarityAndType(key, imagePath, rarity, type);
}).toList(),
),
)
.toList()
..sort((x, y) => x.from.compareTo(y.from));

if (banners.isEmpty) {
throw Exception('Banners associated to version = $version were not found');
}

return banners;
}

@override
List<ItemReleaseHistoryModel> getItemReleaseHistory(String itemKey) {
Expand All @@ -928,12 +936,16 @@ class GenshinServiceImpl implements GenshinService {
if (history.isEmpty) {
throw Exception('There is no banner history associated to itemKey = $itemKey');
}

return history
.groupListsBy((el) => el.version)
.entries
.map((e) => ItemReleaseHistoryModel(version: e.key, dates: e.value.expand((el) => el.dates).toList()))
.toList()
return history.groupListsBy((el) => el.version).entries.map((e) {
//with the multi banners, we need to group the dates to avoid showing up repeated ones
final dates = e.value
.expand((el) => el.dates)
.groupListsBy((d) => '${d.from}__${d.until}')
.values
.map((e) => ItemReleaseHistoryDatesModel(from: e.first.from, until: e.first.until))
.toList();
return ItemReleaseHistoryModel(version: e.key, dates: dates);
}).toList()
..sort((x, y) => x.version.compareTo(y.version));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/banner_history/banner_history_page.dart
Expand Up @@ -208,7 +208,7 @@ class _AppBarSearchDelegate extends SearchDelegate<List<String>> {

@override
Widget buildLeading(BuildContext context) => IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
icon: const Icon(Icons.arrow_back),
onPressed: () => close(context, selected),
);

Expand Down
47 changes: 33 additions & 14 deletions lib/presentation/banner_history/widgets/content.dart
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/presentation/banner_history/widgets/item_release_history_dialog.dart';

class Content extends StatelessWidget {
final List<BannerHistoryItemModel> banners;
Expand Down Expand Up @@ -30,10 +31,12 @@ class Content extends StatelessWidget {
final version = versions[j];
final banner = banners[i];
return _ContentCard(
banner: banner,
number: banner.versions.firstWhere((el) => el.version == version).number,
margin: margin,
cellHeight: cellHeight,
cellWidth: cellWidth,
version: version,
);
},
),
Expand All @@ -44,19 +47,23 @@ class Content extends StatelessWidget {
}

class _ContentCard extends StatelessWidget {
final BannerHistoryItemModel banner;
final EdgeInsets margin;
final double cellWidth;
final double cellHeight;
final int? number;
final double iconSize;
final double version;

const _ContentCard({
Key? key,
required this.banner,
required this.margin,
required this.cellWidth,
required this.cellHeight,
this.number,
this.iconSize = 45,
required this.version,
}) : super(key: key);

@override
Expand All @@ -66,21 +73,33 @@ class _ContentCard extends StatelessWidget {
}
final theme = Theme.of(context);
return SizedBox(
child: Container(
width: cellWidth,
height: cellHeight,
margin: margin,
child: InkWell(
onTap: number != null
? null
: () => showDialog(
context: context,
builder: (_) => ItemReleaseHistoryDialog(
itemKey: banner.key,
itemName: banner.name,
selectedVersion: version,
),
),
child: Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(top: 15, bottom: 15, left: 10, right: 10),
color: theme.brightness == Brightness.dark ? theme.colorScheme.background.withOpacity(0.2) : theme.dividerColor,
child: number != null
? Text(
'$number',
overflow: TextOverflow.ellipsis,
style: theme.textTheme.titleLarge!.copyWith(fontWeight: FontWeight.bold),
)
: Icon(Icons.check_circle, size: iconSize, color: Colors.green),
width: cellWidth,
height: cellHeight,
margin: margin,
child: Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(top: 15, bottom: 15, left: 10, right: 10),
color: theme.brightness == Brightness.dark ? theme.colorScheme.background.withOpacity(0.2) : theme.dividerColor,
child: number != null
? Text(
'$number',
overflow: TextOverflow.ellipsis,
style: theme.textTheme.titleLarge!.copyWith(fontWeight: FontWeight.bold),
)
: Icon(Icons.check_circle, size: iconSize, color: Colors.green),
),
),
),
);
Expand Down
Expand Up @@ -164,7 +164,7 @@ class _ItemCard extends StatelessWidget {
}
break;
case _ItemOptionsType.releaseHistory:
await showDialog(context: context, builder: (_) => ItemReleaseHistoryDialog(itemKey: itemKey));
await showDialog(context: context, builder: (_) => ItemReleaseHistoryDialog(itemKey: itemKey, itemName: name));
break;
}
}
Expand Down
Expand Up @@ -2,28 +2,41 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/domain/extensions/iterable_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/generated/l10n.dart';
import 'package:shiori/injection.dart';
import 'package:shiori/presentation/shared/loading.dart';
import 'package:shiori/presentation/shared/styles.dart';

const _dateFormat = 'yyyy/MM/dd';

class ItemReleaseHistoryDialog extends StatelessWidget {
final String itemKey;
final String itemName;
final double? selectedVersion;

const ItemReleaseHistoryDialog({
Key? key,
required this.itemKey,
required this.itemName,
this.selectedVersion,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final s = S.of(context);
final theme = Theme.of(context);
return BlocProvider<ItemReleaseHistoryBloc>(
create: (context) => Injection.itemReleaseHistoryBloc..add(ItemReleaseHistoryEvent.init(itemKey: itemKey)),
child: AlertDialog(
title: Text(s.bannerHistory),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(s.releaseHistory),
Text(itemName, style: theme.textTheme.subtitle2),
],
),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
Expand All @@ -36,7 +49,15 @@ class ItemReleaseHistoryDialog extends StatelessWidget {
loading: (_) => const Loading(useScaffold: false),
initial: (state) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: state.history.map((e) => _ReleasedOn(history: e)).toList(),
children: state.history
.mapIndex(
(e, i) => _ReleasedOn(
history: e,
selected: e.version == selectedVersion,
lastItem: i == state.history.length - 1,
),
)
.toList(),
),
),
),
Expand All @@ -48,37 +69,48 @@ class ItemReleaseHistoryDialog extends StatelessWidget {

class _ReleasedOn extends StatelessWidget {
final ItemReleaseHistoryModel history;
final bool selected;
final bool lastItem;

const _ReleasedOn({
Key? key,
required this.history,
required this.selected,
required this.lastItem,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final s = S.of(context);
final theme = Theme.of(context);
final dateFormat = DateFormat(_dateFormat);
return Container(
margin: const EdgeInsets.only(bottom: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
final selectedColor = selected ? theme.colorScheme.primary.withOpacity(0.5) : null;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: selectedColor,
padding: selected ? Styles.edgeInsetHorizontal5.add(const EdgeInsets.only(top: 5)) : null,
child: Text(
s.appVersion(history.version),
style: theme.textTheme.titleMedium!.copyWith(fontWeight: FontWeight.bold),
),
...history.dates.map(
(e) => Column(
),
...history.dates.map(
(e) => Container(
color: selectedColor,
padding: selected ? Styles.edgeInsetHorizontal5.add(const EdgeInsets.only(bottom: 5)) : null,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(s.fromDate(dateFormat.format(e.from))),
Text(s.untilDate(dateFormat.format(e.until))),
],
),
),
],
),
),
const Divider(),
],
);
}
}

0 comments on commit 20bf94b

Please sign in to comment.