Skip to content

Commit

Permalink
⚡️ Improved gallery performance
Browse files Browse the repository at this point in the history
  • Loading branch information
LNA-DEV committed Nov 6, 2024
1 parent 93b36ab commit 9ff37cb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 66 deletions.
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:media_kit/media_kit.dart';
Expand Down
55 changes: 28 additions & 27 deletions lib/models/metadata/metadata_movie_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,37 @@ class MetadataMovieModel {
final String? website;

MetadataMovieModel({
required this.year,
required this.rated,
required this.released,
required this.runtime,
required this.genre,
required this.director,
required this.writer,
required this.actors,
required this.plot,
required this.language,
required this.country,
required this.awards,
required this.poster,
required this.logo,
required this.backdrop,
required this.ratings,
required this.metascore,
required this.imdbRating,
required this.imdbVotes,
required this.imdbID,
required this.type,
required this.dvd,
required this.boxOffice,
required this.production,
required this.website,
this.year,
this.rated,
this.released,
this.runtime,
this.genre,
this.director,
this.writer,
this.actors,
this.plot,
this.language,
this.country,
this.awards,
this.poster,
this.logo,
this.backdrop,
this.ratings,
this.metascore,
this.imdbRating,
this.imdbVotes,
this.imdbID,
this.type,
this.dvd,
this.boxOffice,
this.production,
this.website,
});

factory MetadataMovieModel.fromJson(Map<String, dynamic> json) {
var ratingsList = json['ratings'] as List?;
List<Rating>? ratings = ratingsList?.map((ratingJson) => Rating.fromJson(ratingJson)).toList();
List<Rating>? ratings =
ratingsList?.map((ratingJson) => Rating.fromJson(ratingJson)).toList();

var poster = (json['poster'] as String?);

Expand Down Expand Up @@ -123,4 +124,4 @@ class MetadataMovieModel {
'website': website,
};
}
}
}
93 changes: 56 additions & 37 deletions lib/views/gallery.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:open_media_server_app/apis/inventory_api.dart';
import 'package:open_media_server_app/apis/metadata_api.dart';
import 'package:open_media_server_app/helpers/preferences.dart';
import 'package:open_media_server_app/models/internal/grid_item_model.dart';
import 'package:open_media_server_app/models/inventory/inventory_item.dart';
import 'package:open_media_server_app/models/metadata/metadata_model.dart';
import 'package:open_media_server_app/widgets/grid_item.dart';
import 'package:open_media_server_app/views/detail_views/movie_detail.dart';
Expand All @@ -23,7 +25,7 @@ class Gallery extends StatelessWidget {

return Padding(
padding: const EdgeInsets.all(8.0),
child: FutureBuilder<List<GridItemModel>>(
child: FutureBuilder<List<InventoryItem>>(
future: futureItems,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
Expand All @@ -34,7 +36,7 @@ class Gallery extends StatelessWidget {
return const Center(child: Text('No movies found.'));
}

List<GridItemModel> items = snapshot.data!;
List<InventoryItem> items = snapshot.data!;

return GridView.builder(
itemCount: items.length,
Expand All @@ -45,23 +47,39 @@ class Gallery extends StatelessWidget {
childAspectRatio: 0.7, // Adjust for desired aspect ratio
),
itemBuilder: (context, index) {
var gridItemFake = GridItemModel(
inventoryItem: items[index],
metadataModel: null,
);

gridItemFake.posterUrl =
"${Preferences.prefs?.getString("BaseUrl")}/images/${items[index].category}/${items[index].metadataId}/poster";

return InkWell(
child: GridItem(
item: items[index],
item: gridItemFake,
desiredItemWidth: desiredItemWidth,
),
onTap: () {
onTap: () async {
GridItemModel gridItem;

if (items[index].category == "Movie") {
gridItem = await getMovie(items[index]);
} else {
gridItem = await getShow(items[index]);
}

Navigator.push(
context,
MaterialPageRoute(builder: (context) {
if (items[index].inventoryItem!.category == "Movie") {
if (items[index].category == "Movie") {
return MovieDetailView(
itemModel: items[index],
itemModel: gridItem,
);
}
if (items[index].inventoryItem!.category == "Show") {
if (items[index].category == "Show") {
return ShowDetailView(
itemModel: items[index],
itemModel: gridItem,
);
}

Expand All @@ -77,52 +95,53 @@ class Gallery extends StatelessWidget {
);
}

Future<List<GridItemModel>> getGridItems() async {
Future<List<InventoryItem>> getGridItems() async {
InventoryApi inventoryApi = InventoryApi();
MetadataApi metadataApi = MetadataApi();

var movies = await inventoryApi.listItems("Movie");
var items = await inventoryApi.listItems("Movie");
var shows = await inventoryApi.listItems("Show");

List<GridItemModel> gridItems = [];

for (var element in movies) {
var movie = await inventoryApi.getMovie(element.id);
items.addAll(shows);

MetadataModel? metadata;
return items;
}

if (movie.metadataId != null) {
metadata = await metadataApi.getMetadata(movie.metadataId!, "Movie");
}
Future<GridItemModel> getMovie(InventoryItem element) async {
InventoryApi inventoryApi = InventoryApi();
MetadataApi metadataApi = MetadataApi();

var gridItem =
GridItemModel(inventoryItem: movie, metadataModel: metadata);
var movie = await inventoryApi.getMovie(element.id);

gridItem.posterUrl = metadata?.movie?.poster;
gridItem.backdropUrl = metadata?.movie?.backdrop;
MetadataModel? metadata;

gridItems.add(gridItem);
if (movie.metadataId != null) {
metadata = await metadataApi.getMetadata(movie.metadataId!, "Movie");
}

for (var element in shows) {
var show = await inventoryApi.getShow(element.id);
var gridItem = GridItemModel(inventoryItem: movie, metadataModel: metadata);

MetadataModel? metadata;
gridItem.posterUrl = metadata?.movie?.poster;
gridItem.backdropUrl = metadata?.movie?.backdrop;
return gridItem;
}

if (show.metadataId != null) {
metadata = await metadataApi.getMetadata(show.metadataId!, "Show");
}
Future<GridItemModel> getShow(InventoryItem element) async {
InventoryApi inventoryApi = InventoryApi();
MetadataApi metadataApi = MetadataApi();

var gridItem =
GridItemModel(inventoryItem: show, metadataModel: metadata);
var show = await inventoryApi.getShow(element.id);

gridItem.posterUrl = metadata?.show?.poster;
gridItem.backdropUrl = metadata?.show?.backdrop;
gridItem.childIds = show.seasonIds;
MetadataModel? metadata;

gridItems.add(gridItem);
if (show.metadataId != null) {
metadata = await metadataApi.getMetadata(show.metadataId!, "Show");
}

return gridItems;
var gridItem = GridItemModel(inventoryItem: show, metadataModel: metadata);

gridItem.posterUrl = metadata?.show?.poster;
gridItem.backdropUrl = metadata?.show?.backdrop;
gridItem.childIds = show.seasonIds;
return gridItem;
}
}
12 changes: 11 additions & 1 deletion lib/widgets/custom_image.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:open_media_server_app/globals/globals.dart';

class CustomImage extends StatelessWidget {
const CustomImage({
super.key,
required this.imageUrl,
this.fit,
this.width,
this.height,
this.height,
this.disableAdaptiveImage = false,
});

Expand All @@ -32,6 +33,15 @@ class CustomImage extends StatelessWidget {
width: width,
height: height,
httpHeaders: BaseApi.getHeaders(),
errorWidget: (context, url, error) {
return CustomImage(
imageUrl: Globals.PictureNotFoundUrl,
width: width,
height: height,
fit: fit,
disableAdaptiveImage: disableAdaptiveImage,
);
},
);
}
}

0 comments on commit 9ff37cb

Please sign in to comment.