Skip to content

Commit

Permalink
Initial version of queue history and restore page.
Browse files Browse the repository at this point in the history
  • Loading branch information
Komodo5197 committed Dec 4, 2023
1 parent e913e6e commit bd88212
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 57 deletions.
7 changes: 7 additions & 0 deletions lib/components/MusicScreen/music_screen_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:finamp/screens/queue_restore_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';
Expand Down Expand Up @@ -80,6 +81,12 @@ class MusicScreenDrawer extends StatelessWidget {
onTap: () => Navigator.of(context)
.pushNamed(LogsScreen.routeName),
),
ListTile(
leading: const Icon(Icons.auto_delete),
title: Text(AppLocalizations.of(context)!.queuesScreen),
onTap: () => Navigator.of(context)
.pushNamed(QueueRestoreScreen.routeName),
),
ListTile(
leading: const Icon(Icons.settings),
title: Text(AppLocalizations.of(context)!.settings),
Expand Down
37 changes: 37 additions & 0 deletions lib/components/queue_restore_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';

import '../models/finamp_models.dart';
import '../services/queue_service.dart';
import 'error_snackbar.dart';

class QueueRestoreTile extends StatelessWidget {
const QueueRestoreTile({Key? key, required this.info}) : super(key: key);

final FinampStorableQueueInfo info;

@override
Widget build(BuildContext context) {
final _queuesBox = Hive.box<FinampStorableQueueInfo>("Queues");
final _queueService = GetIt.instance<QueueService>();
int itemCount = info.queue.length + info.nextUp.length + ((info.currentTrack == null)?0:1);

return ListTile(
// TODO attempt to load current track album cover here
title: Text('Queue contaning $itemCount songs.'),
subtitle: Text('Created '+DateTime.fromMillisecondsSinceEpoch(info.creation).toString()),// TODO format date better?
trailing: IconButton(
icon: const Icon(Icons.delete),//TODO change to button with word restore or better icon
onPressed: () async {
var latest = _queuesBox.get("latest");
if ( latest != null ){
await _queuesBox.put(latest.creation.toString(), latest);
}
await _queueService.loadSavedQueue(info).catchError((x) => errorSnackbar(x,context));
// TODO add some sort of loading spinner on click
Navigator.of(context).pop();
}),
);
}
}
4 changes: 4 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,9 @@
"shuffleAllQueueSource": "Shuffle All",
"@shuffleAllQueueSource": {
"description": "Title for the queue source when the user is shuffling all tracks. Should be capitalized (if applicable) to be more recognizable throughout the UI"
},
"queuesScreen": "Restore Now Playing",
"@queuesScreen": {
"description": "Title for the screen where older now playing queues can be restored"
}
}
10 changes: 6 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:ui';
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:finamp/screens/playback_history_screen.dart';
import 'package:finamp/screens/queue_restore_screen.dart';
import 'package:finamp/services/finamp_settings_helper.dart';
import 'package:finamp/services/finamp_user_helper.dart';
import 'package:finamp/services/playback_history_service.dart';
Expand Down Expand Up @@ -97,8 +98,8 @@ void main() async {
runApp(const Finamp());

// Place after runApp so errors are not fatal
QueueService queueService = GetIt.instance<QueueService>();
await queueService.loadSavedQueue();
//QueueService queueService = GetIt.instance<QueueService>();
//await queueService.loadSavedQueue();
}
}

Expand Down Expand Up @@ -170,6 +171,7 @@ Future<void> setupHive() async {
Hive.registerAdapter(ThemeModeAdapter());
Hive.registerAdapter(LocaleAdapter());
Hive.registerAdapter(FinampLoopModeAdapter());
Hive.registerAdapter(FinampStorableQueueInfoAdapter());
await Future.wait([
Hive.openBox<DownloadedParent>("DownloadedParents"),
Hive.openBox<DownloadedSong>("DownloadedItems"),
Expand All @@ -180,8 +182,7 @@ Future<void> setupHive() async {
Hive.openBox<DownloadedImage>("DownloadedImages"),
Hive.openBox<String>("DownloadedImageIds"),
Hive.openBox<ThemeMode>("ThemeMode"),
Hive.openBox<List<String>>("Queues"),
Hive.openBox<int>("QueuePosition"),
Hive.openBox<FinampStorableQueueInfo>("Queues"),
Hive.openBox<Locale?>(LocaleHelper.boxName),
]);

Expand Down Expand Up @@ -320,6 +321,7 @@ class Finamp extends StatelessWidget {
PlaybackHistoryScreen.routeName: (context) =>
const PlaybackHistoryScreen(),
LogsScreen.routeName: (context) => const LogsScreen(),
QueueRestoreScreen.routeName: (context) => const QueueRestoreScreen(),
SettingsScreen.routeName: (context) =>
const SettingsScreen(),
TranscodingSettingsScreen.routeName: (context) =>
Expand Down
45 changes: 44 additions & 1 deletion lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:io';

import 'package:finamp/services/queue_service.dart';
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 @@ -801,3 +800,47 @@ class FinampHistoryItem {
@HiveField(2)
DateTime? endTime;
}

@HiveType(typeId: 61)
class FinampStorableQueueInfo {
FinampStorableQueueInfo({
required this.previousTracks,
required this.currentTrack,
required this.currentTrackSeek,
required this.nextUp,
required this.queue,
required this.creation,
});

FinampStorableQueueInfo.fromQueueInfo(FinampQueueInfo info, int? seek):
previousTracks=info.previousTracks.map<String>((track) => track.item.extras?["itemJson"]["Id"]).toList(),
currentTrack=info.currentTrack?.item.extras?["itemJson"]["Id"],
currentTrackSeek=seek,
nextUp=info.nextUp.map<String>((track) => track.item.extras?["itemJson"]["Id"]).toList(),
queue=info.queue.map<String>((track) => track.item.extras?["itemJson"]["Id"]).toList(),
creation=DateTime.now().millisecondsSinceEpoch
;

@HiveField(0)
List<String> previousTracks;

@HiveField(1)
String? currentTrack;

@HiveField(2)
int? currentTrackSeek;

@HiveField(3)
List<String> nextUp;

@HiveField(4)
List<String> queue;

@HiveField(5)
// timestamp, milliseconds since epoch
int creation;

String toString(){
return "previous:$previousTracks current:$currentTrack seek:$currentTrackSeek next:$nextUp queue:$queue";
}
}
7 changes: 7 additions & 0 deletions lib/screens/music_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:finamp/screens/playback_history_screen.dart';
import 'package:finamp/services/queue_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
Expand Down Expand Up @@ -36,10 +37,12 @@ class _MusicScreenState extends State<MusicScreen>
final _musicScreenLogger = Logger("MusicScreen");

TabController? _tabController;
bool _queueInitialized=false;

final _audioServiceHelper = GetIt.instance<AudioServiceHelper>();
final _finampUserHelper = GetIt.instance<FinampUserHelper>();
final _jellyfinApiHelper = GetIt.instance<JellyfinApiHelper>();
final _queueService = GetIt.instance<QueueService>();

void _stopSearching() {
setState(() {
Expand Down Expand Up @@ -161,6 +164,10 @@ class _MusicScreenState extends State<MusicScreen>

@override
Widget build(BuildContext context) {
if ( ! _queueInitialized ){
_queueInitialized=true;
_queueService.performInitialQueueLoad().catchError((x) => errorSnackbar(x,context));
}
if (_tabController == null) {
_buildTabController();
}
Expand Down
39 changes: 39 additions & 0 deletions lib/screens/queue_restore_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive/hive.dart';

import '../components/queue_restore_tile.dart';
import '../models/finamp_models.dart';

class QueueRestoreScreen extends StatelessWidget {
const QueueRestoreScreen({Key? key}) : super(key: key);

static const routeName = "/queues";


@override
Widget build(BuildContext context) {
final _queuesBox = Hive.box<FinampStorableQueueInfo>("Queues");
var queueMap = _queuesBox.toMap();
queueMap.remove("latest");
var queueList = queueMap.values.toList();
queueList.sort((x,y)=>x.creation-y.creation);

return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.queuesScreen),
),
body: Scrollbar(
child: ListView.builder(
itemCount: queueList.length,
reverse: true,
itemBuilder: (context, index) {
return QueueRestoreTile(
info: queueList.reversed.elementAt(index));
},
),
),
);
}
}
Loading

0 comments on commit bd88212

Please sign in to comment.