mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
feat: persistent volume percentage
This commit is contained in:
parent
91c72f9ec9
commit
3724bd5a10
@ -19,6 +19,7 @@ import 'package:spotube/models/logger.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
|
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
|
||||||
import 'package:spotube/provider/user_preferences_provider.dart';
|
import 'package:spotube/provider/user_preferences_provider.dart';
|
||||||
|
import 'package:spotube/provider/volume_provider.dart';
|
||||||
import 'package:spotube/services/audio_player/audio_player.dart';
|
import 'package:spotube/services/audio_player/audio_player.dart';
|
||||||
import 'package:spotube/utils/platform.dart';
|
import 'package:spotube/utils/platform.dart';
|
||||||
import 'package:spotube/utils/type_conversion_utils.dart';
|
import 'package:spotube/utils/type_conversion_utils.dart';
|
||||||
@ -116,9 +117,9 @@ class BottomPlayer extends HookConsumerWidget {
|
|||||||
height: 40,
|
height: 40,
|
||||||
constraints: const BoxConstraints(maxWidth: 250),
|
constraints: const BoxConstraints(maxWidth: 250),
|
||||||
child: HookBuilder(builder: (context) {
|
child: HookBuilder(builder: (context) {
|
||||||
final volume =
|
final volume = ref.watch(volumeProvider);
|
||||||
useStream(audioPlayer.volumeStream).data ??
|
final volumeNotifier =
|
||||||
audioPlayer.volume;
|
ref.watch(volumeProvider.notifier);
|
||||||
|
|
||||||
return Row(
|
return Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
@ -136,9 +137,9 @@ class BottomPlayer extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (volume == 0) {
|
if (volume == 0) {
|
||||||
audioPlayer.setVolume(1);
|
volumeNotifier.setVolume(1);
|
||||||
} else {
|
} else {
|
||||||
audioPlayer.setVolume(0);
|
volumeNotifier.setVolume(0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -147,11 +148,11 @@ class BottomPlayer extends HookConsumerWidget {
|
|||||||
if (event is PointerScrollEvent) {
|
if (event is PointerScrollEvent) {
|
||||||
if (event.scrollDelta.dy > 0) {
|
if (event.scrollDelta.dy > 0) {
|
||||||
final value = volume - .2;
|
final value = volume - .2;
|
||||||
audioPlayer
|
volumeNotifier
|
||||||
.setVolume(value < 0 ? 0 : value);
|
.setVolume(value < 0 ? 0 : value);
|
||||||
} else {
|
} else {
|
||||||
final value = volume + .2;
|
final value = volume + .2;
|
||||||
audioPlayer
|
volumeNotifier
|
||||||
.setVolume(value > 1 ? 1 : value);
|
.setVolume(value > 1 ? 1 : value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,7 +161,7 @@ class BottomPlayer extends HookConsumerWidget {
|
|||||||
min: 0,
|
min: 0,
|
||||||
max: 1,
|
max: 1,
|
||||||
value: volume,
|
value: volume,
|
||||||
onChanged: audioPlayer.setVolume,
|
onChanged: volumeNotifier.setVolume,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
32
lib/provider/volume_provider.dart
Normal file
32
lib/provider/volume_provider.dart
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:spotube/services/audio_player/audio_player.dart';
|
||||||
|
import 'package:spotube/utils/persisted_state_notifier.dart';
|
||||||
|
|
||||||
|
class VolumeProvider extends PersistedStateNotifier<double> {
|
||||||
|
VolumeProvider() : super(1, 'volume');
|
||||||
|
|
||||||
|
Future<void> setVolume(double volume) async {
|
||||||
|
state = volume;
|
||||||
|
await audioPlayer.setVolume(volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<void> onInit() async {
|
||||||
|
await audioPlayer.setVolume(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<double> fromJson(Map<String, dynamic> json) {
|
||||||
|
return json['volume'] as double? ?? 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {'volume': state};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final volumeProvider =
|
||||||
|
StateNotifierProvider<VolumeProvider, double>((ref) => VolumeProvider());
|
Loading…
Reference in New Issue
Block a user