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? locationDescriptions; List? deptRoom; LocationDescriptionResponse({ this.locationDescriptions, this.deptRoom, }); factory LocationDescriptionResponse.fromJson(Map 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 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? _locationDescriptions; List? get locationDescriptions => _locationDescriptions; // contain dept room list List? _deptRoomList; List? 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 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 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; } } }