Autoformat for better readable code,
using autofocus instead of useState and focus element
This commit is contained in:
istalri 2026-06-02 21:43:14 +02:00
commit 26feae4485
4 changed files with 181 additions and 177 deletions

View file

@ -263,12 +263,12 @@ class ClientController extends AsyncNotifier<int> {
} }
} }
Future<String?> discoverHomeserver(Uri homeserver) async { Future<Uri?> discoverHomeserver(Uri homeserver) async {
try { try {
final response = await _sendCommand("discover_homeserver", { final response = await _sendCommand("discover_homeserver", {
"user_id": "@fake-user:${homeserver.host}", "user_id": "@fake-user:${homeserver.host}",
}); });
return response["m.homeserver"]?["base_url"]; return Uri.parse(response["m.homeserver"]?["base_url"]);
} catch (error) { } catch (error) {
return null; return null;
} }

View file

@ -15,17 +15,8 @@ class LoginPage extends HookConsumerWidget {
final client = ref.watch(ClientController.provider.notifier); final client = ref.watch(ClientController.provider.notifier);
final isLoggingIn = useState(false); final isLoggingIn = useState(false);
final hasError = useState(false); final hasError = useState(false);
final userNameFocusNode = useFocusNode();
final passwordFocusNode = useFocusNode(); final passwordFocusNode = useFocusNode();
//This is the safe way to request things directly after page load.
useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((_) {
userNameFocusNode.requestFocus();
});
return null;
}, []);
final theme = Theme.of(context); final theme = Theme.of(context);
final username = useTextEditingController(); final username = useTextEditingController();
@ -40,8 +31,8 @@ class LoginPage extends HookConsumerWidget {
LoginRequest( LoginRequest(
username: username.text, username: username.text,
password: password.text, password: password.text,
homeserverUrl: homeserver.origin homeserverUrl: homeserver.origin,
) ),
); );
if (!context.mounted) return; if (!context.mounted) return;
@ -52,16 +43,13 @@ class LoginPage extends HookConsumerWidget {
SnackBar( SnackBar(
content: Text( content: Text(
"Login failed. Is your password right?\nError: $error", "Login failed. Is your password right?\nError: $error",
style: TextStyle( style: TextStyle(color: theme.colorScheme.onErrorContainer),
color: theme.colorScheme.onErrorContainer,
),
), ),
backgroundColor: theme.colorScheme.errorContainer, backgroundColor: theme.colorScheme.errorContainer,
), ),
); );
isLoggingIn.value = false; isLoggingIn.value = false;
} } else {
else{
Navigator.pop(context); Navigator.pop(context);
} }
passwordFocusNode.requestFocus(); passwordFocusNode.requestFocus();
@ -72,7 +60,7 @@ class LoginPage extends HookConsumerWidget {
appBar: Appbar( appBar: Appbar(
leading: IconButton( leading: IconButton(
icon: Icon(Icons.arrow_back), icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context) onPressed: () => Navigator.pop(context),
), ),
), ),
body: AlertDialog( body: AlertDialog(
@ -81,12 +69,10 @@ class LoginPage extends HookConsumerWidget {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Text("Enter your login credentials:"),
"Enter your login credentials:",
),
SizedBox(height: 12), SizedBox(height: 12),
TextField( TextField(
focusNode: userNameFocusNode, autofocus: true,
textInputAction: TextInputAction.next, textInputAction: TextInputAction.next,
onChanged: (newVal) { onChanged: (newVal) {
if (hasError.value) { if (hasError.value) {
@ -100,9 +86,9 @@ class LoginPage extends HookConsumerWidget {
width: hasError.value ? 4 : 2, width: hasError.value ? 4 : 2,
color: hasError.value color: hasError.value
? theme.colorScheme.error ? theme.colorScheme.error
: theme.colorScheme.primary : theme.colorScheme.primary,
),
), ),
)
), ),
controller: username, controller: username,
), ),
@ -124,17 +110,17 @@ class LoginPage extends HookConsumerWidget {
width: hasError.value ? 4 : 2, width: hasError.value ? 4 : 2,
color: hasError.value color: hasError.value
? theme.colorScheme.error ? theme.colorScheme.error
: theme.colorScheme.primary : theme.colorScheme.primary,
) ),
), ),
enabledBorder: OutlineInputBorder( enabledBorder: OutlineInputBorder(
borderSide: BorderSide( borderSide: BorderSide(
width: hasError.value ? 4 : 2, width: hasError.value ? 4 : 2,
color: hasError.value color: hasError.value
? theme.colorScheme.error ? theme.colorScheme.error
: theme.colorScheme.primary : theme.colorScheme.primary,
),
), ),
)
), ),
controller: password, controller: password,
obscureText: true, obscureText: true,
@ -142,10 +128,7 @@ class LoginPage extends HookConsumerWidget {
], ],
), ),
actions: [ actions: [
TextButton( TextButton(onPressed: () => tryLogin(), child: Text("Sign In")),
onPressed: () => tryLogin(),
child: Text("Sign In"),
),
], ],
), ),
); );

View file

@ -27,7 +27,7 @@ class SelectServerPage extends HookConsumerWidget {
Future<void> setHomeserver(Uri? newHomeserver) async { Future<void> setHomeserver(Uri? newHomeserver) async {
isLoading.value = true; isLoading.value = true;
if(newHomeserver?.hasScheme == false){ if (newHomeserver?.hasScheme == false) {
newHomeserver = Uri.https(newHomeserver!.path); newHomeserver = Uri.https(newHomeserver!.path);
} }
@ -49,13 +49,20 @@ class SelectServerPage extends HookConsumerWidget {
); );
} else { } else {
homeserverUrl.text = newHomeserver!.origin; homeserverUrl.text = newHomeserver!.origin;
Navigator.push(context, MaterialPageRoute(builder: (_) => LoginPage(homeserver: Uri.parse(newUrl)))); Navigator.push(
context,
MaterialPageRoute(
builder: (_) => LoginPage(homeserver: Uri.parse(newUrl)),
),
);
} }
} }
if (context.mounted) {
homeserverFocusNode.requestFocus(); homeserverFocusNode.requestFocus();
isLoading.value = false; isLoading.value = false;
} }
}
return Scaffold( return Scaffold(
appBar: Appbar(), appBar: Appbar(),
@ -74,7 +81,10 @@ class SelectServerPage extends HookConsumerWidget {
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text("Nexus", style: theme.textTheme.displayMedium), Text(
"Nexus",
style: theme.textTheme.displayMedium,
),
Text( Text(
"A Simple Matrix Client", "A Simple Matrix Client",
style: theme.textTheme.headlineMedium, style: theme.textTheme.headlineMedium,
@ -97,7 +107,8 @@ class SelectServerPage extends HookConsumerWidget {
child: TextField( child: TextField(
focusNode: homeserverFocusNode, focusNode: homeserverFocusNode,
textInputAction: TextInputAction.done, textInputAction: TextInputAction.done,
onSubmitted: (_) => setHomeserver(Uri.tryParse(homeserverUrl.text)), onSubmitted: (_) =>
setHomeserver(Uri.tryParse(homeserverUrl.text)),
onChanged: (newVal) { onChanged: (newVal) {
if (hasError.value) { if (hasError.value) {
hasError.value = false; hasError.value = false;
@ -112,16 +123,16 @@ class SelectServerPage extends HookConsumerWidget {
width: hasError.value ? 4 : 2, width: hasError.value ? 4 : 2,
color: hasError.value color: hasError.value
? theme.colorScheme.error ? theme.colorScheme.error
: theme.colorScheme.primary : theme.colorScheme.primary,
) ),
), ),
enabledBorder: OutlineInputBorder( enabledBorder: OutlineInputBorder(
borderSide: BorderSide( borderSide: BorderSide(
width: hasError.value ? 4 : 2, width: hasError.value ? 4 : 2,
color: hasError.value color: hasError.value
? theme.colorScheme.error ? theme.colorScheme.error
: theme.colorScheme.primary : theme.colorScheme.primary,
) ),
), ),
), ),
), ),
@ -130,7 +141,9 @@ class SelectServerPage extends HookConsumerWidget {
tooltip: "Confirm homeserver choice", tooltip: "Confirm homeserver choice",
onPressed: isLoading.value onPressed: isLoading.value
? null ? null
: () => setHomeserver(Uri.tryParse(homeserverUrl.text)), : () => setHomeserver(
Uri.tryParse(homeserverUrl.text),
),
icon: Icon(Icons.check), icon: Icon(Icons.check),
), ),
], ],
@ -139,7 +152,9 @@ class SelectServerPage extends HookConsumerWidget {
child: ListView( child: ListView(
padding: EdgeInsets.only(top: 12), padding: EdgeInsets.only(top: 12),
children: [ children: [
DividerText("Or, choose from some popular homeservers:"), DividerText(
"Or, choose from some popular homeservers:",
),
...(<Homeserver>[ ...(<Homeserver>[
Homeserver( Homeserver(
name: "Matrix.org", name: "Matrix.org",
@ -154,20 +169,25 @@ class SelectServerPage extends HookConsumerWidget {
description: description:
"Federated Nexus is a community resource hosting multiple FOSS (especially federated) services, including Matrix and Forgejo. By the same developers who made Nexus client.", "Federated Nexus is a community resource hosting multiple FOSS (especially federated) services, including Matrix and Forgejo. By the same developers who made Nexus client.",
url: Uri.https("federated.nexus"), url: Uri.https("federated.nexus"),
iconUrl: "https://federated.nexus/images/icon.png", iconUrl:
"https://federated.nexus/images/icon.png",
), ),
Homeserver( Homeserver(
name: "Unredacted", name: "Unredacted",
description: description:
"Unredacted is a 501(c)(3) non-profit organization that builds Internet infrastructure and services to help people evade censorship and protect their right to privacy.", "Unredacted is a 501(c)(3) non-profit organization that builds Internet infrastructure and services to help people evade censorship and protect their right to privacy.",
url: Uri.https("unredacted.org", "services/si/matrix"), url: Uri.https(
"unredacted.org",
"services/si/matrix",
),
iconUrl: "https://unredacted.org/favicon.ico", iconUrl: "https://unredacted.org/favicon.ico",
), ),
Homeserver( Homeserver(
name: "Lorem ipsum", name: "Lorem ipsum",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", description:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
url: Uri.https("loremipsum.io"), url: Uri.https("loremipsum.io"),
iconUrl: "https://loremipsum.io/favicon.ico" iconUrl: "https://loremipsum.io/favicon.ico",
), ),
].map( ].map(
(homeserver) => Card( (homeserver) => Card(
@ -195,7 +215,8 @@ class SelectServerPage extends HookConsumerWidget {
), ),
SizedBox(height: 5), SizedBox(height: 5),
TextButton( TextButton(
onPressed: () => launch(Uri.https("servers.joinmatrix.org")), onPressed: () =>
launch(Uri.https("servers.joinmatrix.org")),
child: Text("See more homeservers..."), child: Text("See more homeservers..."),
), ),
], ],