Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concluido branch level 2 #14

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Feat: finalizado level 2
  • Loading branch information
Bielz99 committed Jul 23, 2024
commit 9169f3aef50ebd90f2842712d7979a8048c68c83
11 changes: 9 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import 'package:flutter/material.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:mottu_marvel/src/application/firebase/firebase_class.dart';
import 'package:mottu_marvel/src/application/pages/characters/characters_module.dart';
import 'package:mottu_marvel/src/application/ui/app_ui_config.dart';
import 'package:mottu_marvel/src/life_cycle_controller.dart';

void main() async {
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

await GetStorage.init();

final firebaseClass = FirebaseClass();
await Future.wait([
firebaseClass.initializeFirebase(),
]);
Get.put(LifeCycleController());

runApp(const MyApp());
}

Expand Down
11 changes: 11 additions & 0 deletions lib/src/application/pages/characters/characters_bindings.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:mottu_marvel/src/application/firebase/firebase_class.dart';
import 'package:mottu_marvel/src/application/pages/characters/characters_controller.dart';
import 'package:mottu_marvel/src/repositories/characters/characters_repository.dart';
import 'package:mottu_marvel/src/repositories/characters/characters_repository_impl.dart';
import 'package:mottu_marvel/src/rest_client/custom_dio.dart';
import 'package:mottu_marvel/src/services/characters/characters_service.dart';
import 'package:mottu_marvel/src/services/characters/characters_service_impl.dart';
import 'package:mottu_marvel/src/storage/storage_service.dart';

class CharactersBindings implements Bindings {
@override
Expand All @@ -16,10 +18,19 @@ class CharactersBindings implements Bindings {
Get.lazyPut<FirebaseClass>(
() => FirebaseClass(),
);
Get.lazyPut<StorageService>(
() => StorageService(
Get.find(),
),
);
Get.lazyPut<GetStorage>(
() => GetStorage(),
);
Get.lazyPut<CharactersRepository>(
() => CharactersRepositoryImpl(
dio: Get.find(),
firebaseClass: Get.find(),
storageService: Get.find(),
),
);
Get.lazyPut<CharactersService>(
Expand Down
71 changes: 68 additions & 3 deletions lib/src/application/pages/characters/characters_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CharactersController extends GetxController
late final Worker workerPage;

final _offset = 0.obs;
final _limit = 5;
final _limit = 1;
final RxString search = ''.obs;
final loading = false.obs;

Expand All @@ -27,8 +27,10 @@ class CharactersController extends GetxController
super.onInit();

loaderListener(loading);

//getAllCharacters();
workerPage = ever<int>(_offset, (_) {
print('Offset alterado para: $_offset');
getAllCharacters();
});
}

@override
Expand Down Expand Up @@ -60,4 +62,67 @@ class CharactersController extends GetxController
loading(false);
}
}

Future<void> searchCharacter() async {
if (search.value.isNotEmpty) {
try {
loading(true);
final result = await _charactersService.searchCharacter(search.value);
allCharacters.assignAll(result);
} catch (e, s) {
log('Erro ao buscar personagens: $e', stackTrace: s);
} finally {
loading(false);
}
} else {
log('O campo de busca está vazio.');
}
}

Future<void> resetCharacters() async {
search.value = '';
allCharacters.clear();
await getAllCharacters();
}

Future<void> fetchRelatedCharacters(
AllCharactersResultModel character) async {
try {
loading(true);

var comicsIds = character.comics!.items!
.map((c) => c.resourceURI?.split('/').last)
.take(10)
.join(',');
var seriesIds = character.series!.items!
.map((s) => s.resourceURI?.split('/').last)
.take(10)
.join(',');
var storiesIds = character.stories!.items!
.map((s) => s.resourceURI?.split('/').last)
.take(10)
.join(',');
var eventsIds = character.events!.items!
.map((e) => e.resourceURI?.split('/').last)
.take(10)
.join(',');

final result = await _charactersService.getCharactersByFilter(
comicsIds,
seriesIds,
storiesIds,
eventsIds,
);

if (result.isNotEmpty) {
relatedCharacters.assignAll(result);
} else {
relatedCharacters.clear();
}
} catch (e, s) {
log('Erro ao buscar personagens relacionados: $e', stackTrace: s);
} finally {
loading(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mottu_marvel/src/application/pages/characters/characters_controller.dart';
import 'package:mottu_marvel/src/application/pages/characters/widgets/characters_card.dart';
import 'package:mottu_marvel/src/models/characters/characters_result/all_characters_result_model.dart';

class CharactersDetailsPage extends StatelessWidget {
final AllCharactersResultModel character;

const CharactersDetailsPage({
super.key,
required this.character,
});

@override
Widget build(BuildContext context) {
final CharactersController controller = Get.find();

WidgetsBinding.instance.addPostFrameCallback((_) {
controller.fetchRelatedCharacters(character);
});

String description = character.description ?? '';
if (description == "") {
description = 'Description not found';
}

return Scaffold(
appBar: AppBar(
title: Text(character.name ?? 'Detalhes do Personagem'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: Get.height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'${character.thumbnail?.path}.${character.thumbnail?.extension}',
),
fit: BoxFit.cover),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: Get.height * 0.04,
color: Colors.black.withOpacity(0.5),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 6.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
character.name ?? '',
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: Colors.white,
),
),
],
),
),
),
],
),
),
SizedBox(
height: Get.height * 0.030,
),
Text(
description,
style: const TextStyle(color: Colors.white),
),
SizedBox(
height: Get.height * 0.030,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
color: Colors.red,
height: Get.height * 0.07,
width: Get.width * 0.29,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Comics'),
Text('${character.comics?.available ?? 0}'),
],
),
),
Container(
color: Colors.red,
height: Get.height * 0.07,
width: Get.width * 0.29,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Stories'),
Text('${character.stories?.available ?? 0}'),
],
),
),
Container(
color: Colors.red,
height: Get.height * 0.07,
width: Get.width * 0.29,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Series'),
Text('${character.series?.available ?? 0}'),
],
),
),
],
),
SizedBox(
height: Get.height * 0.030,
),
const Text(
'Related Characters',
style: TextStyle(color: Colors.white),
),
SizedBox(
height: Get.height * 0.020,
),
Obx(() {
final relatedCharacters = controller.relatedCharacters;
if (relatedCharacters.isEmpty) {
return const Center(
child: Text('No related characters found.'));
}

return GridView.builder(
padding: const EdgeInsets.all(8.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.2,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
),
itemCount: relatedCharacters.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (_, index) {
final relatedCharacter = relatedCharacters[index];
return CharactersCard(
title: relatedCharacter.name ?? '',
url:
'${relatedCharacter.thumbnail?.path}.${relatedCharacter.thumbnail?.extension}',
);
},
);
}),
],
),
),
),
);
}
}
53 changes: 49 additions & 4 deletions lib/src/application/pages/characters/characters_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mottu_marvel/src/application/pages/characters/characters_details/characters_details_page.dart';

import 'characters_controller.dart';
import 'widgets/characters_card.dart';
Expand All @@ -9,7 +10,44 @@ class CharactersPage extends GetView<CharactersController> {

@override
Widget build(BuildContext context) {
final searchEC = TextEditingController();
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(100.0),
child: AppBar(
title: const Text('Marvel Characters'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
controller.resetCharacters();
},
),
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(48.0),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: searchEC,
decoration: InputDecoration(
hintText: 'Search Characters',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
filled: true,
fillColor: Colors.white,
),
onFieldSubmitted: (value) {
controller.search.value = value;
controller.searchCharacter();
searchEC.clear();
},
),
),
),
),
),
body: SafeArea(
child: Obx(() {
if (controller.allCharacters.isEmpty) {
Expand Down Expand Up @@ -41,10 +79,17 @@ class CharactersPage extends GetView<CharactersController> {
itemCount: controller.allCharacters.length,
itemBuilder: (_, index) {
final character = controller.allCharacters[index];
return CharactersCard(
title: character.name ?? '',
url:
'${character.thumbnail?.path}.${character.thumbnail?.extension}',
return GestureDetector(
onTap: () {
Get.to(() => CharactersDetailsPage(
character: character,
));
},
child: CharactersCard(
title: character.name ?? '',
url:
'${character.thumbnail?.path}.${character.thumbnail?.extension}',
),
);
},
),
Expand Down
Loading