diff --git a/lib/src/localization/app_en.arb b/lib/src/localization/app_en.arb index 03f9151..3272218 100644 --- a/lib/src/localization/app_en.arb +++ b/lib/src/localization/app_en.arb @@ -10,6 +10,8 @@ "chatsTitle": "Chats", "searchUsernameInput": "Username", "searchUsernameTitle": "Search username", + "searchUsernameNotFound": "Username not found", + "searchUsernameNotFoundLong": "\"{username}\" is not a twonly user. Please check the username and try again.", "errorUnknown": "An unexpected error has occurred. Please try again later.", "errorBadRequest": "The request could not be understood by the server due to malformed syntax. Please check your input and try again.", "errorTooManyRequests": "You have made too many requests in a short period. Please wait a moment before trying again.", diff --git a/lib/src/utils.dart b/lib/src/utils.dart index f7f2eac..89b8a63 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -61,16 +61,14 @@ Uint8List getRandomUint8List(int length) { return randomBytes; } -Future addNewUser(String username) async { +Future addNewUser(String username) async { final res = await apiProvider.getUserData(username); - // if (res.isSuccess) { - // print("Got user_id ${res.value}"); - // final userData = UserData( - // userId: res.value.userid, username: username, displayName: username); - // } + if (res.isSuccess) { + print("Found user: ${res.value}"); + } - return res; + return res.isSuccess; } Future createNewUser(String username, String inviteCode) async { diff --git a/lib/src/views/search_username_view.dart b/lib/src/views/search_username_view.dart index ed7af1d..65e59ff 100644 --- a/lib/src/views/search_username_view.dart +++ b/lib/src/views/search_username_view.dart @@ -1,5 +1,10 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:logging/logging.dart'; +import 'package:twonly/src/utils.dart'; +import 'package:twonly/src/views/register_view.dart'; class SearchUsernameView extends StatefulWidget { const SearchUsernameView({super.key}); @@ -11,6 +16,38 @@ class SearchUsernameView extends StatefulWidget { class _SearchUsernameView extends State { final TextEditingController searchUserName = TextEditingController(); + bool _isLoading = false; + + Future _addNewUser(BuildContext context) async { + Timer timer = Timer(Duration(milliseconds: 500), () { + setState(() { + _isLoading = true; + }); + }); + + final status = await addNewUser(searchUserName.text); + + timer.cancel(); + // loaderDelay.timeout(Duration(microseconds: 0)); + setState(() { + _isLoading = false; + }); + Logger("search_user_name").warning("Replace instead of pop"); + + if (context.mounted) { + if (status) { + Navigator.pop(context); + } else if (context.mounted) { + showAlertDialog( + context, + AppLocalizations.of(context)!.searchUsernameNotFound, + AppLocalizations.of(context)! + .searchUsernameNotFoundLong(searchUserName.text)); + } + } + return status; + } + @override Widget build(BuildContext context) { InputDecoration getInputDecoration(hintText) { @@ -42,15 +79,26 @@ class _SearchUsernameView extends State { Padding( padding: EdgeInsets.symmetric(horizontal: 10), child: TextField( - onSubmitted: (value) { - print(value); + onSubmitted: (_) { + _addNewUser(context); }, + controller: searchUserName, decoration: getInputDecoration( AppLocalizations.of(context)!.searchUsernameInput))), - const SizedBox(height: 10), + const SizedBox(height: 40), + if (_isLoading) const Center(child: CircularProgressIndicator()) ], ), ), + floatingActionButton: Padding( + padding: const EdgeInsets.only(bottom: 30.0), + child: FloatingActionButton( + onPressed: () { + _addNewUser(context); + }, + child: const Icon(Icons.arrow_right_rounded), + ), + ), ); } }