Skip to content

Commit

Permalink
Lookup audios by pageId
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Jan 5, 2024
1 parent 92d09da commit 8c50468
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 87 deletions.
10 changes: 5 additions & 5 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const kAudioQueueThreshHold = 100;
const kMainPageIconPadding = EdgeInsets.only(right: 4.0);

const kLikedAudiosFileName = 'likedAudios.json';
const kLikedAudios = 'likedAudios';
const kTagFavsFileName = 'tagFavs.json';
const kLastFav = 'lastFav';
const kPlaylistsFileName = 'playlists.json';
Expand All @@ -107,7 +106,6 @@ const kLocalAudioCacheFileName = 'localaudiocache.json';
const kDownloads = 'downloads.json';
const kFeedsWithDownloads = 'feedswithdownloads.json';
const kLocalAudioCache = 'localAudioCache';
const kLocalAudio = 'localAudio';
const kUseLocalAudioCache = 'cacheSuggestionDisposed';
const kCreateCacheLimit = 1000;
const kDirectoryProperty = 'directory';
Expand All @@ -123,9 +121,11 @@ const kPodcastIndex = 'podcastIndex';
const kNeverShowImportFails = 'neverShowImportFails';
const kLastCountryCode = 'lastCountryCode';
const kSearchResult = 'searchResult';
const kPodcasts = 'podcasts';
const kRadio = 'radio';
const kNewPlaylist = 'newPlaylist';
const kLocalAudioPageId = 'localAudio';
const kPodcastsPageId = 'podcasts';
const kRadioPageId = 'radio';
const kNewPlaylistPageId = 'newPlaylist';
const kLikedAudiosPageId = 'likedAudios';

const shops = <String, String>{
'https://us.7digital.com/': '7digital',
Expand Down
3 changes: 1 addition & 2 deletions lib/src/app/master_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class MasterDetailPage extends StatelessWidget {
final item = masterItems[index];

return MasterTile(
audios: item.content.$2,
pageId: item.content.$1,
pageId: item.pageId,
libraryModel: libraryModel,
selected: selected,
title: item.titleBuilder(context),
Expand Down
7 changes: 2 additions & 5 deletions lib/src/app/master_item.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import 'package:flutter/widgets.dart';

import '../../data.dart';

class MasterItem {
MasterItem({
required this.titleBuilder,
this.subtitleBuilder,
required this.pageBuilder,
this.iconBuilder,
required this.content,
required this.pageId,
});

final WidgetBuilder titleBuilder;
final WidgetBuilder? subtitleBuilder;
final WidgetBuilder pageBuilder;
final Widget Function(BuildContext context, bool selected)? iconBuilder;
// TODO: lookup Set for pageID instead of copying it
final (String, Set<Audio>) content;
final String pageId;
}
18 changes: 9 additions & 9 deletions lib/src/app/master_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ List<MasterItem> createMasterItems({
iconBuilder: (context, selected) => LocalAudioPageIcon(
selected: selected,
),
content: (kLocalAudio, {}),
pageId: kLocalAudioPageId,
),
MasterItem(
titleBuilder: (context) => Text(context.l10n.radio),
Expand All @@ -39,7 +39,7 @@ List<MasterItem> createMasterItems({
iconBuilder: (context, selected) => RadioPageIcon(
selected: selected,
),
content: (kRadio, {}),
pageId: kRadioPageId,
),
MasterItem(
titleBuilder: (context) => Text(context.l10n.podcasts),
Expand All @@ -52,17 +52,17 @@ List<MasterItem> createMasterItems({
iconBuilder: (context, selected) => PodcastsPageIcon(
selected: selected,
),
content: (kPodcasts, {}),
pageId: kPodcastsPageId,
),
MasterItem(
iconBuilder: (context, selected) => Icon(Iconz().plus),
titleBuilder: (context) => Text(context.l10n.playlistDialogTitleNew),
pageBuilder: (context) => const SizedBox.shrink(),
content: (kNewPlaylist, {}),
pageId: kNewPlaylistPageId,
),
MasterItem(
titleBuilder: (context) => Text(context.l10n.likedSongs),
content: (kLikedAudios, libraryModel.likedAudios),
pageId: kLikedAudiosPageId,
pageBuilder: (context) => LikedAudioPage(
onTextTap: onTextTap,
likedLocalAudios: libraryModel.likedAudios,
Expand All @@ -75,7 +75,7 @@ List<MasterItem> createMasterItems({
MasterItem(
titleBuilder: (context) => Text(playlist.key),
subtitleBuilder: (context) => Text(context.l10n.playlist),
content: (playlist.key, playlist.value),
pageId: playlist.key,
pageBuilder: (context) => PlaylistPage(
onTextTap: onTextTap,
playlist: playlist,
Expand All @@ -99,7 +99,7 @@ List<MasterItem> createMasterItems({
subtitleBuilder: (context) => Text(
podcast.value.firstOrNull?.artist ?? context.l10n.podcast,
),
content: (podcast.key, podcast.value),
pageId: podcast.key,
pageBuilder: (context) => PodcastPage(
pageId: podcast.key,
title: podcast.value.firstOrNull?.album ??
Expand All @@ -125,7 +125,7 @@ List<MasterItem> createMasterItems({
),
subtitleBuilder: (context) =>
Text(album.value.firstOrNull?.artist ?? context.l10n.album),
content: (album.key, album.value),
pageId: album.key,
pageBuilder: (context) => AlbumPage(
onTextTap: onTextTap,
album: album.value,
Expand All @@ -145,7 +145,7 @@ List<MasterItem> createMasterItems({
subtitleBuilder: (context) {
return Text(context.l10n.station);
},
content: (station.key, station.value),
pageId: station.key,
pageBuilder: (context) => isOnline
? StationPage(
isStarred: true,
Expand Down
149 changes: 89 additions & 60 deletions lib/src/common/master_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import 'package:yaru_widgets/yaru_widgets.dart';
import '../../build_context_x.dart';
import '../../common.dart';
import '../../constants.dart';
import '../../data.dart';
import '../../l10n.dart';
import '../../library.dart';
import '../../player.dart';

class MasterTile extends StatefulWidget {
class MasterTile extends StatelessWidget {
const MasterTile({
super.key,
this.selected,
Expand All @@ -20,7 +19,6 @@ class MasterTile extends StatefulWidget {
this.trailing,
required this.libraryModel,
required this.pageId,
required this.audios,
});

final bool? selected;
Expand All @@ -30,51 +28,108 @@ class MasterTile extends StatefulWidget {
final Widget? trailing;
final LibraryModel libraryModel;
final String pageId;
final Set<Audio> audios;

@override
State<MasterTile> createState() => _MasterTileState();
}

class _MasterTileState extends State<MasterTile> {
var _hovered = false;

@override
Widget build(BuildContext context) {
final yaruMasterTile = Padding(
padding: widget.pageId == kLocalAudio
padding: pageId == kLocalAudioPageId
? const EdgeInsets.only(top: 5)
: EdgeInsets.zero,
child: YaruMasterTile(
title: widget.title,
onTap: widget.pageId == kNewPlaylist
title: title,
onTap: pageId == kNewPlaylistPageId
? () => showDialog(
context: context,
builder: (context) {
return PlaylistDialog(
playlistName: context.l10n.createNewPlaylist,
allowCreate: true,
libraryModel: widget.libraryModel,
libraryModel: libraryModel,
);
},
)
: null,
selected: widget.selected,
leading: widget.leading,
subtitle: widget.subtitle,
trailing: widget.trailing,
selected: selected,
leading: leading,
subtitle: subtitle,
trailing: trailing,
),
);

final Widget tile;
if (widget.pageId == kNewPlaylist) {
tile = _EmbracedMasterTile(yaruMasterTile: yaruMasterTile);
if (pageId == kNewPlaylistPageId) {
tile = _FramedMasterTile(tile: yaruMasterTile);
} else {
tile = yaruMasterTile;
}

if (widget.audios.isEmpty) {
return tile;
return _PlayAbleMasterTile(
selected: selected,
pageId: pageId,
tile: tile,
libraryModel: libraryModel,
);
}
}

class _FramedMasterTile extends StatelessWidget {
const _FramedMasterTile({
required this.tile,
});

final Widget tile;

@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SpacedDivider(
top: 10,
bottom: 10,
right: 0,
left: 0,
),
tile,
const SpacedDivider(
top: 10,
bottom: 10,
right: 0,
left: 0,
),
],
);
}
}

class _PlayAbleMasterTile extends StatefulWidget {
const _PlayAbleMasterTile({
required this.pageId,
required this.tile,
this.selected,
required this.libraryModel,
});

final String pageId;
final Widget tile;
final bool? selected;
final LibraryModel libraryModel;

@override
State<_PlayAbleMasterTile> createState() => __PlayAbleMasterTileState();
}

class __PlayAbleMasterTileState extends State<_PlayAbleMasterTile> {
bool _hovered = false;

@override
Widget build(BuildContext context) {
final audios = widget.libraryModel.getAudiosById(widget.pageId);

if (audios == null || audios.isEmpty) {
return widget.tile;
}

final isEnQueued = context.select(
Expand All @@ -90,10 +145,16 @@ class _MasterTileState extends State<MasterTile> {
if (isEnQueued) {
isPlaying ? playerModel.pause() : playerModel.resume();
} else {
playerModel.startPlaylist(
audios: widget.audios,
listName: widget.pageId,
);
playerModel
.startPlaylist(
audios: audios,
listName: widget.pageId,
)
.then(
(_) => widget.libraryModel.removePodcastUpdate(
widget.pageId,
),
);
}
}

Expand All @@ -102,7 +163,7 @@ class _MasterTileState extends State<MasterTile> {
onExit: (e) => setState(() => _hovered = false),
child: Stack(
children: [
tile,
widget.tile,
if (_hovered || widget.selected == true)
Positioned(
right: 25,
Expand All @@ -126,35 +187,3 @@ class _MasterTileState extends State<MasterTile> {
);
}
}

class _EmbracedMasterTile extends StatelessWidget {
const _EmbracedMasterTile({
required this.yaruMasterTile,
});

final Widget yaruMasterTile;

@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SpacedDivider(
top: 10,
bottom: 10,
right: 0,
left: 0,
),
yaruMasterTile,
const SpacedDivider(
top: 10,
bottom: 10,
right: 0,
left: 0,
),
],
);
}
}
12 changes: 12 additions & 0 deletions lib/src/library/library_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:safe_change_notifier/safe_change_notifier.dart';

import '../../constants.dart';
import '../../data.dart';
import 'library_service.dart';

Expand Down Expand Up @@ -100,6 +101,17 @@ class LibraryModel extends SafeChangeNotifier {
kFixedListAmount;
}

Set<Audio>? getAudiosById(String pageId) {
if (pageId == kLikedAudiosPageId) {
return likedAudios;
} else {
return playlists[pageId] ??
pinnedAlbums[pageId] ??
podcasts[pageId] ??
starredStations[pageId];
}
}

//
// Liked Audios
//
Expand Down
Loading

0 comments on commit 8c50468

Please sign in to comment.