|
|
|
|
@ -4,6 +4,10 @@ import 'package:easy_localization/easy_localization.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
|
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
|
import 'package:huawei_location/location/fused_location_provider_client.dart';
|
|
|
|
|
import 'package:huawei_location/location/location_request.dart';
|
|
|
|
|
import 'package:huawei_location/location/location_settings_request.dart';
|
|
|
|
|
import 'package:huawei_location/permission/permission_handler.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/api/dashboard_api_client.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/colors.dart';
|
|
|
|
|
@ -21,6 +25,7 @@ import 'package:mohem_flutter_app/widgets/location/Location.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/widgets/nfc/nfc_reader_sheet.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/widgets/qr_scanner_dialog.dart';
|
|
|
|
|
import 'package:nfc_manager/nfc_manager.dart';
|
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
import 'package:wifi_iot/wifi_iot.dart';
|
|
|
|
|
|
|
|
|
|
class MarkAttendanceWidget extends StatefulWidget {
|
|
|
|
|
@ -39,6 +44,8 @@ class MarkAttendanceWidget extends StatefulWidget {
|
|
|
|
|
class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
bool isNfcEnabled = false, isNfcLocationEnabled = false, isQrEnabled = false, isQrLocationEnabled = false, isWifiEnabled = false, isWifiLocationEnabled = false;
|
|
|
|
|
|
|
|
|
|
int _locationUpdateCbId = 0;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
@ -66,6 +73,36 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkHuaweiLocationPermission(String attendanceType) async {
|
|
|
|
|
PermissionHandler permissionHandler = PermissionHandler();
|
|
|
|
|
|
|
|
|
|
if (await permissionHandler.hasLocationPermission()) {
|
|
|
|
|
getHuaweiCurrentLocation(attendanceType);
|
|
|
|
|
} else {
|
|
|
|
|
bool has = await requestPermissions();
|
|
|
|
|
if (has) {
|
|
|
|
|
getHuaweiCurrentLocation(attendanceType);
|
|
|
|
|
} else {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext cxt) => ConfirmDialog(
|
|
|
|
|
message: "You need to give location permission to mark attendance",
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> requestPermissions() async {
|
|
|
|
|
var result = await [
|
|
|
|
|
Permission.location,
|
|
|
|
|
].request();
|
|
|
|
|
return (result[Permission.location] == PermissionStatus.granted || result[Permission.locationAlways] == PermissionStatus.granted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
@ -95,6 +132,9 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
// if (isNfcEnabled)
|
|
|
|
|
attendanceMethod("NFC", "assets/images/nfc.svg", isNfcEnabled, () {
|
|
|
|
|
if (isNfcLocationEnabled) {
|
|
|
|
|
if (AppState().getIsHuawei) {
|
|
|
|
|
checkHuaweiLocationPermission("NFC");
|
|
|
|
|
} else {
|
|
|
|
|
Location.getCurrentLocation((Position position, bool isMocked) {
|
|
|
|
|
if (isMocked) {
|
|
|
|
|
markFakeAttendance("NFC", position.latitude.toString() ?? "", position.longitude.toString() ?? "");
|
|
|
|
|
@ -102,6 +142,7 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
performNfcAttendance(widget.model, lat: position.latitude.toString() ?? "", lng: position.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
}, context);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
performNfcAttendance(widget.model);
|
|
|
|
|
}
|
|
|
|
|
@ -109,6 +150,9 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
if (isWifiEnabled)
|
|
|
|
|
attendanceMethod("Wifi", "assets/images/wufu.svg", isWifiEnabled, () {
|
|
|
|
|
if (isWifiLocationEnabled) {
|
|
|
|
|
if (AppState().getIsHuawei) {
|
|
|
|
|
checkHuaweiLocationPermission("WIFI");
|
|
|
|
|
} else {
|
|
|
|
|
Location.getCurrentLocation((Position position, bool isMocked) {
|
|
|
|
|
if (isMocked) {
|
|
|
|
|
markFakeAttendance("WIFI", position.latitude.toString() ?? "", position.longitude.toString() ?? "");
|
|
|
|
|
@ -116,6 +160,7 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
performWifiAttendance(widget.model, lat: position.latitude.toString() ?? "", lng: position.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
}, context);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
performWifiAttendance(widget.model);
|
|
|
|
|
}
|
|
|
|
|
@ -124,6 +169,9 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
if (isQrEnabled)
|
|
|
|
|
attendanceMethod("QR", "assets/images/ic_qr.svg", isQrEnabled, () async {
|
|
|
|
|
if (isQrLocationEnabled) {
|
|
|
|
|
if (AppState().getIsHuawei) {
|
|
|
|
|
checkHuaweiLocationPermission("QR");
|
|
|
|
|
} else {
|
|
|
|
|
Location.getCurrentLocation((Position position, bool isMocked) {
|
|
|
|
|
if (isMocked) {
|
|
|
|
|
markFakeAttendance("QR", position.latitude.toString() ?? "", position.longitude.toString() ?? "");
|
|
|
|
|
@ -131,6 +179,7 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
performQrCodeAttendance(widget.model, lat: position.latitude.toString() ?? "", lng: position.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
}, context);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
performQrCodeAttendance(widget.model);
|
|
|
|
|
}
|
|
|
|
|
@ -143,6 +192,45 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getHuaweiCurrentLocation(String attendanceType) {
|
|
|
|
|
FusedLocationProviderClient locationService = FusedLocationProviderClient();
|
|
|
|
|
LocationRequest locationRequest = LocationRequest();
|
|
|
|
|
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
|
|
|
|
|
locationRequest.interval = 1000;
|
|
|
|
|
List<LocationRequest> locationRequestList = <LocationRequest>[locationRequest];
|
|
|
|
|
LocationSettingsRequest locationSettingsRequest = LocationSettingsRequest(requests: locationRequestList);
|
|
|
|
|
|
|
|
|
|
locationService.checkLocationSettings(locationSettingsRequest).then((settings) async {
|
|
|
|
|
await locationService.getLastLocation().then((value) {
|
|
|
|
|
if (value.latitude == null || value.longitude == null) {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext cxt) => ConfirmDialog(
|
|
|
|
|
message: "Unable to get your location, Please check your location settings & try again.",
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
if (attendanceType == "QR") {
|
|
|
|
|
performQrCodeAttendance(widget.model, lat: value.latitude.toString() ?? "", lng: value.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
if (attendanceType == "WIFI") {
|
|
|
|
|
performWifiAttendance(widget.model, lat: value.latitude.toString() ?? "", lng: value.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
if (attendanceType == "NFC") {
|
|
|
|
|
performNfcAttendance(widget.model, lat: value.latitude.toString() ?? "", lng: value.longitude.toString() ?? "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}).catchError((error) {
|
|
|
|
|
if (error.code == "LOCATION_SETTINGS_NOT_AVAILABLE") {
|
|
|
|
|
// Location service not enabled.
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> performNfcAttendance(DashboardProviderModel model, {String lat = "0", String lng = "0"}) async {
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
|
Utils.readNFc(onRead: (String nfcId) async {
|
|
|
|
|
@ -252,12 +340,26 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
bool status = await model.fetchAttendanceTracking(context);
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
await closeWifiRequest();
|
|
|
|
|
if (g?.messageStatus == 2) {
|
|
|
|
|
showDialog(
|
|
|
|
|
barrierDismissible: true,
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (cxt) => ConfirmDialog(
|
|
|
|
|
message: g?.errorEndUserMessage ?? "",
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
},
|
|
|
|
|
onCloseTap: () {},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
showMDialog(
|
|
|
|
|
context,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
isDismissable: false,
|
|
|
|
|
child: SuccessDialog(widget.isFromDashboard),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
await closeWifiRequest();
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
@ -292,12 +394,26 @@ class _MarkAttendanceWidgetState extends State<MarkAttendanceWidget> {
|
|
|
|
|
GenericResponseModel? g = await DashboardApiClient().markAttendance(pointType: 1, isGpsRequired: isQrLocationEnabled, lat: lat, long: lng, QRValue: qrCodeValue);
|
|
|
|
|
bool status = await model.fetchAttendanceTracking(context);
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
if (g?.messageStatus == 2) {
|
|
|
|
|
showDialog(
|
|
|
|
|
barrierDismissible: true,
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (cxt) => ConfirmDialog(
|
|
|
|
|
message: g?.errorEndUserMessage ?? "",
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
},
|
|
|
|
|
onCloseTap: () {},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
showMDialog(
|
|
|
|
|
context,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
isDismissable: true,
|
|
|
|
|
child: SuccessDialog(widget.isFromDashboard),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
print(ex);
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
|