timer function

void timer(
  1. SendPort isolateToMainStream
)

Implementation

void timer(SendPort isolateToMainStream) {
  ReceivePort mainToIsolateStream = ReceivePort();

  isolateToMainStream.send(mainToIsolateStream.sendPort);

  String captchaData = "";
  bool checkCaptcha = false;
  bool checkCaptchaError = false;
  bool checkOTP = false;
  int captchaTime = 0;
  int captchaErrorTime = 0;
  int otpTime = 0;

  mainToIsolateStream.listen((data) {
    if (data == TimerStatus.captchaTimer) {
      captchaTime = DateTime.now().millisecondsSinceEpoch;
      checkCaptcha = true;
    } else if (data == TimerStatus.captchaErrorTimer) {
      captchaErrorTime = DateTime.now().millisecondsSinceEpoch;
      checkCaptchaError = true;
    } else if (data == TimerStatus.otpTimer) {
      otpTime = DateTime.now().millisecondsSinceEpoch;
      checkOTP = true;
    } else if (data == TimerStatus.stopCaptchaTimer) {
      checkCaptcha = false;
    } else if (data == TimerStatus.stopCaptchaErrorTimer) {
      checkCaptchaError = false;
    } else if (data == TimerStatus.stopOtpTimer) {
      checkOTP = false;
    } else if (data is String) {
      if (data.contains("base64") && captchaData != data) {
        var base64 = data.split("base64,")[1].split("\"")[0];
        if (base64.endsWith("\\")) {
          base64 = base64.substring(0, base64.length - 1);
        }
        isolateToMainStream.send(base64);
        captchaData = data;
      }
    }
  });
  Timer.periodic(const Duration(seconds: 1), (Timer t) async {
    var currentMilli = DateTime.now().millisecondsSinceEpoch;
    if (checkCaptcha && captchaTime + 2000 < currentMilli) {
      captchaTime = currentMilli;
      isolateToMainStream.send(TimerStatus.captchaTimer);
    }
    if (checkCaptchaError && captchaErrorTime + 1000 < currentMilli) {
      captchaErrorTime = currentMilli;
      isolateToMainStream.send(TimerStatus.captchaErrorTimer);
    }
    if (checkOTP && otpTime + 1500 < currentMilli) {
      otpTime = currentMilli;
      isolateToMainStream.send(TimerStatus.otpTimer);
    }
    isolateToMainStream.send(TimerStatus.mainErrorTimer);
  });
}