Move the logic for loading photos to the Gallery widget

This commit is contained in:
Vishnu Mohandas 2020-06-15 09:55:58 +05:30
parent 69d5ea1199
commit 583f087da5
6 changed files with 52 additions and 69 deletions

View file

@ -47,8 +47,8 @@ class _DeviceFolderPageState extends State<DeviceFolderPage> {
},
),
body: Gallery(
_getFilteredPhotos(PhotoRepository.instance.photos),
_selectedPhotos,
() => Future.value(_getFilteredPhotos(PhotoRepository.instance.photos)),
selectedPhotos: _selectedPhotos,
photoSelectionChangeCallback: (Set<Photo> selectedPhotos) {
setState(() {
_selectedPhotos = selectedPhotos;

View file

@ -2,10 +2,8 @@ import 'package:flutter/material.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/face_search_manager.dart';
import 'package:photos/models/face.dart';
import 'package:photos/models/photo.dart';
import 'package:photos/ui/circular_network_image_widget.dart';
import 'package:photos/ui/gallery.dart';
import 'package:photos/ui/loading_widget.dart';
class FaceSearchResultsPage extends StatelessWidget {
final FaceSearchManager _faceSearchManager = FaceSearchManager.instance;
@ -27,24 +25,10 @@ class FaceSearchResultsPage extends StatelessWidget {
],
),
body: Container(
child: _getBody(),
child: Gallery(
() => _faceSearchManager.getFaceSearchResults(face),
),
),
);
}
FutureBuilder<List<Photo>> _getBody() {
return FutureBuilder<List<Photo>>(
future: _faceSearchManager.getFaceSearchResults(face),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Gallery(
snapshot.data,
Set<Photo>(),
);
} else {
return Center(child: loadWidget);
}
},
);
}
}

View file

@ -8,18 +8,23 @@ import 'package:photos/core/event_bus.dart';
import 'package:photos/events/photo_opened_event.dart';
import 'package:photos/models/photo.dart';
import 'package:photos/ui/detail_page.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/thumbnail_widget.dart';
import 'package:photos/utils/date_time_util.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class Gallery extends StatefulWidget {
final List<Photo> photos;
final Future<List<Photo>> Function() loadFunction;
final Set<Photo> selectedPhotos;
final Function(Set<Photo>) photoSelectionChangeCallback;
final Future<void> Function() syncFunction;
Gallery(this.photos, this.selectedPhotos,
{this.photoSelectionChangeCallback, this.syncFunction});
Gallery(
this.loadFunction, {
this.selectedPhotos,
this.photoSelectionChangeCallback,
this.syncFunction,
});
@override
_GalleryState createState() {
@ -55,8 +60,26 @@ class _GalleryState extends State<Gallery> {
@override
Widget build(BuildContext context) {
// TODO: Investigate reason for multiple rebuilds on selection change
_photos = widget.photos;
_selectedPhotos = widget.selectedPhotos;
return FutureBuilder<List<Photo>>(
future: widget.loadFunction(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return _onDataLoaded(snapshot);
} else if (snapshot.hasError) {
return Center(child: Text(snapshot.error.toString()));
} else {
return Center(child: loadWidget);
}
},
);
}
Widget _onDataLoaded(AsyncSnapshot<List<Photo>> snapshot) {
if (snapshot.data.isEmpty) {
return Center(child: Text("Nothing to see here."));
}
_photos = snapshot.data;
_selectedPhotos = widget.selectedPhotos ?? Set<Photo>();
_collatePhotos();
final list = ListView.builder(
itemCount: _collatedPhotos.length,
@ -76,8 +99,9 @@ class _GalleryState extends State<Gallery> {
failedText: "Sync unsuccessful.",
),
onRefresh: () {
widget.syncFunction().then((value) {
widget.syncFunction().then((_) {
_refreshController.refreshCompleted();
widget.loadFunction().then((_) => setState(() {}));
}).catchError((e) {
_refreshController.refreshFailed();
});

View file

@ -106,8 +106,8 @@ class _HomeWidgetState extends State<HomeWidget> {
Gallery _getMainGalleryWidget() {
return Gallery(
_getFilteredPhotos(PhotoRepository.instance.photos),
_selectedPhotos,
() => Future.value(_getFilteredPhotos(PhotoRepository.instance.photos)),
selectedPhotos: _selectedPhotos,
photoSelectionChangeCallback: (Set<Photo> selectedPhotos) {
setState(() {
_selectedPhotos = selectedPhotos;

View file

@ -38,32 +38,13 @@ class _LocationSearchResultsPageState extends State<LocationSearchResultsPage> {
title: Text(widget.name),
),
body: Container(
child: _getBody(),
child: Gallery(
() => _getResult(),
),
),
);
}
FutureBuilder<List<Photo>> _getBody() {
return FutureBuilder<List<Photo>>(
future: _getResult(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data.isEmpty) {
return Center(child: Text("Nothing to see here."));
}
return Gallery(
snapshot.data,
Set<Photo>(),
);
} else if (snapshot.hasError) {
return Center(child: Text(snapshot.error.toString()));
} else {
return Center(child: loadWidget);
}
},
);
}
FutureOr<List<Photo>> _getResult() async {
final photos = PhotoRepository.instance.photos;
final args = Map<String, dynamic>();

View file

@ -4,12 +4,12 @@ import 'package:flutter/material.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/photo_db.dart';
import 'package:photos/events/remote_sync_event.dart';
import 'package:photos/folder_service.dart';
import 'package:photos/models/folder.dart';
import 'package:photos/models/photo.dart';
import 'package:photos/ui/gallery.dart';
import 'package:photos/ui/gallery_app_bar_widget.dart';
import 'package:logging/logging.dart';
import 'package:photos/ui/loading_widget.dart';
class RemoteFolderPage extends StatefulWidget {
final Folder folder;
@ -49,24 +49,18 @@ class _RemoteFolderPageState extends State<RemoteFolderPage> {
});
},
),
body: FutureBuilder<List<Photo>>(
future: PhotoDB.instance.getAllPhotosInFolder(widget.folder.id),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Gallery(snapshot.data, _selectedPhotos,
photoSelectionChangeCallback: (Set<Photo> selectedPhotos) {
setState(() {
body: Gallery(
() => PhotoDB.instance.getAllPhotosInFolder(widget.folder.id),
syncFunction: () =>
FolderSharingService.instance.syncDiff(widget.folder),
selectedPhotos: _selectedPhotos,
photoSelectionChangeCallback: (Set<Photo> selectedPhotos) {
setState(
() {
_selectedPhotos = selectedPhotos;
});
});
} else if (snapshot.hasError) {
_logger.shout(snapshot.error);
return Text(snapshot.error.toString());
} else {
return loadWidget;
}
},
),
},
);
}),
);
}