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.
141 lines
4.2 KiB
Dart
141 lines
4.2 KiB
Dart
|
2 months ago
|
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/lookup.dart';
|
||
|
|
|
||
|
|
class LocationDescriptionResponse {
|
||
|
|
List<Lookup>? locationDescriptions;
|
||
|
|
List<DepartmentRoomItem>? deptRoom;
|
||
|
|
|
||
|
|
LocationDescriptionResponse({
|
||
|
|
this.locationDescriptions,
|
||
|
|
this.deptRoom,
|
||
|
|
});
|
||
|
|
|
||
|
|
factory LocationDescriptionResponse.fromJson(Map<String, dynamic> json) {
|
||
|
|
return LocationDescriptionResponse(
|
||
|
|
locationDescriptions: json['locationDescriptions'] != null
|
||
|
|
? (json['locationDescriptions'] as List)
|
||
|
|
.map((e) => Lookup(name: e['locationDescriptionName']))
|
||
|
|
.toList()
|
||
|
|
: null,
|
||
|
|
deptRoom: json['deptRoom'] != null
|
||
|
|
? (json['deptRoom'] as List)
|
||
|
|
.map((e) => DepartmentRoomItem.fromJson(e))
|
||
|
|
.toList()
|
||
|
|
: null,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class DepartmentRoomItem {
|
||
|
|
String? locationDescriptionName;
|
||
|
|
String? deptAndRoomName;
|
||
|
|
String? deptName;
|
||
|
|
String? roomName;
|
||
|
|
int? deptId;
|
||
|
|
int? roomId;
|
||
|
|
|
||
|
|
DepartmentRoomItem({
|
||
|
|
this.locationDescriptionName,
|
||
|
|
this.deptAndRoomName,
|
||
|
|
this.deptName,
|
||
|
|
this.roomName,
|
||
|
|
this.deptId,
|
||
|
|
this.roomId,
|
||
|
|
});
|
||
|
|
|
||
|
|
factory DepartmentRoomItem.fromJson(Map<String, dynamic> json) {
|
||
|
|
return DepartmentRoomItem(
|
||
|
|
locationDescriptionName: json['locationDescriptionName'],
|
||
|
|
deptAndRoomName: json['deptAndRoomName'],
|
||
|
|
deptName: json['deptName'],
|
||
|
|
roomName: json['roomName'],
|
||
|
|
deptId: json['deptId'],
|
||
|
|
roomId: json['roomId'],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class FmsLocationDescriptionProvider extends ChangeNotifier {
|
||
|
|
//reset provider data
|
||
|
|
void reset() {
|
||
|
|
_locationDescriptions = null;
|
||
|
|
_deptRoomList = null;
|
||
|
|
_stateCode = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// state code of current request to defied error message
|
||
|
|
int? _stateCode;
|
||
|
|
int? get stateCode => _stateCode;
|
||
|
|
|
||
|
|
// contain location descriptions data
|
||
|
|
List<Lookup>? _locationDescriptions;
|
||
|
|
List<Lookup>? get locationDescriptions => _locationDescriptions;
|
||
|
|
|
||
|
|
// contain dept room list
|
||
|
|
List<DepartmentRoomItem>? _deptRoomList;
|
||
|
|
List<DepartmentRoomItem>? get deptRoomList => _deptRoomList;
|
||
|
|
|
||
|
|
// loading state
|
||
|
|
bool _loading = false;
|
||
|
|
bool get isLoading => _loading;
|
||
|
|
|
||
|
|
set isLoading(bool isLoading) {
|
||
|
|
_loading = isLoading;
|
||
|
|
notifyListeners();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Get location descriptions by site, building, and floor ID
|
||
|
|
Future<int> getLocationDescriptions(int siteId, int buildingId, int floorId) async {
|
||
|
|
if (_loading == true) return -2;
|
||
|
|
_loading = true;
|
||
|
|
notifyListeners();
|
||
|
|
Response response;
|
||
|
|
try {
|
||
|
|
response = await ApiManager.instance.get(
|
||
|
|
"${URLs.getCustomerLocationDescription}?siteId=$siteId&buildingId=$buildingId&floorId=$floorId",
|
||
|
|
enableToastMessage: false,
|
||
|
|
);
|
||
|
|
_stateCode = response.statusCode;
|
||
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||
|
|
// client's request was successfully received
|
||
|
|
final jsonData = json.decode(response.body)["data"];
|
||
|
|
LocationDescriptionResponse locationResponse = LocationDescriptionResponse.fromJson(jsonData);
|
||
|
|
_locationDescriptions = locationResponse.locationDescriptions;
|
||
|
|
_deptRoomList = locationResponse.deptRoom;
|
||
|
|
}
|
||
|
|
_loading = false;
|
||
|
|
notifyListeners();
|
||
|
|
return response.statusCode;
|
||
|
|
} catch (error) {
|
||
|
|
_loading = false;
|
||
|
|
_stateCode = -1;
|
||
|
|
notifyListeners();
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Get location descriptions and dept room list directly without provider state
|
||
|
|
Future<LocationDescriptionResponse?> getLocationDescriptionsList(int siteId, int buildingId, int floorId) async {
|
||
|
|
Response response;
|
||
|
|
try {
|
||
|
|
response = await ApiManager.instance.get(
|
||
|
|
"${URLs.getCustomerLocationDescription}?siteId=$siteId&buildingId=$buildingId&floorId=$floorId",
|
||
|
|
enableToastMessage: false,
|
||
|
|
);
|
||
|
|
|
||
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||
|
|
final jsonData = json.decode(response.body)["data"];
|
||
|
|
return LocationDescriptionResponse.fromJson(jsonData);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
} catch (error) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|