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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async';
import 'package:scoped_model/scoped_model.dart'; import 'package:scoped_model/scoped_model.dart';
import 'package:veggieseasons/data/veggie.dart'; import 'package:veggieseasons/data/veggie.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -10,19 +12,29 @@ import 'package:shared_preferences/shared_preferences.dart';
/// in shared preferences. /// in shared preferences.
class Preferences extends Model { class Preferences extends Model {
Preferences() { Preferences() {
_load(); _loadingFuture = _load();
} }
static const caloriesKey = 'calories'; // Keys to use with shared preferences.
static const preferredCategoriesKey = 'preferredCategories'; 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> _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) { void addPreferredCategory(VeggieCategory category) {
_preferredCategories.add(category); _preferredCategories.add(category);
@ -44,19 +56,19 @@ class Preferences extends Model {
Future<void> _save() async { Future<void> _save() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
prefs.setInt(caloriesKey, _desiredCalories); 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, prefs.setString(_preferredCategoriesKey,
_preferredCategories.map((c) => c.index.toString()).join(',')); _preferredCategories.map((c) => c.index.toString()).join(','));
} }
Future<void> _load() async { Future<void> _load() async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
_desiredCalories = prefs.getInt(caloriesKey) ?? 2000; _desiredCalories = prefs.getInt(_caloriesKey) ?? 2000;
_preferredCategories.clear(); _preferredCategories.clear();
final names = prefs.getString(preferredCategoriesKey) ?? ''; final names = prefs.getString(_preferredCategoriesKey) ?? '';
for (final name in names.split(',')) { for (final name in names.split(',')) {
final index = int.parse(name) ?? 0; 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/data/veggie.dart';
import 'package:veggieseasons/styles.dart'; import 'package:veggieseasons/styles.dart';
/// A circular widget that indicates in which seasons a particular veggie can be /// A circular widget that represents a season of the year.
/// harvested. It displays the first two letters of the season and uses a ///
/// different background color to represent each of the seasons as well. /// 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 { class SeasonCircle extends StatelessWidget {
final bool active; SeasonCircle(this.season, this.isHarvestTime);
/// Season to be displayed by this widget.
final Season season; 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 { String get _firstChars {
return '${season.toString().substring(7, 8).toUpperCase()}' return '${season.toString().substring(7, 8).toUpperCase()}'
@ -29,7 +34,9 @@ class SeasonCircle extends StatelessWidget {
padding: const EdgeInsets.all(4.0), padding: const EdgeInsets.all(4.0),
child: DecoratedBox( child: DecoratedBox(
decoration: BoxDecoration( decoration: BoxDecoration(
color: active ? Styles.seasonColors[season] : Styles.transparentColor, color: isHarvestTime
? Styles.seasonColors[season]
: Styles.transparentColor,
borderRadius: BorderRadius.circular(25.0), borderRadius: BorderRadius.circular(25.0),
border: Styles.seasonBorder, border: Styles.seasonBorder,
), ),
@ -39,8 +46,9 @@ class SeasonCircle extends StatelessWidget {
child: Center( child: Center(
child: Text( child: Text(
_firstChars, _firstChars,
style: style: isHarvestTime
active ? Styles.activeSeasonText : Styles.inactiveSeasonText, ? Styles.activeSeasonText
: Styles.inactiveSeasonText,
), ),
), ),
), ),
@ -121,12 +129,9 @@ class DetailsScreen extends StatelessWidget {
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
children: [ children: [
Wrap( Wrap(
children: Season.values children: Season.values.map((s) {
.map((s) => SeasonCircle( return SeasonCircle(s, veggie.seasons.contains(s));
s, }).toList(),
veggie.seasons.contains(s),
))
.toList(),
), ),
SizedBox(width: 8.0), SizedBox(width: 8.0),
Expanded( Expanded(

Loading…
Cancel
Save