| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'dart:async'; |
| import 'package:shared_preferences/shared_preferences.dart'; |
| |
| // This is just a development prototype for locally storing consumables. Do not |
| // use this. |
| class ConsumableStore { |
| static const String _kPrefKey = 'consumables'; |
| static Future<void> _writes = Future.value(); |
| |
| static Future<void> save(String id) { |
| _writes = _writes.then((void _) => _doSave(id)); |
| return _writes; |
| } |
| |
| static Future<void> consume(String id) { |
| _writes = _writes.then((void _) => _doConsume(id)); |
| return _writes; |
| } |
| |
| static Future<List<String>> load() async { |
| return (await SharedPreferences.getInstance()).getStringList(_kPrefKey) ?? |
| []; |
| } |
| |
| static Future<void> _doSave(String id) async { |
| List<String> cached = await load(); |
| SharedPreferences prefs = await SharedPreferences.getInstance(); |
| cached.add(id); |
| await prefs.setStringList(_kPrefKey, cached); |
| } |
| |
| static Future<void> _doConsume(String id) async { |
| List<String> cached = await load(); |
| SharedPreferences prefs = await SharedPreferences.getInstance(); |
| cached.remove(id); |
| await prefs.setStringList(_kPrefKey, cached); |
| } |
| } |