Skip to content

Commit

Permalink
issue-120 localized sort (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
gennadyterekhov authored Jun 9, 2024
1 parent feebde4 commit 219f1d3
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 30 deletions.
37 changes: 26 additions & 11 deletions lib/layers/data/resources/effect_resource.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/not_found.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/effect.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/constant.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/data_resource.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/storage/l10n/search_indices.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/not_found.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/wrong_game.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/effect.dart';

import 'custom_localization.dart';

class EffectResource {
late String gameName;
Expand Down Expand Up @@ -45,9 +46,23 @@ class EffectResource {
return effect;
}

List<Effect> getEffectsByNames(List<String> names) {
List<Effect> getEffectsByNames(List<String> names, String locale, bool isSort) {
List<Effect> effects = names.map((name) => getEffectByName(name)).toList();

effects.sort((Effect a, Effect b) {
final aLocalized = CustomLocalization.getEffectName(
gameName: this.gameName,
englishEffectName: a.name,
languageCode: locale,
);
final bLocalized = CustomLocalization.getEffectName(
gameName: this.gameName,
englishEffectName: b.name,
languageCode: locale,
);
return aLocalized.compareTo(bLocalized);
});

return effects;
}

Expand All @@ -66,14 +81,14 @@ class EffectResource {
final List<String> englishNames = currentMap.keys.toList();

if (nameFromQuery.length <= _searchQueryLengthThreshold) {
return getEffectsByNames(englishNames);
return getEffectsByNames(englishNames, languageCode, true);
}

List<String> filteredNames =
englishNames.where((element) => element.toLowerCase().contains(nameFromQuery.toLowerCase())).toList();
List<String> filteredNames = [];

if (languageCode == Constant.lcEnglish) {
return getEffectsByNames(filteredNames);
filteredNames =
englishNames.where((element) => element.toLowerCase().contains(nameFromQuery.toLowerCase())).toList();
return getEffectsByNames(filteredNames, languageCode, true);
}

if (languageCode == Constant.lcRussian) {
Expand All @@ -89,10 +104,10 @@ class EffectResource {
filteredNames.add(correspondingEnglishName);
}

return getEffectsByNames(filteredNames);
return getEffectsByNames(filteredNames, languageCode, true);
}
}

return getEffectsByNames(filteredNames);
return getEffectsByNames(filteredNames, languageCode, true);
}
}
36 changes: 26 additions & 10 deletions lib/layers/data/resources/ingredient_resource.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/not_found.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/ingredient.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/constant.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/data_resource.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/storage/l10n/search_indices.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/not_found.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/exception/wrong_game.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/ingredient.dart';

import 'custom_localization.dart';

class IngredientResource {
String gameName;
Map<String, dynamic> currentMap = {};
static const int _searchQueryLengthThreshold = 2;

IngredientResource({this.gameName = 'skyrim'}) {
currentMap = DataResource.getMap()[gameName]['ingredients'];
}
Expand All @@ -28,33 +30,33 @@ class IngredientResource {
final List<String> englishNames = currentMap.keys.toList();

if (nameFromQuery.length <= _searchQueryLengthThreshold) {
return getIngredientsByNames(englishNames);
return getIngredientsByNames(englishNames, languageCode);
}

final filteredNames = englishNames.where((element) => element.toLowerCase().contains(nameFromQuery)).toList();

if (languageCode == Constant.lcEnglish) {
return getIngredientsByNames(filteredNames);
return getIngredientsByNames(filteredNames, languageCode);
}

if (languageCode == Constant.lcRussian) {
///now there are only russian translations for effects and ingredients
Map<String, String> localNameToEnglishNameMap =
SearchLocalizedNameIndex.allIndices[this.gameName]!['ingredients']!;
SearchLocalizedNameIndex.allIndices[this.gameName]!['ingredients']!;
List<String> localNames = localNameToEnglishNameMap.keys.toList();

final filteredLocalNames =
localNames.where((element) => element.toLowerCase().contains(nameFromQuery.toLowerCase())).toList();
localNames.where((element) => element.toLowerCase().contains(nameFromQuery.toLowerCase())).toList();

for (String foundLocalName in filteredLocalNames) {
final correspondingEnglishName = localNameToEnglishNameMap[foundLocalName]!;
filteredNames.add(correspondingEnglishName);
}

return getIngredientsByNames(filteredNames);
return getIngredientsByNames(filteredNames, languageCode);
}

return getIngredientsByNames(filteredNames);
return getIngredientsByNames(filteredNames, languageCode);
}

Map<String, Ingredient> getAllIngredients() {
Expand All @@ -65,9 +67,23 @@ class IngredientResource {
return resultMap;
}

List<Ingredient> getIngredientsByNames(List<String> names) {
List<Ingredient> getIngredientsByNames(List<String> names, String locale) {
List<Ingredient> ingredients = names.map((name) => getIngredientByName(name)).toList();

ingredients.sort((Ingredient a, Ingredient b) {
final aLocalized = CustomLocalization.getIngredientName(
gameName: this.gameName,
englishIngredientName: a.name,
languageCode: locale,
);
final bLocalized = CustomLocalization.getIngredientName(
gameName: this.gameName,
englishIngredientName: b.name,
languageCode: locale,
);
return aLocalized.compareTo(bLocalized);
});

return ingredients;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/layers/presentation/pages/effects.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:the_elder_scrolls_alchemy_client/app.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/effect.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/effect_resource.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/state_management/app_state.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/presentation/widgets/components/cards/effect/effect_small.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/effect.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/presentation/widgets/components/cards_grid.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/presentation/widgets/components/search_field.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/state_management/app_state.dart';

class EffectsPage extends StatefulWidget {
const EffectsPage({Key? key, required this.gameName}) : super(key: key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _CommonIngredientsByColumnState extends State<CommonIngredientsByColumn> {
return ingredients;
}

List<Widget> _getIngredientsCardsByEffect(Effect effect, Ingredient currentIngredient) {
List<Widget> _getIngredientsCardsByEffect(BuildContext context, Effect effect, Ingredient currentIngredient) {
List<String> names = [];
List<dynamic> tempDynamicNames = [];

Expand All @@ -47,7 +47,19 @@ class _CommonIngredientsByColumnState extends State<CommonIngredientsByColumn> {
}
}

names.sort((dynamic a, dynamic b) => a.compareTo(b));
names.sort((dynamic a, dynamic b) {
final aLocalized = CustomLocalization.getIngredientName(
gameName: widget.gameName,
englishIngredientName: a,
languageCode: TheElderScrollsAlchemyClientApp.getLocaleLanguageCode(context),
);
final bLocalized = CustomLocalization.getIngredientName(
gameName: widget.gameName,
englishIngredientName: b,
languageCode: TheElderScrollsAlchemyClientApp.getLocaleLanguageCode(context),
);
return aLocalized.compareTo(bLocalized);
});

List<Widget> ingredients = [];
ingredients.addAll(_getCards(_getIngredientsByNames(names)));
Expand All @@ -66,6 +78,7 @@ class _CommonIngredientsByColumnState extends State<CommonIngredientsByColumn> {
List<Widget> cards = [];
for (var i = 0; i < widget.ingredient.effectsNames.length; i += 1) {
final ingredientsCardsList = _getIngredientsCardsByEffect(
context,
EffectResource(gameName: widget.gameName).getEffectByName(widget.ingredient.effectsNames[i]),
widget.ingredient);
if (ingredientsCardsList.isNotEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/effect.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/business_logic/models/ingredient.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/custom_localization.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/data/resources/ingredient_resource.dart';
import 'package:the_elder_scrolls_alchemy_client/layers/presentation/widgets/components/cards/ingredient/ingredient_micro.dart';

import '../../../../../app.dart';

class IngredientsByEffect extends StatefulWidget {
const IngredientsByEffect({Key? key, required this.gameName, required this.effect}) : super(key: key);
final Effect effect;
Expand All @@ -17,13 +20,25 @@ class IngredientsByEffect extends StatefulWidget {
}

class _IngredientsByEffectState extends State<IngredientsByEffect> {
List<Ingredient> _getIngredientsByIndex(int index) {
List<Ingredient> _getIngredientsByIndex(BuildContext context, int index) {
if (index < widget.effect.ingredientsNamesByPosition.length) {
final List names = widget.effect.ingredientsNamesByPosition[index];
final List<Ingredient> ingredients =
names.map((name) => IngredientResource(gameName: widget.gameName).getIngredientByName(name)).toList();

ingredients.sort((Ingredient a, Ingredient b) => a.name.compareTo(b.name));
ingredients.sort((Ingredient a, Ingredient b) {
final aLocalized = CustomLocalization.getIngredientName(
gameName: widget.gameName,
englishIngredientName: a.name,
languageCode: TheElderScrollsAlchemyClientApp.getLocaleLanguageCode(context),
);
final bLocalized = CustomLocalization.getIngredientName(
gameName: widget.gameName,
englishIngredientName: b.name,
languageCode: TheElderScrollsAlchemyClientApp.getLocaleLanguageCode(context),
);
return aLocalized.compareTo(bLocalized);
});

return ingredients;
}
Expand All @@ -36,11 +51,11 @@ class _IngredientsByEffectState extends State<IngredientsByEffect> {
return widgets;
}

List<Widget> _getColumns() {
List<Widget> _getColumns(BuildContext context) {
List<Widget> columns = List.filled(4, Column());

for (int i = 0; i < 4; i += 1) {
final cards = _getCards(_getIngredientsByIndex(i));
final cards = _getCards(_getIngredientsByIndex(context, i));

final text = Container(
margin: const EdgeInsets.only(bottom: 10.0),
Expand Down Expand Up @@ -84,7 +99,7 @@ class _IngredientsByEffectState extends State<IngredientsByEffect> {
}
var columns;
try {
columns = _getColumns();
columns = _getColumns(context);
} catch (exception) {
return Column(children: [CupertinoActivityIndicator(), Text(exception.toString())]);
}
Expand Down
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
build:
dependency: transitive
description:
name: build
sha256: "80184af8b6cb3e5c1c4ec6d8544d27711700bc3e6d2efad04238c7b5290889f0"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
built_collection:
dependency: transitive
description:
name: built_collection
sha256: "376e3dd27b51ea877c28d525560790aee2e6fbb5f20e2f85d5081027d94e2100"
url: "https://pub.dev"
source: hosted
version: "5.1.1"
built_value:
dependency: transitive
description:
name: built_value
sha256: c7913a9737ee4007efedaffc968c049fd0f3d0e49109e778edc10de9426005cb
url: "https://pub.dev"
source: hosted
version: "8.9.2"
characters:
dependency: transitive
description:
Expand All @@ -81,6 +105,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.1"
code_builder:
dependency: transitive
description:
name: code_builder
sha256: f692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37
url: "https://pub.dev"
source: hosted
version: "4.10.0"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -129,6 +161,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
dart_style:
dependency: transitive
description:
name: dart_style
sha256: "99e066ce75c89d6b29903d788a7bb9369cf754f7b24bf70bf4b6d6d6b26853b9"
url: "https://pub.dev"
source: hosted
version: "2.3.6"
diff_match_patch:
dependency: transitive
description:
Expand Down Expand Up @@ -169,6 +209,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
float_column:
dependency: transitive
description:
Expand Down Expand Up @@ -396,6 +444,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.5"
mockito:
dependency: "direct dev"
description:
name: mockito
sha256: "6841eed20a7befac0ce07df8116c8b8233ed1f4486a7647c7fc5a02ae6163917"
url: "https://pub.dev"
source: hosted
version: "5.4.4"
mocktail:
dependency: transitive
description:
Expand Down Expand Up @@ -649,6 +705,14 @@ packages:
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
sha256: "14658ba5f669685cd3d63701d01b31ea748310f7ab854e471962670abcf57832"
url: "https://pub.dev"
source: hosted
version: "1.5.0"
source_map_stack_trace:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
flutter_bloc: ^8.1.3
get_it: ^7.7.0
dev_dependencies:
mockito: ^5.4.4
flutter_test:
sdk: flutter
bloc_test: ^9.1.4
Expand Down
Loading

0 comments on commit 219f1d3

Please sign in to comment.