From 4683a6c85742560efb3f0db103858e034182dc99 Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Wed, 22 Jan 2025 22:44:26 -0300 Subject: [PATCH] =?UTF-8?q?fix(#64):=20conex=C3=A3o=20com=20o=20backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/network/studio_maker_api.dart | 67 ++++++++++++++++ lib/ui/journey/view/journey_view.dart | 76 ++++++++++--------- .../journey/viewmodel/journey_viewmodel.dart | 33 +------- lib/ui/shared/erro_screen.dart | 11 ++- lib/ui/subjects/model/subject.dart | 33 ++++++++ lib/ui/subjects/model/subjects_request.dart | 27 ------- lib/ui/subjects/model/subjects_response.dart | 16 ---- lib/ui/subjects/service/subjects_service.dart | 34 ++++----- lib/ui/subjects/view/subjects_view.dart | 12 +-- .../viewmodel/subjects_viewmodel.dart | 20 +++-- 10 files changed, 181 insertions(+), 148 deletions(-) create mode 100644 lib/core/network/studio_maker_api.dart create mode 100644 lib/ui/subjects/model/subject.dart delete mode 100644 lib/ui/subjects/model/subjects_request.dart delete mode 100644 lib/ui/subjects/model/subjects_response.dart diff --git a/lib/core/network/studio_maker_api.dart b/lib/core/network/studio_maker_api.dart new file mode 100644 index 0000000..66891ee --- /dev/null +++ b/lib/core/network/studio_maker_api.dart @@ -0,0 +1,67 @@ + +import 'package:aranduapp/core/log/log.dart'; +import 'package:dio/dio.dart'; + +class StudioMakerApi { + final Dio _dio; + + static StudioMakerApi? _instance; + + final String url = 'https://arandu-studio-maker.onrender.com'; + + StudioMakerApi._internal() : _dio = Dio() { + _dio.options.baseUrl = url; + _dio.options.connectTimeout = const Duration(seconds: 5); + _dio.options.receiveTimeout = const Duration(seconds: 5); + + + _dio.interceptors.add(LogInterceptor( + requestBody: true, + responseBody: true, + requestHeader: true, + error: true, + responseHeader: true, + request: true)); + } + + static StudioMakerApi getInstance() { + return _instance ??= StudioMakerApi._internal(); + } + + Future get( + {required String path, Map? data}) async { + try { + return await _dio.get(path, data: data); + } catch (e) { + Log.e(e); + rethrow; + } + } + + Future post({required String path, Object? data}) async { + try { + return await _dio.post(path, data: data); + } catch (e) { + Log.e(e); + rethrow; + } + } + + Future patch({required String path, Object? data}) async { + try { + return await _dio.patch(path, data: data); + } catch (e) { + Log.e(e); + rethrow; + } + } + + Future put({required String path, Object? data}) async { + try { + return await _dio.put(path, data: data); + } catch (e) { + Log.e(e); + rethrow; + } + } +} diff --git a/lib/ui/journey/view/journey_view.dart b/lib/ui/journey/view/journey_view.dart index 5fc4638..4b711b4 100644 --- a/lib/ui/journey/view/journey_view.dart +++ b/lib/ui/journey/view/journey_view.dart @@ -57,43 +57,45 @@ class _JourneyScreen extends StatelessWidget { JourneyViewModel viewModel = Provider.of(context); return RefreshIndicator( - onRefresh: viewModel.journeyCommand.execute, - child: ListenableBuilder( - listenable: viewModel.journeyCommand, - builder: (context, child) { - if (viewModel.journeyCommand.isOk) { - return ListView.builder( - itemCount: viewModel - .journeyCommand.result!.asValue!.value.length, - shrinkWrap: true, - itemBuilder: (context, index) { - var journey = viewModel - .journeyCommand.result!.asValue!.value[index]; - return ListTile( - leading: Icon( - Icons.border_right, - color: Theme.of(context).colorScheme.primary, - size: 32, - ), - title: Text(journey.title), - subtitle: Text(journey.description), - trailing: Icon( - Icons.chevron_right, - color: Theme.of(context).colorScheme.primary, - size: 32, - ), - onTap: () { - Log.d("tap"); - }, - ); - }); - } else if (viewModel.journeyCommand.isError) { - return const ErrorScreen(message: "Deslize para baixo"); - } else { - return const LoadingWidget(); - } - }, - ), + onRefresh: viewModel.journeyCommand.execute, + child: ListenableBuilder( + listenable: viewModel.journeyCommand, + builder: (context, child) { + if (viewModel.journeyCommand.isOk) { + return listView(viewModel); + } else if (viewModel.journeyCommand.isError) { + return const ErrorScreen(message: "Deslize para baixo"); + } else { + return const LoadingWidget(); + } + }, + ), ); } + + ListView listView(JourneyViewModel viewModel) { + return ListView.builder( + itemCount: viewModel.journeyCommand.result!.asValue!.value.length, + shrinkWrap: true, + itemBuilder: (context, index) { + var journey = viewModel.journeyCommand.result!.asValue!.value[index]; + return ListTile( + leading: Icon( + Icons.border_right, + color: Theme.of(context).colorScheme.primary, + size: 32, + ), + title: Text(journey.title), + subtitle: Text(journey.description), + trailing: Icon( + Icons.chevron_right, + color: Theme.of(context).colorScheme.primary, + size: 32, + ), + onTap: () { + Log.d("tap"); + }, + ); + }); + } } diff --git a/lib/ui/journey/viewmodel/journey_viewmodel.dart b/lib/ui/journey/viewmodel/journey_viewmodel.dart index 23f7e1e..54d150e 100644 --- a/lib/ui/journey/viewmodel/journey_viewmodel.dart +++ b/lib/ui/journey/viewmodel/journey_viewmodel.dart @@ -1,52 +1,21 @@ import 'package:aranduapp/core/state/command.dart'; import 'package:aranduapp/ui/journey/model/journey_request.dart'; import 'package:aranduapp/ui/journey/model/journey_response.dart'; -import 'package:aranduapp/ui/journey/service/journey_service.dart'; import 'package:async/async.dart'; import 'package:flutter/material.dart'; -import 'package:get_it/get_it.dart'; class JourneyViewModel extends ChangeNotifier { - final GlobalKey formKey; - final TextEditingController titleController; - final TextEditingController descriptionController; - final TextEditingController pointIdController; - List journeys = []; late Command0> journeyCommand; - JourneyViewModel() - : formKey = GlobalKey(), - titleController = TextEditingController(), - descriptionController = TextEditingController(), - pointIdController = TextEditingController() { + JourneyViewModel() { journeyCommand = Command0(journey); - - journeyCommand.execute(); } Future>> journey() async { -// if (!formKey.currentState!.validate()) { -// return Result.error('Valores inválidos'); -// } - -// JourneyRequest request = JourneyRequest( -// title: titleController.text, -// description: descriptionController.text, -// pointId: pointIdController.text); - -// List? journeysResponse = -// await GetIt.instance().getJourneys(request); - -// if (journeysResponse != null) { -// journeys = journeysResponse; -// notifyListeners(); -// return Result.value(null); -// } -// return Result.error('nenhuma jornada encontrada'); await Future.delayed(const Duration(seconds: 1)); diff --git a/lib/ui/shared/erro_screen.dart b/lib/ui/shared/erro_screen.dart index 3a3a2d1..5cfd1ce 100644 --- a/lib/ui/shared/erro_screen.dart +++ b/lib/ui/shared/erro_screen.dart @@ -9,6 +9,7 @@ class ErrorScreen extends StatelessWidget { Widget build(BuildContext context) { return Center( child: Column( + mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.error_sharp, color: Theme.of(context).colorScheme.error), Text( @@ -18,8 +19,16 @@ class ErrorScreen extends StatelessWidget { color: Theme.of(context).colorScheme.error, ), ), + Text( + message, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.bodySmall!.apply( + color: Theme.of(context).colorScheme.error, + ), + ), ], ), ); } -} \ No newline at end of file +} + diff --git a/lib/ui/subjects/model/subject.dart b/lib/ui/subjects/model/subject.dart new file mode 100644 index 0000000..869536d --- /dev/null +++ b/lib/ui/subjects/model/subject.dart @@ -0,0 +1,33 @@ +import 'dart:convert'; + +class Subject { + final String name; + final String shortName; + final String description; + + + Subject({ + required this.name, + required this.shortName, + required this.description, + }); + + Map toJson() { + return { + 'name': name, + 'shortName': shortName, + 'description': description , + }; + } + + factory Subject.fromJson(String jsonString) { + final json = jsonDecode(jsonString); + + return Subject( + name: json['name']! as String, + shortName: json['shortName']! as String, + description: json['description']! as String, + ); + } +} + diff --git a/lib/ui/subjects/model/subjects_request.dart b/lib/ui/subjects/model/subjects_request.dart deleted file mode 100644 index a2716c9..0000000 --- a/lib/ui/subjects/model/subjects_request.dart +++ /dev/null @@ -1,27 +0,0 @@ -import 'dart:convert'; - -class SubjectsRequest { - final String title; - final String description; - - SubjectsRequest({ - required this.title, - required this.description, - }); - - Map toJson() { - return { - 'title': title, - 'description': description, - }; - } - - factory SubjectsRequest.fromJsonString(String jsonString) { - final json = jsonDecode(jsonString); - - return SubjectsRequest( - title: json['title']! as String, - description: json['description']! as String, - ); - } -} diff --git a/lib/ui/subjects/model/subjects_response.dart b/lib/ui/subjects/model/subjects_response.dart deleted file mode 100644 index 9a83b7c..0000000 --- a/lib/ui/subjects/model/subjects_response.dart +++ /dev/null @@ -1,16 +0,0 @@ -import 'dart:convert'; - -class SubjectsResponse { - final String title; - final String description; - - SubjectsResponse(this.title, this.description); - - factory SubjectsResponse.fromJsonString(String jsonString) { - Map json = jsonDecode(jsonString); - return SubjectsResponse( - json['title'] as String, - json['description'] as String - ); - } -} \ No newline at end of file diff --git a/lib/ui/subjects/service/subjects_service.dart b/lib/ui/subjects/service/subjects_service.dart index 64f5be0..c2f4910 100644 --- a/lib/ui/subjects/service/subjects_service.dart +++ b/lib/ui/subjects/service/subjects_service.dart @@ -1,28 +1,24 @@ import 'package:aranduapp/core/log/log.dart'; -import 'package:aranduapp/core/network/base_api.dart'; -import 'package:aranduapp/ui/subjects/model/subjects_request.dart'; -import 'package:aranduapp/ui/subjects/model/subjects_response.dart'; +import 'package:aranduapp/core/network/studio_maker_api.dart'; +import 'package:aranduapp/ui/subjects/model/subject.dart'; import 'package:dio/dio.dart'; class SubjectService { - Future?> getSubjects( - SubjectsRequest subjectRequest) async { - Log.d( - 'Request Subject: ${subjectRequest.title}, ${subjectRequest.description}'); + Future> getSubjects() async { + Response response = + await StudioMakerApi.getInstance().get(path: '/subjects'); - Response response = await BaseApi.getInstance(auth: true) - .get(path: '/subjects', data: subjectRequest.toJson()); + List subjectList = response.data as List; - Log.d('Response Subject: ${response.toString()}'); + Log.i(subjectList); - if (response.data != null) { - List subjectList = response.data as List; - return subjectList - .map((subjectJson) => SubjectsResponse.fromJsonString(subjectJson)) - .toList(); - } else { - Log.e('Não é uma lista'); - return null; - } + return subjectList.map((e) { + final Map subjectMap = e as Map; + + return Subject( + name: subjectMap['name']!, + shortName: subjectMap['shortName']!, + description: subjectMap['description']!); + }).toList(); } } diff --git a/lib/ui/subjects/view/subjects_view.dart b/lib/ui/subjects/view/subjects_view.dart index dcdf4af..24d1f4a 100644 --- a/lib/ui/subjects/view/subjects_view.dart +++ b/lib/ui/subjects/view/subjects_view.dart @@ -13,13 +13,13 @@ class Subject extends StatelessWidget { Widget build(BuildContext context) { return ChangeNotifierProvider.value( value: GetIt.instance(), - child: const SubjectScreen(), + child: const _SubjectScreen(), ); } } -class SubjectScreen extends StatelessWidget { - const SubjectScreen({super.key}); +class _SubjectScreen extends StatelessWidget { + const _SubjectScreen(); @override Widget build(BuildContext context) { @@ -56,7 +56,9 @@ class SubjectScreen extends StatelessWidget { if (viewModel.subjectCommand.isOk) { return listView(viewModel, screenHeight); } else if (viewModel.subjectCommand.isError) { - return const ErrorScreen(message: "Deslize para baixo"); + return ErrorScreen( + message: + "Deslize para baixo \n\n ${viewModel.subjectCommand.result!.asError!.error.toString()}"); } else { return const LoadingWidget(); } @@ -86,7 +88,7 @@ class SubjectScreen extends StatelessWidget { size: 64, ), ), - title: Text(subject.title), + title: Text(subject.name), subtitle: Text(subject.description), trailing: Icon( Icons.chevron_right, diff --git a/lib/ui/subjects/viewmodel/subjects_viewmodel.dart b/lib/ui/subjects/viewmodel/subjects_viewmodel.dart index 5ca79c3..9857b71 100644 --- a/lib/ui/subjects/viewmodel/subjects_viewmodel.dart +++ b/lib/ui/subjects/viewmodel/subjects_viewmodel.dart @@ -1,13 +1,15 @@ +import 'package:aranduapp/core/log/log.dart'; import 'package:aranduapp/core/state/command.dart'; -import 'package:aranduapp/ui/subjects/model/subjects_request.dart'; -import 'package:aranduapp/ui/subjects/model/subjects_response.dart'; +import 'package:aranduapp/ui/subjects/model/subject.dart'; +import 'package:aranduapp/ui/subjects/service/subjects_service.dart'; import 'package:async/async.dart'; import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; class SubjectsViewmodel extends ChangeNotifier { - List subjects = []; + List subjects = []; - late Command0> subjectCommand; + late Command0> subjectCommand; SubjectsViewmodel() { subjectCommand = Command0(subject); @@ -15,14 +17,10 @@ class SubjectsViewmodel extends ChangeNotifier { subjectCommand.execute(); } - Future>> subject() async { - await Future.delayed(const Duration(seconds: 1)); + Future>> subject() async { - final subjectRequest = SubjectsRequest( - title: "Viagem ao Parque", - description: "Explorar o parque local com os amigos.", - ); + final res = await GetIt.instance().getSubjects(); - return Result.value(List.generate(20, (_) => subjectRequest)); + return Result.value(res); } }