From 01048fab430aa6a322d31ef82ce43391144ed17d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pegoraro?= Date: Thu, 31 Aug 2023 16:55:21 -0300 Subject: [PATCH 1/3] atualizei chaves do json da nova api --- lib/data/api/api_service.dart | 14 ++----- lib/data/models/campeonato.dart | 11 +++--- lib/data/models/noticia.dart | 18 ++++++--- lib/data/models/partida.dart | 43 +++++++++++---------- lib/data/models/posicao.dart | 35 ++++++++--------- lib/data/repositories/auth_repository.dart | 44 ---------------------- lib/ui/app/widgets/app_scaffold.dart | 27 ------------- lib/ui/notifiers/auth_notifier.dart | 32 ---------------- pubspec.lock | 24 ------------ pubspec.yaml | 1 - 10 files changed, 64 insertions(+), 185 deletions(-) delete mode 100644 lib/data/repositories/auth_repository.dart delete mode 100644 lib/ui/notifiers/auth_notifier.dart diff --git a/lib/data/api/api_service.dart b/lib/data/api/api_service.dart index 7fd04db..2b19ea0 100644 --- a/lib/data/api/api_service.dart +++ b/lib/data/api/api_service.dart @@ -9,7 +9,6 @@ import 'package:meu_mengao/data/models/campeonato.dart'; import 'package:meu_mengao/data/models/noticia.dart'; import 'package:meu_mengao/data/models/partida.dart'; import 'package:meu_mengao/data/models/posicao.dart'; -import 'package:meu_mengao/data/repositories/auth_repository.dart'; abstract class ApiService { Future?> getNoticias(); @@ -28,10 +27,6 @@ abstract class ApiService { } class ApiServiceImpl extends ApiService { - ApiServiceImpl(this._authRepository); - - final AuthRepository _authRepository; - @override Future?> getNoticias() async { return _getData("noticias", (response) { @@ -94,17 +89,16 @@ class ApiServiceImpl extends ApiService { try { final baseUrl = dotenv.env['BASE_URL']; final url = Uri.parse("$baseUrl$endpoint"); - final token = await _authRepository.getToken(); - final response = await http.get(url, headers: {'Authorization': 'Bearer $token'}); + final response = await http.get(url); return response.statusCode == 200 ? parseResponse(response) : null; - } catch (e) { + } catch (e, stackTrace) { if (kDebugMode) print(e); + print(stackTrace); return null; } } } final apiServiceProvider = Provider((ref) { - final authRepository = ref.watch(authRepositoryProvider); - return ApiServiceImpl(authRepository); + return ApiServiceImpl(); }); diff --git a/lib/data/models/campeonato.dart b/lib/data/models/campeonato.dart index 1ee6954..00438ce 100644 --- a/lib/data/models/campeonato.dart +++ b/lib/data/models/campeonato.dart @@ -44,8 +44,8 @@ class Campeonato { 'nome': nome, 'ano': ano, 'logo': logo, - 'rodadaAtual': rodadaAtual, - 'possuiClassificacao': possuiClassificacao, + 'rodada_atual': rodadaAtual, + 'possui_classificacao': possuiClassificacao, }; } @@ -55,8 +55,8 @@ class Campeonato { nome: map['nome'] as String, ano: map['ano'] as String, logo: map['logo'] as String, - rodadaAtual: map['rodadaAtual'] as int, - possuiClassificacao: map['possuiClassificacao'] as bool, + rodadaAtual: map['rodada_atual'] as int, + possuiClassificacao: map['possui_classificacao'] as bool, ); } @@ -89,5 +89,6 @@ class Campeonato { String toJson() => json.encode(toMap()); - factory Campeonato.fromJson(String source) => Campeonato.fromMap(json.decode(source) as Map); + factory Campeonato.fromJson(String source) => + Campeonato.fromMap(json.decode(source) as Map); } diff --git a/lib/data/models/noticia.dart b/lib/data/models/noticia.dart index a1ac012..abc8d7e 100644 --- a/lib/data/models/noticia.dart +++ b/lib/data/models/noticia.dart @@ -44,7 +44,7 @@ class Noticia { 'link': link, 'data': data?.millisecondsSinceEpoch, 'titulo': titulo, - 'logoSite': logoSite, + 'logo_site': logoSite, 'foto': foto, }; } @@ -53,9 +53,11 @@ class Noticia { return Noticia( id: map['id'] as int, link: map['link'] as String, - data: map['data'] != null ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(map['data']) ?? 0) : null, + data: map['data'] != null + ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(map['data']) ?? 0) + : null, titulo: map['titulo'] as String, - logoSite: map['logoSite'] as String, + logoSite: map['logo_site'] as String, foto: map['foto'] != null ? map['foto'] as String : null, ); } @@ -79,10 +81,16 @@ class Noticia { @override int get hashCode { - return id.hashCode ^ link.hashCode ^ data.hashCode ^ titulo.hashCode ^ logoSite.hashCode ^ foto.hashCode; + return id.hashCode ^ + link.hashCode ^ + data.hashCode ^ + titulo.hashCode ^ + logoSite.hashCode ^ + foto.hashCode; } String toJson() => json.encode(toMap()); - factory Noticia.fromJson(String source) => Noticia.fromMap(json.decode(source) as Map); + factory Noticia.fromJson(String source) => + Noticia.fromMap(json.decode(source) as Map); } diff --git a/lib/data/models/partida.dart b/lib/data/models/partida.dart index 048e1dd..d1f834b 100644 --- a/lib/data/models/partida.dart +++ b/lib/data/models/partida.dart @@ -96,16 +96,16 @@ class Partida { return { 'id': id, 'campeonato': campeonato, - 'campeonatoId': campeonatoId, + 'campeonato_id': campeonatoId, 'data': data?.millisecondsSinceEpoch.toString(), - 'rodadaName': rodadaName, - 'rodadaIndex': rodadaIndex, - 'timeCasa': timeCasa, - 'golsCasa': golsCasa, - 'timeFora': timeFora, - 'golsFora': golsFora, - 'escudoCasa': escudoCasa, - 'escudoFora': escudoFora, + 'rodada_name': rodadaName, + 'rodada_index': rodadaIndex, + 'time_casa': timeCasa, + 'gols_casa': golsCasa, + 'time_fora': timeFora, + 'gols_fora': golsFora, + 'escudo_casa': escudoCasa, + 'escudo_fora': escudoFora, }; } @@ -113,16 +113,18 @@ class Partida { return Partida( id: map['id'] as String, campeonato: map['campeonato'] as String, - campeonatoId: map['campeonatoId'] as String, - data: map['data'] != null ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(map['data']) ?? 0) : null, - rodadaName: map['rodadaName'] != null ? map['rodadaName'] as String : null, - rodadaIndex: map['rodadaIndex'] as int?, - timeCasa: map['timeCasa'] as String, - golsCasa: int.tryParse(map['golsCasa']), - timeFora: map['timeFora'] as String, - golsFora: int.tryParse(map['golsFora']), - escudoCasa: map['escudoCasa'] as String, - escudoFora: map['escudoFora'] as String, + campeonatoId: map['campeonato_id'] as String, + data: map['data'] != null + ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(map['data']) ?? 0) + : null, + rodadaName: map['rodada_name'] != null ? map['rodada_name'] as String : null, + rodadaIndex: map['rodada_index'] as int?, + timeCasa: map['time_casa'] as String, + golsCasa: int.tryParse(map['gols_casa']), + timeFora: map['time_fora'] as String, + golsFora: int.tryParse(map['gols_fora']), + escudoCasa: map['escudo_casa'] as String, + escudoFora: map['escudo_fora'] as String, ); } @@ -167,5 +169,6 @@ class Partida { String toJson() => json.encode(toMap()); - factory Partida.fromJson(String source) => Partida.fromMap(json.decode(source) as Map); + factory Partida.fromJson(String source) => + Partida.fromMap(json.decode(source) as Map); } diff --git a/lib/data/models/posicao.dart b/lib/data/models/posicao.dart index c38e64f..5e2827d 100644 --- a/lib/data/models/posicao.dart +++ b/lib/data/models/posicao.dart @@ -78,19 +78,19 @@ class Posicao { return { 'id': id, 'posicao': posicao, - 'nomeTime': nomeTime, - 'escudoTime': escudoTime, + 'nome_time': nomeTime, + 'escudo_time': escudoTime, 'pontos': pontos, 'jogos': jogos, 'vitorias': vitorias, 'empates': empates, 'derrotas': derrotas, - 'golsFeitos': golsFeitos, - 'golsSofridos': golsSofridos, - 'saldoGols': saldoGols, - 'campeonatoId': campeonatoId, - 'classificacaoName': classificacaoName, - 'classificacaoIndex': classificacaoIndex, + 'gols_feitos': golsFeitos, + 'gols_sofridos': golsSofridos, + 'saldo_gols': saldoGols, + 'campeonato_id': campeonatoId, + 'classificacao_name': classificacaoName, + 'classificacao_index': classificacaoIndex, }; } @@ -98,19 +98,19 @@ class Posicao { return Posicao( id: map['id'] as String, posicao: int.tryParse(map['posicao']) ?? 0, - nomeTime: map['nomeTime'] as String, - escudoTime: map['escudoTime'] != null ? map['escudoTime'] as String : null, + nomeTime: map['nome_time'] as String, + escudoTime: map['escudo_time'] != null ? map['escudo_time'] as String : null, pontos: map['pontos'] as String, jogos: map['jogos'] as String, vitorias: map['vitorias'] as String, empates: map['empates'] as String, derrotas: map['derrotas'] as String, - golsFeitos: map['golsFeitos'] as String, - golsSofridos: map['golsSofridos'] as String, - saldoGols: map['saldoGols'] as String, - campeonatoId: map['campeonatoId'] as String, - classificacaoName: map['classificacaoName'] as String, - classificacaoIndex: map['classificacaoIndex'] as int, + golsFeitos: map['gols_feitos'] as String, + golsSofridos: map['gols_sofridos'] as String, + saldoGols: map['saldo_gols'] as String, + campeonatoId: map['campeonato_id'] as String, + classificacaoName: map['classificacao_name'] as String, + classificacaoIndex: map['classificacao_index'] as int, ); } @@ -161,5 +161,6 @@ class Posicao { String toJson() => json.encode(toMap()); - factory Posicao.fromJson(String source) => Posicao.fromMap(json.decode(source) as Map); + factory Posicao.fromJson(String source) => + Posicao.fromMap(json.decode(source) as Map); } diff --git a/lib/data/repositories/auth_repository.dart b/lib/data/repositories/auth_repository.dart deleted file mode 100644 index 30fc854..0000000 --- a/lib/data/repositories/auth_repository.dart +++ /dev/null @@ -1,44 +0,0 @@ -import 'package:firebase_auth/firebase_auth.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:shared_preferences/shared_preferences.dart'; - -abstract class AuthRepository { - Future signInAnonymously(); - - Future saveToken(String token); - - Future getToken(); -} - -class AuthRepositoryImpl extends AuthRepository { - @override - Future signInAnonymously() async { - final authData = await FirebaseAuth.instance.signInAnonymously(); - final token = await authData.user?.getIdToken(); - if (token != null) { - await saveToken(token); - } - - return token; - } - - @override - Future saveToken(String token) async { - final prefs = await _getPrefs(); - return await prefs.setString(_tokenKey, token); - } - - @override - Future getToken() async { - final prefs = await _getPrefs(); - return prefs.getString(_tokenKey); - } - - Future _getPrefs() async => await SharedPreferences.getInstance(); - - static const _tokenKey = "TOKEN"; -} - -final authRepositoryProvider = Provider((ref) { - return AuthRepositoryImpl(); -}); diff --git a/lib/ui/app/widgets/app_scaffold.dart b/lib/ui/app/widgets/app_scaffold.dart index 07ec80c..d6e5a29 100644 --- a/lib/ui/app/widgets/app_scaffold.dart +++ b/lib/ui/app/widgets/app_scaffold.dart @@ -4,7 +4,6 @@ import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart'; import 'package:meu_mengao/ui/app/widgets/app_bottom_nav.dart'; import 'package:meu_mengao/ui/app/widgets/app_drawer.dart'; import 'package:meu_mengao/ui/noticias/tela_noticias.dart'; -import 'package:meu_mengao/ui/notifiers/auth_notifier.dart'; import 'package:meu_mengao/ui/widgets/topbar.dart'; import '../../calendario/tela_calendario.dart'; @@ -40,10 +39,6 @@ class _AppScaffoldState extends ConsumerState { void initState() { super.initState(); - WidgetsBinding.instance.addPostFrameCallback((_) { - ref.read(authNotifierProvider).signIn(); - }); - _pagerController = PageController( initialPage: _pageIndex, keepPage: true, @@ -97,28 +92,6 @@ class _AppScaffoldState extends ConsumerState { ), body: SafeArea( child: Builder(builder: (_) { - final authNotifier = ref.watch(authNotifierProvider); - final isLoading = authNotifier.isLoading; - final authToken = authNotifier.token; - - if (isLoading) { - return const Center( - child: CircularProgressIndicator(), - ); - } - - if (authToken == null || authToken.isEmpty) { - return const Padding( - padding: EdgeInsets.all(30.0), - child: Center( - child: Text( - "Ocorreu um erro ao tentar se conectar ao servidor. Tente novamente mais tarde ou entre em contato com o desenvolvedor", - textAlign: TextAlign.center, - ), - ), - ); - } - return PageView( controller: _pagerController, onPageChanged: (index) => setState(() { diff --git a/lib/ui/notifiers/auth_notifier.dart b/lib/ui/notifiers/auth_notifier.dart deleted file mode 100644 index 194d52a..0000000 --- a/lib/ui/notifiers/auth_notifier.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:meu_mengao/data/repositories/auth_repository.dart'; - -class AuthNotifier extends ChangeNotifier { - AuthNotifier(this._authRepository); - final AuthRepository _authRepository; - - bool _isLoading = false; - bool get isLoading => _isLoading; - - String? _token; - String? get token => _token; - - void signIn() { - _isLoading = true; - notifyListeners(); - - _authRepository.signInAnonymously().then((token) { - _token = token; - notifyListeners(); - }).whenComplete(() { - _isLoading = false; - notifyListeners(); - }); - } -} - -final authNotifierProvider = ChangeNotifierProvider((ref) { - final authRepository = ref.watch(authRepositoryProvider); - return AuthNotifier(authRepository); -}); diff --git a/pubspec.lock b/pubspec.lock index db1c986..fc5896a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -121,30 +121,6 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.4" - firebase_auth: - dependency: "direct main" - description: - name: firebase_auth - sha256: "64ac4dc04b51aab9d17c23b496c90f948b9ce2065d7b83e0829c7a497d88f9ce" - url: "https://pub.dev" - source: hosted - version: "4.6.1" - firebase_auth_platform_interface: - dependency: transitive - description: - name: firebase_auth_platform_interface - sha256: "63fd67d125ae483722ff3742953e2e06bbc1e6cb3da68e5f7f4430d5f82f9373" - url: "https://pub.dev" - source: hosted - version: "6.15.1" - firebase_auth_web: - dependency: transitive - description: - name: firebase_auth_web - sha256: "241a4ecce80da2014e3cd93d7b7e1a66e9b683e4241d466d73676ac90599b805" - url: "https://pub.dev" - source: hosted - version: "5.5.1" firebase_core: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index f7ffc53..d78af77 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: sdk: flutter firebase_core: ^2.13.0 - firebase_auth: ^4.6.1 collection: ^1.17.1 url_launcher: ^6.1.11 intl: ^0.18.1 From 47af39ea9e012be325ed5bb30cb3bac6949c4cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pegoraro?= Date: Thu, 31 Aug 2023 17:40:04 -0300 Subject: [PATCH 2/3] terminei de integrar nova api --- lib/data/api/api_service.dart | 6 ++++-- lib/data/database/entities/campeonato_entity.dart | 6 ------ lib/data/models/campeonato.dart | 10 +--------- lib/data/models/noticia.dart | 4 ++-- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/data/api/api_service.dart b/lib/data/api/api_service.dart index 2b19ea0..f291910 100644 --- a/lib/data/api/api_service.dart +++ b/lib/data/api/api_service.dart @@ -92,8 +92,10 @@ class ApiServiceImpl extends ApiService { final response = await http.get(url); return response.statusCode == 200 ? parseResponse(response) : null; } catch (e, stackTrace) { - if (kDebugMode) print(e); - print(stackTrace); + if (kDebugMode) { + print(e); + print(stackTrace); + } return null; } } diff --git a/lib/data/database/entities/campeonato_entity.dart b/lib/data/database/entities/campeonato_entity.dart index fb01e90..46d1615 100644 --- a/lib/data/database/entities/campeonato_entity.dart +++ b/lib/data/database/entities/campeonato_entity.dart @@ -6,7 +6,6 @@ class CampeonatoEntity { required this.id, required this.nome, required this.ano, - required this.logo, required this.rodadaAtual, required this.possuiClassificacao, }); @@ -14,7 +13,6 @@ class CampeonatoEntity { final String? id; final String? nome; final String? ano; - final String? logo; final int? rodadaAtual; final bool? possuiClassificacao; @@ -23,7 +21,6 @@ class CampeonatoEntity { id: id ?? "", nome: nome ?? "", ano: ano ?? "", - logo: logo ?? "", rodadaAtual: rodadaAtual ?? 0, possuiClassificacao: possuiClassificacao ?? false, ); @@ -34,7 +31,6 @@ class CampeonatoEntity { id: campeonato.id, nome: campeonato.nome, ano: campeonato.ano, - logo: campeonato.logo, rodadaAtual: campeonato.rodadaAtual, possuiClassificacao: campeonato.possuiClassificacao, ); @@ -45,7 +41,6 @@ class CampeonatoEntity { idColumn: id, nomeColumn: nome, anoColumn: ano, - logoColumn: logo, rodadaAtualColumn: rodadaAtual, possuiClassificacaoColumn: possuiClassificacao == true ? 1 : 0, }; @@ -56,7 +51,6 @@ class CampeonatoEntity { id: map[idColumn] as String?, nome: map[nomeColumn] as String?, ano: map[anoColumn] as String?, - logo: map[logoColumn] as String?, rodadaAtual: map[rodadaAtualColumn] as int?, possuiClassificacao: (map[possuiClassificacaoColumn] as int?) != 0, ); diff --git a/lib/data/models/campeonato.dart b/lib/data/models/campeonato.dart index 00438ce..c64ace1 100644 --- a/lib/data/models/campeonato.dart +++ b/lib/data/models/campeonato.dart @@ -6,7 +6,6 @@ class Campeonato { required this.id, required this.nome, required this.ano, - required this.logo, required this.rodadaAtual, required this.possuiClassificacao, }); @@ -14,7 +13,6 @@ class Campeonato { final String id; final String nome; final String ano; - final String logo; final int rodadaAtual; final bool possuiClassificacao; @@ -24,7 +22,6 @@ class Campeonato { String? id, String? nome, String? ano, - String? logo, int? rodadaAtual, bool? possuiClassificacao, }) { @@ -32,7 +29,6 @@ class Campeonato { id: id ?? this.id, nome: nome ?? this.nome, ano: ano ?? this.ano, - logo: logo ?? this.logo, rodadaAtual: rodadaAtual ?? this.rodadaAtual, possuiClassificacao: possuiClassificacao ?? this.possuiClassificacao, ); @@ -43,7 +39,6 @@ class Campeonato { 'id': id, 'nome': nome, 'ano': ano, - 'logo': logo, 'rodada_atual': rodadaAtual, 'possui_classificacao': possuiClassificacao, }; @@ -54,7 +49,6 @@ class Campeonato { id: map['id'] as String, nome: map['nome'] as String, ano: map['ano'] as String, - logo: map['logo'] as String, rodadaAtual: map['rodada_atual'] as int, possuiClassificacao: map['possui_classificacao'] as bool, ); @@ -62,7 +56,7 @@ class Campeonato { @override String toString() { - return 'Campeonato(id: $id, nome: $nome, ano: $ano, logo: $logo, rodadaAtual: $rodadaAtual, possuiClassificacao: $possuiClassificacao)'; + return 'Campeonato(id: $id, nome: $nome, ano: $ano, rodadaAtual: $rodadaAtual, possuiClassificacao: $possuiClassificacao)'; } @override @@ -72,7 +66,6 @@ class Campeonato { return other.id == id && other.nome == nome && other.ano == ano && - other.logo == logo && other.rodadaAtual == rodadaAtual && other.possuiClassificacao == possuiClassificacao; } @@ -82,7 +75,6 @@ class Campeonato { return id.hashCode ^ nome.hashCode ^ ano.hashCode ^ - logo.hashCode ^ rodadaAtual.hashCode ^ possuiClassificacao.hashCode; } diff --git a/lib/data/models/noticia.dart b/lib/data/models/noticia.dart index abc8d7e..2620077 100644 --- a/lib/data/models/noticia.dart +++ b/lib/data/models/noticia.dart @@ -45,7 +45,7 @@ class Noticia { 'data': data?.millisecondsSinceEpoch, 'titulo': titulo, 'logo_site': logoSite, - 'foto': foto, + 'foto_base_64': foto, }; } @@ -58,7 +58,7 @@ class Noticia { : null, titulo: map['titulo'] as String, logoSite: map['logo_site'] as String, - foto: map['foto'] != null ? map['foto'] as String : null, + foto: map['foto_base_64'] != null ? map['foto_base_64'] as String : null, ); } From 8a1d87692c10e4c171cf230b14b1399753eaae5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pegoraro?= Date: Tue, 5 Sep 2023 16:53:22 -0300 Subject: [PATCH 3/3] atualizei versao --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index d78af77..29161f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: meu_mengao description: A new Flutter project. publish_to: "none" # Remove this line if you wish to publish to pub.dev -version: 1.0.1+3 +version: 1.0.2+4 environment: sdk: ">=3.0.1 <4.0.0"