Skip to content

Commit

Permalink
Aula 05 | DartWeek 4ª Edição 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecastrosales authored Sep 19, 2021
2 parents 08064c8 + c3dd624 commit 26e8918
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 85 deletions.
2 changes: 1 addition & 1 deletion lib/application/auth/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:get/get.dart';
class AuthService extends GetxService {
User? user;

void init() {
Future<void> init() async {
FirebaseAuth.instance.authStateChanges().listen(
(User? user) {
this.user = user;
Expand Down
14 changes: 14 additions & 0 deletions lib/application/bindings/application_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import 'package:app_filmes/application/rest_client/rest_client.dart';
import 'package:app_filmes/application/auth/auth_service.dart';
import 'package:app_filmes/repositories/login/login_repository_impl.dart';
import 'package:app_filmes/repositories/login/login_repository.dart';
import 'package:app_filmes/repositories/movies/movies_repository.dart';
import 'package:app_filmes/repositories/movies/movies_repository_impl.dart';
import 'package:app_filmes/services/movies/movies_service.dart';
import 'package:app_filmes/services/movies/movies_service_impl.dart';
import 'package:app_filmes/services/login/login_service.dart';
import 'package:app_filmes/services/login/login_service_impl.dart';

Expand All @@ -26,5 +30,15 @@ class ApplicationBindings implements Bindings {
Get.put(
AuthService(),
).init();
Get.lazyPut<MoviesRepository>(
() => MoviesRepositoryImpl(
restClient: Get.find(),
),
);
Get.lazyPut<MoviesService>(
() => MoviesServiceImpl(
moviesRepository: Get.find(),
),
);
}
}
98 changes: 47 additions & 51 deletions lib/application/ui/widgets/movie_card.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,68 @@
import 'package:app_filmes/application/ui/filmes_app_icons_icons.dart';
import 'package:app_filmes/models/movie_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';

class MovieCard extends StatelessWidget {
const MovieCard({Key? key}) : super(key: key);
final MovieModel movie;
final dateFormat = DateFormat('dd/MM/y');
MovieCard({Key? key, required this.movie}) : super(key: key);

@override
Widget build(BuildContext context) {
return SizedBox(
height: 280,
width: 158,
return Container(
padding: const EdgeInsets.all(10),
height: 305,
width: 170,
child: Stack(
children: [
Padding(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Material(
elevation: 2,
borderRadius: BorderRadius.circular(20),
child: ClipRRect(
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(20),
child: Image.network(
'https://br.web.img3.acsta.net/pictures/15/09/29/12/57/543717.jpg',
width: 148,
height: 184,
fit: BoxFit.cover,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Material(
elevation: 2,
borderRadius: BorderRadius.circular(15),
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
clipBehavior: Clip.antiAlias,
child: Image.network(
movie.posterPath,
height: 190,
width: 165,
fit: BoxFit.cover,
),
),
const SizedBox(height: 20),
const Text(
'Cristiano Ronaldo',
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
const Text(
'2015',
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
fontSize: 11,
color: Colors.grey,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 20),
Text(
movie.title,
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
Text(
dateFormat.format(DateTime.parse(movie.releaseDate)),
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w300,
color: Colors.grey,
),
],
),
),
],
),
Positioned(
bottom: 80,
right: -8,
bottom: 65,
right: -4,
child: Material(
elevation: 5,
shape: const CircleBorder(),
shape: CircleBorder(),
clipBehavior: Clip.antiAlias,
child: SizedBox(
height: 30,
height: 40,
child: IconButton(
iconSize: 13,
icon: const Icon(
FilmesAppIcons.heart,
iconSize: 18,
icon: Icon(
FilmesAppIcons.heartEmpty,
color: Colors.grey,
),
onPressed: () {},
Expand All @@ -78,4 +74,4 @@ class MovieCard extends StatelessWidget {
),
);
}
}
}
45 changes: 45 additions & 0 deletions lib/models/movie_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:convert';

class MovieModel {
MovieModel({
required this.id,
required this.title,
required this.releaseDate,
required this.posterPath,
required this.genres,
required this.favorite,
});

final int id;
final String title;
final String releaseDate;
final String posterPath;
final List<int> genres;
final bool favorite;

Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'release_date': releaseDate,
'poster_path': posterPath,
'genre_ids': genres,
'favorite': favorite,
};
}

factory MovieModel.fromMap(Map<String, dynamic> map) {
return MovieModel(
id: map['id'] ?? 0,
title: map['title'] ?? '',
releaseDate: map['release_date'] ?? '',
posterPath: 'https://image.tmdb.org/t/p/w200/${map['poster_path']}',
genres: List<int>.from(map['genre_ids'] ?? const []),
favorite: map['favorite'] ?? false,
);
}

String toJson() => json.encode(toMap());

factory MovieModel.fromJson(String source) => MovieModel.fromMap(json.decode(source));
}
1 change: 1 addition & 0 deletions lib/modules/movies/movies_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MoviesBindings implements Bindings {
Get.lazyPut(
() => MoviesController(
genresService: Get.find(),
moviesService: Get.find(),
),
);
}
Expand Down
84 changes: 78 additions & 6 deletions lib/modules/movies/movies_controller.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import 'package:app_filmes/services/movies/movies_service.dart';
import 'package:get/get.dart';

import 'package:app_filmes/application/ui/messages/messages_mixin.dart';
import 'package:app_filmes/models/genre_model.dart';
import 'package:app_filmes/models/movie_model.dart';
import 'package:app_filmes/services/genres/genres_service.dart';

class MoviesController extends GetxController with MessagesMixin {
final GenresService _genresService;
final MoviesService _moviesService;
final _message = Rxn<MessageModel>();
final genres = <GenreModel>[].obs;

final popularMovies = <MovieModel>[].obs;
final topRatedMovies = <MovieModel>[].obs;

var _popularMoviesOriginal = <MovieModel>[];
var _topRatedMoviesOriginal = <MovieModel>[];
final genreSelected = Rxn<GenreModel>();

MoviesController({
required GenresService genresService,
}) : _genresService = genresService;
required MoviesService moviesService,
}) : _genresService = genresService,
_moviesService = moviesService;

@override
void onInit() {
Expand All @@ -20,18 +32,78 @@ class MoviesController extends GetxController with MessagesMixin {
}

@override
void onReady() async {
Future<void> onReady() async {
super.onReady();
try {
final genres = await _genresService.getGenres();
this.genres.assignAll(genres);
} catch (e) {
final genresData = await _genresService.getGenres();
genres.assignAll(genresData);

final popularMoviesData = await _moviesService.getPopularMovies();
final topRatedMoviesData = await _moviesService.getTopRatedMovies();

popularMovies.assignAll(popularMoviesData);
_popularMoviesOriginal = popularMoviesData;
topRatedMovies.assignAll(topRatedMoviesData);
_topRatedMoviesOriginal = topRatedMoviesData;
} catch (e, s) {
print(e);
print(s);
_message(
MessageModel.error(
title: 'Erro',
message: 'Erro ao buscar Categorias',
message: 'Erro ao carregar os dados da Página',
),
);
}
}

void filterByName(String title) {
if (title.isNotEmpty) {
var newPopularMovies = _popularMoviesOriginal.where(
(movie) {
return movie.title.toLowerCase().contains(
title.toLowerCase(),
);
},
);
var newTopRatedMovies = _topRatedMoviesOriginal.where(
(movie) {
return movie.title.toLowerCase().contains(
title.toLowerCase(),
);
},
);
popularMovies.assignAll(newPopularMovies);
topRatedMovies.assignAll(newTopRatedMovies);
} else {
popularMovies.assignAll(_popularMoviesOriginal);
topRatedMovies.assignAll(_topRatedMoviesOriginal);
}
}

void filterMoviesByGenre(GenreModel? genreModel) {
if (genreModel?.id == genreSelected.value?.id) {
genreModel = null;
}

genreSelected.value = genreModel;

if (genreModel != null) {
var newPopularMovies = _popularMoviesOriginal.where(
(movie) {
return movie.genres.contains(genreModel?.id);
},
);
var newTopRatedMovies = _topRatedMoviesOriginal.where(
(movie) {
return movie.genres.contains(genreModel?.id);
},
);
popularMovies.assignAll(newPopularMovies);
topRatedMovies.assignAll(newTopRatedMovies);
} else {
popularMovies.assignAll(_popularMoviesOriginal);
topRatedMovies.assignAll(_topRatedMoviesOriginal);
}
}
}
26 changes: 15 additions & 11 deletions lib/modules/movies/movies_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ import 'package:flutter/material.dart';

import 'package:get/get.dart';

import 'movies_controller.dart';
import 'widgets/movies_filters.dart';
import 'widgets/movies_group.dart';
import 'widgets/movies_header.dart';
class MoviesPage extends GetView<MoviesController> {
const MoviesPage({Key? key}) : super(key: key);

class MoviesPage extends StatelessWidget {

const MoviesPage({ Key? key }) : super(key: key);

@override
Widget build(BuildContext context) {
return SizedBox(
width: Get.width,
height: Get.height,
child: ListView(
children: const [
MoviesHeader(),
MoviesFilters(),
MoviesGroup(title: 'Mais Populares'),
MoviesGroup(title: 'Top Filmes'),
children: [
const MoviesHeader(),
const MoviesFilters(),
MoviesGroup(
title: 'Mais populares',
movies: controller.popularMovies,
),
MoviesGroup(
title: 'Top Filmes',
movies: controller.topRatedMovies,
),
],
),
);
}
}
}
8 changes: 4 additions & 4 deletions lib/modules/movies/widgets/movies_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class MoviesFilters extends GetView<MoviesController> {
return Row(
children: controller.genres
.map(
(g) => FilterTag(
model: g,
onPressed: () {},
selected: false,
(genre) => FilterTag(
model: genre,
onPressed: () => controller.filterMoviesByGenre(genre),
selected: controller.genreSelected.value?.id == genre.id,
),
)
.toList(),
Expand Down
Loading

0 comments on commit 26e8918

Please sign in to comment.