Skip to content

Commit

Permalink
fix connection testing race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaphasilor committed Jan 13, 2024
1 parent bdd3952 commit 333d907
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
42 changes: 24 additions & 18 deletions lib/components/LoginScreen/login_server_selection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,30 @@ class _LoginServerSelectionPageState extends State<LoginServerSelectionPage> {
widget.serverState.isTestingServerConnection = true;
});

String baseUrlToTest = widget.serverState.baseUrl!;

// We trim the base url in case the user accidentally added some trailing whitespace
widget.serverState.baseUrl = widget.serverState.baseUrl!.trim();
baseUrlToTest = baseUrlToTest.trim();

if (!(widget.serverState.baseUrl!.startsWith("http://") ||
widget.serverState.baseUrl!.startsWith("https://"))) {
if (!(baseUrlToTest.startsWith("http://") ||
baseUrlToTest.startsWith("https://"))) {
// use https by default
widget.serverState.baseUrl = "https://${widget.serverState.baseUrl}";
baseUrlToTest = "https://$baseUrlToTest";
unspecifiedProtocol = true;
}

// use regex to check if a port is specified
final portRegex = RegExp(r"[^\/]:\d+");
if (!portRegex.hasMatch(widget.serverState.baseUrl!)) {
if (!portRegex.hasMatch(baseUrlToTest)) {
unspecifiedPort = true;
}

if (widget.serverState.baseUrl!.endsWith("/")) {
widget.serverState.baseUrl = widget.serverState.baseUrl!
.substring(0, widget.serverState.baseUrl!.length - 1);
if (baseUrlToTest.endsWith("/")) {
baseUrlToTest = baseUrlToTest
.substring(0, baseUrlToTest.length - 1);
}

jellyfinApiHelper.baseUrlTemp = Uri.parse(widget.serverState.baseUrl!);
jellyfinApiHelper.baseUrlTemp = Uri.parse(baseUrlToTest);

PublicSystemInfoResult? publicServerInfo;
try {
Expand All @@ -261,9 +263,9 @@ class _LoginServerSelectionPageState extends State<LoginServerSelectionPage> {

if (publicServerInfo == null && unspecifiedProtocol) {
// try http
widget.serverState.baseUrl =
widget.serverState.baseUrl!.replaceFirst("https://", "http://");
jellyfinApiHelper.baseUrlTemp = Uri.parse(widget.serverState.baseUrl!);
Uri url = Uri.parse(baseUrlToTest).replace(scheme: "http");
baseUrlToTest = url.toString(); // update the local url
jellyfinApiHelper.baseUrlTemp = url;
try {
publicServerInfo = await jellyfinApiHelper.loadServerPublicInfo();
} catch (error) {
Expand All @@ -274,8 +276,9 @@ class _LoginServerSelectionPageState extends State<LoginServerSelectionPage> {

if (publicServerInfo == null && unspecifiedPort) {
// try default port 8096
widget.serverState.baseUrl = "${widget.serverState.baseUrl}:8096";
jellyfinApiHelper.baseUrlTemp = Uri.parse(widget.serverState.baseUrl!);
Uri url = Uri.parse(baseUrlToTest).replace(port: 8096);
baseUrlToTest = url.toString(); // update the local url
jellyfinApiHelper.baseUrlTemp = url;
try {
publicServerInfo = await jellyfinApiHelper.loadServerPublicInfo();
} catch (error) {
Expand All @@ -284,10 +287,13 @@ class _LoginServerSelectionPageState extends State<LoginServerSelectionPage> {
}
}

setState(() {
widget.serverState.isTestingServerConnection = false;
widget.serverState.manualServer = publicServerInfo;
});
if (publicServerInfo != null) {
setState(() {
widget.serverState.isTestingServerConnection = false;
widget.serverState.manualServer = publicServerInfo;
widget.serverState.baseUrl = baseUrlToTest;
});
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/services/finamp_user_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class FinampUserHelper {

/// Loads the FinampUser with the id from CurrentUserId. Returns null if no
/// user exists.
FinampUser? get currentUser =>
_finampUserBox.get(_currentUserIdBox.get("CurrentUserId"));
FinampUser? get currentUser => _currentUserIdBox.get("CurrentUserId") != null ?
_finampUserBox.get(_currentUserIdBox.get("CurrentUserId")) : null;

ValueListenable<Box<FinampUser>> get finampUsersListenable =>
_finampUserBox.listenable();
Expand Down Expand Up @@ -64,4 +64,4 @@ class FinampUserHelper {

_finampUserBox.delete(id);
}
}
}

0 comments on commit 333d907

Please sign in to comment.