From 792d88961a87ac35abf33731b2c70de7026f2cb8 Mon Sep 17 00:00:00 2001 From: Yaros Date: Thu, 23 Jul 2026 17:18:15 +0200 Subject: [PATCH] feat(mobile): custom date range for map (#26205) * feat(mobile): custom date range for map * refactor: rename timerange & remove isvalid * refactor: rename customtimerange variables * refactor: add back setRelativeTime * refactor: implement suggestions * refactor: suggestions * fix: ifPresent * fix: context.locale * chore: restrict selection * refactor: move options to mapconfig * refactor: move model to domain * chore: locale toLanguageTag * add map codec tests * rebase --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> --- i18n/en.json | 1 + .../lib/domain/models/config/app_config.dart | 4 + .../lib/domain/models/config/map_config.dart | 20 +++- mobile/lib/domain/models/settings_key.dart | 2 + .../lib/domain/models/time_range.model.dart | 15 +++ .../repositories/map.repository.dart | 13 ++- .../repositories/timeline.repository.dart | 33 ++++++- .../presentation/widgets/map/map.state.dart | 26 +++++ .../widgets/map/map_settings_sheet.dart | 63 ++++++++++-- mobile/lib/utils/option.dart | 11 +++ .../map_settings/map_custom_time_range.dart | 73 ++++++++++++++ .../widgets/timeline/timeline_args_test.dart | 4 +- mobile/test/unit/utils/value_codec_test.dart | 99 +++++++++++++++++++ 13 files changed, 349 insertions(+), 15 deletions(-) create mode 100644 mobile/lib/domain/models/time_range.model.dart create mode 100644 mobile/lib/widgets/map/map_settings/map_custom_time_range.dart create mode 100644 mobile/test/unit/utils/value_codec_test.dart diff --git a/i18n/en.json b/i18n/en.json index 53d1e4a6b2..1dbd50e294 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1524,6 +1524,7 @@ "not_available": "N/A", "not_in_any_album": "Not in any album", "not_selected": "Not selected", + "not_set": "Not set", "notes": "Notes", "nothing_here_yet": "Nothing here yet", "notification_backup_reliability": "Enable notifications to improve background backup reliability", diff --git a/mobile/lib/domain/models/config/app_config.dart b/mobile/lib/domain/models/config/app_config.dart index 6a66d7ed23..e4e11baf9d 100644 --- a/mobile/lib/domain/models/config/app_config.dart +++ b/mobile/lib/domain/models/config/app_config.dart @@ -153,6 +153,8 @@ class AppConfig { .timelineStorageIndicator => timeline.storageIndicator, .mapShowFavoriteOnly => map.favoritesOnly, .mapRelativeDate => map.relativeDays, + .mapCustomFrom => map.customFrom, + .mapCustomTo => map.customTo, .mapIncludeArchived => map.includeArchived, .mapThemeMode => map.themeMode, .mapWithPartners => map.withPartners, @@ -207,6 +209,8 @@ class AppConfig { .timelineStorageIndicator => copyWith(timeline: timeline.copyWith(storageIndicator: value as bool)), .mapShowFavoriteOnly => copyWith(map: map.copyWith(favoritesOnly: value as bool)), .mapRelativeDate => copyWith(map: map.copyWith(relativeDays: value as int)), + .mapCustomFrom => copyWith(map: map.copyWith(customFrom: .fromNullable(value as DateTime?))), + .mapCustomTo => copyWith(map: map.copyWith(customTo: .fromNullable(value as DateTime?))), .mapIncludeArchived => copyWith(map: map.copyWith(includeArchived: value as bool)), .mapThemeMode => copyWith(map: map.copyWith(themeMode: value as ThemeMode)), .mapWithPartners => copyWith(map: map.copyWith(withPartners: value as bool)), diff --git a/mobile/lib/domain/models/config/map_config.dart b/mobile/lib/domain/models/config/map_config.dart index e37ab0f431..85cac80081 100644 --- a/mobile/lib/domain/models/config/map_config.dart +++ b/mobile/lib/domain/models/config/map_config.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:immich_mobile/utils/option.dart'; class MapConfig { final int relativeDays; @@ -6,13 +7,17 @@ class MapConfig { final bool includeArchived; final ThemeMode themeMode; final bool withPartners; + final DateTime? customFrom; + final DateTime? customTo; const MapConfig({ this.relativeDays = 0, this.favoritesOnly = false, this.includeArchived = false, - this.themeMode = ThemeMode.system, + this.themeMode = .system, this.withPartners = false, + this.customFrom, + this.customTo, }); MapConfig copyWith({ @@ -21,12 +26,16 @@ class MapConfig { bool? includeArchived, ThemeMode? themeMode, bool? withPartners, + Option? customFrom, + Option? customTo, }) => MapConfig( relativeDays: relativeDays ?? this.relativeDays, favoritesOnly: favoritesOnly ?? this.favoritesOnly, includeArchived: includeArchived ?? this.includeArchived, themeMode: themeMode ?? this.themeMode, withPartners: withPartners ?? this.withPartners, + customFrom: customFrom.patch(this.customFrom), + customTo: customTo.patch(this.customTo), ); @override @@ -37,12 +46,15 @@ class MapConfig { other.favoritesOnly == favoritesOnly && other.includeArchived == includeArchived && other.themeMode == themeMode && - other.withPartners == withPartners); + other.withPartners == withPartners && + other.customFrom == customFrom && + other.customTo == customTo); @override - int get hashCode => Object.hash(relativeDays, favoritesOnly, includeArchived, themeMode, withPartners); + int get hashCode => + Object.hash(relativeDays, favoritesOnly, includeArchived, themeMode, withPartners, customFrom, customTo); @override String toString() => - 'MapConfig(relativeDays: $relativeDays, favoritesOnly: $favoritesOnly, includeArchived: $includeArchived, themeMode: $themeMode, withPartners: $withPartners)'; + 'MapConfig(relativeDays: $relativeDays, favoritesOnly: $favoritesOnly, includeArchived: $includeArchived, themeMode: $themeMode, withPartners: $withPartners, customFrom: $customFrom, customTo: $customTo)'; } diff --git a/mobile/lib/domain/models/settings_key.dart b/mobile/lib/domain/models/settings_key.dart index 4308d69555..85e58ffcf1 100644 --- a/mobile/lib/domain/models/settings_key.dart +++ b/mobile/lib/domain/models/settings_key.dart @@ -55,6 +55,8 @@ enum SettingsKey { // Map mapShowFavoriteOnly(), mapRelativeDate(), + mapCustomFrom(), + mapCustomTo(), mapIncludeArchived(), mapThemeMode(codec: EnumCodec(ThemeMode.values)), mapWithPartners(), diff --git a/mobile/lib/domain/models/time_range.model.dart b/mobile/lib/domain/models/time_range.model.dart new file mode 100644 index 0000000000..2727a9d5c8 --- /dev/null +++ b/mobile/lib/domain/models/time_range.model.dart @@ -0,0 +1,15 @@ +import 'package:immich_mobile/utils/option.dart'; + +class TimeRange { + final DateTime? from; + final DateTime? to; + + const TimeRange({this.from, this.to}); + + TimeRange copyWith({Option? from, Option? to}) { + return TimeRange(from: from.patch(this.from), to: to.patch(this.to)); + } + + TimeRange clearFrom() => TimeRange(to: to); + TimeRange clearTo() => TimeRange(from: from); +} diff --git a/mobile/lib/infrastructure/repositories/map.repository.dart b/mobile/lib/infrastructure/repositories/map.repository.dart index 95e42337fc..267ea08d20 100644 --- a/mobile/lib/infrastructure/repositories/map.repository.dart +++ b/mobile/lib/infrastructure/repositories/map.repository.dart @@ -27,7 +27,18 @@ class DriftMapRepository extends DriftDatabaseRepository { condition = condition & _db.remoteAssetEntity.isFavorite.equals(true); } - if (options.relativeDays != 0) { + final timeRange = options.timeRange; + final hasCustomRange = timeRange.from != null || timeRange.to != null; + + if (hasCustomRange) { + if (timeRange.from != null) { + condition = condition & _db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(timeRange.from!); + } + + if (timeRange.to != null) { + condition = condition & _db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(timeRange.to!); + } + } else if (options.relativeDays > 0) { final cutoffDate = DateTime.now().toUtc().subtract(Duration(days: options.relativeDays)); condition = condition & _db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(cutoffDate); } diff --git a/mobile/lib/infrastructure/repositories/timeline.repository.dart b/mobile/lib/infrastructure/repositories/timeline.repository.dart index 97ad85aa13..6a9fbdb204 100644 --- a/mobile/lib/infrastructure/repositories/timeline.repository.dart +++ b/mobile/lib/infrastructure/repositories/timeline.repository.dart @@ -4,6 +4,7 @@ import 'package:drift/drift.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:immich_mobile/domain/models/album/album.model.dart'; import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; +import 'package:immich_mobile/domain/models/time_range.model.dart'; import 'package:immich_mobile/domain/models/timeline.model.dart'; import 'package:immich_mobile/domain/services/timeline.service.dart'; import 'package:immich_mobile/infrastructure/entities/local_asset.entity.dart'; @@ -20,6 +21,7 @@ class TimelineMapOptions { final bool includeArchived; final bool withPartners; final int relativeDays; + final TimeRange timeRange; const TimelineMapOptions({ required this.bounds, @@ -27,6 +29,7 @@ class TimelineMapOptions { this.includeArchived = false, this.withPartners = false, this.relativeDays = 0, + this.timeRange = const TimeRange(), }); } @@ -552,8 +555,21 @@ class DriftTimelineRepository extends DriftDatabaseRepository { query.where(_db.remoteAssetEntity.isFavorite.equals(true)); } - if (options.relativeDays != 0) { + final timeRange = options.timeRange; + + final hasCustomRange = timeRange.from != null || timeRange.to != null; + + if (hasCustomRange) { + if (timeRange.from != null) { + query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(timeRange.from!)); + } + + if (timeRange.to != null) { + query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(timeRange.to!)); + } + } else if (options.relativeDays > 0) { final cutoffDate = DateTime.now().toUtc().subtract(Duration(days: options.relativeDays)); + query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(cutoffDate)); } @@ -594,8 +610,21 @@ class DriftTimelineRepository extends DriftDatabaseRepository { query.where(_db.remoteAssetEntity.isFavorite.equals(true)); } - if (options.relativeDays != 0) { + final timeRange = options.timeRange; + + final hasCustomRange = timeRange.from != null || timeRange.to != null; + + if (hasCustomRange) { + if (timeRange.from != null) { + query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(timeRange.from!)); + } + + if (timeRange.to != null) { + query.where(_db.remoteAssetEntity.createdAt.isSmallerOrEqualValue(timeRange.to!)); + } + } else if (options.relativeDays > 0) { final cutoffDate = DateTime.now().toUtc().subtract(Duration(days: options.relativeDays)); + query.where(_db.remoteAssetEntity.createdAt.isBiggerOrEqualValue(cutoffDate)); } diff --git a/mobile/lib/presentation/widgets/map/map.state.dart b/mobile/lib/presentation/widgets/map/map.state.dart index 57ce619771..f1b4f80ec1 100644 --- a/mobile/lib/presentation/widgets/map/map.state.dart +++ b/mobile/lib/presentation/widgets/map/map.state.dart @@ -1,11 +1,13 @@ import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/domain/models/events.model.dart'; +import 'package:immich_mobile/domain/models/time_range.model.dart'; import 'package:immich_mobile/domain/utils/event_stream.dart'; import 'package:immich_mobile/infrastructure/repositories/timeline.repository.dart'; import 'package:immich_mobile/providers/infrastructure/map.provider.dart'; import 'package:immich_mobile/providers/infrastructure/settings.provider.dart'; import 'package:immich_mobile/providers/map/map_state.provider.dart'; +import 'package:immich_mobile/utils/option.dart'; import 'package:maplibre_gl/maplibre_gl.dart'; class MapState { @@ -15,6 +17,7 @@ class MapState { final bool includeArchived; final bool withPartners; final int relativeDays; + final TimeRange timeRange; const MapState({ this.themeMode = ThemeMode.system, @@ -23,6 +26,7 @@ class MapState { this.includeArchived = false, this.withPartners = false, this.relativeDays = 0, + this.timeRange = const TimeRange(), }); @override @@ -40,6 +44,7 @@ class MapState { bool? includeArchived, bool? withPartners, int? relativeDays, + TimeRange? timeRange, }) { return MapState( bounds: bounds ?? this.bounds, @@ -48,6 +53,7 @@ class MapState { includeArchived: includeArchived ?? this.includeArchived, withPartners: withPartners ?? this.withPartners, relativeDays: relativeDays ?? this.relativeDays, + timeRange: timeRange ?? this.timeRange, ); } @@ -57,6 +63,7 @@ class MapState { includeArchived: includeArchived, withPartners: withPartners, relativeDays: relativeDays, + timeRange: timeRange, ); } @@ -103,6 +110,24 @@ class MapStateNotifier extends Notifier { EventStream.shared.emit(const MapMarkerReloadEvent()); } + void setCustomTimeRange(TimeRange range) { + ref.read(settingsProvider).write(.mapCustomFrom, range.from); + ref.read(settingsProvider).write(.mapCustomTo, range.to); + state = state.copyWith(timeRange: range); + EventStream.shared.emit(const MapMarkerReloadEvent()); + } + + Option parseDateOption(String s) { + try { + if (s.trim().isEmpty) { + return const Option.none(); + } + return Option.some(DateTime.parse(s)); + } catch (_) { + return const Option.none(); + } + } + @override MapState build() { final mapConfig = ref.read(appConfigProvider.select((config) => config.map)); @@ -113,6 +138,7 @@ class MapStateNotifier extends Notifier { withPartners: mapConfig.withPartners, relativeDays: mapConfig.relativeDays, bounds: LatLngBounds(northeast: const LatLng(0, 0), southwest: const LatLng(0, 0)), + timeRange: TimeRange(from: mapConfig.customFrom, to: mapConfig.customTo), ); } } diff --git a/mobile/lib/presentation/widgets/map/map_settings_sheet.dart b/mobile/lib/presentation/widgets/map/map_settings_sheet.dart index c581dd6292..2680850bfe 100644 --- a/mobile/lib/presentation/widgets/map/map_settings_sheet.dart +++ b/mobile/lib/presentation/widgets/map/map_settings_sheet.dart @@ -1,21 +1,39 @@ import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:immich_mobile/domain/models/time_range.model.dart'; import 'package:immich_mobile/extensions/translate_extensions.dart'; +import 'package:immich_mobile/generated/translations.g.dart'; import 'package:immich_mobile/presentation/widgets/map/map.state.dart'; +import 'package:immich_mobile/widgets/map/map_settings/map_custom_time_range.dart'; import 'package:immich_mobile/widgets/map/map_settings/map_settings_list_tile.dart'; import 'package:immich_mobile/widgets/map/map_settings/map_settings_time_dropdown.dart'; import 'package:immich_mobile/widgets/map/map_settings/map_theme_picker.dart'; -class DriftMapSettingsSheet extends HookConsumerWidget { +class DriftMapSettingsSheet extends ConsumerStatefulWidget { const DriftMapSettingsSheet({super.key}); @override - Widget build(BuildContext context, WidgetRef ref) { + ConsumerState createState() => _DriftMapSettingsSheetState(); +} + +class _DriftMapSettingsSheetState extends ConsumerState { + late bool useCustomRange; + + @override + void initState() { + super.initState(); + final mapState = ref.read(mapStateProvider); + final timeRange = mapState.timeRange; + useCustomRange = timeRange.from != null || timeRange.to != null; + } + + @override + Widget build(BuildContext context) { final mapState = ref.watch(mapStateProvider); return DraggableScrollableSheet( expand: false, - initialChildSize: 0.6, + initialChildSize: useCustomRange ? 0.7 : 0.6, builder: (ctx, scrollController) => SingleChildScrollView( controller: scrollController, child: Card( @@ -47,10 +65,41 @@ class DriftMapSettingsSheet extends HookConsumerWidget { selected: mapState.withPartners, onChanged: (withPartners) => ref.read(mapStateProvider.notifier).switchWithPartners(withPartners), ), - MapTimeDropDown( - relativeTime: mapState.relativeDays, - onTimeChange: (time) => ref.read(mapStateProvider.notifier).setRelativeTime(time), - ), + if (useCustomRange) ...[ + MapTimeRange( + timeRange: mapState.timeRange, + onChanged: (range) { + ref.read(mapStateProvider.notifier).setCustomTimeRange(range); + }, + ), + Align( + alignment: Alignment.centerLeft, + child: TextButton( + onPressed: () => setState(() { + useCustomRange = false; + ref.read(mapStateProvider.notifier).setRelativeTime(0); + ref.read(mapStateProvider.notifier).setCustomTimeRange(const TimeRange()); + }), + child: Text(context.t.remove_custom_date_range), + ), + ), + ] else ...[ + MapTimeDropDown( + relativeTime: mapState.relativeDays, + onTimeChange: (time) => ref.read(mapStateProvider.notifier).setRelativeTime(time), + ), + Align( + alignment: Alignment.centerLeft, + child: TextButton( + onPressed: () => setState(() { + useCustomRange = true; + ref.read(mapStateProvider.notifier).setRelativeTime(0); + ref.read(mapStateProvider.notifier).setCustomTimeRange(const TimeRange()); + }), + child: Text(context.t.use_custom_date_range), + ), + ), + ], const SizedBox(height: 20), ], ), diff --git a/mobile/lib/utils/option.dart b/mobile/lib/utils/option.dart index e88ae59c1f..93e939a6c0 100644 --- a/mobile/lib/utils/option.dart +++ b/mobile/lib/utils/option.dart @@ -32,6 +32,17 @@ sealed class Option { None() => onNone(), }; + Option flatMap(Option Function(T value) f) => switch (this) { + Some(:final value) => f(value), + None() => const Option.none(), + }; + + void ifPresent(void Function(T value) f) { + if (this case Some(:final value)) { + f(value); + } + } + @override String toString() => switch (this) { Some(:final value) => 'Some($value)', diff --git a/mobile/lib/widgets/map/map_settings/map_custom_time_range.dart b/mobile/lib/widgets/map/map_settings/map_custom_time_range.dart new file mode 100644 index 0000000000..6fa929852a --- /dev/null +++ b/mobile/lib/widgets/map/map_settings/map_custom_time_range.dart @@ -0,0 +1,73 @@ +import 'package:easy_localization/easy_localization.dart'; +import 'package:flutter/material.dart'; +import 'package:immich_mobile/domain/models/time_range.model.dart'; +import 'package:immich_mobile/generated/translations.g.dart'; +import 'package:immich_mobile/utils/option.dart'; + +class MapTimeRange extends StatelessWidget { + const MapTimeRange({super.key, required this.timeRange, required this.onChanged}); + + final TimeRange timeRange; + final Function(TimeRange) onChanged; + + @override + Widget build(BuildContext context) { + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + title: Text(context.t.date_after), + subtitle: Text( + timeRange.from != null + ? DateFormat.yMMMd(context.locale.toLanguageTag()).add_jm().format(timeRange.from!) + : context.t.not_set, + ), + trailing: timeRange.from != null + ? IconButton(icon: const Icon(Icons.close), onPressed: () => onChanged(timeRange.clearFrom())) + : null, + onTap: () async { + final initial = timeRange.from ?? DateTime.now(); + final currentTo = timeRange.to; + + final picked = await showDatePicker( + context: context, + initialDate: currentTo != null && initial.isAfter(currentTo) ? currentTo : initial, + firstDate: DateTime(1970), + lastDate: currentTo ?? DateTime.now(), + ); + + if (picked != null) { + onChanged(timeRange.copyWith(from: Option.some(picked))); + } + }, + ), + ListTile( + title: Text(context.t.date_before), + subtitle: Text( + timeRange.to != null + ? DateFormat.yMMMd(context.locale.toLanguageTag()).add_jm().format(timeRange.to!) + : context.t.not_set, + ), + trailing: timeRange.to != null + ? IconButton(icon: const Icon(Icons.close), onPressed: () => onChanged(timeRange.clearTo())) + : null, + onTap: () async { + final initial = timeRange.to ?? DateTime.now(); + final currentFrom = timeRange.from; + + final picked = await showDatePicker( + context: context, + initialDate: currentFrom != null && initial.isBefore(currentFrom) ? currentFrom : initial, + firstDate: currentFrom ?? DateTime(1970), + lastDate: DateTime.now(), + ); + + if (picked != null) { + onChanged(timeRange.copyWith(to: Option.some(picked))); + } + }, + ), + ], + ); + } +} diff --git a/mobile/test/presentation/widgets/timeline/timeline_args_test.dart b/mobile/test/presentation/widgets/timeline/timeline_args_test.dart index 4eb49bd3d6..0828e8e989 100644 --- a/mobile/test/presentation/widgets/timeline/timeline_args_test.dart +++ b/mobile/test/presentation/widgets/timeline/timeline_args_test.dart @@ -25,6 +25,8 @@ class _FrozenBucketService implements TimelineService { } class _EmptyBucketService implements TimelineService { + const _EmptyBucketService(); + @override Stream> Function() get watchBuckets => () => Stream.value(const []); @@ -109,7 +111,7 @@ void main() { await tester.pumpWidget( ProviderScope( overrides: [ - timelineServiceProvider.overrideWithValue(_EmptyBucketService()), + timelineServiceProvider.overrideWithValue(const _EmptyBucketService()), appConfigProvider.overrideWithValue(const AppConfig()), ], child: MaterialApp( diff --git a/mobile/test/unit/utils/value_codec_test.dart b/mobile/test/unit/utils/value_codec_test.dart new file mode 100644 index 0000000000..8754af1e95 --- /dev/null +++ b/mobile/test/unit/utils/value_codec_test.dart @@ -0,0 +1,99 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:immich_mobile/domain/models/value_codec.dart'; + +enum _Fruit { apple, banana, cherry } + +void main() { + group('MapCodec', () { + group('encode', () { + test('serializes an empty map to an empty JSON object', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.encode({}), '{}'); + }); + + test('encodes a string-to-string map as a JSON object', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.encode({'a': '1', 'b': '2'}), '{"a":"1","b":"2"}'); + }); + + test('stringifies non-string values via the value codec', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.integer); + expect(codec.encode({'x': 10, 'y': 20}), '{"x":"10","y":"20"}'); + }); + + test('stringifies non-string keys via the key codec', () { + const codec = MapCodec(PrimitiveCodec.integer, PrimitiveCodec.string); + expect(codec.encode({1: 'one', 2: 'two'}), '{"1":"one","2":"two"}'); + }); + }); + + group('decode', () { + test('reconstructs a string-to-string map', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.decode('{"a":"1","b":"2"}'), {'a': '1', 'b': '2'}); + }); + + test('parses values back to their domain type', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.integer); + expect(codec.decode('{"x":"10","y":"20"}'), {'x': 10, 'y': 20}); + }); + + test('parses keys back to their domain type', () { + const codec = MapCodec(PrimitiveCodec.integer, PrimitiveCodec.string); + expect(codec.decode('{"1":"one","2":"two"}'), {1: 'one', 2: 'two'}); + }); + + test('returns an empty map for an empty JSON object', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.decode('{}'), isEmpty); + }); + + test('returns an empty map when the payload is not valid JSON', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.decode('not json'), isEmpty); + }); + + test('returns an empty map when the JSON root is not an object', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.decode('[]'), isEmpty); + expect(codec.decode('"a string"'), isEmpty); + expect(codec.decode('42'), isEmpty); + }); + + test('skips entries whose value is not a JSON string, keeping the rest', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.integer); + expect(codec.decode('{"x":1,"y":"20"}'), {'y': 20}); + }); + + test('skips entries whose value is a nested object, keeping the rest', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.string); + expect(codec.decode('{"a":{"nested":"value"},"b":"ok"}'), {'b': 'ok'}); + }); + + test('returns an empty map when every entry is malformed', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.integer); + expect(codec.decode('{"x":1,"y":2}'), isEmpty); + }); + }); + + group('round trip', () { + test('preserves a primitive map through encode then decode', () { + const codec = MapCodec(PrimitiveCodec.string, PrimitiveCodec.integer); + const original = {'one': 1, 'two': 2, 'three': 3}; + expect(codec.decode(codec.encode(original)), original); + }); + + test('preserves an enum-valued map by composing with EnumCodec', () { + const codec = MapCodec(PrimitiveCodec.string, EnumCodec(_Fruit.values)); + const original = {'breakfast': _Fruit.banana, 'snack': _Fruit.apple}; + expect(codec.decode(codec.encode(original)), original); + }); + + test('preserves a DateTime-valued map by composing with DateTimeCodec', () { + const codec = MapCodec(PrimitiveCodec.string, DateTimeCodec()); + final original = {'created': DateTime.utc(2024, 1, 1, 12, 30)}; + expect(codec.decode(codec.encode(original)), original); + }); + }); + }); +}