-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes 154
- Loading branch information
Showing
7 changed files
with
230 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,47 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:json_cache/json_cache.dart'; | ||
import 'package:localstorage/localstorage.dart'; | ||
|
||
/// {@template json_cache_local_storage} | ||
/// | ||
/// Implementation on top of the LocalStorage package. | ||
/// | ||
/// Before using it, don't forget to call: | ||
/// | ||
/// ```dart | ||
/// WidgetsFlutterBinding.ensureInitialized(); | ||
/// await initLocalStorage(); | ||
/// ``` | ||
/// | ||
/// See: [local storage](https://pub.dev/packages/localstorage) | ||
class JsonCacheLocalStorage implements JsonCache { | ||
/// Encapsulates a [LocalStorage] instance. | ||
const JsonCacheLocalStorage(this._storage); | ||
/// | ||
/// {@endtemplate} | ||
final class JsonCacheLocalStorage implements JsonCache { | ||
/// {@macro json_cache_local_storage} | ||
JsonCacheLocalStorage([LocalStorage? customLocalStorage]) | ||
: _storage = customLocalStorage ?? localStorage; | ||
|
||
final LocalStorage _storage; | ||
|
||
@override | ||
Future<void> clear() async { | ||
await _getReady; | ||
await _storage.clear(); | ||
} | ||
Future<void> clear() async => _storage.clear(); | ||
|
||
@override | ||
Future<void> refresh(String key, Map<String, dynamic> value) async { | ||
await _getReady; | ||
await _storage.setItem(key, value); | ||
} | ||
Future<void> refresh(String key, Map<String, dynamic> value) async => | ||
_storage.setItem(key, json.encode(value)); | ||
|
||
@override | ||
Future<void> remove(String key) async { | ||
await _getReady; | ||
await _storage.deleteItem(key); | ||
} | ||
Future<void> remove(String key) async => _storage.removeItem(key); | ||
|
||
@override | ||
Future<Map<String, dynamic>?> value(String key) async { | ||
await _getReady; | ||
return await _storage.getItem(key) as Map<String, dynamic>?; | ||
final strJson = _storage.getItem(key); | ||
return strJson == null | ||
? null | ||
: json.decode(strJson) as Map<String, dynamic>; | ||
} | ||
|
||
@override | ||
Future<bool> contains(String key) async { | ||
await _getReady; | ||
final Object? item = _storage.getItem(key); | ||
return item != null; | ||
} | ||
|
||
Future<bool> get _getReady => _storage.ready; | ||
Future<bool> contains(String key) async => _storage.getItem(key) != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'package:localstorage/localstorage.dart'; | ||
|
||
typedef IndexValue = ({int index, String value}); | ||
|
||
/// {@template fake_local_storage} | ||
/// | ||
/// Unit-testing purposes implementation of the [LocalStorage] interface. | ||
/// | ||
/// {@endtemplate} | ||
final class FakeLocalStorage implements LocalStorage { | ||
/// {@macro fake_local_storage} | ||
FakeLocalStorage(); | ||
|
||
final Map<String, IndexValue> _data = {}; | ||
final List<String> _keys = []; | ||
|
||
@override | ||
void clear() { | ||
_data.clear(); | ||
_keys.clear(); | ||
} | ||
|
||
@override | ||
String? getItem(String key) => _data[key]?.value; | ||
|
||
@override | ||
String? key(int index) { | ||
if (index >= 0 && index < _keys.length) { | ||
return _keys[index]; | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
int get length => _data.keys.length; | ||
|
||
@override | ||
void removeItem(String key) { | ||
final pair = _data[key]; | ||
if (pair != null) { | ||
_removeItemAndDecreaseIndexes(key, pair); | ||
} | ||
} | ||
|
||
@override | ||
void setItem(String key, String value) { | ||
final pair = _data[key]; | ||
if (pair != null) { | ||
final currIndex = pair.index; | ||
_data[key] = (index: currIndex, value: value); | ||
} else { | ||
_keys.add(key); | ||
final newIndex = _keys.length - 1; | ||
_data[key] = (index: newIndex, value: value); | ||
} | ||
} | ||
|
||
void _removeItemAndDecreaseIndexes( | ||
String key, | ||
({int index, String value}) pair, | ||
) { | ||
_keys.removeAt(pair.index); | ||
_data.remove(key); | ||
_data.keys.forEach(_decreaseIndexAt); | ||
} | ||
|
||
void _decreaseIndexAt(String key) { | ||
final pair = _data[key]!; | ||
_data[key] = (index: pair.index - 1, value: pair.value); | ||
} | ||
} |
Oops, something went wrong.
003accb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to retrieve PDD puzzles from the code base and submit them to github. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: