Skip to content

Commit

Permalink
Add lastPlayed sorting for playlists list in library
Browse files Browse the repository at this point in the history
  • Loading branch information
Merlin Sievers committed Mar 23, 2024
1 parent d62f6ae commit 5280a35
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/models/playlist.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:harmonymusic/utils/helper.dart';
import 'package:hive/hive.dart';

import '../models/thumbnail.dart';

class PlaylistContent {
Expand Down Expand Up @@ -64,7 +67,20 @@ class Playlist {
this.title = title;
}

set updateLastPlayed() {
void updateLastPlayed() async {
lastPlayed = DateTime.now();
if (isPipedPlaylist) {
final box = await Hive.openBox('pipedPlaylistAdditionalMetadata');
var pipedMetadata = box.get(playlistId, defaultValue: {});
pipedMetadata['lastPlayed'] = lastPlayed;
box.put(playlistId, pipedMetadata);
await box.close();
} else {
final box = await Hive.openBox("LibraryPlaylists");
if (box.containsKey(playlistId)) {
box.put(playlistId, toJson());
}
await box.close();
}
}
}
2 changes: 1 addition & 1 deletion lib/ui/screens/Library/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class PlaylistNAlbumLibraryWidget extends StatelessWidget {
isSearchFeatureRequired: true,
itemCountTitle:
"${librplstCntrller.libraryPlaylists.length} ${"items".tr}",
requiredSortTypes: buildSortTypeSet(),
requiredSortTypes: buildSortTypeSet(false, false, true),
onSort: (type, ascending) {
librplstCntrller.onSort(type, ascending);
},
Expand Down
8 changes: 8 additions & 0 deletions lib/ui/screens/Library/library_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,27 @@ class LibraryPlaylistsController extends GetxController
})
.whereType<String>()
.toList();

final ppmBox = await Hive.openBox('pipedPlaylistAdditionalMetadata');
//add new playlist from cloud
for (dynamic playlist in res.response) {
if (!libPipedPlaylistsId.contains(playlist['id'])) {
DateTime? lastPlayed;
if (ppmBox.containsKey(playlist['id'])) {
lastPlayed = ppmBox.get(playlist['id'])['lastPlayed'];
}
final plst = Playlist(
title: playlist['name'],
playlistId: playlist['id'],
description: "Piped Playlist",
thumbnailUrl: playlist['thumbnail'],
isPipedPlaylist: true,
lastPlayed: lastPlayed,
);
libraryPlaylists.add(plst);
}
}
await ppmBox.close();

//remove playist if removed from cloud
for (Playlist playlist in libraryPlaylists.toList()) {
Expand Down
20 changes: 20 additions & 0 deletions lib/ui/widgets/sort_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ class SortWidget extends StatelessWidget {
},
))
: const SizedBox.shrink(),
requiredSortTypes.contains(SortType.RecentlyPlayed)
? Obx(() => IconButton(
color: controller.sortType.value == SortType.RecentlyPlayed
? Theme.of(context).textTheme.bodySmall!.color
: Theme.of(context).colorScheme.secondary,
icon: const Icon(Icons.history),
iconSize: 20,
splashRadius: 20,
visualDensity:
const VisualDensity(horizontal: -3, vertical: -3),
onPressed: () {
controller.onSortByRecentlyPlayed(onSort);
}
))
: const SizedBox.shrink(),
const Expanded(child: SizedBox()),
Obx(
() => IconButton(
Expand Down Expand Up @@ -366,6 +381,11 @@ class SortWidgetController extends GetxController {
onSort(sortType.value, isAscending.value);
}

void onSortByRecentlyPlayed(Function onSort) {
sortType.value = SortType.RecentlyPlayed;
onSort(sortType.value, isAscending.value);
}

void onAscendNDescend(Function onSort) {
isAscending.value = !isAscending.value;
onSort(sortType.value, isAscending.value);
Expand Down

0 comments on commit 5280a35

Please sign in to comment.