introduced logging; AudioStore cleanup

This commit is contained in:
Moritz Weber 2020-09-19 20:30:41 +02:00
parent e551879b1b
commit be212b0d81
10 changed files with 59 additions and 98 deletions

View file

@ -2,6 +2,7 @@ import 'package:audio_session/audio_session.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'injection_container.dart'; import 'injection_container.dart';
@ -9,7 +10,6 @@ import 'presentation/pages/currently_playing.dart';
import 'presentation/pages/home_page.dart'; import 'presentation/pages/home_page.dart';
import 'presentation/pages/library_page.dart'; import 'presentation/pages/library_page.dart';
import 'presentation/pages/settings_page.dart'; import 'presentation/pages/settings_page.dart';
import 'presentation/state/audio_store.dart';
import 'presentation/state/navigation_store.dart'; import 'presentation/state/navigation_store.dart';
import 'presentation/theming.dart'; import 'presentation/theming.dart';
import 'presentation/widgets/audio_service_widget.dart'; import 'presentation/widgets/audio_service_widget.dart';
@ -23,6 +23,10 @@ Future<void> main() async {
final session = await AudioSession.instance; final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration.music()); await session.configure(const AudioSessionConfiguration.music());
Logger.root.onRecord.listen((record) {
print('${record.time} [${record.level.name}] ${record.loggerName}: ${record.message}');
});
runApp(MyApp()); runApp(MyApp());
} }
@ -70,8 +74,6 @@ class _RootPageState extends State<RootPage> {
@override @override
void dispose() { void dispose() {
final AudioStore _audioStore = Provider.of<AudioStore>(context);
_audioStore.dispose();
super.dispose(); super.dispose();
} }

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../../domain/entities/song.dart'; import '../../domain/entities/song.dart';
@ -14,9 +15,11 @@ import 'queue_page.dart';
class CurrentlyPlayingPage extends StatelessWidget { class CurrentlyPlayingPage extends StatelessWidget {
const CurrentlyPlayingPage({Key key}) : super(key: key); const CurrentlyPlayingPage({Key key}) : super(key: key);
static final _log = Logger('CurrentlyPlayingPage');
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
print('CurrentlyPlayingPage.build'); _log.info('build started');
final AudioStore audioStore = Provider.of<AudioStore>(context); final AudioStore audioStore = Provider.of<AudioStore>(context);
return Scaffold( return Scaffold(
@ -25,10 +28,8 @@ class CurrentlyPlayingPage extends StatelessWidget {
builder: (BuildContext context, BoxConstraints constraints) => builder: (BuildContext context, BoxConstraints constraints) =>
Observer( Observer(
builder: (BuildContext context) { builder: (BuildContext context) {
print('CurrentlyPlayingPage.build -> Observer.build'); _log.info('Observer.build');
final Song song = audioStore.song; final Song song = audioStore.currentSongStream.value;
print(audioStore.queueIndexStream.value);
return Padding( return Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(

View file

@ -15,7 +15,7 @@ class AudioStore extends _AudioStore with _$AudioStore {
abstract class _AudioStore with Store { abstract class _AudioStore with Store {
_AudioStore(this._audioRepository) { _AudioStore(this._audioRepository) {
currentSong = _audioRepository.currentSongStream.asObservable(); currentSongStream = _audioRepository.currentSongStream.distinct().asObservable();
currentPositionStream = currentPositionStream =
_audioRepository.currentPositionStream.asObservable(initialValue: 0); _audioRepository.currentPositionStream.asObservable(initialValue: 0);
@ -27,22 +27,13 @@ abstract class _AudioStore with Store {
shuffleModeStream = _audioRepository.shuffleModeStream shuffleModeStream = _audioRepository.shuffleModeStream
.asObservable(initialValue: ShuffleMode.none); .asObservable(initialValue: ShuffleMode.none);
_disposers.add(autorun((_) {
updateSong(currentSong.value);
}));
playbackStateStream = _audioRepository.playbackStateStream.asObservable(); playbackStateStream = _audioRepository.playbackStateStream.asObservable();
} }
final AudioRepository _audioRepository; final AudioRepository _audioRepository;
final List<ReactionDisposer> _disposers = [];
// TODO: naming and usage confusing!
@observable @observable
ObservableStream<Song> currentSong; ObservableStream<Song> currentSongStream;
@observable
Song song;
@observable @observable
ObservableStream<PlaybackState> playbackStateStream; ObservableStream<PlaybackState> playbackStateStream;
@ -59,13 +50,6 @@ abstract class _AudioStore with Store {
@observable @observable
ObservableStream<ShuffleMode> shuffleModeStream; ObservableStream<ShuffleMode> shuffleModeStream;
void dispose() {
print('AudioStore.dispose');
for (final ReactionDisposer d in _disposers) {
d();
}
}
@action @action
Future<void> playSong(int index, List<Song> songList) async { Future<void> playSong(int index, List<Song> songList) async {
_audioRepository.playSong(index, songList); _audioRepository.playSong(index, songList);
@ -98,13 +82,4 @@ abstract class _AudioStore with Store {
Future<void> shuffleAll() async { Future<void> shuffleAll() async {
_audioRepository.shuffleAll(); _audioRepository.shuffleAll();
} }
@action
Future<void> updateSong(Song streamValue) async {
print('updateSong');
if (streamValue != null && streamValue != song) {
print('actually updating...');
song = streamValue;
}
}
} }

View file

@ -9,33 +9,18 @@ part of 'audio_store.dart';
// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic // ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic
mixin _$AudioStore on _AudioStore, Store { mixin _$AudioStore on _AudioStore, Store {
final _$currentSongAtom = Atom(name: '_AudioStore.currentSong'); final _$currentSongStreamAtom = Atom(name: '_AudioStore.currentSongStream');
@override @override
ObservableStream<Song> get currentSong { ObservableStream<Song> get currentSongStream {
_$currentSongAtom.reportRead(); _$currentSongStreamAtom.reportRead();
return super.currentSong; return super.currentSongStream;
} }
@override @override
set currentSong(ObservableStream<Song> value) { set currentSongStream(ObservableStream<Song> value) {
_$currentSongAtom.reportWrite(value, super.currentSong, () { _$currentSongStreamAtom.reportWrite(value, super.currentSongStream, () {
super.currentSong = value; super.currentSongStream = value;
});
}
final _$songAtom = Atom(name: '_AudioStore.song');
@override
Song get song {
_$songAtom.reportRead();
return super.song;
}
@override
set song(Song value) {
_$songAtom.reportWrite(value, super.song, () {
super.song = value;
}); });
} }
@ -152,18 +137,10 @@ mixin _$AudioStore on _AudioStore, Store {
return _$skipToPreviousAsyncAction.run(() => super.skipToPrevious()); return _$skipToPreviousAsyncAction.run(() => super.skipToPrevious());
} }
final _$updateSongAsyncAction = AsyncAction('_AudioStore.updateSong');
@override
Future<void> updateSong(Song streamValue) {
return _$updateSongAsyncAction.run(() => super.updateSong(streamValue));
}
@override @override
String toString() { String toString() {
return ''' return '''
currentSong: ${currentSong}, currentSongStream: ${currentSongStream},
song: ${song},
playbackStateStream: ${playbackStateStream}, playbackStateStream: ${playbackStateStream},
currentPositionStream: ${currentPositionStream}, currentPositionStream: ${currentPositionStream},
queueStream: ${queueStream}, queueStream: ${queueStream},

View file

@ -18,14 +18,14 @@ class CurrentlyPlayingBar extends StatelessWidget {
return Observer( return Observer(
builder: (BuildContext context) { builder: (BuildContext context) {
if (audioStore.currentSong.value != null) { if (audioStore.currentSongStream.value != null) {
final Song song = audioStore.currentSong.value; final Song song = audioStore.currentSongStream.value;
return Column( return Column(
children: <Widget>[ children: <Widget>[
Container( Container(
child: LinearProgressIndicator( child: LinearProgressIndicator(
value: audioStore.currentPositionStream.value / audioStore.currentSong.value.duration, value: audioStore.currentPositionStream.value / audioStore.currentSongStream.value.duration,
), ),
height: 2, height: 2,
), ),

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../../domain/entities/song.dart';
import '../state/audio_store.dart'; import '../state/audio_store.dart';
import '../state/music_data_store.dart'; import '../state/music_data_store.dart';
import '../theming.dart'; import '../theming.dart';
@ -15,34 +16,38 @@ class SongCustomizationButtons extends StatelessWidget {
final AudioStore audioStore = Provider.of<AudioStore>(context); final AudioStore audioStore = Provider.of<AudioStore>(context);
return Observer( return Observer(
builder: (BuildContext context) => Row( builder: (BuildContext context) {
children: [ final Song song = audioStore.currentSongStream.value;
const Icon( return Row(
Icons.link, children: [
size: 20.0, const Icon(
), Icons.link,
Container(
width: 40,
),
const Icon(
Icons.favorite,
size: 20.0,
color: RASPBERRY,
),
Container(
width: 40,
),
IconButton(
icon: Icon(
Icons.remove_circle_outline,
size: 20.0, size: 20.0,
color: audioStore.song.blocked ? Colors.red : Colors.white70,
), ),
onPressed: () => musicDataStore.setSongBlocked(audioStore.song, !audioStore.song.blocked), Container(
), width: 40,
], ),
mainAxisAlignment: MainAxisAlignment.center, const Icon(
), Icons.favorite,
size: 20.0,
color: RASPBERRY,
),
Container(
width: 40,
),
IconButton(
icon: Icon(
Icons.remove_circle_outline,
size: 20.0,
color: song.blocked ? Colors.red : Colors.white70,
),
onPressed: () => musicDataStore.setSongBlocked(
song, !song.blocked),
),
],
mainAxisAlignment: MainAxisAlignment.center,
);
},
); );
} }
} }

View file

@ -14,7 +14,7 @@ class TimeProgressIndicator extends StatelessWidget {
return Observer( return Observer(
builder: (BuildContext context) { builder: (BuildContext context) {
final int duration = audioStore.song?.duration ?? 1000; final int duration = audioStore.currentSongStream.value?.duration ?? 1000;
return Padding( return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0), padding: const EdgeInsets.symmetric(horizontal: 16.0),

View file

@ -118,7 +118,7 @@ class AudioPlayerTask extends BackgroundAudioTask {
print('AudioPlayerTask.init'); print('AudioPlayerTask.init');
audioPlayer.positionStream.listen((position) => handlePosition(position)); audioPlayer.positionStream.listen((position) => handlePosition(position));
audioPlayer.playerStateStream.listen((event) => handlePlayerState(event)); audioPlayer.playerStateStream.listen((event) => handlePlayerState(event));
audioPlayer.sequenceStateStream.listen((event) => playbackIndex = event.currentIndex); audioPlayer.sequenceStateStream.listen((event) => playbackIndex = event?.currentIndex);
final connectPort = IsolateNameServer.lookupPortByName(MOOR_ISOLATE); final connectPort = IsolateNameServer.lookupPortByName(MOOR_ISOLATE);
final MoorIsolate moorIsolate = MoorIsolate.fromConnectPort(connectPort); final MoorIsolate moorIsolate = MoorIsolate.fromConnectPort(connectPort);

View file

@ -367,7 +367,7 @@ packages:
source: hosted source: hosted
version: "0.4.0" version: "0.4.0"
logging: logging:
dependency: transitive dependency: "direct main"
description: description:
name: logging name: logging
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"

View file

@ -23,6 +23,7 @@ dependencies:
get_it: ^4.0.2 get_it: ^4.0.2
provider: ^4.0.4 provider: ^4.0.4
logging: ^0.11.4
moor: ^3.0.2 moor: ^3.0.2
moor_ffi: ^0.5.0 moor_ffi: ^0.5.0
path_provider: path_provider: