Skip to content

Commit ee7e084

Browse files
committed
Add user search to invite creation
1 parent 21bb994 commit ee7e084

3 files changed

Lines changed: 105 additions & 32 deletions

File tree

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
23
import 'package:flutter/widgets.dart';
4+
import 'package:shock_alarm_app/components/predefined_spacing.dart';
35
import 'package:shock_alarm_app/components/qr_card.dart';
46
import 'package:shock_alarm_app/dialogs/error_dialog.dart';
57
import 'package:shock_alarm_app/dialogs/info_dialog.dart';
68
import 'package:shock_alarm_app/dialogs/loading_dialog.dart';
79
import 'package:shock_alarm_app/dialogs/share_or_qr_dialog.dart';
810
import 'package:shock_alarm_app/screens/shares/shares.dart';
11+
import 'package:shock_alarm_app/screens/user_shares/user_chip.dart';
912
import 'package:shock_alarm_app/services/alarm_manager.dart';
1013
import 'package:shock_alarm_app/services/limits.dart';
1114
import 'package:shock_alarm_app/services/openshock.dart';
@@ -15,42 +18,86 @@ class CreateUserShareDialog extends StatefulWidget {
1518
OpenShockShareLimits limits = OpenShockShareLimits();
1619
OpenShockUser? user;
1720

18-
1921
CreateUserShareDialog({required this.shockersToShare});
20-
22+
2123
@override
2224
State<StatefulWidget> createState() => _CreateUserShareDialog();
2325
}
2426

2527
class _CreateUserShareDialog extends State<CreateUserShareDialog> {
26-
@override Widget build(BuildContext context) {
28+
TextEditingController usernameController = TextEditingController();
29+
30+
void searchUser() async {
31+
widget.user = await OpenShockClient().getUserByUsername(usernameController.text);
32+
if(widget.user == null) {
33+
ErrorDialog.show("User not found", "Couldn't find a user by the name '${usernameController.text}'");
34+
}
35+
setState(() { });
36+
}
37+
38+
@override
39+
Widget build(BuildContext context) {
2740
// TODO: implement build
28-
return AlertDialog.adaptive(title: Text("Create share invite"),content: ShockerShareEntryEditor(limits: widget.limits),actions: [
29-
TextButton(
30-
onPressed: () {
31-
Navigator.of(context).pop();
32-
},
33-
child: Text("Cancel")),
34-
TextButton(
35-
onPressed: () async {
36-
LoadingDialog.show("Creating invite");
37-
widget.limits.validate();
38-
39-
ErrorContainer<String> error = await OpenShockClient()
40-
.createInvite(widget.shockersToShare, widget.limits, widget.user);
41-
Navigator.of(context).pop();
42-
if (error.error != null) {
43-
ErrorDialog.show("Failed to create invite", error.error!);
44-
return;
45-
}
46-
Navigator.of(context).pop();
47-
if(widget.user != null) {
48-
InfoDialog.show("Invite sent", "${widget.user?.name} got an invite to accept your share. Once they accept it they will have access to the shockers you shares. If you want to revoke the invite just cancel it in the Shares tab.");
49-
} else {
50-
ShareOrQrDialog.show("Invite created", "Share it with your friend! You can always do this at a later time via the Shares tab.", "I invite you to control my shockers. Here's the invite code: ${error.value}", "openshock://invite/${error.value}", "Scan to claim invite");
51-
}
52-
},
53-
child: Text("Create share"))
54-
],);
41+
return AlertDialog.adaptive(
42+
title: Text("Create share invite"),
43+
content: SingleChildScrollView(
44+
child: Column(
45+
children: [
46+
Row(
47+
children: [
48+
Text("Share with: "),
49+
UserChip(user: widget.user, altText: "decide later")
50+
],
51+
),
52+
Row(
53+
children: [
54+
Expanded(child: TextField(
55+
decoration: InputDecoration(labelText: "username"),
56+
57+
controller: usernameController,
58+
),),
59+
IconButton(icon: Icon(Icons.search), onPressed: searchUser,)
60+
],
61+
),
62+
Padding(padding: PredefinedSpacing.paddingMedium()),
63+
ShockerShareEntryEditor(limits: widget.limits)
64+
],
65+
),
66+
),
67+
actions: [
68+
TextButton(
69+
onPressed: () {
70+
Navigator.of(context).pop();
71+
},
72+
child: Text("Cancel")),
73+
TextButton(
74+
onPressed: () async {
75+
LoadingDialog.show("Creating invite");
76+
widget.limits.validate();
77+
78+
ErrorContainer<String> error = await OpenShockClient()
79+
.createInvite(
80+
widget.shockersToShare, widget.limits, widget.user);
81+
Navigator.of(context).pop();
82+
if (error.error != null) {
83+
ErrorDialog.show("Failed to create invite", error.error!);
84+
return;
85+
}
86+
Navigator.of(context).pop();
87+
if (widget.user != null) {
88+
InfoDialog.show("Invite sent",
89+
"${widget.user?.name} got an invite to accept your share. Once they accept it they will have access to the shockers you shares. If you want to revoke the invite just cancel it in the Shares tab.");
90+
} else {
91+
ShareOrQrDialog.show(
92+
"Invite created",
93+
"Share it with your friend! You can always do this at a later time via the Shares tab.",
94+
"I invite you to control my shockers. Here's the invite code: ${error.value}",
95+
"openshock://invite/${error.value}",
96+
"Scan to claim invite");
97+
}
98+
},
99+
child: Text("Create share"))
100+
],
101+
);
55102
}
56-
}
103+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:shock_alarm_app/services/openshock.dart';
3+
4+
class UserChip extends StatelessWidget {
5+
OpenShockUser? user;
6+
String altText;
7+
8+
UserChip({required this.user, this.altText = "Unknown user"});
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return Chip(label: Text(user?.name ?? altText));
13+
}
14+
}

lib/services/openshock.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,18 @@ class OpenShockClient {
907907
null, getErrorCode(response, "Failed to get backend information"));
908908
}
909909

910+
Future<OpenShockUser?> getUserByUsername(String username) async {
911+
Token? t = await AlarmListManager.getInstance().getSpecificUserToken();
912+
if (t == null) return null;
913+
var response = await GetRequest(
914+
t,
915+
"/1/users/by-name/${username}");
916+
if (response.statusCode == 200) {
917+
return OpenShockUser.fromJson(jsonDecode(response.body));
918+
}
919+
return null;
920+
}
921+
910922
}
911923

912924
class OpenShockBackendInformationData {
@@ -1391,7 +1403,7 @@ class OpenShockUser {
13911403
name = json["name"];
13921404
image = json["image"];
13931405
if (json["connectionId"] != null) connectionId = json["connectionId"];
1394-
customName = json["customName"];
1406+
if(json["customName"] != null) customName = json["customName"];
13951407
}
13961408

13971409
toJson() {

0 commit comments

Comments
 (0)