Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
20 kB
2
Indexable
Never
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';
import 'package:evaluator_app/service/api_manager.dart';
import 'package:evaluator_app/service/endpoints.dart';
import 'package:evaluator_app/utils/strings.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../model/response/document/document_response.dart';
import '../../../utils/constants.dart';
import 'package:evaluator_app/utils/globals.dart' as globals;
import '../../../widgets/custom_toast.dart';
import '../../../widgets/progressbar.dart';
import '../dashboard_view_model.dart';
import 'package:workmanager/workmanager.dart';

const fetchBackground = "be.tramckrijte.workmanagerExample.simpleTask";

@pragma('vm:entry-point')
void callbackDispatcher() {
  try {
    Workmanager().executeTask((task, inputData) async {
      switch (task) {
        case fetchBackground:
          try {
            String carId = inputData?['carId'];
            try {
              /*var request = http.MultipartRequest('PATCH', Uri.parse('${EndPoints.baseUrl}${EndPoints.document}/$carId'));
              request.fields.addAll({
                'insurance': inputData?['insurance'],
                'insuranceCompany': inputData?['insuranceCompany'],
                'insuranceIDV': inputData?['insuranceIDV'],
                'insuranceValidity': inputData?['insuranceValidity'],
                'bankName': inputData?['bankName'],
                'hypothecation': inputData?['hypothecation'],
                'loanNoc': inputData?['loanNoc'],
                'ncb': inputData?['ncb'],
                'loanStatus': inputData?['loanStatus'],
                'interStateTransfer': inputData?['interStateTransfer'],
                'form35': inputData?['form35'],
                'rcMismatch': inputData?['rcMismatch'],
                'insuranceMismatch': inputData?['insuranceMismatch'],
                'remarks': inputData?['remarks'],
                'rcFront_remarks': inputData?['rcFront_remarks'],
                'rcBack_remarks': inputData?['rcBack_remarks'],
                'chassisImage_remarks': inputData?['chassisImage_remarks'],
                'form35Image_remarks': inputData?['form35Image_remarks'],
                'nocImage_remarks': inputData?['nocImage_remarks'],
                'evaluationStatusForDocument': inputData?['evaluationStatusForDocument'],
              });
              if (inputData?['rcFront'] != null && !inputData?['rcFront'].startsWith('http') && !inputData?['rcFront'].startsWith('https')) {
                request.files.add(await http.MultipartFile.fromPath(
                  'rcFront',
                  inputData?['rcFront'],
                  contentType: MediaType('image', 'jpg'),
                ));
              }
              if (inputData?['rcBack'] != null && !inputData?['rcBack'].startsWith('http') && !inputData?['rcBack'].startsWith('https')) {
                request.files.add(await http.MultipartFile.fromPath(
                  'rcBack',
                  inputData?['rcBack'],
                  contentType: MediaType('image', 'jpg'),
                ));
              }
              if (inputData?['nocImage'] != null && inputData?['nocImage'] != '' && !inputData?['nocImage'].startsWith('http') && !inputData?['nocImage'].startsWith('https')) {
                request.files.add(await http.MultipartFile.fromPath('nocImage', inputData?['nocImage'], contentType: MediaType('image', 'jpg')));
              }
              if (inputData?['form35Image'] != null && inputData?['form35Image'] != '' && !inputData?['form35Image'].startsWith('http') && !inputData?['form35Image'].startsWith('https')) {
                request.files.add(await http.MultipartFile.fromPath(
                  'form35Image',
                  inputData?['form35Image'],
                  contentType: MediaType('image', 'jpg'),
                ));
              }
              if (inputData?['chassisImage'] != null && inputData?['chassisImage'] != '' && !inputData?['chassisImage'].startsWith('http') && !inputData?['chassisImage'].startsWith('https')) {
                request.files.add(await http.MultipartFile.fromPath(
                  'chassisImage',
                  inputData?['chassisImage'],
                  contentType: MediaType('image', 'jpg'),
                ));
              }
              var headers = {'Authorization': 'Bearer ${inputData?['token']}'};
              request.headers.addAll(headers);

              log(request.toString());

              var response = await request.send();*/
              var response = await ApiManager.multipartRequest(endpoint: '${EndPoints.document}/$carId',
                  fields: {
                    'insurance': inputData?['insurance'],
                    'insuranceCompany': inputData?['insuranceCompany'],
                    'insuranceIDV': inputData?['insuranceIDV'],
                    'insuranceValidity': inputData?['insuranceValidity'],
                    'bankName': inputData?['bankName'],
                    'hypothecation': inputData?['hypothecation'],
                    'loanNoc': inputData?['loanNoc'],
                    'ncb': inputData?['ncb'],
                    'loanStatus': inputData?['loanStatus'],
                    'interStateTransfer': inputData?['interStateTransfer'],
                    'form35': inputData?['form35'],
                    'rcMismatch': inputData?['rcMismatch'],
                    'insuranceMismatch': inputData?['insuranceMismatch'],
                    'remarks': inputData?['remarks'],
                    'rcFront_remarks': inputData?['rcFront_remarks'],
                    'rcBack_remarks': inputData?['rcBack_remarks'],
                    'chassisImage_remarks': inputData?['chassisImage_remarks'],
                    'form35Image_remarks': inputData?['form35Image_remarks'],
                    'nocImage_remarks': inputData?['nocImage_remarks'],
                    'evaluationStatusForDocument': inputData?['evaluationStatusForDocument'],
                  },
                  files: [
                    {'field': 'rcFront' ,'path': inputData?['rcFront']},
                    {'field': 'rcBack', 'path': inputData?['rcBack']},
                    {'field': 'nocImage', 'path': inputData?['nocImage']},
                    {'field': 'form35Image', 'path': inputData?['form35Image']},
                    {'field': 'chassisImage', 'path': inputData?['chassisImage']},
                  ]);
              if (response.statusCode == 200) {
                log(response.stream.toString());
                IsolateNameServer.lookupPortByName("api_call_port")!.send("api_call_success");
                if (Get.isRegistered<DashBoardViewModel>()) {
                  Get.delete<DashBoardViewModel>();
                }
              } else {
                log(response.reasonPhrase.toString());
                IsolateNameServer.lookupPortByName("api_call_port")!.send("api_call_failure");
              }
            } catch (e) {
              IsolateNameServer.lookupPortByName("api_call_port")!.send("api_call_failure");
              log(e.toString());
            }
          } catch (e) {
            IsolateNameServer.lookupPortByName("api_call_port")!.send("api_call_failure");
            log(e.toString());
          }
          break;
      }
      log("Native called background task: $task");
      return Future.value(true);
    });
  } catch (e) {
    log(e.toString());
  }
}

class DocumentViewModel extends GetxController {
  final GlobalKey<FormState> page1Key = GlobalKey<FormState>();
  final GlobalKey<FormState> page2Key = GlobalKey<FormState>();

  final Rx<PageController> pageController = PageController(initialPage: 0).obs;
  var documentResponse = DocumentResponse().obs;
  var id = Get.arguments ?? '';

  // the index of the current page
  var activePage = 0.obs;
  Rx<TextEditingController> insuranceCompanyController = TextEditingController().obs;
  Rx<TextEditingController> insuranceIDVController = TextEditingController().obs;
  Rx<TextEditingController> insuranceValidityController = TextEditingController().obs;
  Rx<TextEditingController> bankNameController = TextEditingController().obs;
  Rx<TextEditingController> remarksController = TextEditingController().obs;
  Rx<TextEditingController> rcFrontUploadRemarksController = TextEditingController().obs;
  Rx<TextEditingController> rcBackUploadRemarksController = TextEditingController().obs;
  Rx<TextEditingController> nocRemarksController = TextEditingController().obs;
  Rx<TextEditingController> chassisRemarksController = TextEditingController().obs;
  Rx<TextEditingController> form35RemarksController = TextEditingController().obs;
  List<String> yesNoList = Constants.yesNoList;
  List<String> insuranceList = Constants.insuranceList;
  Rx<File?> rcFrontImage = Rx<File?>(null);
  Rx<File?> rcBackImage = Rx<File?>(null);
  Rx<File?> nocImage = Rx<File?>(null);
  Rx<File?> form35Image = Rx<File?>(null);
  Rx<File?> chassisImage = Rx<File?>(null);

  var selectedRc = ''.obs;
  var selectedUnderHypothecation = ''.obs;
  var selectedInsurance = ''.obs;
  var selectedInterStateTransfer = ''.obs;
  var selectedNCB = ''.obs;
  var selectedLoanClosed = ''.obs;
  var selectedLoanNoc = ''.obs;
  var selectedForm35 = ''.obs;
  var selectedRcMismatch = ''.obs;
  var selectedInsuranceMismatch = ''.obs;
  var isPage1Fill = false.obs;
  var isPage2Fill = false.obs;

  @override
  void onInit() {
    getDocument();
    super.onInit();
  }

  void registerBackgroundTask() {
    Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
    RawReceivePort receivePort = RawReceivePort();
    IsolateNameServer.registerPortWithName(receivePort.sendPort, "api_call_port");
    receivePort.handler = (message) {
      if (message == "api_call_success") {
        CustomToast.instance.showMsg('${MyStrings.documents} ${MyStrings.updateSuccess}');
        if(Get.isRegistered<DashBoardViewModel>()){
          Get.find<DashBoardViewModel>().getEvaluationStatus();
        }else{
          Get.put(DashBoardViewModel());
          Get.find<DashBoardViewModel>().getEvaluationStatus();
        }
      }else if(message == "api_call_failure"){
        CustomToast.instance.showMsg('${MyStrings.documents} ${MyStrings.uploadFailed}');
      }
    };
    Workmanager().registerOneOffTask(fetchBackground, fetchBackground,
        // existingWorkPolicy: ExistingWorkPolicy.append,
        // constraints: Constraints(networkType: NetworkType.connected),
        inputData: <String, dynamic>{
          'insurance': selectedInsurance.value.toString(),
          'insuranceCompany': insuranceCompanyController.value.text,
          'insuranceIDV': insuranceIDVController.value.text,
          'insuranceValidity': insuranceValidityController.value.text,
          'bankName': bankNameController.value.text,
          'hypothecation': selectedUnderHypothecation.value.toString(),
          'loanNoc': selectedLoanNoc.value.toString(),
          'ncb': selectedNCB.value.toString(),
          'loanStatus': selectedLoanClosed.value.toString(),
          'interStateTransfer': selectedInterStateTransfer.value.toString(),
          'form35': selectedForm35.value.toString(),
          'rcMismatch': selectedRcMismatch.value.toString(),
          'insuranceMismatch': selectedInsuranceMismatch.value.toString(),
          'remarks': remarksController.value.text,
          'rcFront_remarks': rcFrontUploadRemarksController.value.text,
          'rcBack_remarks': rcBackUploadRemarksController.value.text,
          'chassisImage_remarks': chassisRemarksController.value.text,
          'form35Image_remarks': form35RemarksController.value.text,
          'nocImage_remarks': nocRemarksController.value.text,
          'evaluationStatusForDocument': 'COMPLETED',
          // 'headers':globals.headers,
          'rcFront': rcFrontImage.value?.path,
          'rcBack': rcBackImage.value?.path,
          'nocImage': nocImage.value?.path ?? '',
          'form35Image': form35Image.value?.path ?? '',
          'chassisImage': chassisImage.value?.path ?? '',
          'carId': globals.carId,
          'token': globals.token
        });
  }

/*  void addDocuments() async {
    ProgressBar.instance.showProgressbar(Get.context!);
    try {
      var request = http.MultipartRequest('PATCH', Uri.parse('${EndPoints.baseUrl}${EndPoints.document}/${globals.carId}'));
      request.fields.addAll({
        'insurance': selectedInsurance.value,
        'insuranceCompany': insuranceCompanyController.value.text,
        'insuranceIDV': insuranceIDVController.value.text,
        'insuranceValidity': insuranceValidityController.value.text,
        'bankName': bankNameController.value.text,
        'hypothecation': selectedUnderHypothecation.value,
        'loanNoc': selectedLoanNoc.value,
        'ncb': selectedNCB.value,
        'loanStatus': selectedLoanClosed.value,
        'interStateTransfer': selectedInterStateTransfer.value,
        'form35': selectedForm35.value,
        'rcMismatch': selectedRcMismatch.value,
        'insuranceMismatch': selectedInsuranceMismatch.value,
        'remarks': remarksController.value.text,
        'rcFront_remarks': rcFrontUploadRemarksController.value.text,
        'rcBack_remarks': rcBackUploadRemarksController.value.text,
        'chassisImage_remarks': chassisRemarksController.value.text,
        'form35Image_remarks': form35RemarksController.value.text,
        'nocImage_remarks': nocRemarksController.value.text,
        'evaluationStatusForDocument': 'COMPLETED'
      });
      if (rcFrontImage.value != null && !rcFrontImage.value!.path.startsWith('http') && !rcFrontImage.value!.path.startsWith('https')) {
        request.files.add(await http.MultipartFile.fromPath('rcFront', rcFrontImage.value!.path,contentType: MediaType('image', 'jpg'),));
      }
      if (rcBackImage.value != null && !rcBackImage.value!.path.startsWith('http') && !rcBackImage.value!.path.startsWith('https')) {
        request.files.add(await http.MultipartFile.fromPath('rcBack', rcBackImage.value!.path,contentType: MediaType('image', 'jpg'),));
      }
      if(nocImage.value != null *//*&& !nocImage.value!.path.startsWith('http') && !nocImage.value!.path.startsWith('https')*//*) {
        if (!nocImage.value!.path.startsWith('http') && !nocImage.value!.path.startsWith('https')) {
          var type = nocImage.value!.path.split('.').last;
          request.files.add(await http.MultipartFile.fromPath('nocImage', nocImage.value!.path,contentType: MediaType('image', type)));
        }
      }
      if(form35Image.value != null *//*&& !form35Image.value!.path.startsWith('http') && !form35Image.value!.path.startsWith('https')*//*) {
        if (!form35Image.value!.path.startsWith('http') && !form35Image.value!.path.startsWith('https')) {
          var type = form35Image.value!.path.split('.').last;
          request.files.add(await http.MultipartFile.fromPath('form35Image', form35Image.value!.path,contentType: MediaType('image', type),));
        }
      }
      if(chassisImage.value != null *//*&& !chassisImage.value!.path.startsWith('http') && !chassisImage.value!.path.startsWith('https')*//*) {
        if (!chassisImage.value!.path.startsWith('http') && !chassisImage.value!.path.startsWith('https')) {
          var type = chassisImage.value!.path.split('.').last;
          request.files.add(await http.MultipartFile.fromPath('chassisImage', chassisImage.value!.path,contentType: MediaType('image', type),));
        }
      }
      request.headers.addAll(globals.headers);

      log(request.toString());

      var response = await request.send();

      if (response.statusCode == 200) {
        ProgressBar.instance.stopProgressBar(Get.context!);
        log(response.stream.toString());
        // Get.back();
        if (Get.isRegistered<DashBoardViewModel>()) {
          Get.delete<DashBoardViewModel>();
        }
        Get.offNamed(AppRoutes.dashBoardScreen);
      } else {
        ProgressBar.instance.stopProgressBar(Get.context!);
        log(response.reasonPhrase.toString());
      }
    } catch (e) {
      ProgressBar.instance.stopProgressBar(Get.context!);
      log(e.toString());
      CustomToast.instance.showMsg(ExceptionErrorUtil.handleErrors(e).errorMessage ?? '');
    }
  }*/

  void getDocument() async {
    // ProgressBar.instance.showProgressbar(Get.context!);
    try {
      var response = await ApiManager.get(endpoint: '${EndPoints.document}/${globals.carId}');
      if (response.statusCode == 200) {
        log(response.body.toString());
        documentResponse.value = DocumentResponse.fromJson(jsonDecode(response.body));
        loadData();
        ProgressBar.instance.stopProgressBar(Get.context!);
      } else {
        ProgressBar.instance.stopProgressBar(Get.context!);
        log(response.reasonPhrase.toString());
      }
    } catch (e) {
      ProgressBar.instance.stopProgressBar(Get.context!);
      log(e.toString());
      // CustomToast.instance.showMsg(ExceptionErrorUtil.handleErrors(e).errorMessage ?? '');
    }
  }

  void loadData() {
    if (documentResponse.value.data != null && documentResponse.value.data?.rcFront != null && documentResponse.value.data?.rcBack != null) {
      isPage2Fill.value = true;
      isPage1Fill.value = true;
      insuranceCompanyController.value.text = documentResponse.value.data?.insuranceCompany ?? '';
      insuranceIDVController.value.text = documentResponse.value.data?.insuranceIDV ?? '';
      insuranceValidityController.value.text = documentResponse.value.data?.insuranceValidity ?? '';
      bankNameController.value.text = documentResponse.value.data?.bankName ?? '';
      remarksController.value.text = documentResponse.value.data?.remarks ?? '';
      rcFrontUploadRemarksController.value.text = documentResponse.value.data?.rcFront?.remarks ?? '';
      rcBackUploadRemarksController.value.text = documentResponse.value.data?.rcBack?.remarks ?? '';
      chassisRemarksController.value.text = documentResponse.value.data?.chassisImage?.remarks ?? '';
      nocRemarksController.value.text = documentResponse.value.data?.nocImage?.remarks ?? '';
      form35RemarksController.value.text = documentResponse.value.data?.form35Image?.remarks ?? '';
      rcFrontImage.value = documentResponse.value.data?.rcFront?.url != null ? File(documentResponse.value.data?.rcFront?.url ?? '') : null;
      rcBackImage.value = documentResponse.value.data?.rcBack?.url != null ? File(documentResponse.value.data?.rcBack?.url ?? '') : null;
      chassisImage.value = documentResponse.value.data?.chassisImage?.url != null ? File(documentResponse.value.data?.chassisImage?.url ?? '') : null;
      nocImage.value = documentResponse.value.data?.nocImage?.url != null ? File(documentResponse.value.data?.nocImage?.url ?? '') : null;
      form35Image.value = documentResponse.value.data?.form35Image?.url != null ? File(documentResponse.value.data?.form35Image?.url ?? '') : null;
      selectedInterStateTransfer.value = documentResponse.value.data?.interStateTransfer ?? '';
      selectedInsurance.value = documentResponse.value.data?.insurance ?? '';
      selectedNCB.value = documentResponse.value.data?.ncb ?? '';
      selectedLoanClosed.value = documentResponse.value.data?.loanStatus ?? '';
      selectedForm35.value = documentResponse.value.data?.form35 ?? '';
      selectedRcMismatch.value = documentResponse.value.data?.rcMismatch ?? '';
      selectedInsuranceMismatch.value = documentResponse.value.data?.insuranceMismatch ?? '';
      selectedRc.value = documentResponse.value.data?.rcAvailability ?? '';
      selectedLoanNoc.value = documentResponse.value.data!.loanNoc ?? '';
      selectedUnderHypothecation.value = documentResponse.value.data!.hypothecation ?? '';
    }
  }
}
Leave a Comment