Files
receipity/lib/services/item_repository.dart
T
2026-08-28 18:23:56 +02:00

51 lines
1.3 KiB
Dart

/// Chooses between mock data and the HTTP client, then returns [Item] lists.
library;
import '../models/item.dart';
import 'fake_item_data.dart';
import 'item_api.dart';
/// Single entry point for catalog data. UI code should depend on this type,
/// not on [ItemApi] or [FakeItemData] directly.
class ItemRepository {
ItemRepository({
ItemApi? api,
this.useRemoteData = false,
}) : _api = api ?? ItemApi();
final ItemApi _api;
/// When true, [fetchItems] hits the network; otherwise it returns local fakes.
bool useRemoteData;
Future<List<Item>> fetchItems() async {
if (!useRemoteData) {
return List<Item>.from(FakeItemData.items);
}
try {
return await _api.fetchItems();
} catch (_) {
// Keep the demo usable if the device is offline or the API fails.
return List<Item>.from(FakeItemData.items);
}
}
/// Builds a locally-created item (no network round-trip).
Item createLocal({
required String title,
required String subtitle,
required String description,
required ItemCategory category,
}) {
return Item(
id: 'local-${DateTime.now().microsecondsSinceEpoch}',
title: title.trim(),
subtitle: subtitle.trim(),
description: description.trim(),
category: category,
createdAt: DateTime.now(),
);
}
}