Skip to content

Commit

Permalink
chore: register all services and viewmodels lazily (#889)
Browse files Browse the repository at this point in the history
* chore: register all services and viewmodels lazily

* chore: extract to function

* chore: kAppTitle

* fix: ??=
  • Loading branch information
Feichtmeier authored Aug 23, 2024
1 parent fd2d6da commit b3306ba
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 220 deletions.
35 changes: 8 additions & 27 deletions lib/app/app_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/widgets.dart';
import 'package:github/github.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

import '../common/view/snackbars.dart';
Expand All @@ -9,6 +8,7 @@ import '../settings/settings_service.dart';

class AppModel extends SafeChangeNotifier {
AppModel({
required String appVersion,
required SettingsService settingsService,
required GitHub gitHub,
required bool allowManualUpdates,
Expand All @@ -17,7 +17,8 @@ class AppModel extends SafeChangeNotifier {
?.toLowerCase(),
_gitHub = gitHub,
_allowManualUpdates = allowManualUpdates,
_settingsService = settingsService;
_settingsService = settingsService,
_version = appVersion;

final GitHub _gitHub;
final SettingsService _settingsService;
Expand All @@ -42,26 +43,14 @@ class AppModel extends SafeChangeNotifier {
notifyListeners();
}

String? _appName;
String? get appName => _appName;
String? _packageName;
String? get packageName => _packageName;
String? _version;
final String _version;
String? get version => _version;
String? _buildNumber;
String? get buildNumber => _buildNumber;

Future<void> disposePatchNotes() async {
if (_version != null) {
return _settingsService.disposePatchNotes(_version!);
} else {
return Future.error('unknown version');
}
}
Future<void> disposePatchNotes() async =>
_settingsService.disposePatchNotes(_version);

bool recentPatchNotesDisposed() => _version == null
? false
: _settingsService.recentPatchNotesDisposed(_version!);
bool recentPatchNotesDisposed() =>
_settingsService.recentPatchNotesDisposed(_version);
bool? _updateAvailable;
bool? get updateAvailable => _updateAvailable;
String? _onlineVersion;
Expand Down Expand Up @@ -101,14 +90,6 @@ class AppModel extends SafeChangeNotifier {
return versionCells[0] * 100000 + versionCells[1] * 1000 + versionCells[2];
}

Future<void> init() async {
final packageInfo = await PackageInfo.fromPlatform();
_appName = packageInfo.appName;
_packageName = packageInfo.packageName;
_version = packageInfo.version;
_buildNumber = packageInfo.buildNumber;
}

Future<String?> getOnlineVersion() async {
final release = await _gitHub.repositories
.listReleases(RepositorySlug.full(kGitHubShortLink))
Expand Down
6 changes: 2 additions & 4 deletions lib/app/connectivity_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class ConnectivityModel extends SafeChangeNotifier {

return _connectivity
.checkConnectivity()
.then(
_updateConnectivity,
)
.catchError((e) => _updateConnectivity);
.then(_updateConnectivity)
.catchError((_) => _updateConnectivity(ConnectivityResult.values));
}

bool get isOnline => _connectivity.isOnline(_result);
Expand Down
2 changes: 2 additions & 0 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../../external_path/external_path_service.dart';
import '../../l10n/l10n.dart';
import '../../library/library_model.dart';
import '../../settings/settings_model.dart';
import '../connectivity_model.dart';
import 'scaffold.dart';
import 'splash_screen.dart';
import 'system_tray.dart';
Expand Down Expand Up @@ -77,6 +78,7 @@ class _MusicPodAppState extends State<_MusicPodApp>
}

Future<bool> _init() async {
await di<ConnectivityModel>().init();
await di<LibraryModel>().init();
if (!mounted) return false;
di<ExternalPathService>().init();
Expand Down
2 changes: 1 addition & 1 deletion lib/app/view/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class _MusicPodScaffoldState extends State<MusicPodScaffold> {
super.initState();
final appModel = di<AppModel>();
appModel
.checkForUpdate(di<ConnectivityModel>().isOnline, context)
.checkForUpdate(di<ConnectivityModel>().isOnline == true, context)
.then((_) {
if (!appModel.recentPatchNotesDisposed() && mounted) {
showDialog(
Expand Down
1 change: 0 additions & 1 deletion lib/common/view/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ ThemeData? yaruLightWithTweaks(YaruThemeData yaru) {
),
);
}
// TODO: MOVE TO THEME EXTENSIONS where possible!

const yaruFixDarkDividerColor = Color.fromARGB(19, 255, 255, 255);

Expand Down
10 changes: 0 additions & 10 deletions lib/extensions/media_file_x.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:io';

/// TODO: either guarantee that downloads are saved with the correct extension or manage to detect files without file extensions via magic bytes
extension MediaFileX on File {
bool get isValidMedia => path.isValidPath;
}
Expand All @@ -11,14 +9,6 @@ extension MediaFileSystemEntityX on FileSystemEntity {
}

extension _ValidPathX on String {
/// | Bytes | extension | Description |
/// |:--|:--|:--|
/// |`49 44 33`| `.mp3` |MP3 file with an ID3v2 container|
/// |`FF FB / FF F3 / FF F2`|`.mp3`|MPEG-1 Layer 3 file without an ID3 tag or with an ID3v1 tag (which is appended at the end of the file)|
/// |`4F 67 67 53`| `.ogg`, `.oga`, `.ogv` | Ogg, an open source media container format |
/// |`66 4C 61 43`| `.flac` | Free Lossless Audio Codec |
/// |`66 74 79 70 69 73 6F 6D`| `mp4`| ISO Base Media file (MPEG-4)|
/// |`66 74 79 70 4D 53 4E 56`| `mp4`| MPEG-4 video file|
bool get isValidPath => _validMediaExtensions.any((e) => endsWith(e));
}

Expand Down
Loading

0 comments on commit b3306ba

Please sign in to comment.