Add coins to storage

This adds an integer value representing the amount of coins that the
player has. Therefore rxdart is introduced as a dependency.
This commit is contained in:
Elias Projahn 2020-01-19 12:57:16 +01:00
parent b8d9ca91b4
commit b267459114
2 changed files with 19 additions and 1 deletions

View file

@ -1,8 +1,11 @@
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
final storage = Storage(); final storage = Storage();
class Storage { class Storage {
final coins = BehaviorSubject.seeded(0);
SharedPreferences shPref; SharedPreferences shPref;
int get highScore1 { int get highScore1 {
@ -30,7 +33,21 @@ class Storage {
} }
Future<void> init() async { Future<void> init() async {
shPref = await SharedPreferences.getInstance(); if (shPref == null) {
shPref = await SharedPreferences.getInstance();
try {
coins.add(shPref.getInt('coins') ?? 0);
} on TypeError {
// Nothing to do
}
}
}
Future<void> addCoins(int amount) async {
final newCoins = coins.value + amount;
coins.add(newCoins);
await shPref.setInt('coins', newCoins);
} }
Future<void> addScore(int score) async { Future<void> addScore(int score) async {

View file

@ -12,6 +12,7 @@ dependencies:
sdk: flutter sdk: flutter
flutter_localizations: flutter_localizations:
sdk: flutter sdk: flutter
rxdart:
shared_preferences: shared_preferences:
sprintf: sprintf:
vector_math: vector_math: