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.
86 lines
2.3 KiB
Dart
86 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/controllers/api_routes/api_manager.dart';
|
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
|
import 'package:test_sa/models/new_models/floor.dart';
|
|
|
|
class FmsFloorProvider extends ChangeNotifier {
|
|
//reset provider data
|
|
void reset() {
|
|
_floors = null;
|
|
_stateCode = null;
|
|
}
|
|
|
|
// state code of current request to defied error message
|
|
int? _stateCode;
|
|
int? get stateCode => _stateCode;
|
|
|
|
// contain floors data
|
|
List<Floor>? _floors;
|
|
List<Floor>? get floors => _floors;
|
|
|
|
// loading state
|
|
bool _loading = false;
|
|
bool get isLoading => _loading;
|
|
|
|
set isLoading(bool isLoading) {
|
|
_loading = isLoading;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Get floors by site and building ID
|
|
Future<int> getFloorsBySiteAndBuilding(int siteId, int buildingId) async {
|
|
if (_loading == true) return -2;
|
|
_loading = true;
|
|
notifyListeners();
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.post(
|
|
URLs.getCustomerFloors,
|
|
body: {
|
|
"siteId": siteId,
|
|
"buildingId": buildingId,
|
|
},
|
|
);
|
|
_stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// client's request was successfully received
|
|
List floorsListJson = json.decode(response.body)["data"];
|
|
_floors = floorsListJson.map((floor) => Floor.fromJson(floor)).toList();
|
|
}
|
|
_loading = false;
|
|
notifyListeners();
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
_loading = false;
|
|
_stateCode = -1;
|
|
notifyListeners();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/// Get floors list directly without provider state
|
|
Future<List<Floor>> getFloorsListBySiteAndBuilding(int siteId, int buildingId) async {
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.post(
|
|
URLs.getCustomerFloors,
|
|
body: {
|
|
"siteId": siteId,
|
|
"buildingId": buildingId,
|
|
},
|
|
);
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
List floorsListJson = json.decode(response.body)["data"];
|
|
return floorsListJson.map((floor) => Floor.fromJson(floor)).toList();
|
|
}
|
|
return [];
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
}
|
|
}
|