Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homescreen caching #103

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 133 additions & 101 deletions lib/components/genre_banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,127 +9,159 @@ import 'package:jellyflix/components/jfx_text_theme.dart';
import 'package:jellyflix/models/gradients.dart';
import 'package:jellyflix/models/screen_paths.dart';
import 'package:jellyflix/providers/api_provider.dart';
import 'package:jellyflix/providers/database_provider.dart';
import 'package:tentacle/tentacle.dart';

class GenreBanner extends HookConsumerWidget {
const GenreBanner({
class CachedGenreBanner extends HookConsumerWidget {
const CachedGenreBanner({
super.key,
});

@override
Widget build(BuildContext context, WidgetRef ref) {
final pageController = usePageController(viewportFraction: 0.95);
final List? queryData =
ref.read(databaseProvider("queryCache")).get("genres");

return Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: FutureBuilder(
// ref.read(apiProvider).getGenres(includeItemTypes: [
// BaseItemKind.movie,
// BaseItemKind.series,
// BaseItemKind.boxSet
// ]),
if (queryData == null) {
return FutureBuilder(
future: ref.read(apiProvider).getGenres(includeItemTypes: [
BaseItemKind.movie,
BaseItemKind.series,
BaseItemKind.boxSet
]),
builder: (context, snapshot) {
if (!snapshot.hasData) {
if (snapshot.hasData && snapshot.data != null) {
snapshot.data!.shuffle();
ref
.read(databaseProvider("queryCache"))
.put("genres", snapshot.data);
return GenreBanner(
pageController: pageController, queryData: snapshot.data!);
} else {
return const SizedBox.shrink();
}
snapshot.data!.shuffle();
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: ItemCarouselLabel(
title: AppLocalizations.of(context)!.genres,
scrollController: pageController,
offsetWidth: MediaQuery.of(context).size.width * 0.9)),
SizedBox(
height: MediaQuery.of(context).size.width >= 640 ? 250 : 150,
child: PageView.builder(
controller: pageController,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
context.push(Uri(
path: ScreenPaths.library,
queryParameters: {
"genreFilter": snapshot.data![index].id,
}).toString());
},
child: Stack(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
),
child: FutureBuilder(
future: ref
.read(apiProvider)
.getFilterItems(
genreIds: [snapshot.data![index]],
limit: 1),
builder: (context, itemData) {
if (!itemData.hasData ||
itemData.data!.isEmpty) {
return const SizedBox.shrink();
}
var imageType = ImageType.backdrop;
if ((itemData.data![0].imageTags
?.containsKey("Backdrop") ??
false) ==
false) {
imageType = ImageType.primary;
}
});
} else {
ref.read(apiProvider).getGenres(includeItemTypes: [
BaseItemKind.movie,
BaseItemKind.series,
BaseItemKind.boxSet
]).then((value) {
value.shuffle();
ref.read(databaseProvider("queryCache")).put("genres", value);
});
return GenreBanner(pageController: pageController, queryData: queryData);
}
}
}

return JellyfinImage(
id: itemData.data![0].id!,
type: imageType,
blurHash: itemData
.data![0]
.imageBlurHashes
?.backdrop
?.values
.first);
},
)),
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: Gradients.getGradient(index),
stops: const [0, 0.5, 0.9],
begin: Alignment.bottomRight,
end: Alignment.topLeft,
),
borderRadius: BorderRadius.circular(10.0),
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.black.withOpacity(0.3),
),
),
Center(
child: Text(snapshot.data![index].name!,
style: JfxTextTheme.scalingTheme(context)
.headlineSmall!
.copyWith(
fontWeight: FontWeight.bold,
)),
)
],
class GenreBanner extends HookConsumerWidget {
const GenreBanner({
super.key,
required this.pageController,
required this.queryData,
});

final PageController pageController;
final List queryData;

@override
Widget build(BuildContext context, WidgetRef ref) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: ItemCarouselLabel(
title: AppLocalizations.of(context)!.genres,
scrollController: pageController,
offsetWidth: MediaQuery.of(context).size.width * 0.9)),
SizedBox(
height: MediaQuery.of(context).size.width >= 640 ? 250 : 150,
child: PageView.builder(
controller: pageController,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
context.push(
Uri(path: ScreenPaths.library, queryParameters: {
"genreFilter": queryData[index].id,
}).toString());
},
child: Stack(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
),
child: FutureBuilder(
future: ref.read(apiProvider).getFilterItems(
genreIds: [queryData[index]], limit: 1),
builder: (context, itemData) {
if (!itemData.hasData ||
itemData.data!.isEmpty) {
return const SizedBox.shrink();
}
var imageType = ImageType.backdrop;
if ((itemData.data![0].imageTags
?.containsKey("Backdrop") ??
false) ==
false) {
imageType = ImageType.primary;
}

return JellyfinImage(
id: itemData.data![0].id!,
type: imageType,
blurHash: itemData.data![0].imageBlurHashes
?.backdrop?.values.first);
},
)),
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: Gradients.getGradient(index),
stops: const [0, 0.5, 0.9],
begin: Alignment.bottomRight,
end: Alignment.topLeft,
),
borderRadius: BorderRadius.circular(10.0),
),
),
);
},
itemCount: snapshot.data?.length ?? 0,
scrollDirection: Axis.horizontal,
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.black.withOpacity(0.3),
),
),
Center(
child: Text(queryData[index].name!,
style: JfxTextTheme.scalingTheme(context)
.headlineSmall!
.copyWith(
fontWeight: FontWeight.bold,
)),
)
],
),
),
),
],
);
},
));
);
},
itemCount: queryData.length,
scrollDirection: Axis.horizontal,
),
),
],
),
);
}
}
24 changes: 24 additions & 0 deletions lib/models/baseitemdto_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:hive/hive.dart';
import 'package:tentacle/tentacle.dart';

class BaseItemDtoAdapter extends TypeAdapter<BaseItemDto> {
@override
final int typeId = 1;

@override
BaseItemDto read(BinaryReader reader) {
return $BaseItemDto((p0) {
p0.id = reader.read();
p0.name = reader.read();
p0.productionYear = reader.read();
});
}

@override
void write(BinaryWriter writer, BaseItemDto obj) {
writer
..write(obj.id)
..write(obj.name)
..write(obj.productionYear);
}
}
4 changes: 4 additions & 0 deletions lib/providers/pagestorage_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:flutter/widgets.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

final pageStorageProvider = Provider((ref) => PageStorageBucket());
Loading
Loading