Skip to content

Commit

Permalink
feat(characters): update search global
Browse files Browse the repository at this point in the history
  • Loading branch information
leandro-mancini committed Oct 22, 2022
1 parent 7e9658d commit 34b206d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 10 deletions.
27 changes: 25 additions & 2 deletions libs/flutter/characters/lib/src/characters_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,47 @@ var characterService = CharacterService();
class CharactersController = CharactersControllerBase with _$CharactersController;

abstract class CharactersControllerBase with Store {
@observable
List<Character> allCharacters = <Character>[];

@observable
List<Character> characters = <Character>[];

@observable
bool hasCharacters = false;

@observable
bool hasFilterCharacters = false;

@action
getAllCharacters() async {
characters = await characterService.getAllCharacters();
allCharacters = await characterService.getAllCharacters();
characters = allCharacters;
hasCharacters = true;
}

@action
getFilteredCharacters(CharacterFilters filters) async {
hasCharacters = false;

characters = await characterService.getFilteredCharacters(filters);
allCharacters = await characterService.getFilteredCharacters(filters);
characters = allCharacters;

hasCharacters = true;
}

@action
getFilteredToCharacter(String value) async {
if (value.isEmpty) {
hasFilterCharacters = false;
characters = allCharacters;
} else {
List<Character> searchCharacter = allCharacters
.where((element) => element.name.toLowerCase().contains(value.toLowerCase()))
.toList();

hasFilterCharacters = true;
characters = searchCharacter;
}
}
}
46 changes: 45 additions & 1 deletion libs/flutter/characters/lib/src/characters_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions libs/flutter/characters/lib/src/characters_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class _CharactersPageState extends State<CharactersPage> {
searchController: searchValueController,
textInputAction: TextInputAction.search,
onSubmitted: (value) {
Modular.to.pushNamed('/character/search/$value');
Modular.to.pushNamed('/character/search/$value')
.then((value) => charactersController.getAllCharacters());
searchValueController.clear();
},
onChanged: ((value) => charactersController.getFilteredToCharacter(value)),
actions: [
IconButton(
icon: const Icon(
Expand Down Expand Up @@ -70,9 +72,19 @@ class _CharactersPageState extends State<CharactersPage> {
return Observer(
builder: (_) {
if (charactersController.characters.isEmpty && charactersController.hasCharacters) {
return const FeedbackPageWidget(
illustration: Assets.ilSearch,
message: 'Desculpe, não conseguimos \n encontrar o personagem',
return SingleChildScrollView(
child: FeedbackPageWidget(
illustration: Assets.ilSearch,
message: 'Desculpe, não conseguimos \n encontrar o personagem',
description: 'Não se preocupe, ainda podemos \nbuscar no banco de dados.',
enabledAction: true,
textButton: 'Buscar',
onPressed: () {
Modular.to.pushNamed('/character/search/${searchValueController.text}')
.then((value) => charactersController.getAllCharacters());
searchValueController.clear();
},
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,38 @@ var characterService = CharacterService();
class SearchController = SearchControllerBase with _$SearchController;

abstract class SearchControllerBase with Store {
@observable
List<Character> allCharacters = <Character>[];

@observable
List<Character> characters = <Character>[];

@observable
bool hasCharacters = false;

@observable
bool hasFilterCharacters = false;

@action
getFilteredCharacters(String name) async {
characters = await characterService.getFilteredCharacters(CharacterFilters(name: name));

hasCharacters = true;
allCharacters = await characterService.getFilteredCharacters(CharacterFilters(name: name));
characters = allCharacters;
hasCharacters = true;
}

@action
getFilteredToSearchCharacter(String value) async {
if (value.isEmpty) {
hasFilterCharacters = false;
characters = allCharacters;
} else {
List<Character> searchCharacter = allCharacters
.where((element) => element.name.toLowerCase().contains(value.toLowerCase()))
.toList();

hasFilterCharacters = true;
characters = searchCharacter;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class _SearchPageState extends State<SearchPage> {
textInputAction: TextInputAction.search,
onSubmitted: (value) => searchController.getFilteredCharacters(value),
onPressedLeadingIcon: () => Modular.to.pop(),
onChanged: ((value) => searchController.getFilteredToSearchCharacter(value)),
),
body: SafeArea(
child: buildList(),
Expand Down

0 comments on commit 34b206d

Please sign in to comment.