Skip to content

Commit

Permalink
Make view selector only allow music views
Browse files Browse the repository at this point in the history
* Disables non-music list options
* If there are no music views, show an error message
  • Loading branch information
jmshrv committed Sep 22, 2023
1 parent 6dbde0c commit d56f909
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 49 deletions.
40 changes: 40 additions & 0 deletions lib/components/ViewSelector/no_music_libraries_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class NoMusicLibrariesMessage extends StatelessWidget {
const NoMusicLibrariesMessage({
super.key,
this.onRefresh,
});

final VoidCallback? onRefresh;

@override
Widget build(BuildContext context) {
return Center(
child: Scrollbar(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
Text(
AppLocalizations.of(context)!.noMusicLibrariesTitle,
style: Theme.of(context).textTheme.titleLarge,
textAlign: TextAlign.center,
),
Text(
AppLocalizations.of(context)!.noMusicLibrariesBody,
textAlign: TextAlign.center,
),
ElevatedButton(
onPressed: onRefresh,
child: Text(AppLocalizations.of(context)!.refresh))
],
),
),
),
),
);
}
}
8 changes: 7 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,11 @@
"language": "Language",
"confirm": "Confirm",
"showUncensoredLogMessage": "This log contains your login information. Show?",
"resetTabs": "Reset tabs"
"resetTabs": "Reset tabs",
"noMusicLibrariesTitle": "No Music Libraries",
"@noMusicLibrariesTitle": {
"description": "Title for message that shows on the views screen when no music libraries could be found."
},
"noMusicLibrariesBody": "Finamp could not find any music libraries. Please ensure that your Jellyfin server contains at least one library with the content type set to \"Music\".",
"refresh": "REFRESH"
}
98 changes: 50 additions & 48 deletions lib/screens/view_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:get_it/get_it.dart';

import '../components/ViewSelector/no_music_libraries_message.dart';
import '../services/finamp_user_helper.dart';
import 'music_screen.dart';
import '../services/jellyfin_api_helper.dart';
Expand All @@ -22,6 +23,7 @@ class _ViewSelectorState extends State<ViewSelector> {
final _finampUserHelper = GetIt.instance<FinampUserHelper>();
late Future<List<BaseItemDto>> viewListFuture;
final Map<BaseItemDto, bool> _views = {};
bool isSubmitButtonEnabled = false;

@override
void initState() {
Expand All @@ -35,29 +37,36 @@ class _ViewSelectorState extends State<ViewSelector> {
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.selectMusicLibraries),
),
floatingActionButton: FloatingActionButton(
onPressed: _submitChoice,
child: const Icon(Icons.check),
),
floatingActionButton: isSubmitButtonEnabled
? FloatingActionButton(
onPressed: _submitChoice,
child: const Icon(Icons.check),
)
: null,
body: FutureBuilder<List<BaseItemDto>>(
future: viewListFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data!.isEmpty) {
// If snapshot.data is empty, getMusicViews returned no music libraries. This means that the user doesn't have any music libraries.
return Center(
child: Padding(
padding: const EdgeInsets.all(8),
child:
Text(AppLocalizations.of(context)!.couldNotFindLibraries),
),
// Finamp only supports music libraries. We used to allow people to
// select unsupported libraries, but some people selected "general"
// libraries and thought Finamp was broken.
if (snapshot.data!.isEmpty ||
!snapshot.data!
.any((element) => element.collectionType == "music")) {
return NoMusicLibrariesMessage(
onRefresh: () {
setState(() {
_views.clear();
viewListFuture = _jellyfinApiHelper.getViews();
});
},
);
} else {
if (_views.isEmpty) {
_views.addEntries(snapshot.data!
.where((element) => element.collectionType != "playlists")
.map((e) => MapEntry(e, e.collectionType == "music")));
}
}

if (_views.isEmpty) {
_views.addEntries(snapshot.data!
.where((element) => element.collectionType != "playlists")
.map((e) => MapEntry(e, e.collectionType == "music")));

// If only one music library is available and user doesn't have a
// view saved (assuming setup is in progress), skip the selector.
Expand All @@ -66,37 +75,30 @@ class _ViewSelectorState extends State<ViewSelector> {
_finampUserHelper.currentUser!.currentView == null) {
_submitChoice();
}

return Scrollbar(
child: ListView.builder(
itemCount: _views.length,
itemBuilder: (context, index) {
return CheckboxListTile(
value: _views.values.elementAt(index),
title: Text(_views.keys.elementAt(index).name ??
AppLocalizations.of(context)!.unknownName),
onChanged: (value) {
setState(() {
_views[_views.keys.elementAt(index)] = value!;
});
},
// onTap: () async {
// JellyfinApiData jellyfinApiHelper =
// GetIt.instance<JellyfinApiData>();
// try {
// jellyfinApiHelper.saveView(snapshot.data![index],
// jellyfinApiHelper.currentUser!.id);
// Navigator.of(context)
// .pushNamedAndRemoveUntil("/music", (route) => false);
// } catch (e) {
// errorSnackbar(e, context);
// }
// },
);
},
),
);
}

return Scrollbar(
child: ListView.builder(
itemCount: _views.length,
itemBuilder: (context, index) {
final isSelected = _views.values.elementAt(index);
final view = _views.keys.elementAt(index);

return CheckboxListTile(
value: isSelected,
enabled: view.collectionType == "music",
title: Text(_views.keys.elementAt(index).name ??
AppLocalizations.of(context)!.unknownName),
onChanged: (value) {
setState(() {
_views[_views.keys.elementAt(index)] = value!;
isSubmitButtonEnabled = _views.values.contains(true);
});
},
);
},
),
);
} else if (snapshot.hasError) {
errorSnackbar(snapshot.error, context);
// TODO: Let the user refresh the page
Expand Down

0 comments on commit d56f909

Please sign in to comment.