Skip to content

Commit

Permalink
fix(#64): conexão com o backend
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Jan 23, 2025
1 parent f6f3eb1 commit 4683a6c
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 148 deletions.
67 changes: 67 additions & 0 deletions lib/core/network/studio_maker_api.dart
Original file line number Diff line number Diff line change
@@ -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<Response> get(
{required String path, Map<String, dynamic>? data}) async {
try {
return await _dio.get(path, data: data);
} catch (e) {
Log.e(e);
rethrow;
}
}

Future<Response> post({required String path, Object? data}) async {
try {
return await _dio.post(path, data: data);
} catch (e) {
Log.e(e);
rethrow;
}
}

Future<Response> patch({required String path, Object? data}) async {
try {
return await _dio.patch(path, data: data);
} catch (e) {
Log.e(e);
rethrow;
}
}

Future<Response> put({required String path, Object? data}) async {
try {
return await _dio.put(path, data: data);
} catch (e) {
Log.e(e);
rethrow;
}
}
}
76 changes: 39 additions & 37 deletions lib/ui/journey/view/journey_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,45 @@ class _JourneyScreen extends StatelessWidget {
JourneyViewModel viewModel = Provider.of<JourneyViewModel>(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");
},
);
});
}
}
33 changes: 1 addition & 32 deletions lib/ui/journey/viewmodel/journey_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -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<FormState> formKey;
final TextEditingController titleController;
final TextEditingController descriptionController;
final TextEditingController pointIdController;

List<JourneyResponse> journeys = [];

late Command0<List<JourneyRequest>> journeyCommand;

JourneyViewModel()
: formKey = GlobalKey<FormState>(),
titleController = TextEditingController(),
descriptionController = TextEditingController(),
pointIdController = TextEditingController() {
JourneyViewModel() {
journeyCommand = Command0(journey);



journeyCommand.execute();
}

Future<Result<List<JourneyRequest>>> 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<JourneyResponse>? journeysResponse =
// await GetIt.instance<JourneyService>().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));

Expand Down
11 changes: 10 additions & 1 deletion lib/ui/shared/erro_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
),
),
],
),
);
}
}
}

33 changes: 33 additions & 0 deletions lib/ui/subjects/model/subject.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> toJson() {
return <String, dynamic>{
'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,
);
}
}

27 changes: 0 additions & 27 deletions lib/ui/subjects/model/subjects_request.dart

This file was deleted.

16 changes: 0 additions & 16 deletions lib/ui/subjects/model/subjects_response.dart

This file was deleted.

34 changes: 15 additions & 19 deletions lib/ui/subjects/service/subjects_service.dart
Original file line number Diff line number Diff line change
@@ -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<List<SubjectsResponse>?> getSubjects(
SubjectsRequest subjectRequest) async {
Log.d(
'Request Subject: ${subjectRequest.title}, ${subjectRequest.description}');
Future<List<Subject>> getSubjects() async {
Response response =
await StudioMakerApi.getInstance().get(path: '/subjects');

Response response = await BaseApi.getInstance(auth: true)
.get(path: '/subjects', data: subjectRequest.toJson());
List<dynamic> subjectList = response.data as List<dynamic>;

Log.d('Response Subject: ${response.toString()}');
Log.i(subjectList);

if (response.data != null) {
List<dynamic> subjectList = response.data as List<dynamic>;
return subjectList
.map((subjectJson) => SubjectsResponse.fromJsonString(subjectJson))
.toList();
} else {
Log.e('Não é uma lista');
return null;
}
return subjectList.map((e) {
final Map<String, dynamic> subjectMap = e as Map<String, dynamic>;

return Subject(
name: subjectMap['name']!,
shortName: subjectMap['shortName']!,
description: subjectMap['description']!);
}).toList();
}
}
12 changes: 7 additions & 5 deletions lib/ui/subjects/view/subjects_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Subject extends StatelessWidget {
Widget build(BuildContext context) {
return ChangeNotifierProvider<SubjectsViewmodel>.value(
value: GetIt.instance<SubjectsViewmodel>(),
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) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 4683a6c

Please sign in to comment.