Skip to content

Commit

Permalink
code review fixes. Fixed some bugs with offline sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Komodo5197 committed Feb 17, 2024
1 parent 116e699 commit 255d2a7
Show file tree
Hide file tree
Showing 39 changed files with 395 additions and 438 deletions.
7 changes: 4 additions & 3 deletions lib/components/AddToPlaylistScreen/add_to_playlist_list.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:finamp/models/finamp_models.dart';
import 'package:finamp/services/isar_downloads.dart';
import 'package:finamp/services/downloads_service.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

Expand Down Expand Up @@ -55,8 +55,9 @@ class _AddToPlaylistListState extends State<AddToPlaylistList> {
playlistId: snapshot.data![index].id,
ids: [widget.itemToAddId],
);
final isarDownloads = GetIt.instance<IsarDownloads>();
unawaited(isarDownloads.resync(
final downloadsService =
GetIt.instance<DownloadsService>();
unawaited(downloadsService.resync(
DownloadStub.fromItem(
type: DownloadItemType.collection,
item: snapshot.data![index]),
Expand Down
27 changes: 12 additions & 15 deletions lib/components/AlbumScreen/download_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get_it/get_it.dart';

import '../../models/finamp_models.dart';
import '../../services/isar_downloads.dart';
import '../../services/downloads_service.dart';
import '../confirmation_prompt_dialog.dart';
import '../global_snackbar.dart';
import 'download_dialog.dart';
Expand All @@ -25,21 +25,21 @@ class DownloadButton extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final isarDownloads = GetIt.instance<IsarDownloads>();
final downloadsService = GetIt.instance<DownloadsService>();
var status =
ref.watch(isarDownloads.statusProvider((item, children))).value;
ref.watch(downloadsService.statusProvider((item, children))).value;
var isOffline = ref.watch(FinampSettingsHelper.finampSettingsProvider
.select((value) => value.valueOrNull?.isOffline)) ??
true;
String? parentTooltip;
if (status == DownloadItemStatus.incidental ||
status == DownloadItemStatus.incidentalOutdated) {
var parent = isarDownloads.getFirstRequiringItem(item);
var parent = downloadsService.getFirstRequiringItem(item);
if (parent != null) {
var parentName =
"${AppLocalizations.of(context)!.itemTypeSubtitle(parent.baseItemType.name)} ${parent.name}";
var parentName = AppLocalizations.of(context)!
.itemTypeSubtitle(parent.baseItemType.name, parent.name);
parentTooltip =
AppLocalizations.of(context)!.incidentalDownloadTip(parentName);
AppLocalizations.of(context)!.incidentalDownloadTooltip(parentName);
}
}
if (status == null) {
Expand Down Expand Up @@ -95,11 +95,10 @@ class DownloadButton extends ConsumerWidget {
abortButtonText:
AppLocalizations.of(context)!.deleteDownloadsAbortButtonText,
onConfirmed: () async {
final messenger = ScaffoldMessenger.of(context);
final text = AppLocalizations.of(context)!.downloadsDeleted;
try {
await isarDownloads.deleteDownload(stub: item);
messenger.showSnackBar(SnackBar(content: Text(text)));
await downloadsService.deleteDownload(stub: item);
GlobalSnackbar.message((scaffold) =>
AppLocalizations.of(scaffold)!.downloadsDeleted);
} catch (error) {
GlobalSnackbar.error(error);
}
Expand All @@ -113,11 +112,9 @@ class DownloadButton extends ConsumerWidget {
var syncButton = IconButton(
icon: const Icon(Icons.sync),
onPressed: () {
isarDownloads.resync(item, viewId);
downloadsService.resync(item, viewId);
},
color: status.outdated
? Colors.yellow
: null, // TODO yellow is hard to see in light mode
color: status.outdated ? Colors.orange : null,
);
if (isOffline) {
if (status.isRequired) {
Expand Down
38 changes: 21 additions & 17 deletions lib/components/AlbumScreen/download_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';

import '../../models/finamp_models.dart';
import '../../services/downloads_service.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/finamp_user_helper.dart';
import '../../services/isar_downloads.dart';
import '../../services/jellyfin_api_helper.dart';
import '../global_snackbar.dart';

Expand All @@ -18,20 +18,24 @@ class DownloadDialog extends StatefulWidget {
super.key,
required this.item,
required this.viewId,
required this.needDirectory,
required this.needTranscode,
required this.needsDirectory,
required this.needsTranscode,
required this.children,
});

final DownloadStub item;
final String viewId;
final bool needDirectory;
final bool needTranscode;
final bool needsDirectory;
final bool needsTranscode;
final List<BaseItemDto>? children;

@override
State<DownloadDialog> createState() => _DownloadDialogState();

/// Shows a download dialog box to the user. A download location box will be shown
/// if there is more than one location. A transcode setting box will be shown
/// if transcode downloads is set to ask. If neither is needed, the
/// download is initiated immediately with no dialog.
static Future<void> show(
BuildContext context, DownloadStub item, String? viewId) async {
if (viewId == null) {
Expand All @@ -48,15 +52,15 @@ class DownloadDialog extends StatefulWidget {
.length !=
1;
if (!needTranscode && !needDownload) {
final isarDownloads = GetIt.instance<IsarDownloads>();
final downloadsService = GetIt.instance<DownloadsService>();
var profile = FinampSettingsHelper
.finampSettings.shouldTranscodeDownloads ==
TranscodeDownloadsSetting.always
? FinampSettingsHelper.finampSettings.downloadTranscodingProfile
: DownloadProfile(transcodeCodec: FinampTranscodingCodec.original);
profile.downloadLocationId =
FinampSettingsHelper.finampSettings.internalSongDir.id;
unawaited(isarDownloads
unawaited(downloadsService
.addDownload(stub: item, viewId: viewId!, transcodeProfile: profile)
.then((value) => GlobalSnackbar.message(
(scaffold) => AppLocalizations.of(scaffold)!.downloadsAdded)));
Expand All @@ -76,8 +80,8 @@ class DownloadDialog extends StatefulWidget {
builder: (context) => DownloadDialog._build(
item: item,
viewId: viewId!,
needDirectory: needDownload,
needTranscode: needTranscode,
needsDirectory: needDownload,
needsTranscode: needTranscode,
children: children),
);
}
Expand Down Expand Up @@ -130,7 +134,7 @@ class _DownloadDialogState extends State<DownloadDialog> {
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (widget.needDirectory)
if (widget.needsDirectory)
DropdownButton<DownloadLocation>(
hint: Text(AppLocalizations.of(context)!.location),
isExpanded: true,
Expand All @@ -148,7 +152,7 @@ class _DownloadDialogState extends State<DownloadDialog> {
child: Text(e.name),
))
.toList()),
if (widget.needTranscode)
if (widget.needsTranscode)
DropdownButton<bool>(
hint: Text(AppLocalizations.of(context)!.transcodeHint),
isExpanded: true,
Expand Down Expand Up @@ -179,23 +183,23 @@ class _DownloadDialogState extends State<DownloadDialog> {
),
TextButton(
onPressed: (selectedDownloadLocation == null &&
widget.needDirectory) ||
(transcode == null && widget.needTranscode)
widget.needsDirectory) ||
(transcode == null && widget.needsTranscode)
? null
: () async {
Navigator.of(context).pop();
final isarDownloads = GetIt.instance<IsarDownloads>();
var profile = (widget.needTranscode
final downloadsService = GetIt.instance<DownloadsService>();
var profile = (widget.needsTranscode
? transcode
: FinampSettingsHelper
.finampSettings.shouldTranscodeDownloads ==
TranscodeDownloadsSetting.always)!
? transcodeProfile
: originalProfile;
profile.downloadLocationId = widget.needDirectory
profile.downloadLocationId = widget.needsDirectory
? selectedDownloadLocation!.id
: FinampSettingsHelper.finampSettings.internalSongDir.id;
await isarDownloads
await downloadsService
.addDownload(
stub: widget.item,
viewId: widget.viewId,
Expand Down
18 changes: 3 additions & 15 deletions lib/components/AlbumScreen/downloaded_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get_it/get_it.dart';

import '../../models/finamp_models.dart';
import '../../services/isar_downloads.dart';
import '../../services/downloads_service.dart';
import '../global_snackbar.dart';

class DownloadedIndicator extends ConsumerWidget {
Expand All @@ -18,9 +18,9 @@ class DownloadedIndicator extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final isarDownloads = GetIt.instance<IsarDownloads>();
final downloadsService = GetIt.instance<DownloadsService>();
AsyncValue<DownloadItemState?> status =
ref.watch(isarDownloads.stateProvider(item));
ref.watch(downloadsService.stateProvider(item));
if (status.hasValue) {
switch (status.valueOrNull) {
case null:
Expand Down Expand Up @@ -48,18 +48,6 @@ class DownloadedIndicator extends ConsumerWidget {
color: Theme.of(context).colorScheme.secondary,
size: size,
);
/*case DownloadItemState.deleting:
return Icon(
Icons.pause,
color: Colors.yellow,
size: size,
);
case DownloadItemState.paused:
return Icon(
Icons.pause,
color: Colors.yellow,
size: size,
);*/
}
} else if (status.hasError) {
GlobalSnackbar.error(status.error);
Expand Down
4 changes: 2 additions & 2 deletions lib/components/AlbumScreen/song_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'package:get_it/get_it.dart';
import 'package:mini_music_visualizer/mini_music_visualizer.dart';

import '../../services/audio_service_helper.dart';
import '../../services/downloads_service.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/isar_downloads.dart';
import '../../services/music_player_background_task.dart';
import '../../services/process_artist.dart';
import '../album_image.dart';
Expand Down Expand Up @@ -89,7 +89,7 @@ class _SongListTileState extends ConsumerState<SongListTile>
Widget build(BuildContext context) {
bool playable;
if (FinampSettingsHelper.finampSettings.isOffline) {
playable = ref.watch(GetIt.instance<IsarDownloads>()
playable = ref.watch(GetIt.instance<DownloadsService>()
.stateProvider(DownloadStub.fromItem(
type: DownloadItemType.song, item: widget.item))
.select((value) => value.value?.isComplete ?? false));
Expand Down
32 changes: 16 additions & 16 deletions lib/components/AlbumScreen/song_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import '../../models/jellyfin_models.dart';
import '../../screens/add_to_playlist_screen.dart';
import '../../screens/album_screen.dart';
import '../../services/audio_service_helper.dart';
import '../../services/downloads_service.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/isar_downloads.dart';
import '../../services/jellyfin_api_helper.dart';
import '../../services/player_screen_theme_provider.dart';
import '../PlayerScreen/album_chip.dart';
Expand Down Expand Up @@ -173,8 +173,8 @@ class _SongMenuState extends State<SongMenu> {
Theme.of(context).iconTheme.color ??
Colors.white;

final isarDownloads = GetIt.instance<IsarDownloads>();
final bool isDownloadRequired = isarDownloads
final downloadsService = GetIt.instance<DownloadsService>();
final bool isDownloadRequired = downloadsService
.getStatus(
DownloadStub.fromItem(
type: DownloadItemType.song, item: widget.item),
Expand Down Expand Up @@ -555,16 +555,16 @@ class _SongMenuState extends State<SongMenu> {
Text(AppLocalizations.of(context)!.goToAlbum),
enabled: widget.canGoToAlbum,
onTap: () async {
late BaseItemDto? album;
late BaseItemDto album;
try {
if (FinampSettingsHelper
.finampSettings.isOffline) {
final isarDownloads =
GetIt.instance<IsarDownloads>();
final downloadsService =
GetIt.instance<DownloadsService>();
album =
(await isarDownloads.getCollectionInfo(
(await downloadsService.getCollectionInfo(
id: widget.item.parentId!))!
.baseItem;
.baseItem!;
} else {
album = await _jellyfinApiHelper
.getItemById(widget.item.parentId!);
Expand All @@ -577,7 +577,7 @@ class _SongMenuState extends State<SongMenu> {
Navigator.pop(context);
Navigator.of(context).pushNamed(
AlbumScreen.routeName,
arguments: album!);
arguments: album);
}
},
),
Expand All @@ -597,10 +597,10 @@ class _SongMenuState extends State<SongMenu> {
try {
if (FinampSettingsHelper
.finampSettings.isOffline) {
final isarDownloads =
GetIt.instance<IsarDownloads>();
final downloadsService =
GetIt.instance<DownloadsService>();
artist =
(await isarDownloads.getCollectionInfo(
(await downloadsService.getCollectionInfo(
id: widget
.item.artistItems!.first.id))!
.baseItem!;
Expand Down Expand Up @@ -636,10 +636,10 @@ class _SongMenuState extends State<SongMenu> {
try {
if (FinampSettingsHelper
.finampSettings.isOffline) {
final isarDownloads =
GetIt.instance<IsarDownloads>();
final downloadsService =
GetIt.instance<DownloadsService>();
genre =
(await isarDownloads.getCollectionInfo(
(await downloadsService.getCollectionInfo(
id: widget
.item.genreItems!.first.id))!
.baseItem!;
Expand Down Expand Up @@ -679,7 +679,7 @@ class _SongMenuState extends State<SongMenu> {
type: DownloadItemType.song,
item: widget.item);
unawaited(
isarDownloads.deleteDownload(stub: item));
downloadsService.deleteDownload(stub: item));
if (mounted) {
Navigator.pop(context);
}
Expand Down
Loading

0 comments on commit 255d2a7

Please sign in to comment.