freezer/lib/ui/home_screen.dart

346 lines
10 KiB
Dart
Raw Normal View History

2023-07-29 02:17:26 +00:00
import 'package:flutter/material.dart';
import 'package:freezer/api/deezer.dart';
import 'package:freezer/api/definitions.dart';
import 'package:freezer/api/player.dart';
import 'package:freezer/ui/error.dart';
import 'package:freezer/ui/menu.dart';
import 'package:freezer/translations.i18n.dart';
import 'tiles.dart';
import 'details_screens.dart';
import '../settings.dart';
class _SearchHeaderDelegate extends SliverPersistentHeaderDelegate {
@override
double get maxExtent => 76.0;
@override
double get minExtent => 76.0;
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
false;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(56.0)),
child: ListTile(
mouseCursor: MaterialStateMouseCursor.textable,
leading: const Icon(Icons.search),
title: Text('Search or paste URL'.i18n),
onTap: () => Navigator.of(context).pushNamed('/search'))));
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: NestedScrollView(
floatHeaderSlivers: true,
headerSliverBuilder: (context, _) => [
SliverPersistentHeader(
delegate: _SearchHeaderDelegate(), floating: true)
],
body: HomePageScreen(cacheable: true),
2023-07-29 02:17:26 +00:00
),
),
);
}
}
class FreezerTitle extends StatelessWidget {
const FreezerTitle({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 24, 0, 8),
child: LayoutBuilder(builder: (context, constraints) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/icon.png', width: 64, height: 64),
Text(
'freezer',
style: TextStyle(fontSize: 56, fontWeight: FontWeight.w900),
)
],
);
}),
);
}
}
class HomePageScreen extends StatefulWidget {
final HomePage? homePage;
final bool cacheable;
2023-07-29 02:17:26 +00:00
final DeezerChannel? channel;
HomePageScreen(
{this.homePage, this.channel, this.cacheable = false, Key? key})
: super(key: key);
2023-07-29 02:17:26 +00:00
@override
_HomePageScreenState createState() => _HomePageScreenState();
}
class _HomePageScreenState extends State<HomePageScreen> {
HomePage? _homePage;
bool _error = false;
bool _loadExplicitlyRequested = false;
2023-07-29 02:17:26 +00:00
final _indicatorKey = GlobalKey<RefreshIndicatorState>();
Future<void> _loadChannel() async {
HomePage? _hp;
//Fetch channel from api
try {
if (widget.channel == null)
_hp = await deezerAPI.homePage();
else
_hp = await deezerAPI.getChannel(widget.channel!.target);
2023-07-29 02:17:26 +00:00
} catch (e) {
_hp = null;
2023-07-29 02:17:26 +00:00
print(e);
}
if (!mounted) return;
2023-07-29 02:17:26 +00:00
if (_hp == null) {
//On error
setState(() => _error = true);
return;
}
if (_hp!.sections.isEmpty) return;
if (widget.cacheable) _hp.save(widget.channel?.target ?? '');
setState(() => _homePage = _hp!);
2023-07-29 02:17:26 +00:00
}
Future<void> _loadLocalChannel() async {
HomePage? _hp = await HomePage.local(widget.channel?.target ?? '');
//print('LOCAL: ${_hp.sections}');
2023-07-29 02:17:26 +00:00
setState(() => _homePage = _hp);
}
Future<void> _load() async {
print("channel: " + (widget.channel?.target ?? "null"));
if (widget.homePage != null && widget.homePage!.sections.isNotEmpty) {
setState(() => _homePage = widget.homePage);
2023-07-29 02:17:26 +00:00
return;
}
if (!_loadExplicitlyRequested) {
if (_homePage == null && widget.cacheable) await _loadLocalChannel();
if (_homePage == null ||
DateTime.now().difference(_homePage!.lastUpdated) >
HomePage.cacheDuration) await _loadChannel();
_loadExplicitlyRequested = true;
2023-07-29 02:17:26 +00:00
return;
}
await _loadChannel();
2023-07-29 02:17:26 +00:00
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_indicatorKey.currentState!.show();
});
}
@override
Widget build(BuildContext context) {
if (_error) return ErrorScreen();
List<HomePageSection>? sections;
if (_homePage != null) {
sections = _homePage!.sections!;
}
2023-07-29 02:17:26 +00:00
return RefreshIndicator(
key: _indicatorKey,
onRefresh: _load,
child: _homePage == null
? const SizedBox.expand(child: SingleChildScrollView())
: ListView.builder(
itemBuilder: (context, index) {
final section = sections![index];
switch (section.layout) {
case HomePageSectionLayout.GRID:
return HomePageGridSection(section);
case HomePageSectionLayout.ROW:
default:
return HomepageRowSection(section);
}
},
itemCount: sections!.length,
padding: EdgeInsets.symmetric(horizontal: 8.0),
2023-07-29 02:17:26 +00:00
),
);
}
}
class HomepageRowSection extends StatelessWidget {
final HomePageSection section;
HomepageRowSection(this.section);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
section.title ?? '',
textAlign: TextAlign.left,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w900),
),
subtitle: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(section.items!.length + 1, (j) {
//Has more items
if (j == section.items!.length) {
if (section.hasMore ?? false) {
return TextButton(
child: Text(
'Show more'.i18n,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20.0),
),
onPressed: () => Navigator.of(context).pushRoute(
builder: (context) => Scaffold(
appBar: AppBar(title: Text(section.title!)),
body: HomePageScreen(
channel:
DeezerChannel(target: section.pagePath),
),
)),
);
}
return const SizedBox();
}
//Show item
HomePageItem item = section.items![j];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 6.0),
child: HomePageItemWidget(item),
);
2023-07-29 02:17:26 +00:00
}),
),
));
}
}
class HomePageGridSection extends StatelessWidget {
final HomePageSection section;
HomePageGridSection(this.section);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (section.title != null)
Padding(
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 6.0),
child: Text(
section.title!,
textAlign: TextAlign.left,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w900),
),
),
Wrap(
spacing: 4.0,
runSpacing: 4.0,
alignment: WrapAlignment.spaceEvenly,
children: section.items!
.map((e) => HomePageItemWidget(e))
.toList(growable: false)),
],
2023-07-29 02:17:26 +00:00
);
}
}
class HomePageItemWidget extends StatelessWidget {
final HomePageItem item;
HomePageItemWidget(this.item);
@override
Widget build(BuildContext context) {
switch (item.type) {
case HomePageItemType.SMARTTRACKLIST:
return SmartTrackListTile(
item.value,
size: (item.value as SmartTrackList).id == 'flow' ? 96.0 : 128.0,
onTap: () => playerHelper.playFromSmartTrackList(item.value),
2023-07-29 02:17:26 +00:00
);
case HomePageItemType.ALBUM:
return AlbumCard(
item.value,
onTap: () {
Navigator.of(context)
.pushRoute(builder: (context) => AlbumDetails(item.value));
},
onHold: () {
MenuSheet m = MenuSheet(context);
m.defaultAlbumMenu(item.value);
},
);
case HomePageItemType.ARTIST:
return ArtistTile(
item.value,
onTap: () {
Navigator.of(context)
.pushRoute(builder: (context) => ArtistDetails(item.value));
},
onHold: () {
MenuSheet m = MenuSheet(context);
m.defaultArtistMenu(item.value);
},
);
case HomePageItemType.PLAYLIST:
return PlaylistCardTile(
item.value,
onTap: () {
Navigator.of(context)
.pushRoute(builder: (context) => PlaylistDetails(item.value));
},
onHold: () {
MenuSheet m = MenuSheet(context);
m.defaultPlaylistMenu(item.value);
},
);
case HomePageItemType.CHANNEL:
return ChannelTile(
item.value,
onTap: () {
Navigator.of(context).pushRoute(
builder: (context) => Scaffold(
appBar: AppBar(title: Text(item.value.title.toString())),
body: HomePageScreen(channel: item.value),
));
},
);
case HomePageItemType.SHOW:
return ShowCard(
item.value,
onTap: () {
Navigator.of(context)
.pushRoute(builder: (context) => ShowScreen(item.value));
},
);
default:
return const SizedBox(height: 0, width: 0);
}
}
}