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

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

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

Loading…
Cancel
Save