diff --git a/assets/release_notes.md b/assets/release_notes.md index dd851dce..3ed94b80 100644 --- a/assets/release_notes.md +++ b/assets/release_notes.md @@ -2,6 +2,7 @@ --- - Added extra options for transferring stock items +- Fixes bug where API data was not fetched with correct locale ### 0.12.7 - August 2023 --- diff --git a/lib/api.dart b/lib/api.dart index b8a4f21f..ba641710 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -5,6 +5,8 @@ import "dart:io"; import "package:flutter/foundation.dart"; import "package:http/http.dart" as http; import "package:intl/intl.dart"; +import "package:inventree/main.dart"; +import "package:one_context/one_context.dart"; import "package:open_filex/open_filex.dart"; import "package:cached_network_image/cached_network_image.dart"; import "package:flutter/material.dart"; @@ -772,10 +774,9 @@ class InvenTreeAPI { _request = await client.openUrl("GET", _uri).timeout(Duration(seconds: 10)); // Set headers - _request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); - _request.headers.set(HttpHeaders.acceptHeader, "application/json"); - _request.headers.set(HttpHeaders.contentTypeHeader, "application/json"); - _request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); + defaultHeaders().forEach((key, value) { + _request?.headers.set(key, value); + }); } on SocketException catch (error) { debug("SocketException at ${url}: ${error.toString()}"); @@ -1083,10 +1084,9 @@ class InvenTreeAPI { _request = await client.openUrl(method, _uri).timeout(Duration(seconds: 10)); // Set headers - _request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); - _request.headers.set(HttpHeaders.acceptHeader, "application/json"); - _request.headers.set(HttpHeaders.contentTypeHeader, "application/json"); - _request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); + defaultHeaders().forEach((key, value) { + _request?.headers.set(key, value); + }); return _request; } on SocketException catch (error) { @@ -1268,6 +1268,7 @@ class InvenTreeAPI { urlParams: params, ); + if (request == null) { // Return an "invalid" APIResponse return APIResponse( @@ -1305,6 +1306,28 @@ class InvenTreeAPI { ); } + // Find the current locale code for the running app + String get currentLocale { + + if (OneContext.hasContext) { + // Try to get app context + BuildContext? context = OneContext().context; + + if (context != null) { + Locale? locale = InvenTreeApp + .of(context) + ?.locale; + + if (locale != null) { + return locale.languageCode; //.toString(); + } + } + } + + // Fallback value + return Intl.getCurrentLocale(); + } + // Return a list of request headers Map defaultHeaders() { Map headers = {}; @@ -1312,7 +1335,7 @@ class InvenTreeAPI { headers[HttpHeaders.authorizationHeader] = _authorizationHeader(); headers[HttpHeaders.acceptHeader] = "application/json"; headers[HttpHeaders.contentTypeHeader] = "application/json"; - headers[HttpHeaders.acceptLanguageHeader] = Intl.getCurrentLocale(); + headers[HttpHeaders.acceptLanguageHeader] = currentLocale; return headers; } @@ -1527,6 +1550,12 @@ class InvenTreeAPI { InvenTreeStatusCode get StockStatus => _get_status_class("stock/status/"); InvenTreeStatusCode get PurchaseOrderStatus => _get_status_class("order/po/status/"); + void clearStatusCodeData() { + StockHistoryStatus.data.clear(); + StockStatus.data.clear(); + PurchaseOrderStatus.data.clear(); + } + int notification_counter = 0; Timer? _notification_timer; diff --git a/lib/main.dart b/lib/main.dart index aa2adb60..3a7d2f44 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -152,6 +152,8 @@ class InvenTreeAppState extends State { }); } + Locale? get locale => _locale; + @override Widget build(BuildContext context) { diff --git a/lib/settings/app_settings.dart b/lib/settings/app_settings.dart index 1937ff54..f5d2ab48 100644 --- a/lib/settings/app_settings.dart +++ b/lib/settings/app_settings.dart @@ -1,4 +1,5 @@ import "package:flutter/material.dart"; +import "package:inventree/api.dart"; import "package:one_context/one_context.dart"; import "package:adaptive_theme/adaptive_theme.dart"; @@ -120,6 +121,9 @@ class _InvenTreeAppSettingsState extends State { // Refresh the entire app locale InvenTreeApp.of(context)?.setLocale(locale); + + // Clear the cached status label information + InvenTreeAPI().clearStatusCodeData(); } ); } diff --git a/pubspec.yaml b/pubspec.yaml index ab68b3e7..d4d197ab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: inventree description: InvenTree stock management -version: 0.12.7+74 +version: 0.12.8+75 environment: sdk: ">=2.19.5 <3.13.0"