From f395d6c4d2415bc42635d6e471b58b3796a7f546 Mon Sep 17 00:00:00 2001 From: orangdeso Date: Wed, 16 Apr 2025 17:35:32 +0700 Subject: [PATCH] Feat: fixed string data type to timestamp for DepartureTime and ArrivalTime fields --- lib/_core/utils/formatter/date_helper.dart | 56 +++++++++++++++++++ lib/_core/utils/map_helper.dart | 32 +++++++++++ .../pages/ticket_booking_step4_screen.dart | 6 +- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 lib/_core/utils/formatter/date_helper.dart create mode 100644 lib/_core/utils/map_helper.dart diff --git a/lib/_core/utils/formatter/date_helper.dart b/lib/_core/utils/formatter/date_helper.dart new file mode 100644 index 0000000..c7e23c4 --- /dev/null +++ b/lib/_core/utils/formatter/date_helper.dart @@ -0,0 +1,56 @@ +import 'package:intl/intl.dart'; + +class DateFormatterHelper { + static String formatFlightTime(dynamic timeValue) { + if (timeValue == null) return ""; + + if (timeValue is int) { + return DateFormat.jm().format(DateTime.fromMillisecondsSinceEpoch(timeValue)); + } else if (timeValue is String) { + return timeValue; + } + + return ""; + } + + static String formatFlightDate(dynamic timeValue) { + if (timeValue == null) return ""; + + if (timeValue is int) { + return DateFormat("EEE, d MMM").format(DateTime.fromMillisecondsSinceEpoch(timeValue)); + } + + return ""; + } + + static String calculateFlightDuration(dynamic departureTime, dynamic arrivalTime) { + if (departureTime == null || arrivalTime == null) return ""; + + DateTime? departure; + DateTime? arrival; + + if (departureTime is int) { + departure = DateTime.fromMillisecondsSinceEpoch(departureTime); + } + + if (arrivalTime is int) { + arrival = DateTime.fromMillisecondsSinceEpoch(arrivalTime); + } + + if (departure == null || arrival == null) return ""; + + final difference = arrival.difference(departure); + final hours = difference.inHours; + final minutes = difference.inMinutes % 60; + + if (hours > 0) { + if (minutes > 0) { + return "${hours}j ${minutes}m"; + } else { + return "${hours}j"; + } + } else { + return "${minutes}m"; + } + } +} diff --git a/lib/_core/utils/map_helper.dart b/lib/_core/utils/map_helper.dart new file mode 100644 index 0000000..deaed55 --- /dev/null +++ b/lib/_core/utils/map_helper.dart @@ -0,0 +1,32 @@ +class MapHelper { + + static dynamic getNestedValue(dynamic data, List keys, dynamic defaultValue) { + if (data == null) return defaultValue; + + dynamic currentData = data; + for (String key in keys) { + if (currentData is Map && currentData.containsKey(key)) { + currentData = currentData[key]; + } else { + return defaultValue; + } + } + + return currentData ?? defaultValue; + } + + static bool hasNestedPath(dynamic data, List keys) { + if (data == null) return false; + + dynamic currentData = data; + for (String key in keys) { + if (currentData is Map && currentData.containsKey(key)) { + currentData = currentData[key]; + } else { + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/lib/presentation/screens/home/pages/ticket_booking_step4_screen.dart b/lib/presentation/screens/home/pages/ticket_booking_step4_screen.dart index e63be3d..e257f16 100644 --- a/lib/presentation/screens/home/pages/ticket_booking_step4_screen.dart +++ b/lib/presentation/screens/home/pages/ticket_booking_step4_screen.dart @@ -266,7 +266,7 @@ class _TicketBookingStep4ScreenState extends State { // Persiapkan data expiry time final DateTime currentTime = DateTime.now(); - final DateTime expiryTime = currentTime.add(Duration(days: 1)); + final DateTime expiryTime = currentTime.add(Duration(seconds: 20)); // Persiapkan data bandara final bandaraData = { @@ -292,8 +292,8 @@ class _TicketBookingStep4ScreenState extends State { 'cityArrival': flightData?.cityArrival, 'codeDeparture': flightData?.codeDeparture, 'codeArrival': flightData?.codeArrival, - 'departureTime': departureTime, - 'arrivalTime': arrivalTime, + 'departureTime': flightData?.departureTime.millisecondsSinceEpoch, + 'arrivalTime': flightData?.arrivalTime.millisecondsSinceEpoch, 'flightClass': flightData?.flightClass, 'transitAirplane': flightData?.transitAirplane, 'stop': flightData?.stop,