ente/lib/ui/home_widget.dart

164 lines
4.8 KiB
Dart
Raw Normal View History

2020-05-05 12:56:24 +00:00
import 'dart:async';
2020-04-27 15:11:29 +00:00
import 'dart:collection';
import 'package:flutter/cupertino.dart';
2020-04-14 15:36:18 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-05-04 20:03:06 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/models/filters/important_items_filter.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/models/photo.dart';
2020-05-04 20:44:34 +00:00
import 'package:photos/photo_repository.dart';
2020-05-17 14:35:31 +00:00
import 'package:photos/ui/device_folders_gallery_widget.dart';
2020-05-05 15:50:36 +00:00
import 'package:photos/ui/gallery.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/ui/gallery_app_bar_widget.dart';
2020-05-05 15:50:36 +00:00
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/remote_folder_gallery_widget.dart';
2020-06-02 11:32:35 +00:00
import 'package:photos/ui/search_page.dart';
2020-05-02 17:12:03 +00:00
import 'package:photos/utils/logging_util.dart';
import 'package:shake/shake.dart';
import 'package:logging/logging.dart';
2020-04-14 15:36:18 +00:00
class HomeWidget extends StatefulWidget {
final String title;
const HomeWidget(this.title, {Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
2020-05-05 15:50:36 +00:00
static final importantItemsFilter = ImportantItemsFilter();
2020-05-04 20:10:11 +00:00
final _logger = Logger("HomeWidgetState");
ShakeDetector _detector;
int _selectedNavBarItem = 0;
2020-04-27 15:11:29 +00:00
Set<Photo> _selectedPhotos = HashSet<Photo>();
2020-05-05 12:56:24 +00:00
StreamSubscription<LocalPhotosUpdatedEvent> _subscription;
2020-05-30 22:00:32 +00:00
final _deviceFolderGalleryWidget = DeviceFolderGalleryWidget();
final _remoteFolderGalleryWidget = RemoteFolderGalleryWidget();
2020-04-14 15:36:18 +00:00
2020-05-02 17:12:03 +00:00
@override
void initState() {
2020-05-05 12:56:24 +00:00
_subscription = Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
2020-05-04 20:03:06 +00:00
setState(() {});
});
2020-05-05 12:56:24 +00:00
_detector = ShakeDetector.autoStart(
2020-05-04 15:45:47 +00:00
shakeThresholdGravity: 3,
2020-05-02 17:21:50 +00:00
onPhoneShake: () {
2020-05-04 20:10:11 +00:00
_logger.info("Emailing logs");
2020-05-02 17:21:50 +00:00
LoggingUtil.instance.emailLogs();
});
2020-05-04 20:03:06 +00:00
super.initState();
2020-05-02 17:12:03 +00:00
}
2020-04-14 15:36:18 +00:00
@override
Widget build(BuildContext context) {
2020-04-27 11:13:35 +00:00
return Scaffold(
appBar: GalleryAppBarWidget(
2020-05-17 14:23:37 +00:00
GalleryAppBarType.homepage,
2020-04-27 11:13:35 +00:00
widget.title,
2020-05-17 12:39:38 +00:00
"/",
2020-04-27 11:13:35 +00:00
_selectedPhotos,
2020-05-05 15:50:36 +00:00
onSelectionClear: _clearSelectedPhotos,
2020-04-27 11:13:35 +00:00
),
bottomNavigationBar: _buildBottomNavigationBar(),
2020-05-05 15:50:36 +00:00
body: FutureBuilder<bool>(
future: PhotoRepository.instance.loadPhotos(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return IndexedStack(
children: <Widget>[
Gallery(
_getFilteredPhotos(PhotoRepository.instance.photos),
_selectedPhotos,
photoSelectionChangeCallback: (Set<Photo> selectedPhotos) {
setState(() {
_selectedPhotos = selectedPhotos;
});
},
enablePullToSync: true,
2020-05-05 15:50:36 +00:00
),
2020-05-30 22:00:32 +00:00
_deviceFolderGalleryWidget,
_remoteFolderGalleryWidget,
2020-05-05 15:50:36 +00:00
],
index: _selectedNavBarItem,
);
} else if (snapshot.hasError) {
return Text("Error!");
} else {
return loadWidget;
}
},
),
2020-06-02 11:32:35 +00:00
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return SearchPage();
},
),
);
},
2020-06-02 11:40:34 +00:00
child: Icon(
Icons.search,
size: 28,
),
2020-06-02 11:32:35 +00:00
elevation: 1,
2020-06-02 11:40:34 +00:00
backgroundColor: Colors.black38,
2020-06-02 11:32:35 +00:00
foregroundColor: Colors.amber,
),
);
}
BottomNavigationBar _buildBottomNavigationBar() {
return BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.photo_filter),
title: Text('Photos'),
2020-04-14 15:36:18 +00:00
),
BottomNavigationBarItem(
icon: Icon(Icons.photo_library),
title: Text('Gallery'),
),
BottomNavigationBarItem(
icon: Icon(Icons.folder_shared),
title: Text('Shared'),
),
],
currentIndex: _selectedNavBarItem,
selectedItemColor: Colors.yellow[800],
onTap: (index) {
setState(() {
_selectedNavBarItem = index;
});
},
);
}
2020-05-02 17:12:03 +00:00
2020-05-05 15:50:36 +00:00
List<Photo> _getFilteredPhotos(List<Photo> unfilteredPhotos) {
final List<Photo> filteredPhotos = List<Photo>();
for (Photo photo in unfilteredPhotos) {
if (importantItemsFilter.shouldInclude(photo)) {
filteredPhotos.add(photo);
}
}
return filteredPhotos;
}
void _clearSelectedPhotos() {
setState(() {
_selectedPhotos.clear();
});
}
2020-05-02 17:12:03 +00:00
@override
void dispose() {
2020-05-04 20:10:11 +00:00
_detector.stopListening();
2020-05-05 12:56:24 +00:00
_subscription.cancel();
2020-05-02 17:12:03 +00:00
super.dispose();
}
2020-04-14 15:36:18 +00:00
}