Fix remaining issues with displaying shared collection items
This commit is contained in:
parent
54558c4d92
commit
6a73308bd1
9 changed files with 200 additions and 12 deletions
|
@ -92,12 +92,17 @@ class Configuration {
|
|||
Sodium.base642bin(attributes.encryptedKey),
|
||||
kek,
|
||||
Sodium.base642bin(attributes.keyDecryptionNonce));
|
||||
final secretKey = CryptoUtil.decryptSync(
|
||||
Sodium.base642bin(attributes.encryptedSecretKey),
|
||||
kek,
|
||||
Sodium.base642bin(attributes.secretKeyDecryptionNonce));
|
||||
await setKey(Sodium.bin2base64(key));
|
||||
await setSecretKey(Sodium.bin2base64(secretKey));
|
||||
}
|
||||
|
||||
String getHttpEndpoint() {
|
||||
if (kDebugMode) {
|
||||
return "http://192.168.0.100";
|
||||
return "http://192.168.1.3:80";
|
||||
}
|
||||
return "https://api.staging.ente.io";
|
||||
}
|
||||
|
|
|
@ -85,7 +85,8 @@ class CollectionsDB {
|
|||
final db = await instance.database;
|
||||
var batch = db.batch();
|
||||
for (final collection in collections) {
|
||||
batch.insert(collectionsTable, _getRowForSharedCollection(collection),
|
||||
batch.insert(
|
||||
sharedCollectionsTable, _getRowForSharedCollection(collection),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace);
|
||||
}
|
||||
return await batch.commit();
|
||||
|
@ -103,7 +104,7 @@ class CollectionsDB {
|
|||
|
||||
Future<List<SharedCollection>> getAllSharedCollections() async {
|
||||
final db = await instance.database;
|
||||
final rows = await db.query(collectionsTable);
|
||||
final rows = await db.query(sharedCollectionsTable);
|
||||
final collections = List<SharedCollection>();
|
||||
for (final row in rows) {
|
||||
collections.add(_convertToSharedCollection(row));
|
||||
|
|
|
@ -231,6 +231,8 @@ class FilesDB {
|
|||
Future<int> update(
|
||||
int generatedID,
|
||||
int uploadedID,
|
||||
int ownerID,
|
||||
int collectionID,
|
||||
int updationTime,
|
||||
String encryptedKey,
|
||||
String keyDecryptionNonce,
|
||||
|
@ -241,6 +243,8 @@ class FilesDB {
|
|||
final db = await instance.database;
|
||||
final values = new Map<String, dynamic>();
|
||||
values[columnUploadedFileID] = uploadedID;
|
||||
values[columnOwnerID] = ownerID;
|
||||
values[columnCollectionID] = collectionID;
|
||||
values[columnUpdationTime] = updationTime;
|
||||
values[columnEncryptedKey] = encryptedKey;
|
||||
values[columnKeyDecryptionNonce] = keyDecryptionNonce;
|
||||
|
@ -333,6 +337,22 @@ class FilesDB {
|
|||
}
|
||||
}
|
||||
|
||||
Future<File> getLatestFileInCollection(int collectionID) async {
|
||||
final db = await instance.database;
|
||||
final rows = await db.query(
|
||||
table,
|
||||
where: '$columnCollectionID =?',
|
||||
whereArgs: [collectionID],
|
||||
orderBy: '$columnCreationTime DESC',
|
||||
limit: 1,
|
||||
);
|
||||
if (rows.isNotEmpty) {
|
||||
return _getFileFromRow(rows[0]);
|
||||
} else {
|
||||
throw ("No file found in collection " + collectionID.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<File> getLastSyncedFileInRemoteFolder(int folderID) async {
|
||||
final db = await instance.database;
|
||||
final rows = await db.query(
|
||||
|
@ -412,7 +432,7 @@ class FilesDB {
|
|||
file.generatedID = row[columnGeneratedID];
|
||||
file.localID = row[columnLocalID];
|
||||
file.uploadedFileID = row[columnUploadedFileID];
|
||||
file.ownerID = row[columnUploadedFileID];
|
||||
file.ownerID = row[columnOwnerID];
|
||||
file.collectionID = row[columnCollectionID];
|
||||
file.title = row[columnTitle];
|
||||
file.deviceFolder = row[columnDeviceFolder];
|
||||
|
|
|
@ -134,6 +134,7 @@ class File {
|
|||
@override
|
||||
String toString() {
|
||||
return '''File(generatedId: $generatedID, uploadedFileId: $uploadedFileID,
|
||||
ownerID: $ownerID, collectionID: $collectionID,
|
||||
localId: $localID, title: $title, deviceFolder: $deviceFolder,
|
||||
location: $location, fileType: $fileType, creationTime: $creationTime,
|
||||
modificationTime: $modificationTime, updationTime: $updationTime)''';
|
||||
|
|
|
@ -16,7 +16,8 @@ class CollectionsService {
|
|||
CollectionsDB _db;
|
||||
Configuration _config;
|
||||
final _localCollections = Map<String, Collection>();
|
||||
final _collectionIDToCollection = Map<int, Collection>();
|
||||
final _collectionIDToOwnedCollections = Map<int, Collection>();
|
||||
final _collectionIDToSharedCollections = Map<int, SharedCollection>();
|
||||
final _cachedKeys = Map<int, Uint8List>();
|
||||
|
||||
CollectionsService._privateConstructor() {
|
||||
|
@ -43,6 +44,10 @@ class CollectionsService {
|
|||
var sharedCollections =
|
||||
await getSharedCollections(lastSharedCollectionCreationTime ?? 0);
|
||||
await _db.insertSharedCollections(sharedCollections);
|
||||
sharedCollections = await _db.getAllSharedCollections();
|
||||
for (final collection in sharedCollections) {
|
||||
_collectionIDToSharedCollections[collection.id] = collection;
|
||||
}
|
||||
}
|
||||
|
||||
Collection getCollectionForPath(String path) {
|
||||
|
@ -86,19 +91,21 @@ class CollectionsService {
|
|||
|
||||
Uint8List getCollectionKey(int collectionID) {
|
||||
if (!_cachedKeys.containsKey(collectionID)) {
|
||||
final collection = _collectionIDToCollection[collectionID];
|
||||
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
|
||||
var key;
|
||||
if (collection.ownerID == _config.getUserID()) {
|
||||
if (_collectionIDToOwnedCollections.containsKey(collectionID)) {
|
||||
final collection = _collectionIDToOwnedCollections[collectionID];
|
||||
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
|
||||
key = CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
|
||||
Sodium.base642bin(collection.keyDecryptionNonce));
|
||||
} else {
|
||||
final collection = _collectionIDToSharedCollections[collectionID];
|
||||
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
|
||||
key = CryptoUtil.openSealSync(
|
||||
encryptedKey,
|
||||
Sodium.base642bin(_config.getKeyAttributes().publicKey),
|
||||
_config.getSecretKey());
|
||||
}
|
||||
_cachedKeys[collection.id] = key;
|
||||
_cachedKeys[collectionID] = key;
|
||||
}
|
||||
return _cachedKeys[collectionID];
|
||||
}
|
||||
|
@ -191,7 +198,7 @@ class CollectionsService {
|
|||
Sodium.base642bin(collection.pathDecryptionNonce)));
|
||||
_localCollections[path] = collection;
|
||||
}
|
||||
_collectionIDToCollection[collection.id] = collection;
|
||||
_collectionIDToOwnedCollections[collection.id] = collection;
|
||||
getCollectionKey(collection.id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -207,6 +207,8 @@ class SyncService {
|
|||
await _db.update(
|
||||
file.generatedID,
|
||||
uploadedFile.uploadedFileID,
|
||||
uploadedFile.ownerID,
|
||||
uploadedFile.collectionID,
|
||||
uploadedFile.updationTime,
|
||||
file.encryptedKey,
|
||||
file.keyDecryptionNonce,
|
||||
|
@ -232,6 +234,8 @@ class SyncService {
|
|||
await _db.update(
|
||||
existingFile.generatedID,
|
||||
file.uploadedFileID,
|
||||
file.ownerID,
|
||||
file.collectionID,
|
||||
file.updationTime,
|
||||
file.encryptedKey,
|
||||
file.keyDecryptionNonce,
|
||||
|
|
|
@ -21,6 +21,7 @@ import 'package:photos/ui/memories_widget.dart';
|
|||
import 'package:photos/ui/remote_folder_gallery_widget.dart';
|
||||
import 'package:photos/ui/search_page.dart';
|
||||
import 'package:photos/services/user_service.dart';
|
||||
import 'package:photos/ui/shared_collections_gallery.dart';
|
||||
import 'package:photos/utils/logging_util.dart';
|
||||
import 'package:shake/shake.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
@ -38,7 +39,7 @@ class HomeWidget extends StatefulWidget {
|
|||
class _HomeWidgetState extends State<HomeWidget> {
|
||||
static final importantItemsFilter = ImportantItemsFilter();
|
||||
final _logger = Logger("HomeWidgetState");
|
||||
final _remoteFolderGalleryWidget = RemoteFolderGalleryWidget();
|
||||
final _sharedCollectionGallery = SharedCollectionGallery();
|
||||
final _deviceFolderGalleryWidget = DeviceFolderGalleryWidget();
|
||||
final _selectedFiles = SelectedFiles();
|
||||
final _memoriesWidget = MemoriesWidget();
|
||||
|
@ -80,7 +81,7 @@ class _HomeWidgetState extends State<HomeWidget> {
|
|||
? _getMainGalleryWidget()
|
||||
: LoadingPhotosWidget(),
|
||||
_deviceFolderGalleryWidget,
|
||||
_remoteFolderGalleryWidget,
|
||||
_sharedCollectionGallery,
|
||||
],
|
||||
index: _selectedNavBarItem,
|
||||
),
|
||||
|
|
146
lib/ui/shared_collections_gallery.dart
Normal file
146
lib/ui/shared_collections_gallery.dart
Normal file
|
@ -0,0 +1,146 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:photos/core/event_bus.dart';
|
||||
import 'package:photos/db/collections_db.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
import 'package:photos/events/remote_sync_event.dart';
|
||||
import 'package:photos/models/file.dart';
|
||||
import 'package:photos/models/shared_collection.dart';
|
||||
import 'package:photos/ui/common_elements.dart';
|
||||
import 'package:photos/ui/loading_widget.dart';
|
||||
import 'package:photos/ui/thumbnail_widget.dart';
|
||||
|
||||
class SharedCollectionGallery extends StatefulWidget {
|
||||
const SharedCollectionGallery({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SharedCollectionGalleryState createState() =>
|
||||
_SharedCollectionGalleryState();
|
||||
}
|
||||
|
||||
class _SharedCollectionGalleryState extends State<SharedCollectionGallery> {
|
||||
Logger _logger = Logger("SharedCollectionGallery");
|
||||
StreamSubscription<RemoteSyncEvent> _subscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_subscription = Bus.instance.on<RemoteSyncEvent>().listen((event) {
|
||||
if (event.success) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<SharedCollectionWithThumbnail>>(
|
||||
future: CollectionsDB.instance
|
||||
.getAllSharedCollections()
|
||||
.then((collections) async {
|
||||
final c = List<SharedCollectionWithThumbnail>();
|
||||
for (final collection in collections) {
|
||||
var thumbnail;
|
||||
try {
|
||||
thumbnail =
|
||||
await FilesDB.instance.getLatestFileInCollection(collection.id);
|
||||
} catch (e) {
|
||||
_logger.warning(e.toString());
|
||||
}
|
||||
c.add(SharedCollectionWithThumbnail(collection, thumbnail));
|
||||
}
|
||||
return c;
|
||||
}),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (snapshot.data.isEmpty) {
|
||||
return nothingToSeeHere;
|
||||
} else {
|
||||
return _getSharedCollectionsGallery(snapshot.data);
|
||||
}
|
||||
} else if (snapshot.hasError) {
|
||||
_logger.shout(snapshot.error);
|
||||
return Center(child: Text(snapshot.error.toString()));
|
||||
} else {
|
||||
return loadWidget;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getSharedCollectionsGallery(
|
||||
List<SharedCollectionWithThumbnail> collections) {
|
||||
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 _buildCollection(context, collections[index]);
|
||||
},
|
||||
itemCount: collections.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCollection(
|
||||
BuildContext context, SharedCollectionWithThumbnail c) {
|
||||
_logger.info("Building collection " + c.collection.toString());
|
||||
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: "remote_folder" + 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: () {
|
||||
// TODO
|
||||
// final page = RemoteFolderPage(folder);
|
||||
// Navigator.of(context).push(
|
||||
// MaterialPageRoute(
|
||||
// builder: (BuildContext context) {
|
||||
// return page;
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class SharedCollectionWithThumbnail {
|
||||
final SharedCollection collection;
|
||||
final File thumbnail;
|
||||
|
||||
SharedCollectionWithThumbnail(this.collection, this.thumbnail);
|
||||
}
|
|
@ -36,6 +36,9 @@ class DiffFetcher {
|
|||
file.uploadedFileID = item["id"];
|
||||
file.ownerID = item["ownerID"];
|
||||
file.collectionID = item["collectionID"];
|
||||
if (file.collectionID == 4) {
|
||||
_logger.info("Found");
|
||||
}
|
||||
file.updationTime = item["updationTime"];
|
||||
file.isEncrypted = true;
|
||||
file.encryptedKey = item["encryptedKey"];
|
||||
|
|
Loading…
Add table
Reference in a new issue