refactor: migrate to riverpod 3
This commit is contained in:
@@ -49,21 +49,32 @@ class TextToSpeechState {
|
||||
}
|
||||
}
|
||||
|
||||
class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
TextToSpeechController(this._service) : super(const TextToSpeechState()) {
|
||||
_service.bindHandlers(
|
||||
onStart: _handleStart,
|
||||
onComplete: _handleCompletion,
|
||||
onCancel: _handleCancellation,
|
||||
onPause: _handlePause,
|
||||
onContinue: _handleContinue,
|
||||
onError: _handleError,
|
||||
);
|
||||
}
|
||||
|
||||
final TextToSpeechService _service;
|
||||
class TextToSpeechController extends Notifier<TextToSpeechState> {
|
||||
late final TextToSpeechService _service;
|
||||
bool _handlersBound = false;
|
||||
Future<bool>? _initializationFuture;
|
||||
|
||||
@override
|
||||
TextToSpeechState build() {
|
||||
_service = ref.watch(textToSpeechServiceProvider);
|
||||
if (!_handlersBound) {
|
||||
_handlersBound = true;
|
||||
_service.bindHandlers(
|
||||
onStart: _handleStart,
|
||||
onComplete: _handleCompletion,
|
||||
onCancel: _handleCancellation,
|
||||
onPause: _handlePause,
|
||||
onContinue: _handleContinue,
|
||||
onError: _handleError,
|
||||
);
|
||||
|
||||
ref.onDispose(() {
|
||||
unawaited(_service.stop());
|
||||
});
|
||||
}
|
||||
return const TextToSpeechState();
|
||||
}
|
||||
|
||||
Future<bool> _ensureInitialized() {
|
||||
final existing = _initializationFuture;
|
||||
if (existing != null) {
|
||||
@@ -78,7 +89,7 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
final future = _service
|
||||
.initialize()
|
||||
.then((available) {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return available;
|
||||
}
|
||||
|
||||
@@ -90,7 +101,7 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
return available;
|
||||
})
|
||||
.catchError((error, _) {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -132,7 +143,7 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
|
||||
final available = await _ensureInitialized();
|
||||
if (!available) {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -151,14 +162,14 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
|
||||
try {
|
||||
await _service.speak(text);
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
if (state.status == TtsPlaybackStatus.loading) {
|
||||
state = state.copyWith(status: TtsPlaybackStatus.speaking);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -178,7 +189,7 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
|
||||
Future<void> stop() async {
|
||||
await _service.stop();
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -189,14 +200,14 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
}
|
||||
|
||||
void _handleStart() {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(status: TtsPlaybackStatus.speaking);
|
||||
}
|
||||
|
||||
void _handleCompletion() {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -206,7 +217,7 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
}
|
||||
|
||||
void _handleCancellation() {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -216,21 +227,21 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
}
|
||||
|
||||
void _handlePause() {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(status: TtsPlaybackStatus.paused);
|
||||
}
|
||||
|
||||
void _handleContinue() {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(status: TtsPlaybackStatus.speaking);
|
||||
}
|
||||
|
||||
void _handleError(String message) {
|
||||
if (!mounted) {
|
||||
if (!ref.mounted) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(
|
||||
@@ -239,12 +250,6 @@ class TextToSpeechController extends StateNotifier<TextToSpeechState> {
|
||||
clearActiveMessageId: true,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
unawaited(_service.stop());
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
final textToSpeechServiceProvider = Provider<TextToSpeechService>((ref) {
|
||||
@@ -256,7 +261,6 @@ final textToSpeechServiceProvider = Provider<TextToSpeechService>((ref) {
|
||||
});
|
||||
|
||||
final textToSpeechControllerProvider =
|
||||
StateNotifierProvider<TextToSpeechController, TextToSpeechState>((ref) {
|
||||
final service = ref.watch(textToSpeechServiceProvider);
|
||||
return TextToSpeechController(service);
|
||||
});
|
||||
NotifierProvider<TextToSpeechController, TextToSpeechState>(
|
||||
TextToSpeechController.new,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user