Add liked tenders functionality to HomeRepository and ViewModel
- Integrated LikedTendersService into HomeRepository to fetch the count of user-liked tenders. - Updated HomeViewModel to load and manage the userLikedTendersCount. - Adjusted UI components across home pages to display the liked tenders count. - Refactored date handling in date_utils.dart to improve timestamp parsing and validation. - Enhanced unit tests for date utilities to cover new edge cases.
This commit is contained in:
@@ -121,6 +121,7 @@ List<SingleChildWidget> get repositories {
|
||||
(context) => HomeRepository(
|
||||
homeService: context.read(),
|
||||
yourTendersService: context.read(),
|
||||
likedTendersService: context.read(),
|
||||
),
|
||||
lazy: true,
|
||||
),
|
||||
|
||||
@@ -1,24 +1,33 @@
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
/// Parses API date fields that may arrive as int, double, or string.
|
||||
/// Parses an API timestamp field that may arrive as int, double, or string.
|
||||
/// Returns null for missing, empty, unparseable, or non-positive values
|
||||
/// (a non-positive epoch is treated as "no value" by the API).
|
||||
int? unixTimestampFromJson(dynamic value) {
|
||||
if (value == null) return null;
|
||||
if (value is int) return value;
|
||||
if (value is num) return value.toInt();
|
||||
if (value is String) {
|
||||
int? parsed;
|
||||
if (value is int) {
|
||||
parsed = value;
|
||||
} else if (value is num) {
|
||||
parsed = value.toInt();
|
||||
} else if (value is String) {
|
||||
final trimmed = value.trim();
|
||||
if (trimmed.isEmpty) return null;
|
||||
return int.tryParse(trimmed);
|
||||
parsed = int.tryParse(trimmed);
|
||||
}
|
||||
return null;
|
||||
if (parsed == null || parsed <= 0) return null;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/// Converts a Unix timestamp (seconds) from the API to a display string.
|
||||
/// Mirrors Next.js `unixToDate` using `moment.unix(unix)`.
|
||||
/// Converts a Unix timestamp from the API to a display string.
|
||||
///
|
||||
/// The API contract is seconds since epoch, but some legacy fields still
|
||||
/// emit milliseconds. Anything past the year 2286 in seconds (>1e10) is
|
||||
/// assumed to be milliseconds and converted down. Remove this branch once
|
||||
/// every endpoint has been migrated to seconds.
|
||||
String timeConvertor(int? unix, {bool hasTime = false}) {
|
||||
if (unix == null || unix == 0) return '-';
|
||||
if (unix == null || unix <= 0) return '-';
|
||||
|
||||
// Values above ~year 2286 in seconds are treated as milliseconds.
|
||||
final seconds = unix > 9999999999 ? unix ~/ 1000 : unix;
|
||||
final date = DateTime.fromMillisecondsSinceEpoch(
|
||||
seconds * 1000,
|
||||
|
||||
Reference in New Issue
Block a user