Enable sharing of a folder

This commit is contained in:
Vishnu Mohandas 2020-05-18 02:09:21 +05:30
parent 2949d5858d
commit 97c1d0532b
3 changed files with 73 additions and 7 deletions

View file

@ -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()));
}
}

View file

@ -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;
}
}

View file

@ -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();
},
),