fix: insert movies at correct location
parent
a977ad1f34
commit
da366285bb
|
@ -35,7 +35,14 @@ class HomePage extends StatelessWidget {
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text("Release Schedule")),
|
||||
appBar: AppBar(
|
||||
title: const Text("Release Schedule"),
|
||||
actions: [
|
||||
FilledButton(
|
||||
onPressed: () => manager.removeMoviesWhere((movie) => true),
|
||||
child: const Icon(Icons.delete))
|
||||
],
|
||||
),
|
||||
body: MovieManagerList(manager),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.refresh),
|
||||
|
|
|
@ -73,13 +73,14 @@ class MovieManager extends ChangeNotifier {
|
|||
MovieData? existing =
|
||||
firstWhereOrNull(movies, (element) => movie.same(element));
|
||||
if (existing == null) {
|
||||
movies.add(movie);
|
||||
_insertMovie(movie);
|
||||
movie.addListener(() {
|
||||
_moviesModified(withoutAddingOrRemoving: true);
|
||||
});
|
||||
added = true;
|
||||
actualMovies.add(movie);
|
||||
} else {
|
||||
existing.updateWithNew(movie);
|
||||
actualMovies.add(existing);
|
||||
}
|
||||
}
|
||||
|
@ -89,6 +90,21 @@ class MovieManager extends ChangeNotifier {
|
|||
return actualMovies;
|
||||
}
|
||||
|
||||
_insertMovie(MovieData movie) {
|
||||
int min = 0;
|
||||
int max = movies.length - 1;
|
||||
while (min - 1 < max) {
|
||||
int center = ((min + max) / 2).floor();
|
||||
int diff = movie.releaseDate.compareTo(movies[center].releaseDate);
|
||||
if (diff < 0) {
|
||||
max = center - 1;
|
||||
} else {
|
||||
min = center + 1;
|
||||
}
|
||||
}
|
||||
movies.insert(min, movie);
|
||||
}
|
||||
|
||||
removeMoviesWhere(bool Function(MovieData movie) test) {
|
||||
bool removedMovies = false;
|
||||
for (int i = movies.length - 1; i >= 0; i--) {
|
||||
|
|
Loading…
Reference in New Issue