diff --git a/lib/muyu/muyu_page.dart b/lib/muyu/muyu_page.dart index 6545b27..61b4710 100644 --- a/lib/muyu/muyu_page.dart +++ b/lib/muyu/muyu_page.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_first_station/muyu/animate_text.dart'; import 'package:flutter_first_station/muyu/options/select_audio.dart'; import 'package:uuid/uuid.dart'; +import '../storage/sp_storage.dart'; import 'models/audio_option.dart'; import 'models/image_option.dart'; import 'models/merit_record.dart'; @@ -23,7 +24,8 @@ class MuyuPage extends StatefulWidget { State createState() => _MuyuPageState(); } -class _MuyuPageState extends State with AutomaticKeepAliveClientMixin { +class _MuyuPageState extends State + with AutomaticKeepAliveClientMixin { int _counter = 0; MeritRecord? _cruRecord; @@ -52,11 +54,22 @@ class _MuyuPageState extends State with AutomaticKeepAliveClientMixin void initState() { super.initState(); _initAudioPool(); + _initConfig(); + } + + void _initConfig() async{ + Map config = await SpStorage.instance.readMuYUConfig(); + _counter = config['counter']??0; + _activeImageIndex = config['activeImageIndex']??0; + _activeAudioIndex = config['activeAudioIndex']??0; + setState(() { + + }); } void _initAudioPool() async { pool = await FlameAudio.createPool( - 'muyu_1.mp3', + audioOptions[_activeAudioIndex].src, maxPlayers: 1, ); } @@ -84,8 +97,7 @@ class _MuyuPageState extends State with AutomaticKeepAliveClientMixin image: activeImage, onTap: _onKnock, ), - if (_cruRecord != null) - AnimateText(record: _cruRecord!) + if (_cruRecord != null) AnimateText(record: _cruRecord!) ], ), ), @@ -142,11 +154,20 @@ class _MuyuPageState extends State with AutomaticKeepAliveClientMixin audioOptions[_activeAudioIndex].name, ); _counter += _cruRecord!.value; + saveConfig(); // 添加功德记录 _records.add(_cruRecord!); }); } + void saveConfig() { + SpStorage.instance.saveMuYUConfig( + counter: _counter, + activeImageIndex: _activeImageIndex, + activeAudioIndex: _activeAudioIndex, + ); + } + String get activeImage => imageOptions[_activeImageIndex].src; int get knockValue { @@ -160,6 +181,7 @@ class _MuyuPageState extends State with AutomaticKeepAliveClientMixin if (value == _activeImageIndex) return; setState(() { _activeImageIndex = value; + saveConfig(); }); } @@ -169,6 +191,7 @@ class _MuyuPageState extends State with AutomaticKeepAliveClientMixin Navigator.of(context).pop(); if (value == _activeAudioIndex) return; _activeAudioIndex = value; + saveConfig(); pool = await FlameAudio.createPool( activeAudio, maxPlayers: 1, diff --git a/lib/storage/sp_storage.dart b/lib/storage/sp_storage.dart index 6610e10..c063a59 100644 --- a/lib/storage/sp_storage.dart +++ b/lib/storage/sp_storage.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; const String kGuessSpKey = 'guess-config'; +const String kMuYUSpKey = 'muyu-config'; class SpStorage { SpStorage._(); @@ -22,18 +23,37 @@ class SpStorage { } Future saveGuessConfig({ - bool? guessing, - int? value, + required bool guessing, + required int value, }) async { await initSpWhenNull(); String content = json.encode({'guessing': guessing, 'value': value}); return _sp!.setString(kGuessSpKey, content); } - Future> readGuessConfig() async { + Future> readGuessConfig() async { await initSpWhenNull(); - String content = _sp!.getString(kGuessSpKey)??"{}"; + String content = _sp!.getString(kGuessSpKey) ?? "{}"; return json.decode(content); } + Future saveMuYUConfig({ + required int counter, + required int activeImageIndex, + required int activeAudioIndex, + }) async { + await initSpWhenNull(); + String content = json.encode({ + 'counter': counter, + 'activeImageIndex': activeImageIndex, + 'activeAudioIndex': activeAudioIndex, + }); + return _sp!.setString(kMuYUSpKey, content); + } + + Future> readMuYUConfig() async { + await initSpWhenNull(); + String content = _sp!.getString(kMuYUSpKey) ?? "{}"; + return json.decode(content); + } }