Refactored loading of preferences, dart docs

pull/37/head
Andrew Brogdon 8 years ago
parent fae0965201
commit f6e4298278

@ -2,6 +2,8 @@
// 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:scoped_model/scoped_model.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -10,19 +12,29 @@ import 'package:shared_preferences/shared_preferences.dart';
/// in shared preferences.
class Preferences extends Model {
Preferences() {
_load();
_loadingFuture = _load();
}
static const caloriesKey = 'calories';
static const preferredCategoriesKey = 'preferredCategories';
// Keys to use with shared preferences.
static const _caloriesKey = 'calories';
static const _preferredCategoriesKey = 'preferredCategories';
int _desiredCalories = 2000;
// Indicates whether a call to [_load] is in progress;
Future<void> _loadingFuture;
int get desiredCalories => _desiredCalories;
int _desiredCalories = 2000;
Set<VeggieCategory> _preferredCategories = Set<VeggieCategory>();
Set<VeggieCategory> get preferredCategories => Set.from(_preferredCategories);
Future<int> get desiredCalories async {
await _loadingFuture;
return _desiredCalories;
}
Future<Set<VeggieCategory>> get preferredCategories async {
await _loadingFuture;
return Set.from(_preferredCategories);
}
void addPreferredCategory(VeggieCategory category) {
_preferredCategories.add(category);
@ -44,19 +56,19 @@ class Preferences extends Model {
Future<void> _save() async {
final prefs = await SharedPreferences.getInstance();
prefs.setInt(caloriesKey, _desiredCalories);
prefs.setInt(_caloriesKey, _desiredCalories);
// Store preferred categories as a comma-separated string containing their
// indices.
prefs.setString(preferredCategoriesKey,
prefs.setString(_preferredCategoriesKey,
_preferredCategories.map((c) => c.index.toString()).join(','));
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
_desiredCalories = prefs.getInt(caloriesKey) ?? 2000;
_desiredCalories = prefs.getInt(_caloriesKey) ?? 2000;
_preferredCategories.clear();
final names = prefs.getString(preferredCategoriesKey) ?? '';
final names = prefs.getString(_preferredCategoriesKey) ?? '';
for (final name in names.split(',')) {
final index = int.parse(name) ?? 0;

@ -9,14 +9,19 @@ import 'package:veggieseasons/data/model.dart';
import 'package:veggieseasons/data/veggie.dart';
import 'package:veggieseasons/styles.dart';
/// A circular widget that indicates in which seasons a particular veggie can be
/// harvested. It displays the first two letters of the season and uses a
/// different background color to represent each of the seasons as well.
/// A circular widget that represents a season of the year.
///
/// The season can be displayed as a valid harvest season or one during which a
/// particular veggie cannot be harvested. Bright colors are used in the first
/// case, and grays in the latter.
class SeasonCircle extends StatelessWidget {
final bool active;
SeasonCircle(this.season, this.isHarvestTime);
/// Season to be displayed by this widget.
final Season season;
SeasonCircle(this.season, this.active);
/// Whether or not [season] should be presented as a valid harvest season.
final bool isHarvestTime;
String get _firstChars {
return '${season.toString().substring(7, 8).toUpperCase()}'
@ -29,7 +34,9 @@ class SeasonCircle extends StatelessWidget {
padding: const EdgeInsets.all(4.0),
child: DecoratedBox(
decoration: BoxDecoration(
color: active ? Styles.seasonColors[season] : Styles.transparentColor,
color: isHarvestTime
? Styles.seasonColors[season]
: Styles.transparentColor,
borderRadius: BorderRadius.circular(25.0),
border: Styles.seasonBorder,
),
@ -39,8 +46,9 @@ class SeasonCircle extends StatelessWidget {
child: Center(
child: Text(
_firstChars,
style:
active ? Styles.activeSeasonText : Styles.inactiveSeasonText,
style: isHarvestTime
? Styles.activeSeasonText
: Styles.inactiveSeasonText,
),
),
),
@ -121,12 +129,9 @@ class DetailsScreen extends StatelessWidget {
mainAxisSize: MainAxisSize.max,
children: [
Wrap(
children: Season.values
.map((s) => SeasonCircle(
s,
veggie.seasons.contains(s),
))
.toList(),
children: Season.values.map((s) {
return SeasonCircle(s, veggie.seasons.contains(s));
}).toList(),
),
SizedBox(width: 8.0),
Expanded(

Loading…
Cancel
Save