2023-11-06 10:38:26 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-01-09 16:32:53 +01:00
|
|
|
import 'package:release_schedule/model/dates.dart';
|
2023-11-06 10:38:26 +01:00
|
|
|
import 'package:release_schedule/model/movie.dart';
|
|
|
|
import 'package:release_schedule/view/movie_item.dart';
|
2024-01-09 18:49:46 +01:00
|
|
|
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
|
2023-11-06 10:38:26 +01:00
|
|
|
|
|
|
|
class MovieList extends StatelessWidget {
|
|
|
|
final List<MovieData> movies;
|
2024-01-08 12:57:36 +01:00
|
|
|
final bool Function(MovieData)? filter;
|
2024-01-09 19:16:48 +01:00
|
|
|
const MovieList(this.movies, {this.filter, super.key});
|
2023-11-06 10:38:26 +01:00
|
|
|
|
|
|
|
@override
|
2024-01-09 14:48:36 +01:00
|
|
|
Widget build(BuildContext context) {
|
2024-01-09 19:16:48 +01:00
|
|
|
Widget noMovies() {
|
2024-01-09 16:32:53 +01:00
|
|
|
return Center(
|
|
|
|
child: IntrinsicHeight(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
const Icon(
|
|
|
|
Icons.close,
|
|
|
|
size: 100,
|
|
|
|
),
|
|
|
|
Text(
|
2024-01-09 19:16:48 +01:00
|
|
|
"No Movies",
|
2024-01-09 16:32:53 +01:00
|
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-01-09 19:16:48 +01:00
|
|
|
|
|
|
|
if (movies.isEmpty) {
|
|
|
|
return noMovies();
|
|
|
|
}
|
|
|
|
|
2024-01-09 14:48:36 +01:00
|
|
|
Widget buildGroupSeparator(BuildContext context, DateWithPrecision date) {
|
|
|
|
bool highlight = date.includes(DateTime.now());
|
|
|
|
return SizedBox(
|
|
|
|
height: 50,
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Card(
|
2024-01-09 19:16:48 +01:00
|
|
|
elevation: 3,
|
2024-01-09 14:48:36 +01:00
|
|
|
color: highlight
|
|
|
|
? Theme.of(context).colorScheme.primaryContainer
|
|
|
|
: null,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 12,
|
|
|
|
vertical: 8,
|
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
date.toString(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:57:36 +01:00
|
|
|
final localFilter = filter;
|
|
|
|
if (localFilter != null) {
|
|
|
|
List<int> indexMap = [];
|
|
|
|
int index = 0;
|
|
|
|
for (var movie in movies) {
|
|
|
|
if (localFilter(movie)) {
|
|
|
|
indexMap.add(index);
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2024-01-09 19:16:48 +01:00
|
|
|
if (indexMap.isEmpty) {
|
|
|
|
return noMovies();
|
|
|
|
}
|
2024-01-09 16:32:53 +01:00
|
|
|
int firstMovieTodayOrAfterIndex = () {
|
|
|
|
DateWithPrecision today = DateWithPrecision.today();
|
|
|
|
int min = 0;
|
|
|
|
int max = indexMap.length;
|
|
|
|
while (min < max) {
|
|
|
|
int center = (min + max) ~/ 2;
|
|
|
|
DateWithPrecision date =
|
|
|
|
movies[indexMap[center]].releaseDate.dateWithPrecision;
|
|
|
|
if (date.compareTo(today) < 0) {
|
|
|
|
min = center + 1;
|
|
|
|
} else {
|
|
|
|
max = center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}();
|
2024-01-09 18:49:46 +01:00
|
|
|
return GroupedList<DateWithPrecision>(
|
|
|
|
itemCount: indexMap.length,
|
|
|
|
groupBy: (index) =>
|
|
|
|
movies[indexMap[index]].releaseDate.dateWithPrecision,
|
|
|
|
groupSeparatorBuilder: (date) => buildGroupSeparator(context, date),
|
2024-01-08 12:57:36 +01:00
|
|
|
itemBuilder: (context, index) {
|
2024-01-09 18:49:46 +01:00
|
|
|
return MovieItem(movies[indexMap[index]]);
|
2024-01-08 12:57:36 +01:00
|
|
|
},
|
2024-01-09 16:32:53 +01:00
|
|
|
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
2024-01-08 12:57:36 +01:00
|
|
|
);
|
|
|
|
}
|
2024-01-09 19:16:48 +01:00
|
|
|
|
2024-01-09 16:32:53 +01:00
|
|
|
int firstMovieTodayOrAfterIndex = () {
|
|
|
|
DateWithPrecision today = DateWithPrecision.today();
|
|
|
|
int min = 0;
|
|
|
|
int max = movies.length;
|
|
|
|
while (min < max) {
|
|
|
|
int center = (min + max) ~/ 2;
|
|
|
|
DateWithPrecision date = movies[center].releaseDate.dateWithPrecision;
|
|
|
|
if (date.compareTo(today) < 0) {
|
|
|
|
min = center + 1;
|
|
|
|
} else {
|
|
|
|
max = center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}();
|
2024-01-09 18:49:46 +01:00
|
|
|
return GroupedList<DateWithPrecision>(
|
|
|
|
itemCount: movies.length,
|
|
|
|
groupBy: (index) => movies[index].releaseDate.dateWithPrecision,
|
|
|
|
groupSeparatorBuilder: (date) => buildGroupSeparator(context, date),
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return MovieItem(movies[index]);
|
2023-11-06 10:38:26 +01:00
|
|
|
},
|
2024-01-09 16:32:53 +01:00
|
|
|
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
2023-11-06 10:38:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 18:49:46 +01:00
|
|
|
|
|
|
|
class GroupedList<GroupType> extends StatelessWidget {
|
|
|
|
final int itemCount;
|
|
|
|
final int initialScrollIndex;
|
|
|
|
final Widget Function(BuildContext, int) itemBuilder;
|
|
|
|
final Widget Function(GroupType) groupSeparatorBuilder;
|
|
|
|
final GroupType Function(int) groupBy;
|
|
|
|
|
|
|
|
const GroupedList(
|
|
|
|
{required this.itemCount,
|
|
|
|
required this.itemBuilder,
|
|
|
|
required this.groupSeparatorBuilder,
|
|
|
|
required this.groupBy,
|
|
|
|
this.initialScrollIndex = 0,
|
|
|
|
super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (itemCount == 0) {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
List<({int index, GroupType group})> newGroupStarts = [
|
|
|
|
(index: 0, group: groupBy(0))
|
|
|
|
];
|
2024-01-09 21:52:32 +01:00
|
|
|
int internalInitialScrollIndex =
|
|
|
|
initialScrollIndex + (initialScrollIndex > 0 ? 1 : 0);
|
2024-01-09 18:49:46 +01:00
|
|
|
GroupType last = newGroupStarts[0].group;
|
|
|
|
for (int i = 1; i < itemCount; i++) {
|
|
|
|
final GroupType current = groupBy(i);
|
|
|
|
if (current != last) {
|
|
|
|
newGroupStarts.add((index: i, group: current));
|
|
|
|
if (initialScrollIndex > i) {
|
|
|
|
internalInitialScrollIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget itemAndSeparatorBuilder(BuildContext context, int index) {
|
|
|
|
int itemIndex = index;
|
|
|
|
for (int i = 0; i < newGroupStarts.length; i++) {
|
|
|
|
if (newGroupStarts[i].index > itemIndex) {
|
|
|
|
break;
|
|
|
|
} else if (newGroupStarts[i].index == itemIndex) {
|
|
|
|
return groupSeparatorBuilder(groupBy(itemIndex));
|
|
|
|
}
|
|
|
|
itemIndex--;
|
|
|
|
}
|
|
|
|
return itemBuilder(context, itemIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ScrollablePositionedList.builder(
|
|
|
|
itemCount: itemCount + newGroupStarts.length,
|
|
|
|
itemBuilder: itemAndSeparatorBuilder,
|
|
|
|
initialScrollIndex: internalInitialScrollIndex,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|