diff --git a/lib/core/utils/date_utils.dart b/lib/core/utils/date_utils.dart index cc98580..c03b277 100644 --- a/lib/core/utils/date_utils.dart +++ b/lib/core/utils/date_utils.dart @@ -1,169 +1,8 @@ -import 'package:intl/intl.dart'; +String timeConvertor(int time) { + DateTime date = DateTime.fromMillisecondsSinceEpoch(time); -/// Extension on int to provide date formatting utilities for Unix timestamps -extension UnixTimestampExtension on int? { - /// Converts Unix timestamp (seconds since epoch) to a formatted date string - String toDate({String format = 'yyyy-MM-dd'}) { - if (this == null) { - return ''; - } + String formattedDate = + "${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}"; - // Convert seconds to milliseconds if needed - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - return DateFormat(format).format(dateTime); - } - - /// Converts Unix timestamp to a readable date string - String toReadableDate() { - if (this == null) { - return ''; - } - - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - final DateTime now = DateTime.now(); - final Duration difference = now.difference(dateTime); - - if (difference.inDays == 0) { - return 'Today'; - } else if (difference.inDays == 1) { - return 'Yesterday'; - } else if (difference.inDays < 7) { - return '${difference.inDays} days ago'; - } else { - return DateFormat('MMM dd, yyyy').format(dateTime); - } - } - - /// Converts Unix timestamp to a short date format - String toShortDate() { - if (this == null) { - return ''; - } - - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - return DateFormat('MMM dd').format(dateTime); - } - - /// Converts Unix timestamp to a full date and time format - String toDateTime() { - if (this == null) { - return ''; - } - - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - return DateFormat('MMM dd, yyyy HH:mm').format(dateTime); - } - - /// Checks if a Unix timestamp is in the past - bool get isPast { - if (this == null) { - return false; - } - - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - return dateTime.isBefore(DateTime.now()); - } - - /// Gets the number of days remaining until a Unix timestamp - int get daysRemaining { - if (this == null) { - return 0; - } - - int timestampInMs = this! > 1000000000000 ? this! : this! * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - final DateTime now = DateTime.now(); - final Duration difference = dateTime.difference(now); - - return difference.inDays; - } -} - -/// Extension on DateTime to provide additional formatting utilities -extension DateTimeExtension on DateTime { - /// Formats the DateTime to a readable string - String toReadableString() { - final DateTime now = DateTime.now(); - final Duration difference = now.difference(this); - - if (difference.inDays == 0) { - return 'Today'; - } else if (difference.inDays == 1) { - return 'Yesterday'; - } else if (difference.inDays < 7) { - return '${difference.inDays} days ago'; - } else { - return DateFormat('MMM dd, yyyy').format(this); - } - } - - /// Formats the DateTime to a short date string - String toShortDate() { - return DateFormat('MMM dd').format(this); - } - - /// Formats the DateTime to a full date and time string - String toDateTimeString() { - return DateFormat('MMM dd, yyyy HH:mm').format(this); - } - - /// Checks if the DateTime is in the past - bool get isPast { - return isBefore(DateTime.now()); - } - - /// Gets the number of days remaining until this DateTime - int get daysRemaining { - final DateTime now = DateTime.now(); - final Duration difference = this.difference(now); - return difference.inDays; - } -} - -/// Utility class for date operations -class DateUtils { - /// Converts Unix timestamp to a formatted date string - - static String unixToDate(int? timestamp) { - if (timestamp == null) { - return ''; - } - - // Convert seconds to milliseconds if needed - int timestampInMs = - timestamp > 1000000000000 ? timestamp : timestamp * 1000; - - final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch( - timestampInMs, - ); - return DateFormat('MMM dd, yyyy').format(dateTime); - } -} - -/// Global function for backward compatibility -String unixToDate(int? timestamp) { - return DateUtils.unixToDate(timestamp); + return formattedDate; } diff --git a/lib/views/detail/widgets/deadline_item.dart b/lib/views/detail/widgets/deadline_item.dart index 6f30a67..55eda72 100644 --- a/lib/views/detail/widgets/deadline_item.dart +++ b/lib/views/detail/widgets/deadline_item.dart @@ -53,7 +53,7 @@ class DeadlineItem extends StatelessWidget { ), SizedBox(height: 4.0.h()), Text( - unixToDate(detail.submissionDeadline), + timeConvertor(detail.submissionDeadline!), style: TextStyle( color: AppColors.grey80, fontWeight: FontWeight.w400, @@ -82,7 +82,7 @@ class DeadlineItem extends StatelessWidget { ), SizedBox(height: 4.0.h()), Text( - unixToDate(detail.applicationDeadline), + timeConvertor(detail.applicationDeadline!), style: TextStyle( color: AppColors.grey80, fontWeight: FontWeight.w400, @@ -128,7 +128,7 @@ class DeadlineItem extends StatelessWidget { ), ), Text( - unixToDate(detail.submissionDeadline), + timeConvertor(detail.submissionDeadline!), style: TextStyle( color: AppColors.grey70, fontWeight: FontWeight.w400, @@ -150,7 +150,7 @@ class DeadlineItem extends StatelessWidget { ), ), Text( - unixToDate(detail.applicationDeadline), + timeConvertor(detail.applicationDeadline!), style: TextStyle( color: AppColors.grey70, fontWeight: FontWeight.w400, diff --git a/lib/views/detail/widgets/tender_detail_header.dart b/lib/views/detail/widgets/tender_detail_header.dart index 4a9b24c..1fae266 100644 --- a/lib/views/detail/widgets/tender_detail_header.dart +++ b/lib/views/detail/widgets/tender_detail_header.dart @@ -49,7 +49,7 @@ class TenderDetailHeader extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( - unixToDate(detail.publicationDate), + timeConvertor(detail.publicationDate!), style: TextStyle( color: AppColors.grey60, fontWeight: FontWeight.w400, diff --git a/lib/views/home/tenders_list_item.dart b/lib/views/home/tenders_list_item.dart index 1cd774c..7e34c71 100644 --- a/lib/views/home/tenders_list_item.dart +++ b/lib/views/home/tenders_list_item.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:tm_app/core/utils/date_utils.dart'; import 'package:tm_app/data/services/model/tender_data/tender_data.dart'; import 'package:tm_app/views/home/strings/home_strings.dart'; import 'package:tm_app/views/home/widgets/tender_card_progress_bar.dart'; @@ -123,13 +124,4 @@ class TendersListItem extends StatelessWidget { ), ); } -} - -String timeConvertor(int time) { - DateTime date = DateTime.fromMillisecondsSinceEpoch(time); - - String formattedDate = - "${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}"; - - return formattedDate; -} +} \ No newline at end of file diff --git a/lib/views/tenders/widgets/tender_card.dart b/lib/views/tenders/widgets/tender_card.dart index 9dfc6a1..0bf5a55 100644 --- a/lib/views/tenders/widgets/tender_card.dart +++ b/lib/views/tenders/widgets/tender_card.dart @@ -113,7 +113,7 @@ class TenderCard extends StatelessWidget { Widget _dateText() { return Text( - DateUtils.unixToDate(tender.publicationDate ?? 0), + timeConvertor(tender.publicationDate ?? 0), style: TextStyle( color: AppColors.grey60, fontSize: 12.0.sp(), @@ -143,7 +143,7 @@ class TenderCard extends StatelessWidget { ), ), Text( - DateUtils.unixToDate(tender.tenderDeadline ?? 0), + timeConvertor(tender.tenderDeadline ?? 0), style: TextStyle( color: AppColors.grey80, fontSize: 14.0.sp(), diff --git a/lib/views/your_tenders/widgets/tender_card.dart b/lib/views/your_tenders/widgets/tender_card.dart index 361ce29..e3414ab 100644 --- a/lib/views/your_tenders/widgets/tender_card.dart +++ b/lib/views/your_tenders/widgets/tender_card.dart @@ -74,7 +74,7 @@ class TenderCard extends StatelessWidget { children: [ // Date Text( - unixToDate(int.parse(tender.publicationDate!.toString())), + timeConvertor(int.parse(tender.publicationDate!.toString())), style: TextStyle( color: AppColors.grey60, fontSize: 12.0.sp(),