localSourceTemplate function

String localSourceTemplate(
  1. String feature,
  2. String pascal
)

Implementation

String localSourceTemplate(String feature, String pascal) => '''
import '../../../../core/storage/local_storage.dart';
import '../${feature}_repository_impl.dart';

class ${pascal}LocalSourceImpl implements ${pascal}LocalSource {
  final LocalStorage _storage;
  static const _keyPrefix = '${feature}_';
  static const _itemKey = '\\\${_keyPrefix}item_';
  static const _listKey = '\\\${_keyPrefix}list';

  ${pascal}LocalSourceImpl({required LocalStorage storage}) : _storage = storage;

  @override
  Future<Map<String, dynamic>?> getCached(String id) async => _storage.getJson('\\\$_itemKey\\\$id');

  @override
  Future<List<Map<String, dynamic>>> getCachedList() async => _storage.getJsonList(_listKey);

  @override
  Future<void> cache(Map<String, dynamic> data) async {
    final id = data['id'] as String;
    await _storage.putJson('\\\$_itemKey\\\$id', data);
  }

  @override
  Future<void> cacheList(List<Map<String, dynamic>> data) async => _storage.putJsonList(_listKey, data);

  @override
  Future<void> clearCache() async => _storage.removeByPrefix(_keyPrefix);
}
''';