Vishnu Mohandas преди 4 години
родител
ревизия
0849ef21c1
променени са 2 файла, в които са добавени 50 реда и са изтрити 14 реда
  1. 20 1
      lib/services/collections_service.dart
  2. 30 13
      lib/ui/share_folder_widget.dart

+ 20 - 1
lib/services/collections_service.dart

@@ -55,10 +55,29 @@ class CollectionsService {
     )
     )
         .then((response) {
         .then((response) {
       _logger.info(response.toString());
       _logger.info(response.toString());
-      return response.data["emails"] as List<String>;
+      final emails = List<String>();
+      for (final email in response.data["emails"]) {
+        emails.add(email);
+      }
+      return emails;
     });
     });
   }
   }
 
 
+  Future<void> share(int collectionID, String email, String publicKey) {
+    final encryptedKey = CryptoUtil.sealSync(
+        getCollectionKey(collectionID), Sodium.base642bin(publicKey));
+    return Dio().post(
+      Configuration.instance.getHttpEndpoint() + "/collections/share",
+      data: {
+        "collectionID": collectionID,
+        "email": email,
+        "encryptedKey": Sodium.bin2base64(encryptedKey),
+      },
+      options:
+          Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
+    );
+  }
+
   Uint8List getCollectionKey(int collectionID) {
   Uint8List getCollectionKey(int collectionID) {
     if (!_cachedKeys.containsKey(collectionID)) {
     if (!_cachedKeys.containsKey(collectionID)) {
       final collection = _collectionIDToCollection[collectionID];
       final collection = _collectionIDToCollection[collectionID];

+ 30 - 13
lib/ui/share_folder_widget.dart

@@ -30,18 +30,15 @@ class ShareFolderWidget extends StatefulWidget {
 }
 }
 
 
 class _ShareFolderWidgetState extends State<ShareFolderWidget> {
 class _ShareFolderWidgetState extends State<ShareFolderWidget> {
-  bool _showEntryField = false;
-  String _email;
-
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     return FutureBuilder<List<String>>(
     return FutureBuilder<List<String>>(
       future: widget.collection == null
       future: widget.collection == null
-          ? List<String>()
+          ? Future.value(List<String>())
           : CollectionsService.instance.getSharees(widget.collection.id),
           : CollectionsService.instance.getSharees(widget.collection.id),
       builder: (context, snapshot) {
       builder: (context, snapshot) {
         if (snapshot.hasData) {
         if (snapshot.hasData) {
-          return _getSharingDialog(snapshot.data);
+          return SharingDialog(widget.collection, snapshot.data);
         } else if (snapshot.hasError) {
         } else if (snapshot.hasError) {
           return Text(snapshot.error.toString());
           return Text(snapshot.error.toString());
         } else {
         } else {
@@ -50,15 +47,32 @@ class _ShareFolderWidgetState extends State<ShareFolderWidget> {
       },
       },
     );
     );
   }
   }
+}
 
 
-  Widget _getSharingDialog(List<String> sharees) {
-    log(sharees.toString());
+class SharingDialog extends StatefulWidget {
+  final Collection collection;
+  final List<String> sharees;
+
+  SharingDialog(this.collection, this.sharees, {Key key}) : super(key: key);
+
+  @override
+  _SharingDialogState createState() => _SharingDialogState();
+}
+
+class _SharingDialogState extends State<SharingDialog> {
+  bool _showEntryField = false;
+  List<String> _sharees;
+  String _email;
+
+  @override
+  Widget build(BuildContext context) {
+    _sharees = widget.sharees;
     final children = List<Widget>();
     final children = List<Widget>();
     if (!_showEntryField &&
     if (!_showEntryField &&
-        (widget.collection == null || sharees.length == 0)) {
+        (widget.collection == null || _sharees.length == 0)) {
       children.add(Text("Click the + button to share this folder."));
       children.add(Text("Click the + button to share this folder."));
     } else {
     } else {
-      for (final email in sharees) {
+      for (final email in _sharees) {
         children.add(EmailItemWidget(email));
         children.add(EmailItemWidget(email));
       }
       }
     }
     }
@@ -164,10 +178,13 @@ class _ShareFolderWidgetState extends State<ShareFolderWidget> {
         // TODO: Create collection
         // TODO: Create collection
         // TODO: Add files to collection
         // TODO: Add files to collection
       }
       }
-      // TODO: Add email to collection
-      setState(() {
-        // sharees.add(email);
-        _showEntryField = false;
+      CollectionsService.instance
+          .share(widget.collection.id, _email, publicKey)
+          .then((value) {
+        setState(() {
+          _sharees.add(_email);
+          _showEntryField = false;
+        });
       });
       });
     }
     }
   }
   }