Przeglądaj źródła

Enable sharing of a folder

Vishnu Mohandas 5 lat temu
rodzic
commit
97c1d0532b
3 zmienionych plików z 73 dodań i 7 usunięć
  1. 12 1
      lib/folder_service.dart
  2. 46 4
      lib/models/folder.dart
  3. 15 2
      lib/ui/share_folder_widget.dart

+ 12 - 1
lib/folder_service.dart

@@ -26,9 +26,11 @@ class FolderSharingService {
             headers: {"X-Auth-Token": Configuration.instance.getToken()}),
       )
           .then((foldersResponse) {
+        log(foldersResponse.toString());
         var folders = (foldersResponse.data as List)
             .map((f) => Folder.fromMap(f))
             .toList();
+        log(folders.toString());
         Folder sharedFolder;
         for (var folder in folders) {
           if (folder.owner == Configuration.instance.getUsername() &&
@@ -52,5 +54,14 @@ class FolderSharingService {
     });
   }
 
-  void shareFolder(String path) {}
+  Future<void> shareFolder(String name, String path, Set<String> users) {
+    var folder = Folder(0, name, Configuration.instance.getUsername(), path,
+        users.toList(), -1);
+    return _dio
+        .put(Configuration.instance.getHttpEndpoint() + "/folders/",
+            options: Options(
+                headers: {"X-Auth-Token": Configuration.instance.getToken()}),
+            data: folder.toMap())
+        .then((response) => log(response.toString()));
+  }
 }

+ 46 - 4
lib/models/folder.dart

@@ -1,5 +1,9 @@
+import 'dart:convert';
+
+import 'package:flutter/foundation.dart';
+
 class Folder {
-  final int folderID;
+  final int id;
   final String name;
   final String owner;
   final String deviceFolder;
@@ -7,7 +11,7 @@ class Folder {
   final int updateTimestamp;
 
   Folder(
-    this.folderID,
+    this.id,
     this.name,
     this.owner,
     this.deviceFolder,
@@ -19,7 +23,7 @@ class Folder {
     if (map == null) return null;
 
     return Folder(
-      map['folderID'],
+      map['id'],
       map['name'],
       map['owner'],
       map['deviceFolder'],
@@ -30,6 +34,44 @@ class Folder {
 
   @override
   String toString() {
-    return 'Folder(folderID: $folderID, name: $name, owner: $owner, deviceFolder: $deviceFolder, sharedWith: $sharedWith, updateTimestamp: $updateTimestamp)';
+    return 'Folder(id: $id, name: $name, owner: $owner, deviceFolder: $deviceFolder, sharedWith: $sharedWith, updateTimestamp: $updateTimestamp)';
+  }
+
+  Map<String, dynamic> toMap() {
+    return {
+      'id': id,
+      'name': name,
+      'owner': owner,
+      'deviceFolder': deviceFolder,
+      'sharedWith': sharedWith,
+      'updateTimestamp': updateTimestamp,
+    };
+  }
+
+  String toJson() => json.encode(toMap());
+
+  static Folder fromJson(String source) => fromMap(json.decode(source));
+
+  @override
+  bool operator ==(Object o) {
+    if (identical(this, o)) return true;
+
+    return o is Folder &&
+        o.id == id &&
+        o.name == name &&
+        o.owner == owner &&
+        o.deviceFolder == deviceFolder &&
+        listEquals(o.sharedWith, sharedWith) &&
+        o.updateTimestamp == updateTimestamp;
+  }
+
+  @override
+  int get hashCode {
+    return id.hashCode ^
+        name.hashCode ^
+        owner.hashCode ^
+        deviceFolder.hashCode ^
+        sharedWith.hashCode ^
+        updateTimestamp.hashCode;
   }
 }

+ 15 - 2
lib/ui/share_folder_widget.dart

@@ -18,12 +18,15 @@ class ShareFolderWidget extends StatefulWidget {
 }
 
 class _ShareFolderWidgetState extends State<ShareFolderWidget> {
+  Map<String, bool> _sharingStatus;
+
   @override
   Widget build(BuildContext context) {
     return FutureBuilder<Map<String, bool>>(
       future: FolderSharingService.instance.getSharingStatus(widget.path),
       builder: (context, snapshot) {
         if (snapshot.hasData) {
+          _sharingStatus = snapshot.data;
           return _getSharingDialog(snapshot.data);
         } else if (snapshot.hasError) {
           return Text(snapshot.error.toString());
@@ -47,8 +50,18 @@ class _ShareFolderWidgetState extends State<ShareFolderWidget> {
       actions: <Widget>[
         FlatButton(
           child: Text("Share"),
-          onPressed: () {
-            // TODO: FolderSharingService.instance.shareFolder();
+          onPressed: () async {
+            var sharedWith = Set<String>();
+            for (var user in _sharingStatus.keys) {
+              if (_sharingStatus[user]) {
+                sharedWith.add(user);
+              }
+            }
+            await FolderSharingService.instance.shareFolder(
+              "namewa",
+              widget.path,
+              sharedWith,
+            );
             Navigator.of(context).pop();
           },
         ),