Renames load future, adds awaits in prefs.

pull/38/head
Andrew Brogdon 8 years ago
parent 501ff27ced
commit f1d89c5b2d

@ -16,51 +16,51 @@ class Preferences extends Model {
static const _preferredCategoriesKey = 'preferredCategories'; static const _preferredCategoriesKey = 'preferredCategories';
// Indicates whether a call to [_loadFromSharedPrefs] is in progress; // Indicates whether a call to [_loadFromSharedPrefs] is in progress;
Future<void> _loadResult; Future<void> _loading;
int _desiredCalories = 2000; int _desiredCalories = 2000;
Set<VeggieCategory> _preferredCategories = Set<VeggieCategory>(); Set<VeggieCategory> _preferredCategories = Set<VeggieCategory>();
Future<int> get desiredCalories async { Future<int> get desiredCalories async {
await _loadResult; await _loading;
return _desiredCalories; return _desiredCalories;
} }
Future<Set<VeggieCategory>> get preferredCategories async { Future<Set<VeggieCategory>> get preferredCategories async {
await _loadResult; await _loading;
return Set.from(_preferredCategories); return Set.from(_preferredCategories);
} }
void addPreferredCategory(VeggieCategory category) { void addPreferredCategory(VeggieCategory category) async {
_preferredCategories.add(category); _preferredCategories.add(category);
_saveToSharedPrefs(); await _saveToSharedPrefs();
notifyListeners(); notifyListeners();
} }
void removePreferredCategory(VeggieCategory category) { void removePreferredCategory(VeggieCategory category) async {
_preferredCategories.remove(category); _preferredCategories.remove(category);
_saveToSharedPrefs(); await _saveToSharedPrefs();
notifyListeners(); notifyListeners();
} }
void setDesiredCalories(int calories) { void setDesiredCalories(int calories) async {
_desiredCalories = calories; _desiredCalories = calories;
_saveToSharedPrefs(); await _saveToSharedPrefs();
notifyListeners(); notifyListeners();
} }
void load() { void load() {
_loadResult = _loadFromSharedPrefs(); _loading = _loadFromSharedPrefs();
} }
Future<void> _saveToSharedPrefs() async { Future<void> _saveToSharedPrefs() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
prefs.setInt(_caloriesKey, _desiredCalories); await prefs.setInt(_caloriesKey, _desiredCalories);
// Store preferred categories as a comma-separated string containing their // Store preferred categories as a comma-separated string containing their
// indices. // indices.
prefs.setString(_preferredCategoriesKey, await prefs.setString(_preferredCategoriesKey,
_preferredCategories.map((c) => c.index.toString()).join(',')); _preferredCategories.map((c) => c.index.toString()).join(','));
} }

@ -7,8 +7,6 @@ import 'package:flutter/widgets.dart';
import 'package:veggieseasons/data/veggie.dart'; import 'package:veggieseasons/data/veggie.dart';
abstract class Styles { abstract class Styles {
static String createHeroTag(Veggie veggie) => 'veggie_hero_${veggie.name}';
static const headlineText = TextStyle( static const headlineText = TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.8), color: Color.fromRGBO(0, 0, 0, 0.8),
fontFamily: 'NotoSans', fontFamily: 'NotoSans',

@ -20,7 +20,7 @@ class VeggieCard extends StatelessWidget {
final widgets = <Widget>[]; final widgets = <Widget>[];
widgets.add(Hero( widgets.add(Hero(
tag: Styles.createHeroTag(veggie), tag: veggie.id,
child: Image.asset( child: Image.asset(
veggie.imageAssetPath, veggie.imageAssetPath,
fit: BoxFit.cover, fit: BoxFit.cover,

Loading…
Cancel
Save