Skip to content

Commit

Permalink
chore: only init radio model once, print all errors in debug mode (#1001
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Feichtmeier authored Nov 8, 2024
1 parent e305900 commit 873e411
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 192 deletions.
2 changes: 2 additions & 0 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../../common/view/theme.dart';
import '../../external_path/external_path_service.dart';
import '../../l10n/l10n.dart';
import '../../library/library_model.dart';
import '../../radio/radio_model.dart';
import '../../settings/settings_model.dart';
import '../connectivity_model.dart';
import 'scaffold.dart';
Expand Down Expand Up @@ -75,6 +76,7 @@ class _MusicPodAppState extends State<_MusicPodApp> {
Future<bool> _init() async {
await di<ConnectivityModel>().init();
await di<LibraryModel>().init();
await di<RadioModel>().init();
if (!mounted) return false;
di<ExternalPathService>().init();
return true;
Expand Down
5 changes: 5 additions & 0 deletions lib/app/view/master_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';

import '../../common/data/audio.dart';
import '../../constants.dart';
import '../../extensions/build_context_x.dart';
import '../../library/library_model.dart';
import '../../player/player_model.dart';
import '../../common/view/icons.dart';
import '../../common/view/spaced_divider.dart';
import '../../radio/radio_model.dart';

class MasterTile extends StatelessWidget {
const MasterTile({
Expand Down Expand Up @@ -131,6 +133,9 @@ class __PlayAbleMasterTileState extends State<_PlayAbleMasterTile> {
final playerModel = di<PlayerModel>();

void onPlay() {
if (audios.first.audioType == AudioType.radio) {
di<RadioModel>().clickStation(audios.first);
}
if (isEnQueued) {
isPlaying ? playerModel.pause() : playerModel.resume();
} else {
Expand Down
5 changes: 5 additions & 0 deletions lib/common/view/avatar_play_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:watch_it/watch_it.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';
import '../../player/player_model.dart';
import '../../radio/radio_model.dart';
import '../data/audio.dart';
import 'icons.dart';
import 'theme.dart';
Expand Down Expand Up @@ -37,6 +38,10 @@ class AvatarPlayButton extends StatelessWidget with WatchItMixin {
child: IconButton(
tooltip: context.l10n.playAll,
onPressed: () {
if (audios.isNotEmpty &&
audios.first.audioType == AudioType.radio) {
di<RadioModel>().clickStation(audios.first);
}
if (isPlayerPlaying) {
if (pageIsQueue) {
playerModel.pause();
Expand Down
5 changes: 3 additions & 2 deletions lib/common/view/snackbars.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:flutter/material.dart';

ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar({
required BuildContext context,
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? showSnackBar({
required BuildContext? context,
Widget? content,
SnackBar? snackBar,
Duration? duration,
bool clear = true,
}) {
if (context == null || !context.mounted) return null;
if (clear) {
ScaffoldMessenger.of(context).clearSnackBars();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/extensions/int_x.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:intl/intl.dart';
import '../common/logging.dart';

extension IntX on int? {
String get unixTimeToDateString {
Expand All @@ -13,7 +14,8 @@ extension IntX on int? {
s = DateFormat.yMMMEd(
Platform.localeName == 'und' ? 'en_US' : Platform.localeName,
).format(dateTime);
} on Exception catch (_) {
} on Exception catch (e) {
printMessageInDebugMode(e);
return s;
}
return s;
Expand Down
13 changes: 10 additions & 3 deletions lib/external_path/external_path_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:pls/pls.dart';
import 'package:yaru/yaru.dart';

import '../common/data/audio.dart';
import '../common/logging.dart';
import '../player/player_service.dart';

class ExternalPathService {
Expand Down Expand Up @@ -46,7 +47,9 @@ class ExternalPathService {
audios: [Audio.fromMetadata(path: path, data: data)],
),
);
} catch (_) {}
} on Exception catch (e) {
printMessageInDebugMode(e);
}
}

void dispose() {
Expand All @@ -65,7 +68,9 @@ class ExternalPathService {
),
);
});
} on Exception catch (_) {}
} on Exception catch (e) {
printMessageInDebugMode(e);
}
}
}

Expand All @@ -84,7 +89,9 @@ class ExternalPathService {
} else if (path.endsWith('.pls')) {
audios = await _parsePlsPlaylist(path);
}
} on Exception catch (_) {}
} on Exception catch (e) {
printMessageInDebugMode(e);
}
}
return (path, audios);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/local_audio/local_audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';

import '../common/data/audio.dart';
import '../common/logging.dart';
import '../common/view/audio_filter.dart';
import '../extensions/media_file_x.dart';
import '../settings/settings_service.dart';
Expand Down Expand Up @@ -273,7 +274,8 @@ FutureOr<ImportResult> _readAudiosFromDirectory(String? directory) async {
final metadata = await readMetadata(e, getImage: false);
newAudios.add(Audio.fromMetadata(path: e.path, data: metadata));
}
} on Exception catch (_) {
} on Exception catch (error) {
printMessageInDebugMode(error);
failedImports.add(e.path);
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/local_audio/view/failed_imports_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

import '../../common/view/snackbars.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';
import '../../settings/settings_model.dart';

Expand All @@ -17,6 +18,7 @@ class FailedImportsContent extends StatelessWidget {

@override
Widget build(BuildContext context) {
final textColor = context.theme.snackBarTheme.actionTextColor;
return SizedBox(
height: 400,
width: 400,
Expand All @@ -30,9 +32,10 @@ class FailedImportsContent extends StatelessWidget {
padding: const EdgeInsets.all(10.0),
child: Text(
context.l10n.failedToImport,
style: const TextStyle(
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: textColor,
),
overflow: TextOverflow.ellipsis,
),
Expand All @@ -46,6 +49,7 @@ class FailedImportsContent extends StatelessWidget {
child: Text(
context.l10n.dontShowAgain,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: textColor),
),
),
],
Expand All @@ -55,7 +59,10 @@ class FailedImportsContent extends StatelessWidget {
itemCount: failedImports.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(failedImports[index]),
title: Text(
failedImports[index],
style: TextStyle(color: textColor),
),
);
},
),
Expand Down
7 changes: 2 additions & 5 deletions lib/local_audio/view/genre_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import '../../common/view/icons.dart';
import '../../constants.dart';
import '../../l10n/l10n.dart';
import '../../library/library_model.dart';
import '../../radio/radio_model.dart';
import '../../search/search_model.dart';
import '../../search/search_type.dart';
import '../local_audio_model.dart';
Expand Down Expand Up @@ -46,8 +45,6 @@ class _GenrePageState extends State<GenrePage> {

@override
Widget build(BuildContext context) {
final radioModel = di<RadioModel>();

return Scaffold(
resizeToAvoidBottomInset: isMobile ? false : null,
appBar: HeaderBar(
Expand All @@ -58,7 +55,7 @@ class _GenrePageState extends State<GenrePage> {
children: [
IconButton(
tooltip: context.l10n.searchForRadioStationsWithGenreName,
onPressed: () => radioModel.init().then((value) {
onPressed: () {
di<LibraryModel>().push(pageId: kSearchPageId);
di<SearchModel>()
..setTag(
Expand All @@ -67,7 +64,7 @@ class _GenrePageState extends State<GenrePage> {
..setAudioType(AudioType.radio)
..setSearchType(SearchType.radioTag)
..search();
}),
},
icon: Icon(Iconz.radio),
),
const SizedBox(
Expand Down
5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ void registerServicesAndViewModels({
),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<RadioService>(() => RadioService())
..registerLazySingleton<RadioService>(
RadioService.new,
dispose: (s) => s.dispose(),
)
..registerLazySingleton<GitHub>(() => GitHub())
..registerLazySingleton<ConnectivityModel>(
() => ConnectivityModel(
Expand Down
7 changes: 5 additions & 2 deletions lib/persistence_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:xdg_directories/xdg_directories.dart';

import 'common/logging.dart';
import 'constants.dart';
import 'common/data/audio.dart';

Expand Down Expand Up @@ -219,7 +220,8 @@ Future<Map<String, List<Audio>>> readAudioMap(String fileName) async {
} else {
return <String, List<Audio>>{};
}
} on Exception catch (_) {
} on Exception catch (e) {
printMessageInDebugMode(e);
return <String, List<Audio>>{};
}
}
Expand All @@ -246,7 +248,8 @@ Future<Map<String, String>> readStringMap(String fileName) async {
} else {
return <String, String>{};
}
} on Exception catch (_) {
} on Exception catch (e) {
printMessageInDebugMode(e);
return <String, String>{};
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/player/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:smtc_windows/smtc_windows.dart';
import '../common/data/audio.dart';
import '../common/data/mpv_meta_data.dart';
import '../common/data/player_state.dart';
import '../common/logging.dart';
import '../constants.dart';
import '../expose/expose_service.dart';
import '../extensions/string_x.dart';
Expand Down Expand Up @@ -234,7 +235,9 @@ class PlayerService {
imageUrl: audio?.imageUrl ?? audio?.albumArtUrl,
);
_firstPlay = false;
} on Exception catch (_) {}
} on Exception catch (e) {
printMessageInDebugMode(e);
}
}

Future<void> playOrPause() async {
Expand Down Expand Up @@ -807,7 +810,8 @@ class PlayerService {
} else {
return null;
}
} on Exception catch (_) {
} on Exception catch (e) {
printMessageInDebugMode(e);
return null;
}
}
Expand Down
31 changes: 16 additions & 15 deletions lib/radio/radio_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,39 @@ import 'radio_service.dart';

class RadioModel extends SafeChangeNotifier {
final RadioService _radioService;
StreamSubscription<bool>? _propertiesChangedSub;

RadioModel({required RadioService radioService})
: _radioService = radioService;

Future<void> clickStation(Audio? station) async {
if (station?.description != null) {
return _radioService.clickStation(station!.description!);
if (station?.uuid != null) {
return _radioService.clickStation(station!.uuid!);
}
}

bool showConnectSnackBar = true;
// The empty string is used so before the first check the UI does not overreact
String? _connectedHost = '';
String? get connectedHost => _connectedHost;
// TODO: make init a one time call...
Future<String?> init() async {
final oldHost = _connectedHost;
_connectedHost = await _radioService.init();
if (oldHost == _connectedHost) {
showConnectSnackBar = false;
}
notifyListeners();
return _connectedHost;
String? get connectedHost => _radioService.connectedHost;
Future<void> init() async {
await _radioService.init();
_propertiesChangedSub ??=
_radioService.propertiesChanged.listen((_) => notifyListeners());
}

Future<void> reconnect() async => _radioService.init();

RadioCollectionView _radioCollectionView = RadioCollectionView.stations;
RadioCollectionView get radioCollectionView => _radioCollectionView;
void setRadioCollectionView(RadioCollectionView value) {
if (value == _radioCollectionView) return;
_radioCollectionView = value;
notifyListeners();
}

@override
Future<void> dispose() async {
await _propertiesChangedSub?.cancel();
super.dispose();
}
}

enum RadioCollectionView {
Expand Down
Loading

0 comments on commit 873e411

Please sign in to comment.