Skip to content

Commit

Permalink
feat: Implement export as M3U8
Browse files Browse the repository at this point in the history
  • Loading branch information
Losses committed Nov 23, 2024
1 parent cbb10e5 commit 07d7860
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
26 changes: 25 additions & 1 deletion lib/utils/context_menu/collection_item_context_menu.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:io';

import 'package:file_selector/file_selector.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:rune/utils/context_menu/utils/build_m3u8.dart';

import '../../utils/api/get_all_mixes.dart';
import '../../utils/api/add_item_to_mix.dart';
Expand Down Expand Up @@ -268,14 +271,35 @@ MenuFlyout buildLargeScreenCollectionItemContextMenu(
text: Text(S.of(context).exportM3u8),
onPressed: () async {
Flyout.of(context).close();

final Directory appDocumentsDir =
await getApplicationDocumentsDirectory();

final FileSaveLocation? path = await getSaveLocation(
suggestedName: '$title.m3u8',
initialDirectory: appDocumentsDir.path,
acceptedTypeGroups: const [
XTypeGroup(
label: 'playlist',
extensions: <String>['m3u8'],
)
],
);

if (path == null) return;

final playlist = await buildM3u8(type, id);

final file = File(path.path);
await file.writeAsString(playlist);
},
),
MenuFlyoutItem(
leading: const Icon(Symbols.wall_art),
text: Text(S.of(context).exportCoverWall),
onPressed: () async {
Flyout.of(context).close();
showExportCoverWallDialog(context, type, id);
showExportCoverWallDialog(context, type, title, id);
},
),
],
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/context_menu/utils/build_m3u8.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '../../../utils/query_list.dart';
import '../../../messages/all.dart';

import '../../build_query.dart';
import '../../api/query_mix_tracks.dart';

Future<String> buildM3u8(
CollectionType type,
int id,
) async {
final queries = await buildQuery(type, id);
final newItems = await queryMixTracks(
QueryList(queries),
0,
999,
);

final filePaths = newItems.map((x) => x.path).join('\r\n');

return filePaths;
}
15 changes: 11 additions & 4 deletions lib/utils/dialogs/export_cover_wall/export_cover_wall_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:ui' as ui;
import 'package:fluent_ui/fluent_ui.dart';
import 'package:file_selector/file_selector.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import 'package:path_provider/path_provider.dart';

import '../../../utils/l10n.dart';
import '../../../utils/dialogs/unavailable_dialog_on_band.dart';
Expand Down Expand Up @@ -90,12 +91,14 @@ List<SelectItem> frameItem(BuildContext context) => <SelectItem>[
class ExportCoverWallDialog extends StatefulWidget {
final CollectionType type;
final int id;
final String title;
final void Function(void) $close;

const ExportCoverWallDialog({
super.key,
required this.type,
required this.id,
required this.title,
required this.$close,
});

Expand Down Expand Up @@ -178,8 +181,12 @@ class ExportCoverWallDialogState extends State<ExportCoverWallDialog> {
: Colors.white,
);

final FileSaveLocation? result = await getSaveLocation(
suggestedName: 'cover_wall.png',
final Directory appDocumentsDir =
await getApplicationDocumentsDirectory();

final FileSaveLocation? path = await getSaveLocation(
suggestedName: '${widget.title}.png',
initialDirectory: appDocumentsDir.path,
acceptedTypeGroups: const [
XTypeGroup(
label: 'images',
Expand All @@ -188,13 +195,13 @@ class ExportCoverWallDialogState extends State<ExportCoverWallDialog> {
],
);

if (result == null) return;
if (path == null) return;

final pngBytes = await image.toByteData(
format: ui.ImageByteFormat.png,
);

File(result.path).writeAsBytesSync(
File(path.path).writeAsBytesSync(
pngBytes!.buffer.asInt8List(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import 'export_cover_wall_dialog.dart';
void showExportCoverWallDialog(
BuildContext context,
CollectionType type,
String title,
int id,
) async {
await $showModal<void>(
context,
(context, $close) => ExportCoverWallDialog(
type: type,
id: id,
title: title,
$close: $close,
),
dismissWithEsc: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:rune/utils/query_list.dart';

import '../../../../messages/all.dart';
import '../../../../widgets/cover_wall_background/utils/calculate_cover_wall_size.dart';
import '../../../../widgets/cover_wall_background/utils/generate_tiles_of_size.dart';
import '../../../../widgets/cover_wall_background/utils/calculate_cover_wall_size.dart';
import '../../../../widgets/cover_wall_background/utils/cover_wall_background_painter.dart';
import '../../../../widgets/cover_wall_background/constants/max_random_grid_config_size.dart';

Expand Down

0 comments on commit 07d7860

Please sign in to comment.