Untitled

 avatar
unknown
dart
3 years ago
4.2 kB
2
Indexable
  Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      print("bg handler - bg handler");
      print("bg handler: " + message.data.toString());
      if (message != null && message.data != null) {
        String dataType = message.data['type'];
        if (dataType == 'JobRequest' && message.data["RequestData"] != null) {
          //bla bla bla bla
          //bla bla bla bla
          //bla bla bla bla
        } else if (dataType == "TripCompleted") {
          var jsonTripInfo = jsonDecode(message.data["TripResult"]);
          var jsonRequest = jsonDecode(message.data["CustomerRequest"]);
          String price = jsonTripInfo["RentedTripFare"] == null
              ? "0"
              : (((jsonTripInfo["RentedTripFare"]) +
                          (jsonTripInfo["RentedTripExtras"])) /
                      1000)
                  .toString();
          String distance = jsonTripInfo["RentedTripDistance"] == null
              ? "0"
              : (double.parse(jsonTripInfo["RentedTripDistance"].toString()) / 1000)
                  .toString();
          String paymentMethod = jsonRequest["PayMethods"] == null
              ? ""
              : int.parse(jsonRequest["PayMethods"].toString()) == 0
                  ? "Nakit"
                  : int.parse(jsonRequest["PayMethods"].toString()) == 1
                      ? "Online Ödeme"
                      : "";
          int myCurrentNTP =
              (await NTP.now()).toUtc().millisecondsSinceEpoch ~/ 1000;
          SharedPreferences prefs = await SharedPreferences.getInstance();
          await prefs.reload();
          prefs.setString("tripcompleted", "true");
          prefs.setInt("ntp", myCurrentNTP);
          prefs.setString("currentdriverid", prefs.getString("userid"));
          prefs.setString("tripcost", price);
          prefs.setString("distance", distance);
          prefs.setString("paymethod", paymentMethod);
        }
      }
}
  
  void checkIfTripCompletedOnBackground() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.reload();
    String isTripCompleted = prefs.getString("tripcompleted");
    if (isTripCompleted != null) {
      int myCurrentNTP =
          (await NTP.now()).toUtc().millisecondsSinceEpoch ~/ 1000;
      int ntpDiff = myCurrentNTP - prefs.getInt("ntp") ?? 0;
      if (prefs.getString("currentdriverid") == GlobalV.user.driver.sId &&
          ntpDiff < 300) {
        queueEntryAfterTrip = true;
        WebServices.socket.emit("check-taxi-station-area", GlobalV.tabletID);

        if (dismissableDialog != null) {
          dismissableDialog.dismiss();
          dismissableDialog = null;
        }

        GlobalV.playSound();
        dismissableDialog = AwesomeDialog(
            context: context,
            title:
                'Mesafe: ${prefs.getString("distance")} km\nÜcret: ${prefs.getString("tripcost")} TL\nÖdeme Tipi: ${prefs.getString("paymethod")}',
            btnOkOnPress: () {},
            btnOkText: "Tamam",
            btnCancelOnPress: null,
            btnCancelText: null,
            dialogType: DialogType.INFO);
        dismissableDialog.show();

        prefs.remove("tripcompleted");
        prefs.remove("currentdriverid");
        prefs.remove("ntp");
        prefs.remove("tripcost");
        prefs.remove("distance");
        prefs.remove("paymethod");
      } else {
        prefs.remove("tripcompleted");
        prefs.remove("currentdriverid");
        prefs.remove("ntp");
        prefs.remove("tripcost");
        prefs.remove("distance");
        prefs.remove("paymethod");
      }
    }
  }
  
    void didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed) {
      print("------------Lifecycle: foreground");
      GlobalV.isAppOnForeground = true;
      flutterLocalNotificationsPlugin.cancelAll();
      if (lifeCycleActive) {
        getJobFromBackground();
        checkIfTripCompletedOnBackground(); // <-------------------------
      }
      await myGetStateFunc();
    } else if (state != AppLifecycleState.resumed) {
      print("------------Lifecycle: background");
      GlobalV.isAppOnForeground = false;
    }
  }