Untitled

mail@pastecode.io avatar
unknown
dart
2 months ago
2.8 kB
1
Indexable
Never
Future sinkronAbsen() async{
    bool isConnected = true;
    try {
      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        print('connected');
      }
    } on SocketException catch (_) {
      isConnected = false;
    }
    if (isConnected == true) {
      //checkin
      final LocalStorage storage = LocalStorage("upcoming");
      String? tmpNumber = storage.getItem('tmpNumberAbsenIn') ?? '0';
      if(tmpNumber != '0') {
        print("sinkron");
        for (int tmpCurrent = 1; tmpCurrent <= int.parse(tmpNumber!); tmpCurrent++) {
          print("masuk");
          var userId = await SPUtill.getIntValue(SPUtill.keyUserId);
          var token = await SPUtill.getValue(SPUtill.keyAuthToken);
          var headers = {
            'Authorization': 'Bearer $token',
            'Content-Type': 'application/json'
          };
          String checkin = "";
          String checkout = "";
          if(storage.getItem('checkIn' + tmpCurrent.toString()) != null){
            checkin = "${storage.getItem('dateIn' + tmpCurrent.toString()).toString()} ${storage.getItem('checkIn' + tmpCurrent.toString()).toString()}";
          }
          if(storage.getItem('checkOut' + tmpCurrent.toString())  != null){
            checkout = "${storage.getItem('dateIn' + tmpCurrent.toString()).toString()} ${storage.getItem('checkOut' + tmpCurrent.toString()).toString()}";
          }

          var data = json.encode({
            "remote_mode_in": "1",
            "date": storage.getItem('dateIn' + tmpCurrent.toString()).toString(),
            "checkin": checkin,
            "checkout": checkout,
            "checkin_ip": "",
            "check_in_latitude": storage.getItem('check_in_latitude' + tmpCurrent.toString()).toString(),
            "check_in_longitude": storage.getItem('check_in_longitude' + tmpCurrent.toString()).toString(),
            "city": "",
            "country_code": "",
            "country": "",
            "check_out_latitude": storage.getItem('check_out_latitude' + tmpCurrent.toString()).toString(),
            "check_out_longitude": storage.getItem('check_out_longitude' + tmpCurrent.toString()).toString()
          });
          print("xxxxxxx");
          print(data);
          var dio = Dio();
          var response = await dio.request(
            'https://www.sistemweb.my.id/api/V11/user/attendance/check-inout',
            options: Options(
              method: 'POST',
              headers: headers,
            ),
            data: data,
          );

          if (response.statusCode == 200) {
            print(json.encode(response.data));
          }
          else {
            print(response.statusMessage);
          }
        }
        storage.setItem('tmpNumberAbsenIn', '0');
      }
    }
  }
Leave a Comment