2020-05-05 12:56:24 +00:00
|
|
|
import 'dart:async';
|
2020-04-27 15:11:29 +00:00
|
|
|
import 'dart:collection';
|
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
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';
|
2020-05-06 17:16:30 +00:00
|
|
|
import 'package:photos/models/filters/important_items_filter.dart';
|
2020-06-19 23:03:26 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
|
|
|
import 'package:photos/file_repository.dart';
|
2020-06-15 03:12:02 +00:00
|
|
|
import 'package:photos/photo_sync_manager.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-06-15 19:57:48 +00:00
|
|
|
import 'package:photos/ui/loading_photos_widget.dart';
|
2020-05-05 15:50:36 +00:00
|
|
|
import 'package:photos/ui/loading_widget.dart';
|
2020-05-22 18:44:14 +00:00
|
|
|
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");
|
2020-06-15 19:57:48 +00:00
|
|
|
final _remoteFolderGalleryWidget = RemoteFolderGalleryWidget();
|
|
|
|
final _deviceFolderGalleryWidget = DeviceFolderGalleryWidget();
|
|
|
|
|
2020-05-04 20:10:11 +00:00
|
|
|
ShakeDetector _detector;
|
2020-04-17 20:42:38 +00:00
|
|
|
int _selectedNavBarItem = 0;
|
2020-06-19 23:03:26 +00:00
|
|
|
Set<File> _selectedPhotos = HashSet<File>();
|
2020-06-15 19:57:48 +00:00
|
|
|
StreamSubscription<LocalPhotosUpdatedEvent>
|
|
|
|
_localPhotosUpdatedEventSubscription;
|
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
|
|
|
_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-06-15 19:57:48 +00:00
|
|
|
_localPhotosUpdatedEventSubscription =
|
|
|
|
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
|
|
|
|
setState(() {});
|
|
|
|
});
|
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>(
|
2020-06-15 19:57:48 +00:00
|
|
|
future: PhotoSyncManager.instance.hasScannedDisk(),
|
2020-05-05 15:50:36 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
return IndexedStack(
|
|
|
|
children: <Widget>[
|
2020-06-15 19:57:48 +00:00
|
|
|
snapshot.data ? _getMainGalleryWidget() : LoadingPhotosWidget(),
|
2020-05-30 22:00:32 +00:00
|
|
|
_deviceFolderGalleryWidget,
|
|
|
|
_remoteFolderGalleryWidget,
|
2020-05-05 15:50:36 +00:00
|
|
|
],
|
|
|
|
index: _selectedNavBarItem,
|
|
|
|
);
|
|
|
|
} else if (snapshot.hasError) {
|
2020-06-15 19:57:48 +00:00
|
|
|
return Center(child: Text(snapshot.error.toString()));
|
2020-05-05 15:50:36 +00:00
|
|
|
} else {
|
|
|
|
return loadWidget;
|
|
|
|
}
|
|
|
|
},
|
2020-04-17 20:42:38 +00:00
|
|
|
),
|
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,
|
|
|
|
),
|
2020-04-17 20:42:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-15 19:57:48 +00:00
|
|
|
Widget _getMainGalleryWidget() {
|
|
|
|
return FutureBuilder<bool>(
|
2020-06-19 23:03:26 +00:00
|
|
|
future: FileRepository.instance.loadFiles(),
|
2020-06-15 19:57:48 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
return Gallery(
|
2020-07-12 22:17:44 +00:00
|
|
|
syncLoader: () => _getFilteredPhotos(FileRepository.instance.files),
|
2020-06-16 12:56:23 +00:00
|
|
|
reloadEvent: Bus.instance.on<LocalPhotosUpdatedEvent>(),
|
2020-07-12 21:45:50 +00:00
|
|
|
onRefresh: PhotoSyncManager.instance.sync,
|
2020-06-19 23:03:26 +00:00
|
|
|
selectedFiles: _selectedPhotos,
|
|
|
|
onFileSelectionChange: (Set<File> selectedPhotos) {
|
2020-06-15 19:57:48 +00:00
|
|
|
setState(() {
|
|
|
|
_selectedPhotos = selectedPhotos;
|
|
|
|
});
|
|
|
|
},
|
2020-07-13 21:33:43 +00:00
|
|
|
tagPrefix: "home_gallery",
|
2020-06-15 19:57:48 +00:00
|
|
|
);
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
return Center(child: Text(snapshot.error.toString()));
|
|
|
|
} else {
|
|
|
|
return loadWidget;
|
|
|
|
}
|
2020-06-15 03:12:02 +00:00
|
|
|
},
|
2020-06-06 14:24:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:42:38 +00:00
|
|
|
BottomNavigationBar _buildBottomNavigationBar() {
|
|
|
|
return BottomNavigationBar(
|
|
|
|
items: const <BottomNavigationBarItem>[
|
|
|
|
BottomNavigationBarItem(
|
2020-06-15 19:59:18 +00:00
|
|
|
icon: Icon(Icons.photo_library),
|
2020-04-17 20:42:38 +00:00
|
|
|
title: Text('Photos'),
|
2020-04-14 15:36:18 +00:00
|
|
|
),
|
2020-04-17 20:42:38 +00:00
|
|
|
BottomNavigationBarItem(
|
2020-06-15 19:59:18 +00:00
|
|
|
icon: Icon(Icons.folder),
|
|
|
|
title: Text('Folders'),
|
2020-04-17 20:42:38 +00:00
|
|
|
),
|
2020-05-22 18:44:14 +00:00
|
|
|
BottomNavigationBarItem(
|
|
|
|
icon: Icon(Icons.folder_shared),
|
|
|
|
title: Text('Shared'),
|
|
|
|
),
|
2020-04-17 20:42:38 +00:00
|
|
|
],
|
|
|
|
currentIndex: _selectedNavBarItem,
|
|
|
|
selectedItemColor: Colors.yellow[800],
|
|
|
|
onTap: (index) {
|
|
|
|
setState(() {
|
|
|
|
_selectedNavBarItem = index;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-05-02 17:12:03 +00:00
|
|
|
|
2020-06-19 23:03:26 +00:00
|
|
|
List<File> _getFilteredPhotos(List<File> unfilteredFiles) {
|
2020-07-10 18:18:21 +00:00
|
|
|
_logger.info("Filtering " + unfilteredFiles.length.toString());
|
2020-06-19 23:03:26 +00:00
|
|
|
final List<File> filteredPhotos = List<File>();
|
|
|
|
for (File file in unfilteredFiles) {
|
|
|
|
if (importantItemsFilter.shouldInclude(file)) {
|
|
|
|
filteredPhotos.add(file);
|
2020-05-05 15:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 21:45:50 +00:00
|
|
|
_logger.info("Filtered down to " + filteredPhotos.length.toString());
|
2020-05-05 15:50:36 +00:00
|
|
|
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-06-15 19:57:48 +00:00
|
|
|
_localPhotosUpdatedEventSubscription.cancel();
|
2020-05-02 17:12:03 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
2020-04-14 15:36:18 +00:00
|
|
|
}
|