Skip to content

Commit

Permalink
Add DiagnosticDataRequest class and update ExpertDoctor repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Polabiel committed Nov 15, 2024
1 parent 7156934 commit a324d36
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 33 deletions.
16 changes: 16 additions & 0 deletions lib/api/models/expert_doctor_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ class SymptomsDataRequest {
return data;
}
}

class DiagnosticDataRequest {
final String title;
final String description;
final String score;

DiagnosticDataRequest({required this.title, required this.description, required this.score});

factory DiagnosticDataRequest.fromJson(Map<String, dynamic> json) {
return DiagnosticDataRequest(
title: json['title'],
description: json['description'],
score: json['score'],
);
}
}
57 changes: 30 additions & 27 deletions lib/api/repositories/expert_doctor_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:gohealth/api/models/expert_doctor_models.dart';
import 'package:gohealth/api/services/http_client.dart';
import 'dart:async';


class ExpertDoctor {
late MedicalExpertService userNetworkClient;
Expand All @@ -11,40 +13,41 @@ class ExpertDoctor {
// O expertDoctor é uma classe que irá representar a API da Inteligencia artificial que irá retornar os dados de cada função

// ! Esta função recebe um prompt do usuário e a IA retorna um checklist de sintomas. O usuário deve marcar os sintomas que está sentindo, conforme o texto que ele escreveu e que gerou o checklist.

Future<SymptomsDataRequest> getSymptoms(String prompt) async {
try {
var response = await userNetworkClient.client.post(
'/symptoms',
data: {
'text': prompt,
},
).timeout(Duration(seconds: 45));

if (response.statusCode == 500) {
throw Exception('Erro interno do servidor');
}

if (response.statusCode != 200) {
throw Exception('Erro ao buscar os sintomas');
}

return SymptomsDataRequest.fromJson(response.data);
}

// ! Esta função irá enviar o checklist de sintomas marcados pelo usuário e a IA irá retornar um texto e uma descrição do que ele tem!
Future<DiagnosticDataRequest> getDiagnosis(List<String> symptoms) async {
var response = await userNetworkClient.client.post(
'/symptoms',
data: {
'text': prompt,
},
);
'/diagnosis',
data: {"sintomas": symptoms},
).timeout(Duration(seconds: 45));

if (response.data == null) {
throw Exception('Erro ao buscar os sintomas');
throw Exception('Erro ao buscar o diagnóstico');
}

if (response.statusCode != 200) {
throw Exception('Erro ao buscar os sintomas');
throw Exception('Erro ao buscar o diagnóstico');
}

return SymptomsDataRequest.fromJson(response.data);
} catch (e) {
print(e.toString());
rethrow;
}
}

// ! Esta função irá enviar o checklist de sintomas marcados pelo usuário e a IA irá retornar um texto e uma descrição do que ele tem!
Future<List<dynamic>> getDiagnosis(Map<String, dynamic> symptoms) async {
try {
var response = await userNetworkClient.client.post(
'/diagnosis',
data: symptoms,
);
return response.data;
} catch (e) {
rethrow;
}
return DiagnosticDataRequest.fromJson(response.data);
}
}
}
18 changes: 16 additions & 2 deletions lib/src/components/checklist/ChecklistPage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/expert_doctor_models.dart';
import 'package:gohealth/src/components/checklist/Diagnois.dart';

class ChecklistPage extends StatefulWidget {
final SymptomsDataRequest symptoms;
Expand Down Expand Up @@ -40,8 +41,21 @@ class _ChecklistPageState extends State<ChecklistPage> {
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Implement your logic to send the suspicion and return the result
// For example, you can navigate to another page or show a dialog with the result

List<String> array = [];

for (int i = 0; i < _checked.length; i++) {
if (_checked[i] == true) {
array.add(widget.symptoms.sintomas![i]);
}
}

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Diagnois(array: array),
),
);
},
child: const Icon(Icons.send),
),
Expand Down
70 changes: 70 additions & 0 deletions lib/src/components/checklist/Diagnois.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/expert_doctor_models.dart';
import 'package:gohealth/api/repositories/expert_doctor_repository.dart';

class Diagnois extends StatefulWidget {
Diagnois({Key? key, required this.array}) : super(key: key);

final List<String> array;

@override
_DiagnoisState createState() => _DiagnoisState();
}

class _DiagnoisState extends State<Diagnois> {
final ExpertDoctor _expertDoctor = ExpertDoctor();
late final DiagnosticDataRequest _diagnosticDataRequest;

@override
void initState() {
super.initState();
_fetchSymptoms();
}

Future<void> _fetchSymptoms() async {
try {
final result = await _expertDoctor.getDiagnosis(widget.array);
setState(() {
_diagnosticDataRequest = result;
});
} catch (e) {
_fetchSymptoms();
rethrow;
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Diagnóstico'),
),
body: _diagnosticDataRequest == null
? const Center(
child: CircularProgressIndicator(),
)
: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Text(
'Diagnóstico: ${_diagnosticDataRequest.title}',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 10),
Text(
'Descrição: ${_diagnosticDataRequest.description}',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 10),
Text(
'Porcentagem: ${_diagnosticDataRequest.score} / 10',
style: const TextStyle(fontSize: 20),
),
],
),
),

);
}
}
13 changes: 11 additions & 2 deletions lib/src/components/checklist/Expert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ class _ExpertState extends State<Expert> {
}

Future<void> _fetchSymptoms() async {
symptoms = await _expertDoctor.getSymptoms(_textController.text);
setState(() {});
try {
symptoms = await _expertDoctor.getSymptoms(_textController.text);
setState(() {});
} catch (e) {
final scaffoldMessenger = ScaffoldMessenger.of(context);
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text('Erro: ${e.toString()}'),
),
);
}
}

@override
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev"
source: hosted
version: "14.2.5"
version: "14.2.4"
web:
dependency: transitive
description:
Expand Down

0 comments on commit a324d36

Please sign in to comment.