Skip to content

Commit

Permalink
Switched to background_downloader. Fully removed downloads_helper. Al…
Browse files Browse the repository at this point in the history
…bum/Playlist ordering now works.
  • Loading branch information
Komodo5197 committed Dec 20, 2023
1 parent 2444d93 commit 0d98815
Show file tree
Hide file tree
Showing 30 changed files with 937 additions and 1,098 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.unicornsonlsd.finamp"
minSdkVersion 21
minSdkVersion 24
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
14 changes: 5 additions & 9 deletions lib/components/AlbumScreen/album_screen_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'download_button.dart';
import 'song_list_tile.dart';
import 'playlist_name_edit_button.dart';

typedef BaseItemDtoCallback = void Function(BaseItemDto item);
typedef DeleteCallback = void Function(int index);

class AlbumScreenContent extends StatefulWidget {
const AlbumScreenContent({
Expand All @@ -32,13 +32,9 @@ class AlbumScreenContent extends StatefulWidget {
class _AlbumScreenContentState extends State<AlbumScreenContent> {
@override
Widget build(BuildContext context) {
void onDelete(BaseItemDto item) {
// This is pretty inefficient (has to search through whole list) but
// SongsSliverList gets passed some weird split version of children to
// handle multi-disc albums and it's 00:35 so I can't be bothered to get
// it to return an index
void onDelete(int index) {
setState(() {
widget.children.remove(item);
widget.children.removeAt(index);
});
}

Expand Down Expand Up @@ -134,7 +130,7 @@ class SongsSliverList extends StatefulWidget {
final List<BaseItemDto> childrenForList;
final List<BaseItemDto> childrenForQueue;
final BaseItemDto parent;
final BaseItemDtoCallback? onDelete;
final DeleteCallback? onDelete;

@override
State<SongsSliverList> createState() => _SongsSliverListState();
Expand Down Expand Up @@ -180,7 +176,7 @@ class _SongsSliverListState extends State<SongsSliverList> {
onDelete: () {
final item = removeItem();
if (widget.onDelete != null) {
widget.onDelete!(item);
widget.onDelete!(index + indexOffset);
}
},
isInPlaylist: widget.parent.type == "Playlist",
Expand Down
1 change: 0 additions & 1 deletion lib/components/AlbumScreen/download_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';

import '../../services/downloads_helper.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/finamp_user_helper.dart';
import '../../models/jellyfin_models.dart';
Expand Down
2 changes: 0 additions & 2 deletions lib/components/AlbumScreen/download_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';

import '../../services/finamp_settings_helper.dart';
import '../../services/downloads_helper.dart';
import '../../models/finamp_models.dart';
import '../../models/jellyfin_models.dart';
import '../../services/isar_downloads.dart';
Expand All @@ -25,7 +24,6 @@ class DownloadDialog extends StatefulWidget {
}

class _DownloadDialogState extends State<DownloadDialog> {
DownloadsHelper downloadsHelper = GetIt.instance<DownloadsHelper>();
DownloadLocation? selectedDownloadLocation;

@override
Expand Down
7 changes: 2 additions & 5 deletions lib/components/AlbumScreen/downloaded_indicator.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';

import '../../models/finamp_models.dart';
import '../../services/downloads_helper.dart';
import '../../services/download_update_stream.dart';
import '../../models/jellyfin_models.dart';
import '../../services/isar_downloads.dart';
import '../error_snackbar.dart';
Expand Down Expand Up @@ -49,7 +46,7 @@ class DownloadedIndicator extends ConsumerWidget {
color: Theme.of(context).colorScheme.secondary,
size: size,
);
case DownloadItemState.deleting:
/*case DownloadItemState.deleting:
return Icon(
Icons.pause,
color: Colors.yellow,
Expand All @@ -60,7 +57,7 @@ class DownloadedIndicator extends ConsumerWidget {
Icons.pause,
color: Colors.yellow,
size: size,
);
);*/
}
} else if (status.hasError) {
errorSnackbar(status.error, context);
Expand Down
1 change: 0 additions & 1 deletion lib/components/ArtistScreen/artist_play_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import '../../services/isar_downloads.dart';
import '../../services/jellyfin_api_helper.dart';
import '../../services/audio_service_helper.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/downloads_helper.dart';

class ArtistPlayButton extends StatefulWidget {
const ArtistPlayButton({
Expand Down
1 change: 0 additions & 1 deletion lib/components/ArtistScreen/artist_shuffle_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import '../../services/isar_downloads.dart';
import '../../services/jellyfin_api_helper.dart';
import '../../services/audio_service_helper.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/downloads_helper.dart';

class ArtistShuffleButton extends StatefulWidget {
const ArtistShuffleButton({
Expand Down
8 changes: 5 additions & 3 deletions lib/components/DownloadsErrorScreen/download_error_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/*
TODO reimplement somthing?
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand All @@ -24,8 +25,8 @@ class _DownloadErrorListState extends State<DownloadErrorList> {
@override
void initState() {
super.initState();
downloadErrorListFuture =
downloadsHelper.getDownloadsWithStatus(DownloadTaskStatus.failed);
//downloadErrorListFuture =
// downloadsHelper.getDownloadsWithStatus(DownloadTaskStatus.failed);
}
@override
Expand Down Expand Up @@ -80,3 +81,4 @@ class _DownloadErrorListState extends State<DownloadErrorList> {
);
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*
TODO reimplement somthing?
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -42,4 +44,4 @@ class DownloadErrorListTile extends StatelessWidget {
// ),
);
}
}
}*/
1 change: 0 additions & 1 deletion lib/components/DownloadsScreen/album_file_size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

import '../../models/finamp_models.dart';
import '../../services/downloads_helper.dart';
import '../../services/isar_downloads.dart';

class AlbumFileSize extends StatelessWidget {
Expand Down
14 changes: 6 additions & 8 deletions lib/components/DownloadsScreen/download_error_screen_button.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';

import '../../services/downloads_helper.dart';
import '../../screens/downloads_error_screen.dart';

class DownloadErrorScreenButton extends StatefulWidget {
Expand All @@ -15,19 +13,19 @@ class DownloadErrorScreenButton extends StatefulWidget {
}

class _DownloadErrorScreenButtonState extends State<DownloadErrorScreenButton> {
DownloadsHelper downloadsHelper = GetIt.instance<DownloadsHelper>();
late Future<List<DownloadTask>?> downloadErrorScreenButtonFuture;
//late Future<List<DownloadTask>?> downloadErrorScreenButtonFuture;

@override
void initState() {
super.initState();
downloadErrorScreenButtonFuture =
downloadsHelper.getDownloadsWithStatus(DownloadTaskStatus.failed);
//downloadErrorScreenButtonFuture =
// downloadsHelper.getDownloadsWithStatus(DownloadTaskStatus.failed);
}

@override
Widget build(BuildContext context) {
return FutureBuilder<List<DownloadTask>?>(
return Text("TODO"); // TODO do something here
/*return FutureBuilder<List<DownloadTask>?>(
future: downloadErrorScreenButtonFuture,
builder: (context, snapshot) {
return IconButton(
Expand All @@ -42,6 +40,6 @@ class _DownloadErrorScreenButtonState extends State<DownloadErrorScreenButton> {
Navigator.of(context).pushNamed(DownloadsErrorScreen.routeName),
);
},
);
);*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';

import '../../services/downloads_helper.dart';
import '../../services/finamp_settings_helper.dart';
import '../../services/isar_downloads.dart';

Expand Down
3 changes: 0 additions & 3 deletions lib/components/DownloadsScreen/downloaded_albums_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

import '../../models/finamp_models.dart';
import '../../services/downloads_helper.dart';
import '../../models/jellyfin_models.dart';
import '../../services/isar_downloads.dart';
import '../album_image.dart';
Expand All @@ -20,8 +19,6 @@ class DownloadedAlbumsList extends StatefulWidget {
}

class _DownloadedAlbumsListState extends State<DownloadedAlbumsList> {
final DownloadsHelper downloadsHelper = GetIt.instance<DownloadsHelper>();

final JellyfinApiHelper jellyfinApiHelper = JellyfinApiHelper();
final IsarDownloads isarDownloads = GetIt.instance<IsarDownloads>();

Expand Down
104 changes: 25 additions & 79 deletions lib/components/DownloadsScreen/downloads_overview.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:finamp/services/downloads_helper.dart';
import 'package:background_downloader/background_downloader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';
import 'package:background_downloader/background_downloader.dart';

import '../../models/finamp_models.dart';
import '../../services/download_update_stream.dart';
import '../../services/isar_downloads.dart';
import '../error_snackbar.dart';

Expand All @@ -20,90 +19,37 @@ class DownloadsOverview extends StatefulWidget {
}

class _DownloadsOverviewState extends State<DownloadsOverview> {
late Future<List<DownloadTask>?> _downloadsOverviewFuture;
final _downloadUpdateStream = GetIt.instance<DownloadUpdateStream>();
late Future<List<int?>> _downloadsOverviewFuture;
final isarDownloads = GetIt.instance<IsarDownloads>();

Map<String, DownloadTaskStatus>? _downloadTaskStatuses;

final Map<DownloadTaskStatus, int> _downloadCount = {
DownloadTaskStatus.undefined: 0,
DownloadTaskStatus.enqueued: 0,
DownloadTaskStatus.running: 0,
DownloadTaskStatus.complete: 0,
DownloadTaskStatus.failed: 0,
DownloadTaskStatus.canceled: 0,
DownloadTaskStatus.paused: 0,
};

bool _initialCountDone = false;

@override
void initState() {
super.initState();
_downloadsOverviewFuture = FlutterDownloader.loadTasks();

// Like in DownloadedIndicator, we use our own listener instead of a
// StreamBuilder to ensure that we capture all events.
_downloadUpdateStream.stream.listen((event) {
if (_downloadTaskStatuses != null &&
_downloadTaskStatuses!.containsKey(event.id) &&
_downloadTaskStatuses![event.id] != event.status) {
setState(() {
_downloadCount[_downloadTaskStatuses![event.id]!] =
_downloadCount[_downloadTaskStatuses![event.id]]! - 1;
_downloadCount[event.status] = _downloadCount[event.status]! + 1;
_downloadTaskStatuses![event.id] = event.status;
});
}
});
// TODO figure out what to track, how to do it.
_downloadsOverviewFuture = Future.wait([
Future.value(-123),
Future.value(
isarDownloads.getDownloadCount(state: DownloadItemState.failed)),
Future.value(-123),
FileDownloader().allTasks().then((value) => value?.length)
]);
}

@override
Widget build(BuildContext context) {
return FutureBuilder<List<DownloadTask>?>(
return FutureBuilder<List<int?>>(
future: _downloadsOverviewFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
_downloadTaskStatuses ??= Map.fromEntries(
snapshot.data!.map((e) => MapEntry(e.taskId, e.status)));

if (!_initialCountDone) {
// Switch cases don't work for some reason
for (var element in snapshot.data!) {
if (element.status == DownloadTaskStatus.undefined) {
_downloadCount[DownloadTaskStatus.undefined] =
_downloadCount[DownloadTaskStatus.undefined]! + 1;
} else if (element.status == DownloadTaskStatus.enqueued) {
_downloadCount[DownloadTaskStatus.enqueued] =
_downloadCount[DownloadTaskStatus.enqueued]! + 1;
} else if (element.status == DownloadTaskStatus.running) {
_downloadCount[DownloadTaskStatus.running] =
_downloadCount[DownloadTaskStatus.running]! + 1;
} else if (element.status == DownloadTaskStatus.complete) {
_downloadCount[DownloadTaskStatus.complete] =
_downloadCount[DownloadTaskStatus.complete]! + 1;
} else if (element.status == DownloadTaskStatus.failed) {
_downloadCount[DownloadTaskStatus.failed] =
_downloadCount[DownloadTaskStatus.failed]! + 1;
} else if (element.status == DownloadTaskStatus.canceled) {
_downloadCount[DownloadTaskStatus.canceled] =
_downloadCount[DownloadTaskStatus.canceled]! + 1;
} else if (element.status == DownloadTaskStatus.paused) {
_downloadCount[DownloadTaskStatus.paused] =
_downloadCount[DownloadTaskStatus.paused]! + 1;
}
}
_initialCountDone = true;
}

// We have to awkwardly get two strings like this because Flutter's
// internationalisation stuff doesn't support multiple plurals.
// https://github.com/flutter/flutter/issues/86906
final downloadedItemsString = AppLocalizations.of(context)!
.downloadedItemsCount(isarDownloads.getDownloadCount(DownloadItemType.song));
.downloadedItemsCount(
isarDownloads.getDownloadCount(type: DownloadItemType.song));
final downloadedImagesString = AppLocalizations.of(context)!
.downloadedImagesCount(isarDownloads.getDownloadCount(DownloadItemType.image));
.downloadedImagesCount(
isarDownloads.getDownloadCount(type: DownloadItemType.image));

return Card(
child: Padding(
Expand All @@ -119,7 +65,7 @@ class _DownloadsOverviewState extends State<DownloadsOverview> {
children: [
AutoSizeText(
AppLocalizations.of(context)!
.downloadCount(snapshot.data!.length),
.downloadCount(-123),
style: const TextStyle(fontSize: 28),
maxLines: 1,
),
Expand All @@ -138,23 +84,23 @@ class _DownloadsOverviewState extends State<DownloadsOverview> {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
AppLocalizations.of(context)!.dlComplete(
_downloadCount[DownloadTaskStatus.complete]!),
AppLocalizations.of(context)!
.dlComplete(snapshot.data?[0] ?? 0),
style: const TextStyle(color: Colors.green),
),
Text(
AppLocalizations.of(context)!.dlFailed(
_downloadCount[DownloadTaskStatus.failed]!),
AppLocalizations.of(context)!
.dlFailed(snapshot.data?[1] ?? 0),
style: const TextStyle(color: Colors.red),
),
Text(
AppLocalizations.of(context)!.dlEnqueued(
_downloadCount[DownloadTaskStatus.enqueued]!),
AppLocalizations.of(context)!
.dlEnqueued(snapshot.data?[2] ?? 0),
style: const TextStyle(color: Colors.grey),
),
Text(
AppLocalizations.of(context)!.dlRunning(
_downloadCount[DownloadTaskStatus.running]!),
AppLocalizations.of(context)!
.dlRunning(snapshot.data?[3] ?? 0),
style: const TextStyle(color: Colors.grey),
),
],
Expand Down
Loading

0 comments on commit 0d98815

Please sign in to comment.