You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
Dart
105 lines
3.4 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:test_sa/api/preventive_maintenance_api_client.dart';
|
|
import 'package:test_sa/controllers/providers/loading_notifier.dart';
|
|
|
|
import '../../../models/visits/visits_group.dart';
|
|
import '../../../models/visits/visits_search.dart';
|
|
import '../../../views/pages/user/visits/update_visits_group_sheet.dart';
|
|
import '../../http_status_manger/http_status_manger.dart';
|
|
import '../../localization/localization.dart';
|
|
|
|
class PreventiveMaintenanceVisitsProvider extends LoadingNotifier {
|
|
// number of items call in each request
|
|
final pageItemNumber = 50;
|
|
|
|
//reset provider data
|
|
void reset() {
|
|
PreventiveMaintenanceApiClient().visits.clear();
|
|
nextPage = true;
|
|
stateCode = null;
|
|
}
|
|
|
|
// state code of current request to defied error message
|
|
// like 400 customer request failed
|
|
// 500 service not available
|
|
int? stateCode;
|
|
|
|
// true if there is next page in product list and false if not
|
|
bool? nextPage = true;
|
|
|
|
VisitsSearch? visitsSearch = VisitsSearch();
|
|
|
|
Future getVisits() async {
|
|
await waitApiRequest(
|
|
() async {
|
|
await PreventiveMaintenanceApiClient().getVisits(pageItemNumber: pageItemNumber, visitsSearch: visitsSearch);
|
|
},
|
|
onSuccess: () {
|
|
/// TODO : this is temporary
|
|
stateCode = 200;
|
|
if (PreventiveMaintenanceApiClient().visits.length == pageItemNumber) {
|
|
nextPage = true;
|
|
} else {
|
|
nextPage = false;
|
|
}
|
|
},
|
|
onError: (error) {
|
|
/// TODO : this is temporary
|
|
stateCode = error.error?.errorCode;
|
|
},
|
|
);
|
|
}
|
|
|
|
Future updateGroupOfVisits(BuildContext context) async {
|
|
final subtitle = AppLocalization.of(context)?.subtitle;
|
|
VisitsGroup? group = await showModalBottomSheet(
|
|
isScrollControlled: true,
|
|
context: context,
|
|
builder: (context) {
|
|
return UpdateVisitsGroupSheet(visits: PreventiveMaintenanceApiClient().visits, title: subtitle?.updatePreventiveMaintenance);
|
|
},
|
|
) as VisitsGroup?;
|
|
if (context.mounted && group != null) {
|
|
showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return CupertinoAlertDialog(
|
|
title: Text(subtitle?.updatingDots ?? ''),
|
|
content: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
},
|
|
);
|
|
await waitApiRequest(
|
|
() async {
|
|
await PreventiveMaintenanceApiClient().updateGroupOfVisits(group: group);
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
onSuccess: () {
|
|
/// TODO : this is temporary
|
|
stateCode = 200;
|
|
Navigator.of(context).pop();
|
|
Fluttertoast.showToast(
|
|
msg: subtitle?.regularVisitsUpdatedSuccessfully ?? '',
|
|
toastLength: Toast.LENGTH_LONG,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
},
|
|
onError: (error) {
|
|
/// TODO : this is temporary
|
|
stateCode = error.error?.errorCode;
|
|
Fluttertoast.showToast(
|
|
msg: HttpStatusManger.getStatusMessage(status: error.error?.errorCode, subtitle: subtitle),
|
|
toastLength: Toast.LENGTH_LONG,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|