Skip to content

Commit

Permalink
The materials are by default sorted in a grouped way
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Mar 27, 2021
1 parent 722435e commit 8fdbf70
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 189 deletions.
499 changes: 335 additions & 164 deletions assets/db/materials.json

Large diffs are not rendered by default.

33 changes: 13 additions & 20 deletions lib/application/materials/materials_bloc.dart
@@ -1,7 +1,9 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:darq/darq.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/domain/app_constants.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/enums/material_type.dart';
import 'package:genshindb/domain/models/models.dart';
Expand Down Expand Up @@ -55,16 +57,15 @@ class MaterialsBloc extends Bloc<MaterialsEvent, MaterialsState> {
String search,
int rarity = 0,
MaterialType type = MaterialType.all,
MaterialFilterType filterType = MaterialFilterType.rarity,
MaterialFilterType filterType = MaterialFilterType.grouped,
SortDirectionType sortDirectionType = SortDirectionType.asc,
}) {
final isLoaded = state is _LoadedState;
var data = _genshinService.getAllMaterialsForCard();

if (!isLoaded) {
_sortData(data, filterType, sortDirectionType);
return MaterialsState.loaded(
materials: data,
materials: _sortData(data, filterType, sortDirectionType),
search: search,
rarity: rarity,
tempRarity: rarity,
Expand Down Expand Up @@ -101,10 +102,8 @@ class MaterialsBloc extends Bloc<MaterialsEvent, MaterialsState> {
}
}

_sortData(data, filterType, sortDirectionType);

final s = currentState.copyWith.call(
materials: data,
materials: _sortData(data, filterType, sortDirectionType),
search: search,
rarity: rarity,
tempRarity: rarity,
Expand All @@ -118,28 +117,22 @@ class MaterialsBloc extends Bloc<MaterialsEvent, MaterialsState> {
return s;
}

void _sortData(
List<MaterialCardModel> _sortData(
List<MaterialCardModel> data,
MaterialFilterType filterType,
SortDirectionType sortDirectionType,
) {
switch (filterType) {
case MaterialFilterType.name:
if (sortDirectionType == SortDirectionType.asc) {
data.sort((x, y) => x.name.compareTo(y.name));
} else {
data.sort((x, y) => y.name.compareTo(x.name));
}
break;
return sortDirectionType == SortDirectionType.asc ? data.orderBy((el) => el.name).toList() : data.orderByDescending((el) => el.name).toList();
case MaterialFilterType.rarity:
if (sortDirectionType == SortDirectionType.asc) {
data.sort((x, y) => x.rarity.compareTo(y.rarity));
} else {
data.sort((x, y) => y.rarity.compareTo(x.rarity));
}
break;
return sortDirectionType == SortDirectionType.asc
? data.orderBy((el) => el.rarity).toList()
: data.orderByDescending((el) => el.rarity).toList();
case MaterialFilterType.grouped:
return sortMaterialsByGrouping(data, sortDirectionType);
default:
break;
return data;
}
}
}
45 changes: 45 additions & 0 deletions lib/domain/app_constants.dart
@@ -1,4 +1,5 @@
//This order matches the one in the game, and the numbers represent each image
import 'package:darq/darq.dart';
import 'package:genshindb/domain/enums/enums.dart';

import 'models/models.dart';
Expand Down Expand Up @@ -239,3 +240,47 @@ double getItemTotalExp(int currentLevel, int desiredLevel, bool forCharacters) {
.map((item) => item.nextLevelExp)
.fold(0, (previous, current) => previous + current);
}

List<MaterialCardModel> sortMaterialsByGrouping(List<MaterialCardModel> data, SortDirectionType sortDirectionType) {
final expChar = data.where((el) => el.type == MaterialType.expCharacter);
final expWeapon = data.where((el) => el.type == MaterialType.expWeapon);
final common = data.where((el) => el.type == MaterialType.common);
final weaponPrimary = data.where((el) => el.type == MaterialType.weaponPrimary);
final weapon = data.where((el) => el.type == MaterialType.weapon);
final stones = data.where((el) => el.type == MaterialType.elementalStone);
final jewels = data.where((el) => el.type == MaterialType.jewels);
final local = data.where((el) => el.type == MaterialType.local);
final currency = data.where((el) => el.type == MaterialType.currency);
final ingredients = data.where((el) => el.type == MaterialType.ingredient);
final talents = data.where((el) => el.type == MaterialType.talents);
final talentsWithSiblings = talents.where((el) => el.hasSiblings);
final talentsWithoutSiblings = talents.where((el) => !el.hasSiblings);

if (sortDirectionType == SortDirectionType.asc) {
return jewels.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
expChar.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
expWeapon.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
weaponPrimary.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
weapon.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
talentsWithSiblings.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
talentsWithoutSiblings.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
common.orderBy((el) => el.key).thenBy((el) => el.level).toList() +
stones.orderBy((el) => el.rarity).toList() +
local.orderBy((el) => el.rarity).toList() +
currency.orderBy((el) => el.rarity).toList() +
ingredients.orderBy((el) => el.rarity).toList();
}

return jewels.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
expChar.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
expWeapon.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
weaponPrimary.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
weapon.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
talentsWithSiblings.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
talentsWithoutSiblings.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
common.orderByDescending((el) => el.key).thenByDescending((el) => el.level).toList() +
stones.orderByDescending((el) => el.rarity).toList() +
local.orderByDescending((el) => el.rarity).toList() +
currency.orderByDescending((el) => el.rarity).toList() +
ingredients.orderByDescending((el) => el.rarity).toList();
}
1 change: 1 addition & 0 deletions lib/domain/enums/material_filter_type.dart
@@ -1,4 +1,5 @@
enum MaterialFilterType {
name,
rarity,
grouped,
}
1 change: 1 addition & 0 deletions lib/domain/models/db/materials/material_file_model.dart
Expand Up @@ -29,6 +29,7 @@ abstract class MaterialFileModel implements _$MaterialFileModel {
@required List<int> days,
@required double level,
@required List<ObtainedFromFileModel> obtainedFrom,
@required bool hasSiblings,
Map<String, dynamic> attributes,
}) = _MaterialFileModel;

Expand Down
2 changes: 2 additions & 0 deletions lib/domain/models/materials/material_card_model.dart
Expand Up @@ -12,6 +12,8 @@ abstract class MaterialCardModel implements _$MaterialCardModel {
@required int rarity,
@required String image,
@required MaterialType type,
@required double level,
@required bool hasSiblings,
@Default(0) int quantity,
@Default(0) int usedQuantity,
}) = _Item;
Expand Down
3 changes: 2 additions & 1 deletion lib/infrastructure/data_service.dart
@@ -1,5 +1,6 @@
import 'package:genshindb/domain/app_constants.dart';
import 'package:genshindb/domain/assets.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/enums/item_type.dart';
import 'package:genshindb/domain/extensions/string_extensions.dart';
import 'package:genshindb/domain/models/entities.dart';
Expand Down Expand Up @@ -240,7 +241,7 @@ class DataServiceImpl implements DataService {
}
}

return allMaterials..sort((x, y) => x.rarity.compareTo(y.rarity));
return sortMaterialsByGrouping(allMaterials, SortDirectionType.asc);
}

@override
Expand Down
2 changes: 2 additions & 0 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -585,6 +585,8 @@ class GenshinServiceImpl implements GenshinService {
rarity: material.rarity,
type: material.type,
name: translation.name,
level: material.level,
hasSiblings: material.hasSiblings,
);
}
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_en.arb
Expand Up @@ -273,5 +273,6 @@
"calcSessionInfoMsgB": "If you opted to use materials from your inventory, the order of the sessions will determine the priority in which they will be used.",
"calcSessionInfoMsgC": "The order of the items inside a session is also considered as well.",
"calcSessionInfoMsgD": "Changing the session's priority will automatically redistribute the materials only if you selected the ' {useMaterialsFromInventory} ' option.",
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page"
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page",
"grouped": "Grouped"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_es_ES.arb
Expand Up @@ -273,5 +273,6 @@
"calcSessionInfoMsgB": "Si optaste por usar materiales de tu inventario, el orden de las sesiones determinara la prioridad en la que son usados.",
"calcSessionInfoMsgC": "El orden de los items dentro de una sesión también es considerado.",
"calcSessionInfoMsgD": "Al cambiar la prioridad de una sesión, ocasionará que se redistribuyan los materiales solo si seleccionaste la opción ' {useMaterialsFromInventory} ' option.",
"calcSessionInfoMsgE": "Un indicador de los materiales usados en tus sesiones será mostrado en la página ' {myInventory} '"
"calcSessionInfoMsgE": "Un indicador de los materiales usados en tus sesiones será mostrado en la página ' {myInventory} '",
"grouped": "Agrupado"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_ru.arb
Expand Up @@ -273,5 +273,6 @@
"calcSessionInfoMsgB": "If you opted to use materials from your inventory, the order of the sessions will determine the priority in which they will be used.",
"calcSessionInfoMsgC": "The order of the items inside a session is also considered as well.",
"calcSessionInfoMsgD": "Changing the session's priority will automatically redistribute the materials only if you selected the ' {useMaterialsFromInventory} ' option.",
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page"
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page",
"grouped": "Grouped"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_zh_CN.arb
Expand Up @@ -273,5 +273,6 @@
"calcSessionInfoMsgB": "If you opted to use materials from your inventory, the order of the sessions will determine the priority in which they will be used.",
"calcSessionInfoMsgC": "The order of the items inside a session is also considered as well.",
"calcSessionInfoMsgD": "Changing the session's priority will automatically redistribute the materials only if you selected the ' {useMaterialsFromInventory} ' option.",
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page"
"calcSessionInfoMsgE": "An indicator of the used materials by your sessions will be shown in the ' {myInventory} ' page",
"grouped": "Grouped"
}
2 changes: 2 additions & 0 deletions lib/presentation/shared/extensions/i18n_extensions.dart
Expand Up @@ -359,6 +359,8 @@ extension I18nExtensions on S {
return name;
case MaterialFilterType.rarity:
return rarity;
case MaterialFilterType.grouped:
return grouped;
default:
throw Exception('Invalid material filter type = $type');
}
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Expand Up @@ -183,6 +183,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
darq:
dependency: "direct main"
description:
name: darq
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.1"
dart_style:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -22,6 +22,7 @@ environment:

dependencies:
cupertino_icons: ^1.0.2
darq: ^0.5.1
data_connection_checker: ^0.3.4
device_info: ^2.0.0
devicelocale: ^0.4.1
Expand Down

0 comments on commit 8fdbf70

Please sign in to comment.