Skip to content

Commit

Permalink
feat: persistent volume percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 11, 2023
1 parent 91c72f9 commit 3724bd5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/components/root/bottom_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:spotube/models/logger.dart';
import 'package:flutter/material.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_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/utils/platform.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
Expand Down Expand Up @@ -116,9 +117,9 @@ class BottomPlayer extends HookConsumerWidget {
height: 40,
constraints: const BoxConstraints(maxWidth: 250),
child: HookBuilder(builder: (context) {
final volume =
useStream(audioPlayer.volumeStream).data ??
audioPlayer.volume;
final volume = ref.watch(volumeProvider);
final volumeNotifier =
ref.watch(volumeProvider.notifier);

return Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -136,9 +137,9 @@ class BottomPlayer extends HookConsumerWidget {
),
onPressed: () {
if (volume == 0) {
audioPlayer.setVolume(1);
volumeNotifier.setVolume(1);
} else {
audioPlayer.setVolume(0);
volumeNotifier.setVolume(0);
}
},
),
Expand All @@ -147,11 +148,11 @@ class BottomPlayer extends HookConsumerWidget {
if (event is PointerScrollEvent) {
if (event.scrollDelta.dy > 0) {
final value = volume - .2;
audioPlayer
volumeNotifier
.setVolume(value < 0 ? 0 : value);
} else {
final value = volume + .2;
audioPlayer
volumeNotifier
.setVolume(value > 1 ? 1 : value);
}
}
Expand All @@ -160,7 +161,7 @@ class BottomPlayer extends HookConsumerWidget {
min: 0,
max: 1,
value: volume,
onChanged: audioPlayer.setVolume,
onChanged: volumeNotifier.setVolume,
),
),
],
Expand Down
32 changes: 32 additions & 0 deletions lib/provider/volume_provider.dart
Original file line number Diff line number Diff line change
@@ -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());

0 comments on commit 3724bd5

Please sign in to comment.