Segregate collections and device folders
This commit is contained in:
parent
d5d49e3474
commit
3d56f71be3
7 changed files with 306 additions and 159 deletions
|
@ -159,6 +159,10 @@ class CollectionsService {
|
|||
});
|
||||
}
|
||||
|
||||
Collection getOwnedCollectionByID(int collectionID) {
|
||||
return _collectionIDToOwnedCollections[collectionID];
|
||||
}
|
||||
|
||||
Future<Collection> getOrCreateForPath(String path) async {
|
||||
if (_localCollections.containsKey(path)) {
|
||||
return _localCollections[path];
|
||||
|
|
|
@ -47,7 +47,7 @@ class FavoritesService {
|
|||
}
|
||||
|
||||
Future<void> addToFavorites(File file) async {
|
||||
final collectionID = await getOrCreateFavoriteCollectionID();
|
||||
final collectionID = await _getOrCreateFavoriteCollectionID();
|
||||
var fileID = file.uploadedFileID;
|
||||
if (fileID == null) {
|
||||
file.collectionID = collectionID;
|
||||
|
@ -72,7 +72,7 @@ class FavoritesService {
|
|||
}
|
||||
|
||||
Future<void> removeFromFavorites(File file) async {
|
||||
final collectionID = await getOrCreateFavoriteCollectionID();
|
||||
final collectionID = await _getOrCreateFavoriteCollectionID();
|
||||
var fileID = file.uploadedFileID;
|
||||
if (fileID == null) {
|
||||
// Do nothing, ignore
|
||||
|
@ -83,7 +83,15 @@ class FavoritesService {
|
|||
}
|
||||
}
|
||||
|
||||
Future<int> getOrCreateFavoriteCollectionID() async {
|
||||
Collection getFavoritesCollection() {
|
||||
if (!_preferences.containsKey(_favoritesCollectionIDKey)) {
|
||||
return null;
|
||||
}
|
||||
return _collectionsService
|
||||
.getOwnedCollectionByID(_preferences.getInt(_favoritesCollectionIDKey));
|
||||
}
|
||||
|
||||
Future<int> _getOrCreateFavoriteCollectionID() async {
|
||||
if (_preferences.containsKey(_favoritesCollectionIDKey)) {
|
||||
return _preferences.getInt(_favoritesCollectionIDKey);
|
||||
}
|
||||
|
|
44
lib/ui/collection_page.dart
Normal file
44
lib/ui/collection_page.dart
Normal file
|
@ -0,0 +1,44 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/models/collection.dart';
|
||||
import 'package:photos/models/selected_files.dart';
|
||||
|
||||
import 'gallery.dart';
|
||||
import 'gallery_app_bar_widget.dart';
|
||||
|
||||
class CollectionPage extends StatefulWidget {
|
||||
final Collection collection;
|
||||
|
||||
const CollectionPage(this.collection, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CollectionPageState createState() => _CollectionPageState();
|
||||
}
|
||||
|
||||
class _CollectionPageState extends State<CollectionPage> {
|
||||
final _selectedFiles = SelectedFiles();
|
||||
|
||||
@override
|
||||
Widget build(Object context) {
|
||||
var gallery = Gallery(
|
||||
asyncLoader: (lastFile, limit) => FilesDB.instance
|
||||
.getAllInCollectionBeforeCreationTime(
|
||||
widget.collection.id,
|
||||
lastFile == null
|
||||
? DateTime.now().microsecondsSinceEpoch
|
||||
: lastFile.creationTime,
|
||||
limit),
|
||||
tagPrefix: "collection",
|
||||
selectedFiles: _selectedFiles,
|
||||
);
|
||||
return Scaffold(
|
||||
appBar: GalleryAppBarWidget(
|
||||
GalleryAppBarType.collection,
|
||||
widget.collection.name,
|
||||
_selectedFiles,
|
||||
),
|
||||
body: gallery,
|
||||
);
|
||||
}
|
||||
}
|
240
lib/ui/collections_gallery_widget.dart
Normal file
240
lib/ui/collections_gallery_widget.dart
Normal file
|
@ -0,0 +1,240 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:photos/core/event_bus.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/events/local_photos_updated_event.dart';
|
||||
import 'package:photos/models/collection.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/repositories/file_repository.dart';
|
||||
import 'package:photos/services/favorites_service.dart';
|
||||
import 'package:photos/models/device_folder.dart';
|
||||
import 'package:photos/ui/collection_page.dart';
|
||||
import 'package:photos/ui/device_folder_page.dart';
|
||||
import 'package:photos/ui/loading_widget.dart';
|
||||
import 'package:photos/ui/thumbnail_widget.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class CollectionsGalleryWidget extends StatefulWidget {
|
||||
const CollectionsGalleryWidget({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CollectionsGalleryWidgetState createState() =>
|
||||
_CollectionsGalleryWidgetState();
|
||||
}
|
||||
|
||||
class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget> {
|
||||
StreamSubscription<LocalPhotosUpdatedEvent> _subscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_subscription = Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
|
||||
setState(() {});
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<CollectionItems>(
|
||||
future: _getCollections(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return _getCollectionsGalleryWidget(snapshot.data);
|
||||
} else if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else {
|
||||
return loadWidget;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getCollectionsGalleryWidget(CollectionItems items) {
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
SectionTitle("Device Folders"),
|
||||
Container(
|
||||
height: 160,
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
|
||||
physics: ScrollPhysics(), // to disable GridView's scrolling
|
||||
itemBuilder: (context, index) {
|
||||
return _buildFolder(context, items.folders[index]);
|
||||
},
|
||||
itemCount: items.folders.length,
|
||||
),
|
||||
),
|
||||
Divider(height: 12),
|
||||
SectionTitle("Collections"),
|
||||
Padding(padding: EdgeInsets.all(6)),
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
padding: EdgeInsets.only(bottom: 12),
|
||||
physics: ScrollPhysics(), // to disable GridView's scrolling
|
||||
itemBuilder: (context, index) {
|
||||
return _buildCollection(context, items.collections[index]);
|
||||
},
|
||||
itemCount: items.collections.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<CollectionItems> _getCollections() async {
|
||||
final paths = await FilesDB.instance.getLocalPaths();
|
||||
final folders = List<DeviceFolder>();
|
||||
for (final path in paths) {
|
||||
final files = List<File>();
|
||||
for (File file in FileRepository.instance.files) {
|
||||
if (file.deviceFolder == path) {
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
final folderName = p.basename(path);
|
||||
folders.add(DeviceFolder(folderName, path, () => files, files[0]));
|
||||
}
|
||||
folders.sort((first, second) {
|
||||
return second.thumbnail.creationTime
|
||||
.compareTo(first.thumbnail.creationTime);
|
||||
});
|
||||
|
||||
final collections = List<CollectionWithThumbnail>();
|
||||
final favorites = FavoritesService.instance.getFavoriteFiles().toList();
|
||||
favorites.sort((first, second) {
|
||||
return second.creationTime.compareTo(first.creationTime);
|
||||
});
|
||||
if (favorites.length > 0) {
|
||||
collections.add(CollectionWithThumbnail(
|
||||
FavoritesService.instance.getFavoritesCollection(), favorites[0]));
|
||||
}
|
||||
return CollectionItems(folders, collections);
|
||||
}
|
||||
|
||||
Widget _buildFolder(BuildContext context, DeviceFolder folder) {
|
||||
return GestureDetector(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
child: Hero(
|
||||
tag: "device_folder:" + folder.path + folder.thumbnail.tag(),
|
||||
child: ThumbnailWidget(folder.thumbnail)),
|
||||
height: 110,
|
||||
width: 110,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(6.0),
|
||||
child: Text(
|
||||
folder.name,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return DeviceFolderPage(folder);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCollection(BuildContext context, CollectionWithThumbnail c) {
|
||||
return GestureDetector(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
child: c.thumbnail ==
|
||||
null // When the user has shared a folder without photos
|
||||
? Icon(Icons.error)
|
||||
: Hero(
|
||||
tag: "collection" + c.thumbnail.tag(),
|
||||
child: ThumbnailWidget(c.thumbnail)),
|
||||
height: 150,
|
||||
width: 150,
|
||||
),
|
||||
Padding(padding: EdgeInsets.all(2)),
|
||||
Expanded(
|
||||
child: Text(
|
||||
c.collection.name,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () {
|
||||
final page = CollectionPage(c.collection);
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return page;
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class SectionTitle extends StatelessWidget {
|
||||
final String title;
|
||||
const SectionTitle(this.title, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.fromLTRB(12, 12, 0, 0),
|
||||
child: Column(children: [
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).primaryColorLight,
|
||||
),
|
||||
),
|
||||
),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
class CollectionItems {
|
||||
final List<DeviceFolder> folders;
|
||||
final List<CollectionWithThumbnail> collections;
|
||||
|
||||
CollectionItems(this.folders, this.collections);
|
||||
}
|
||||
|
||||
class CollectionWithThumbnail {
|
||||
final Collection collection;
|
||||
final File thumbnail;
|
||||
|
||||
CollectionWithThumbnail(this.collection, this.thumbnail);
|
||||
}
|
|
@ -1,150 +0,0 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:photos/core/event_bus.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/events/local_photos_updated_event.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/repositories/file_repository.dart';
|
||||
import 'package:photos/services/favorites_service.dart';
|
||||
import 'package:photos/models/device_folder.dart';
|
||||
import 'package:photos/ui/common_elements.dart';
|
||||
import 'package:photos/ui/device_folder_page.dart';
|
||||
import 'package:photos/ui/loading_widget.dart';
|
||||
import 'package:photos/ui/thumbnail_widget.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class DeviceFolderGalleryWidget extends StatefulWidget {
|
||||
const DeviceFolderGalleryWidget({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_DeviceFolderGalleryWidgetState createState() =>
|
||||
_DeviceFolderGalleryWidgetState();
|
||||
}
|
||||
|
||||
class _DeviceFolderGalleryWidgetState extends State<DeviceFolderGalleryWidget> {
|
||||
StreamSubscription<LocalPhotosUpdatedEvent> _subscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_subscription = Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
|
||||
setState(() {});
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<DeviceFolder>>(
|
||||
future: _getDeviceFolders(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (snapshot.data.length == 0) {
|
||||
return nothingToSeeHere;
|
||||
}
|
||||
return _getDeviceFolderGalleryWidget(snapshot.data);
|
||||
} else if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else {
|
||||
return loadWidget;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getDeviceFolderGalleryWidget(List<DeviceFolder> folders) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 24),
|
||||
child: GridView.builder(
|
||||
shrinkWrap: true,
|
||||
padding: EdgeInsets.only(bottom: 12),
|
||||
physics: ScrollPhysics(), // to disable GridView's scrolling
|
||||
itemBuilder: (context, index) {
|
||||
return _buildFolder(context, folders[index]);
|
||||
},
|
||||
itemCount: folders.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<DeviceFolder>> _getDeviceFolders() async {
|
||||
final paths = await FilesDB.instance.getLocalPaths();
|
||||
final folders = List<DeviceFolder>();
|
||||
for (final path in paths) {
|
||||
final files = List<File>();
|
||||
for (File file in FileRepository.instance.files) {
|
||||
if (file.deviceFolder == path) {
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
final folderName = p.basename(path);
|
||||
folders.add(DeviceFolder(folderName, path, () => files, files[0]));
|
||||
}
|
||||
folders.sort((first, second) {
|
||||
return second.thumbnail.creationTime
|
||||
.compareTo(first.thumbnail.creationTime);
|
||||
});
|
||||
final favorites = FavoritesService.instance.getFavoriteFiles().toList();
|
||||
favorites.sort((first, second) {
|
||||
return second.creationTime.compareTo(first.creationTime);
|
||||
});
|
||||
if (favorites.length > 0) {
|
||||
folders.insert(
|
||||
0,
|
||||
DeviceFolder("Favorites", "/Favorites", () {
|
||||
final favorites =
|
||||
FavoritesService.instance.getFavoriteFiles().toList();
|
||||
favorites.sort((first, second) {
|
||||
return second.creationTime.compareTo(first.creationTime);
|
||||
});
|
||||
return favorites;
|
||||
}, favorites[0]));
|
||||
}
|
||||
return folders;
|
||||
}
|
||||
|
||||
Widget _buildFolder(BuildContext context, DeviceFolder folder) {
|
||||
return GestureDetector(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
child: Hero(
|
||||
tag: "device_folder:" + folder.path + folder.thumbnail.tag(),
|
||||
child: ThumbnailWidget(folder.thumbnail)),
|
||||
height: 140,
|
||||
width: 140,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
folder.name,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return DeviceFolderPage(folder);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@ enum GalleryAppBarType {
|
|||
homepage,
|
||||
local_folder,
|
||||
shared_collection,
|
||||
collection,
|
||||
search_results,
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ import 'package:photos/models/file.dart';
|
|||
import 'package:photos/repositories/file_repository.dart';
|
||||
import 'package:photos/models/selected_files.dart';
|
||||
import 'package:photos/services/sync_service.dart';
|
||||
import 'package:photos/ui/device_folders_gallery_widget.dart';
|
||||
import 'package:photos/ui/collections_gallery_widget.dart';
|
||||
import 'package:photos/ui/gallery.dart';
|
||||
import 'package:photos/ui/gallery_app_bar_widget.dart';
|
||||
import 'package:photos/ui/loading_photos_widget.dart';
|
||||
|
@ -39,7 +39,7 @@ class _HomeWidgetState extends State<HomeWidget> {
|
|||
static final importantItemsFilter = ImportantItemsFilter();
|
||||
final _logger = Logger("HomeWidgetState");
|
||||
final _sharedCollectionGallery = SharedCollectionGallery();
|
||||
final _deviceFolderGalleryWidget = DeviceFolderGalleryWidget();
|
||||
final _deviceFolderGalleryWidget = CollectionsGalleryWidget();
|
||||
final _selectedFiles = SelectedFiles();
|
||||
final _memoriesWidget = MemoriesWidget();
|
||||
|
||||
|
@ -174,15 +174,15 @@ class _HomeWidgetState extends State<HomeWidget> {
|
|||
items: const <BottomNavigationBarItem>[
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.photo_library),
|
||||
title: Text('Photos'),
|
||||
label: "Photos",
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.folder),
|
||||
title: Text('Folders'),
|
||||
icon: Icon(Icons.folder_special),
|
||||
label: "Collections",
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.folder_shared),
|
||||
title: Text('Shared'),
|
||||
label: "Shared",
|
||||
),
|
||||
],
|
||||
currentIndex: _selectedNavBarItem,
|
||||
|
|
Loading…
Add table
Reference in a new issue