Refactoring

pull/113/head
faizatflutter 1 month ago
parent 2ffd4cc1ae
commit 9410ad9b60

@ -66,28 +66,23 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
/// Get count of selected organs
int get selectedOrgansCount => _selectedOrganIds.length;
/// Get organ symptoms from API response
List<OrganSymptomResult> get organSymptomsResults {
List<OrganSymptomResult> get organSymptomsResults {
if (bodySymptomResponse?.dataDetails?.result == null) {
return [];
}
return bodySymptomResponse!.dataDetails!.result ?? [];
}
/// Get total selected symptoms count across all organs
int get totalSelectedSymptomsCount {
int get totalSelectedSymptomsCount {
return _selectedSymptomsByOrgan.values.fold(0, (sum, symptomIds) => sum + symptomIds.length);
}
/// Check if at least one symptom is selected
bool get hasSelectedSymptoms {
bool get hasSelectedSymptoms {
return _selectedSymptomsByOrgan.values.any((symptomIds) => symptomIds.isNotEmpty);
}
// Methods
/// Toggle between front and back body view
void toggleView() {
void toggleView() {
_currentView = _currentView == BodyView.front ? BodyView.back : BodyView.front;
notifyListeners();
}
@ -97,8 +92,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
notifyListeners();
}
/// Toggle organ selection (add if not selected, remove if selected)
void toggleOrganSelection(String organId) {
void toggleOrganSelection(String organId) {
if (_selectedOrganIds.contains(organId)) {
_selectedOrganIds.remove(organId);
} else {
@ -111,13 +105,10 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
notifyListeners();
}
/// Show tooltip for an organ
void _showTooltip(String organId) {
// Cancel any existing timer
_tooltipTimer?.cancel();
void _showTooltip(String organId) {
_tooltipTimer?.cancel();
// Set the tooltip organ
_tooltipOrganId = organId;
_tooltipOrganId = organId;
notifyListeners();
// Hide tooltip after 2 seconds
@ -134,47 +125,39 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
notifyListeners();
}
/// Remove a specific organ from selection
void removeOrgan(String organId) {
void removeOrgan(String organId) {
_selectedOrganIds.remove(organId);
notifyListeners();
}
/// Clear all selected organs
void clearAllSelections() {
void clearAllSelections() {
_selectedOrganIds.clear();
notifyListeners();
}
/// Toggle bottom sheet expanded/collapsed state
void toggleBottomSheet() {
void toggleBottomSheet() {
_isBottomSheetExpanded = !_isBottomSheetExpanded;
notifyListeners();
}
/// Set bottom sheet expanded state
void setBottomSheetExpanded(bool isExpanded) {
void setBottomSheetExpanded(bool isExpanded) {
_isBottomSheetExpanded = isExpanded;
notifyListeners();
}
/// Validate if at least one organ is selected
bool validateSelection() {
bool validateSelection() {
return _selectedOrganIds.isNotEmpty;
}
/// Get selected organ IDs as a list
List<String> getSelectedOrganIds() {
List<String> getSelectedOrganIds() {
return _selectedOrganIds.toList();
}
/// Get selected organ names as a list
List<String> getSelectedOrganNames() {
List<String> getSelectedOrganNames() {
return selectedOrgans.map((organ) => organ.description).toList();
}
/// Initialize symptoms from API based on selected organs
Future<void> initializeSymptomGroups({
Future<void> initializeSymptomGroups({
Function()? onSuccess,
Function(String)? onError,
}) async {
@ -185,15 +168,12 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
return;
}
// Get the 'name' field from selected organs to send to API
List<String> organNames = selectedOrgans.map((organ) => organ.name).toList();
List<String> organNames = selectedOrgans.map((organ) => organ.name).toList();
// Fetch symptoms from API
await getBodySymptomsByName(
await getBodySymptomsByName(
organNames: organNames,
onSuccess: (response) {
// API response is already stored in bodySymptomResponse
if (onSuccess != null) {
if (onSuccess != null) {
onSuccess();
}
},
@ -205,8 +185,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
);
}
/// Toggle symptom selection for a specific organ
void toggleSymptomSelection(String organId, String symptomId) {
void toggleSymptomSelection(String organId, String symptomId) {
if (!_selectedSymptomsByOrgan.containsKey(organId)) {
_selectedSymptomsByOrgan[organId] = {};
}
@ -219,13 +198,11 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
notifyListeners();
}
/// Check if a symptom is selected
bool isSymptomSelected(String organId, String symptomId) {
bool isSymptomSelected(String organId, String symptomId) {
return _selectedSymptomsByOrgan[organId]?.contains(symptomId) ?? false;
}
/// Get all selected symptoms across all organs (using API models)
List<BodySymptom> getAllSelectedSymptoms() {
List<BodySymptom> getAllSelectedSymptoms() {
List<BodySymptom> allSymptoms = [];
if (bodySymptomResponse?.dataDetails?.result == null) {
@ -233,8 +210,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
}
for (var organResult in bodySymptomResponse!.dataDetails!.result!) {
// Find matching organ ID
String? matchingOrganId;
String? matchingOrganId;
for (var organ in selectedOrgans) {
if (organ.name == organResult.name) {
matchingOrganId = organ.id;
@ -258,14 +234,12 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
return allSymptoms;
}
/// Clear all symptom selections
void clearAllSymptomSelections() {
void clearAllSymptomSelections() {
_selectedSymptomsByOrgan.clear();
notifyListeners();
}
/// Reset the view model to initial state
void reset() {
void reset() {
_currentView = BodyView.front;
_selectedOrganIds.clear();
_selectedSymptomsByOrgan.clear();
@ -276,8 +250,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
notifyListeners();
}
/// Fetch body symptoms by organ names from API
Future<void> getBodySymptomsByName({
Future<void> getBodySymptomsByName({
required List<String> organNames,
Function(BodySymptomResponseModel)? onSuccess,
Function(String)? onError,
@ -290,8 +263,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
);
result.fold(
// Handle failure
(failure) async {
(failure) async {
isBodySymptomsLoading = false;
notifyListeners();
await errorHandlerService.handleError(failure: failure);
@ -299,8 +271,7 @@ class SymptomsCheckerViewModel extends ChangeNotifier {
onError(failure.toString());
}
},
// Handle success
(apiResponse) {
(apiResponse) {
isBodySymptomsLoading = false;
if (apiResponse.messageStatus == 1 && apiResponse.data != null) {
bodySymptomResponse = apiResponse.data;

@ -123,7 +123,6 @@ class _SymptomsSelectorScreenState extends State<SymptomsSelectorScreen> {
),
if (!viewModel.isBodySymptomsLoading) ...[
_buildStickyBottomCard(context, viewModel),
],
],
);

Loading…
Cancel
Save