Skip to content

Commit

Permalink
fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz-weber committed Mar 11, 2023
1 parent fce520a commit 5a64e50
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- Fix bug with smart lists in history entries
- Added logging to files
- Implement natural sorting for album songs (#59)
- Add option for counting songs as played (#38)
2 changes: 2 additions & 0 deletions src/lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ const String SETTING_PLAY_ALBUMS_IN_ORDER = 'SETTING_PLAY_ALBUMS_IN_ORDER';

const String ALBUM_OF_DAY = 'ALBUM_OF_DAY';
const String ARTIST_OF_DAY = 'ARTIST_OF_DAY';

const String LISTENED_PERCENTAGE = 'LISTENED_PERCENTAGE';
11 changes: 9 additions & 2 deletions src/lib/domain/actors/audio_player_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ import '../entities/song.dart';
import '../repositories/audio_player_repository.dart';
import '../repositories/music_data_repository.dart';
import '../repositories/platform_integration_repository.dart';
import '../repositories/settings_repository.dart';

class AudioPlayerActor {
AudioPlayerActor(
this._audioPlayerRepository,
this._musicDataRepository,
this._platformIntegrationRepository,
this._settingsInfoRepository,
) {
_audioPlayerRepository.currentSongStream.listen(_handleCurrentSong);
_audioPlayerRepository.playbackEventStream
.listen(_platformIntegrationRepository.handlePlaybackEvent);
_audioPlayerRepository.positionStream
.listen((duration) => _handlePosition(duration, _currentSong));

_listenedPercentage = _settingsInfoRepository.listenedPercentageStream.valueOrNull ?? 50;
_settingsInfoRepository.listenedPercentageStream.listen((event) => _listenedPercentage = event);
}

final AudioPlayerRepository _audioPlayerRepository;
final MusicDataRepository _musicDataRepository;
final PlatformIntegrationRepository _platformIntegrationRepository;
final SettingsInfoRepository _settingsInfoRepository;

Song? _currentSong;
bool _countSongPlayback = false;
late int _listenedPercentage;

Future<void> _handleCurrentSong(Song? song) async {
_currentSong = song;
Expand All @@ -33,9 +40,9 @@ class AudioPlayerActor {
return;
}

if (position < song.duration * 0.05) {
if (position < song.duration * 0.01) {
_countSongPlayback = true;
} else if (position > song.duration * 0.95 && _countSongPlayback) {
} else if (position > song.duration * (_listenedPercentage / 100) && _countSongPlayback) {
_countSongPlayback = false;
await _musicDataRepository.incrementPlayCount(song);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/domain/repositories/settings_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ abstract class SettingsInfoRepository {
Stream<List<String>> get libraryFoldersStream;
ValueStream<String> get fileExtensionsStream;
ValueStream<bool> get playAlbumsInOrderStream;
ValueStream<int> get listenedPercentageStream;
}

abstract class SettingsRepository extends SettingsInfoRepository {
Future<void> addLibraryFolder(String? path);
Future<void> removeLibraryFolder(String? path);
Future<void> setFileExtension(String extensions);
Future<void> setPlayAlbumsInOrder(bool playInOrder);
Future<void> setListenedPercentage(int percentage);
}
4 changes: 4 additions & 0 deletions src/lib/injection_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ Future<void> setupGetIt() async {
getIt(),
),
);
getIt.registerLazySingleton<SettingsInfoRepository>(
() => getIt<SettingsRepository>(),
);
getIt.registerLazySingleton<PlatformIntegrationRepository>(
() => PlatformIntegrationRepositoryImpl(
getIt(),
Expand Down Expand Up @@ -369,6 +372,7 @@ Future<void> setupGetIt() async {
getIt(),
getIt(),
getIt(),
getIt(),
),
);

Expand Down
72 changes: 72 additions & 0 deletions src/lib/presentation/pages/settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:get_it/get_it.dart';
Expand Down Expand Up @@ -158,6 +160,10 @@ class SettingsPage extends StatelessWidget {
isThreeLine: true,
),
),
const Divider(
height: 4.0,
),
PercentageSlider(settingsStore),
],
),
),
Expand Down Expand Up @@ -191,3 +197,69 @@ class SettingsSection extends StatelessWidget {
);
}
}

class PercentageSlider extends StatefulWidget {
const PercentageSlider(this.settingsStore, {super.key});

final SettingsStore settingsStore;

@override
State<PercentageSlider> createState() => _PercentageSliderState();
}

class _PercentageSliderState extends State<PercentageSlider> {
late double _value;
late StreamSubscription _streamSubscription;

@override
void initState() {
_value = (widget.settingsStore.listenedPercentageStream.value ?? 50).toDouble();
_streamSubscription = widget.settingsStore.listenedPercentageStream.listen(
(value) => setState(() => _value = value.toDouble()),
);
super.initState();
}

@override
void dispose() {
_streamSubscription.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
left: HORIZONTAL_PADDING,
right: HORIZONTAL_PADDING,
top: 16.0,
),
child: Text(
'Count songs as played after: ${_value.round()}%',
style: Theme.of(context).textTheme.titleMedium,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Slider(
value: _value,
onChanged: (value) {
setState(() {
_value = value;
});
},
onChangeEnd: (value) {
widget.settingsStore.setListenedPercentage(value.round());
},
min: 1,
max: 99,
),
),
],
);
}
}
8 changes: 8 additions & 0 deletions src/lib/presentation/state/settings_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ abstract class _SettingsStore with Store {
late ObservableStream<Set<String>> blockedFilesStream =
_musicDataRepository.blockedFilesStream.asObservable(initialValue: {});

@observable
late ObservableStream<int> listenedPercentageStream =
_settingsRepository.listenedPercentageStream.asObservable();

@computed
int get numBlockedFiles => blockedFilesStream.value!.length;

Expand Down Expand Up @@ -66,5 +70,9 @@ abstract class _SettingsStore with Store {
await _settingsRepository.setPlayAlbumsInOrder(playInOrder);
}

Future<void> setListenedPercentage(int percentage) async {
await _settingsRepository.setListenedPercentage(percentage);
}

void dispose() {}
}
18 changes: 18 additions & 0 deletions src/lib/presentation/state/settings_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/lib/system/datasources/drift/settings_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ class SettingsDao extends DatabaseAccessor<MainDatabase>

@override
Future<void> setPlayAlbumsInOrder(bool playInOrder) async {
print(playInOrder);
await (update(keyValueEntries)..where((tbl) => tbl.key.equals(SETTING_PLAY_ALBUMS_IN_ORDER)))
.write(KeyValueEntriesCompanion(value: Value(playInOrder ? 'true' : 'false')));
}

@override
Stream<int> get listenedPercentageStream =>
(select(keyValueEntries)..where((tbl) => tbl.key.equals(LISTENED_PERCENTAGE)))
.watchSingle()
.map((event) => int.parse(event.value));

@override
Future<void> setListenedPercentage(int percentage) async {
await (update(keyValueEntries)..where((tbl) => tbl.key.equals(LISTENED_PERCENTAGE)))
.write(KeyValueEntriesCompanion(value: Value(percentage.toString())));
}
}
20 changes: 18 additions & 2 deletions src/lib/system/datasources/drift_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class MainDatabase extends _$MainDatabase {
MainDatabase.withQueryExecutor(QueryExecutor e) : super(e);

@override
int get schemaVersion => 16;
int get schemaVersion => 17;

@override
MigrationStrategy get migration => MigrationStrategy(
Expand All @@ -238,7 +238,9 @@ class MainDatabase extends _$MainDatabase {
);
await into(keyValueEntries).insert(
const KeyValueEntriesCompanion(
key: Value(SETTING_ALLOWED_EXTENSIONS), value: Value(ALLOWED_FILE_EXTENSIONS)),
key: Value(SETTING_ALLOWED_EXTENSIONS),
value: Value(ALLOWED_FILE_EXTENSIONS),
),
);
await into(keyValueEntries).insert(
const KeyValueEntriesCompanion(
Expand Down Expand Up @@ -301,6 +303,12 @@ class MainDatabase extends _$MainDatabase {
),
),
);
await into(keyValueEntries).insert(
const KeyValueEntriesCompanion(
key: Value(LISTENED_PERCENTAGE),
value: Value('95'),
),
);
}
},
onUpgrade: (Migrator m, int from, int to) async {
Expand Down Expand Up @@ -409,6 +417,14 @@ class MainDatabase extends _$MainDatabase {
await m.addColumn(songs, songs.color);
await m.addColumn(albums, albums.color);
}
if (from < 17) {
await into(keyValueEntries).insert(
const KeyValueEntriesCompanion(
key: Value(LISTENED_PERCENTAGE),
value: Value('95'),
),
);
}
},
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/system/datasources/settings_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ abstract class SettingsDataSource {

Stream<bool> get playAlbumsInOrderStream;
Future<void> setPlayAlbumsInOrder(bool playInOrder);

Stream<int> get listenedPercentageStream;
Future<void> setListenedPercentage(int percentage);
}
12 changes: 12 additions & 0 deletions src/lib/system/repositories/settings_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ class SettingsRepositoryImpl implements SettingsRepository {
SettingsRepositoryImpl(this._settingsDataSource) {
_settingsDataSource.fileExtensionsStream.listen(_fileExtensionsSubject.add);
_settingsDataSource.playAlbumsInOrderStream.listen(_playAlbumsInOrderSubject.add);
_settingsDataSource.listenedPercentageStream.listen(
(value) => _listenedPercentageSubject.add(value),
);
}

final SettingsDataSource _settingsDataSource;

final BehaviorSubject<String> _fileExtensionsSubject = BehaviorSubject();
final BehaviorSubject<bool> _playAlbumsInOrderSubject = BehaviorSubject();
final BehaviorSubject<int> _listenedPercentageSubject = BehaviorSubject();

@override
Stream<List<String>> get libraryFoldersStream => _settingsDataSource.libraryFoldersStream;
Expand Down Expand Up @@ -44,4 +48,12 @@ class SettingsRepositoryImpl implements SettingsRepository {
Future<void> setPlayAlbumsInOrder(bool playInOrder) async {
await _settingsDataSource.setPlayAlbumsInOrder(playInOrder);
}

@override
ValueStream<int> get listenedPercentageStream => _listenedPercentageSubject.stream;

@override
Future<void> setListenedPercentage(int percentage) async {
await _settingsDataSource.setListenedPercentage(percentage);
}
}

0 comments on commit 5a64e50

Please sign in to comment.