Skip to content

Commit

Permalink
Merge pull request #94 from fga-eps-mds/feat#64/Journey-backend
Browse files Browse the repository at this point in the history
Feat#64/journey backend
  • Loading branch information
GabrielCostaDeOliveira authored Jan 23, 2025
2 parents f6f3eb1 + 6f3a7a9 commit e5c3321
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 173 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;
}
}
}
89 changes: 48 additions & 41 deletions lib/ui/journey/view/journey_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ import 'package:aranduapp/core/log/log.dart';
import 'package:aranduapp/ui/journey/viewmodel/journey_viewmodel.dart';
import 'package:aranduapp/ui/shared/erro_screen.dart';
import 'package:aranduapp/ui/shared/loading_widget.dart';
import 'package:aranduapp/ui/subjects/model/subject_model.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart';

class Journey extends StatelessWidget {
const Journey({super.key});
final SubjectModel subject;

const Journey({super.key, required this.subject});

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<JourneyViewModel>.value(
value: GetIt.instance<JourneyViewModel>(),
child: const _JourneyScreen(),
child: _JourneyScreen(subject: subject),
);
}
}

class _JourneyScreen extends StatelessWidget {
const _JourneyScreen({super.key});
final SubjectModel subject;

const _JourneyScreen({required this.subject});

@override
Widget build(BuildContext context) {
Expand All @@ -34,7 +39,7 @@ class _JourneyScreen extends StatelessWidget {
backgroundColor: Theme.of(context).colorScheme.onPrimary,
scrolledUnderElevation: 0,
title: Text(
'Jornadas de Lógica',
'Jornadas de ${subject.name}',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
Expand All @@ -57,43 +62,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_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:convert';

class SubjectModel {
final String name;
final String shortName;
final String description;


SubjectModel({
required this.name,
required this.shortName,
required this.description,
});

Map<String, dynamic> toJson() {
return <String, dynamic>{
'name': name,
'shortName': shortName,
'description': description ,
};
}

factory SubjectModel.fromJson(String jsonString) {
final json = jsonDecode(jsonString);

return SubjectModel(
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_model.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<SubjectModel>> 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 SubjectModel(
name: subjectMap['name']!,
shortName: subjectMap['shortName']!,
description: subjectMap['description']!);
}).toList();
}
}
Loading

0 comments on commit e5c3321

Please sign in to comment.