mirror of
https://github.com/johrpan/christmas_cats.git
synced 2025-10-28 19:27:25 +01:00
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:
parent
b8d9ca91b4
commit
b267459114
2 changed files with 19 additions and 1 deletions
|
|
@ -1,8 +1,11 @@
|
|||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
final storage = Storage();
|
||||
|
||||
class Storage {
|
||||
final coins = BehaviorSubject.seeded(0);
|
||||
|
||||
SharedPreferences shPref;
|
||||
|
||||
int get highScore1 {
|
||||
|
|
@ -30,7 +33,21 @@ class Storage {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue