Skip to content

Commit

Permalink
Remove _BASE_URL accessor
Browse files Browse the repository at this point in the history
- Fixes URL caching bug
  • Loading branch information
SchrodingersGat committed Oct 22, 2023
1 parent ecc147e commit 5d1766c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
14 changes: 4 additions & 10 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,9 @@ class InvenTreeAPI {
static const _URL_ROLES = "user/roles/";
static const _URL_ME = "user/me/";

// Base URL for InvenTree API e.g. http://192.168.120.10:8000
String _BASE_URL = "";

// Accessors for various url endpoints
String get baseUrl {
String url = _BASE_URL;
String url = profile?.server ?? "";

if (!url.endsWith("/")) {
url += "/";
Expand Down Expand Up @@ -385,8 +382,6 @@ class InvenTreeAPI {
*/
Future<bool> _connectToServer() async {

debug("Connecting to server...");

if (!await _checkServer()) {
return false;
}
Expand Down Expand Up @@ -441,9 +436,6 @@ class InvenTreeAPI {
address = address + "/";
}

// Save the base URL
_BASE_URL = address;

// Cache the "strictHttps" setting, so we can use it later without async requirement
_strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;

Expand Down Expand Up @@ -522,7 +514,9 @@ class InvenTreeAPI {
*/
Future<bool> fetchToken(UserProfile userProfile, String authHeader) async {

debug("Fetching user token");
debug("Fetching user token from ${userProfile.server}");

profile = userProfile;

// Form a name to request the token with
String platform_name = "inventree-mobile-app";
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "package:inventree/widget/progress.dart";

class InvenTreeLoginWidget extends StatefulWidget {

InvenTreeLoginWidget(this.profile);
InvenTreeLoginWidget(this.profile) : super();

UserProfile profile;

Expand Down
5 changes: 3 additions & 2 deletions lib/settings/select_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class _InvenTreeSelectServerState extends State<InvenTreeSelectServerWidget> {

await UserProfileDBManager().selectProfile(key);

UserProfile? prf = await UserProfileDBManager().getSelectedProfile();
UserProfile? prf = await UserProfileDBManager().getProfileByKey(key);

if (prf == null) {
return;
Expand All @@ -97,7 +97,8 @@ class _InvenTreeSelectServerState extends State<InvenTreeSelectServerWidget> {
MaterialPageRoute(builder: (context) => InvenTreeLoginWidget(profile))
).then((value) async {
_reload();
prf = await UserProfileDBManager().getSelectedProfile();
// Reload profile
prf = await UserProfileDBManager().getProfileByKey(key);
if (prf?.hasToken ?? false) {
InvenTreeAPI().connectToServer(prf!).then((result) {
_reload();
Expand Down
20 changes: 20 additions & 0 deletions lib/user_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ class UserProfileDBManager {
return profileList;
}


/*
* Retrieve a profile by key (or null if no match exists)
*/
Future<UserProfile?> getProfileByKey(int key) async {
final profiles = await getAllProfiles();

UserProfile? prf;

for (UserProfile profile in profiles) {
if (profile.key == key) {
prf = profile;
break;
}
}

return prf;
}


/*
* Retrieve a profile by name (or null if no match exists)
*/
Expand Down

0 comments on commit 5d1766c

Please sign in to comment.