diff --git a/lib/i18n/app_en.arb b/lib/i18n/app_en.arb index f426eff..e06ff66 100644 --- a/lib/i18n/app_en.arb +++ b/lib/i18n/app_en.arb @@ -36,6 +36,9 @@ "friendActive": "Active", "friendBlocked": "Blocked", "friendListHint": "Swipe left to decline, swipe right to approve", + "friendAdd": "Add friend", + "friendAddHint": "Use your their username to send a friend request to your best friend!", + "friendAddDone": "Friend request sent, go reach your friend!", "reaction": "Reaction", "reactVerb": "React", "post": "Post", diff --git a/lib/i18n/app_zh.arb b/lib/i18n/app_zh.arb index 1aafd55..29680ed 100644 --- a/lib/i18n/app_zh.arb +++ b/lib/i18n/app_zh.arb @@ -36,6 +36,9 @@ "friendActive": "活跃的好友", "friendBlocked": "封锁中", "friendListHint": "左滑来拒绝,右滑来接受", + "friendAdd": "添加好友", + "friendAddHint": "使用用户名来给你的好朋友发一个好友请求吧!", + "friendAddDone": "好友请求已发送,快告诉你的朋友吧!", "reaction": "反应", "reactVerb": "作出反应", "post": "帖子", diff --git a/lib/screens/account/friend.dart b/lib/screens/account/friend.dart index c0b1e29..dd3c22f 100644 --- a/lib/screens/account/friend.dart +++ b/lib/screens/account/friend.dart @@ -46,6 +46,30 @@ class _FriendScreenState extends State { } } + Future createFriendship(String username) async { + setState(() => _isSubmitting = true); + + final auth = context.read(); + if (!await auth.isAuthorized()) { + setState(() => _isSubmitting = false); + return; + } + + var res = await auth.client!.post( + getRequestUri('passport', '/api/users/me/friends?related=$username'), + ); + if (res.statusCode == 200) { + await fetchFriendships(); + } else { + var message = utf8.decode(res.bodyBytes); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Something went wrong... $message")), + ); + } + + setState(() => _isSubmitting = false); + } + Future updateFriendship(Friendship relation, int status) async { setState(() => _isSubmitting = true); @@ -67,6 +91,9 @@ class _FriendScreenState extends State { }), ); if (res.statusCode == 200) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(AppLocalizations.of(context)!.friendAddDone)), + ); await fetchFriendships(); } else { var message = utf8.decode(res.bodyBytes); @@ -78,6 +105,54 @@ class _FriendScreenState extends State { setState(() => _isSubmitting = false); } + void promptAddFriend() async { + final controller = TextEditingController(); + final input = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text(AppLocalizations.of(context)!.friendAdd), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text(AppLocalizations.of(context)!.friendAddHint), + const SizedBox(height: 18), + TextField( + controller: controller, + decoration: InputDecoration( + isDense: true, + prefixIcon: const Icon(Icons.account_circle), + border: const OutlineInputBorder(), + labelText: AppLocalizations.of(context)!.username, + ), + onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(), + ), + ], + ), + actions: [ + TextButton( + style: TextButton.styleFrom( + foregroundColor: Theme.of(context).colorScheme.onSurface.withOpacity(0.8), + ), + onPressed: () => Navigator.pop(context), + child: Text(AppLocalizations.of(context)!.cancel), + ), + TextButton( + child: Text(AppLocalizations.of(context)!.next), + onPressed: () { + Navigator.pop(context, controller.text); + }, + ), + ], + ); + }, + ); + + WidgetsBinding.instance.addPostFrameCallback((_) => controller.dispose()); + + await createFriendship(input); + } + List filterWithStatus(int status) { return _friendships.where((x) => x.status == status).toList(); } @@ -153,6 +228,12 @@ class _FriendScreenState extends State { return IndentWrapper( title: AppLocalizations.of(context)!.friend, + appBarActions: [ + IconButton( + icon: const Icon(Icons.add), + onPressed: () => promptAddFriend(), + ), + ], child: RefreshIndicator( onRefresh: () => fetchFriendships(), child: CustomScrollView(