mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-31 05:15:42 +00:00 
			
		
		
		
	Code cleanup
This commit is contained in:
		
							
								
								
									
										48
									
								
								lib/api.dart
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								lib/api.dart
									
									
									
									
									
								
							| @@ -14,10 +14,8 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||
| import 'package:flutter_cache_manager/flutter_cache_manager.dart'; | ||||
| import 'package:InvenTree/widget/dialogs.dart'; | ||||
| import 'package:InvenTree/l10.dart'; | ||||
| import 'package:InvenTree/inventree/sentry.dart'; | ||||
|  | ||||
| import 'package:http/http.dart' as http; | ||||
| import 'package:one_context/one_context.dart'; | ||||
|  | ||||
| /** | ||||
|  * Custom FileService for caching network images | ||||
| @@ -26,7 +24,7 @@ import 'package:one_context/one_context.dart'; | ||||
|  */ | ||||
| class InvenTreeFileService extends FileService { | ||||
|  | ||||
|   HttpClient? _client = null; | ||||
|   HttpClient? _client; | ||||
|  | ||||
|   InvenTreeFileService({HttpClient? client, bool strictHttps = false}) { | ||||
|     _client = client ?? HttpClient(); | ||||
| @@ -113,7 +111,7 @@ class InvenTreeAPI { | ||||
|  | ||||
|   String makeUrl(String endpoint) => _makeUrl(endpoint); | ||||
|  | ||||
|   UserProfile? profile = null; | ||||
|   UserProfile? profile; | ||||
|  | ||||
|   Map<String, dynamic> roles = {}; | ||||
|  | ||||
| @@ -187,8 +185,6 @@ class InvenTreeAPI { | ||||
|  | ||||
|     if (profile == null) return false; | ||||
|  | ||||
|     var ctx = OneContext().context; | ||||
|  | ||||
|     String address = profile?.server ?? ""; | ||||
|     String username = profile?.username ?? ""; | ||||
|     String password = profile?.password ?? ""; | ||||
| @@ -451,7 +447,7 @@ class InvenTreeAPI { | ||||
|     } on SocketException catch (error) { | ||||
|       showServerError(L10().connectionRefused, error.toString()); | ||||
|       return null; | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -468,10 +464,10 @@ class InvenTreeAPI { | ||||
|     var data = json.encode(_body); | ||||
|  | ||||
|     // Set headers | ||||
|     request.headers.set('Accept', 'application/json'); | ||||
|     request.headers.set('Content-type', 'application/json'); | ||||
|     request.headers.set('Accept-Language', Intl.getCurrentLocale()); | ||||
|     request.headers.set('Content-Length', data.length.toString()); | ||||
|     request.headers.set(HttpHeaders.acceptHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); | ||||
|     request.headers.set(HttpHeaders.contentLengthHeader, data.length.toString()); | ||||
|     request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); | ||||
|  | ||||
|     request.add(utf8.encode(data)); | ||||
| @@ -486,7 +482,7 @@ class InvenTreeAPI { | ||||
|           error.toString() | ||||
|       ); | ||||
|       return null; | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -594,16 +590,16 @@ class InvenTreeAPI { | ||||
|     try { | ||||
|       request = await client.openUrl("OPTIONS", uri).timeout(Duration(seconds: 10)); | ||||
|  | ||||
|       request.headers.set('Accept', 'application/json'); | ||||
|       request.headers.set('Accept-Language', Intl.getCurrentLocale()); | ||||
|       request.headers.set('Content-type', 'application/json'); | ||||
|       request.headers.set(HttpHeaders.acceptHeader, 'application/json'); | ||||
|       request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); | ||||
|       request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); | ||||
|       request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); | ||||
|  | ||||
|       response = await request.close().timeout(Duration(seconds: 10)); | ||||
|     } on SocketException catch (error) { | ||||
|       showServerError(L10().connectionRefused, error.toString()); | ||||
|       return null; | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -647,7 +643,7 @@ class InvenTreeAPI { | ||||
|           error.toString() | ||||
|       ); | ||||
|       return null; | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -666,9 +662,10 @@ class InvenTreeAPI { | ||||
|  | ||||
|     // Set headers | ||||
|     // Ref: https://stackoverflow.com/questions/59713003/body-not-sending-using-map-in-flutter | ||||
|     request.headers.set('Accept', 'application/json'); | ||||
|     request.headers.set('Content-type', 'application/json'); | ||||
|     request.headers.set('Content-Length', data.length.toString()); | ||||
|     request.headers.set(HttpHeaders.acceptHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); | ||||
|     request.headers.set(HttpHeaders.contentLengthHeader, data.length.toString()); | ||||
|     request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); | ||||
|  | ||||
|     // Add JSON data to the request | ||||
| @@ -684,7 +681,7 @@ class InvenTreeAPI { | ||||
|           error.toString() | ||||
|       ); | ||||
|       return null; | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -795,13 +792,13 @@ class InvenTreeAPI { | ||||
|     try { | ||||
|       // Open a connection | ||||
|       request = await client.getUrl(uri).timeout(Duration(seconds: 10)); | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } on SocketException catch (error) { | ||||
|       showServerError(L10().connectionRefused, error.toString()); | ||||
|       return null; | ||||
|     } on Exception catch (error) { | ||||
|     } on FormatException { | ||||
|       showServerError(L10().invalidHost, L10().invalidHostDetails); | ||||
|       return null; | ||||
|     } catch (error, stackTrace) { | ||||
| @@ -811,15 +808,16 @@ class InvenTreeAPI { | ||||
|     } | ||||
|  | ||||
|     // Set connection headers | ||||
|     request.headers.set(HttpHeaders.acceptHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); | ||||
|     request.headers.set(HttpHeaders.acceptLanguageHeader, Intl.getCurrentLocale()); | ||||
|     request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); | ||||
|  | ||||
|  | ||||
|     try { | ||||
|       HttpClientResponse response = await request.close().timeout(Duration(seconds: 10)); | ||||
|       return response; | ||||
|  | ||||
|     } on TimeoutException catch (error) { | ||||
|     } on TimeoutException { | ||||
|       showTimeoutError(); | ||||
|       return null; | ||||
|     } on SocketException catch (error) { | ||||
|   | ||||
| @@ -246,8 +246,6 @@ class BarcodeScanHandler extends BarcodeHandler { | ||||
|         success: false, | ||||
|         onAction: () { | ||||
|  | ||||
|           var _ctx = OneContext().context; | ||||
|  | ||||
|           OneContext().showDialog( | ||||
|               builder: (BuildContext context) => SimpleDialog( | ||||
|                 title: Text(L10().unknownResponse), | ||||
|   | ||||
| @@ -1,18 +1,10 @@ | ||||
| import 'dart:async'; | ||||
| import 'dart:io'; | ||||
|  | ||||
| import 'package:InvenTree/api.dart'; | ||||
| import 'package:InvenTree/widget/dialogs.dart'; | ||||
| import 'package:flutter/cupertino.dart'; | ||||
| import 'package:one_context/one_context.dart'; | ||||
| import 'package:url_launcher/url_launcher.dart'; | ||||
|  | ||||
| // import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||||
|  | ||||
| import 'dart:convert'; | ||||
|  | ||||
| import 'package:path/path.dart' as path; | ||||
| import 'package:http/http.dart' as http; | ||||
|  | ||||
|  | ||||
| // Paginated response object | ||||
|   | ||||
| @@ -1,14 +1,10 @@ | ||||
| import 'dart:convert'; | ||||
|  | ||||
| import 'package:InvenTree/api.dart'; | ||||
| import 'package:InvenTree/inventree/stock.dart'; | ||||
| import 'package:flutter/cupertino.dart'; | ||||
| import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||
| import 'package:InvenTree/l10.dart'; | ||||
|  | ||||
| import 'model.dart'; | ||||
| import 'dart:io'; | ||||
|  | ||||
| import 'package:path/path.dart' as path; | ||||
| import 'package:http/http.dart' as http; | ||||
|  | ||||
| class InvenTreePartCategory extends InvenTreeModel { | ||||
| @@ -42,7 +38,7 @@ class InvenTreePartCategory extends InvenTreeModel { | ||||
|     String p = psplit.join("/"); | ||||
|  | ||||
|     if (p.isEmpty) { | ||||
|       p = "Top level part category"; | ||||
|       p = L10().partCategoryTopLevel; | ||||
|     } | ||||
|  | ||||
|     return p; | ||||
|   | ||||
| @@ -35,8 +35,7 @@ class InvenTreeStockItemTestResult extends InvenTreeModel { | ||||
|  | ||||
|   InvenTreeStockItemTestResult() : super(); | ||||
|  | ||||
|   InvenTreeStockItemTestResult.fromJson(Map<String, dynamic> json) : super.fromJson(json) { | ||||
|   } | ||||
|   InvenTreeStockItemTestResult.fromJson(Map<String, dynamic> json) : super.fromJson(json); | ||||
|  | ||||
|   @override | ||||
|   InvenTreeStockItemTestResult createFromJson(Map<String, dynamic> json) { | ||||
| @@ -388,9 +387,8 @@ class InvenTreeStockItem extends InvenTreeModel { | ||||
|   } | ||||
|  | ||||
|   String get locationPathString { | ||||
|     String path = ''; | ||||
|  | ||||
|     if (locationId == -1 || !jsondata.containsKey('location_detail')) return 'No location specified'; | ||||
|     if (locationId == -1 || !jsondata.containsKey('location_detail')) return L10().locationNotSet; | ||||
|  | ||||
|     return jsondata['location_detail']['pathstring'] ?? ''; | ||||
|   } | ||||
|   | ||||
| @@ -17,8 +17,7 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> { | ||||
|  | ||||
|   final GlobalKey<_InvenTreeAppSettingsState> _settingsKey = GlobalKey<_InvenTreeAppSettingsState>(); | ||||
|  | ||||
|   _InvenTreeAppSettingsState() { | ||||
|   } | ||||
|   _InvenTreeAppSettingsState(); | ||||
|  | ||||
|   bool barcodeSounds = true; | ||||
|   bool serverSounds = true; | ||||
|   | ||||
| @@ -8,7 +8,6 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||
| import 'package:InvenTree/l10.dart'; | ||||
|  | ||||
| import '../api.dart'; | ||||
| import '../preferences.dart'; | ||||
| import '../user_profile.dart'; | ||||
|  | ||||
| class InvenTreeLoginSettingsWidget extends StatefulWidget { | ||||
| @@ -22,8 +21,6 @@ class _InvenTreeLoginSettingsState extends State<InvenTreeLoginSettingsWidget> { | ||||
|  | ||||
|   final GlobalKey<_InvenTreeLoginSettingsState> _loginKey = GlobalKey<_InvenTreeLoginSettingsState>(); | ||||
|  | ||||
|   final GlobalKey<FormState> _formKey = new GlobalKey<FormState>(); | ||||
|  | ||||
|   final GlobalKey<FormState> _addProfileKey = new GlobalKey<FormState>(); | ||||
|  | ||||
|   List<UserProfile> profiles = []; | ||||
| @@ -250,7 +247,7 @@ class _InvenTreeLoginSettingsState extends State<InvenTreeLoginSettingsWidget> { | ||||
|  | ||||
|     List<Widget> children = []; | ||||
|  | ||||
|     if (profiles != null && profiles.length > 0) { | ||||
|     if (profiles.length > 0) { | ||||
|       for (int idx = 0; idx < profiles.length; idx++) { | ||||
|         UserProfile profile = profiles[idx]; | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| import 'package:flutter/cupertino.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_markdown/flutter_markdown.dart'; | ||||
| import 'package:markdown/markdown.dart' as md; | ||||
| import 'package:InvenTree/l10.dart'; | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -116,8 +116,6 @@ class UserProfileDBManager { | ||||
|   } | ||||
|  | ||||
|   Future deleteProfile(UserProfile profile) async { | ||||
|     final finder = Finder(filter: Filter.equals("name", profile.name)); | ||||
|  | ||||
|     await store.record(profile.key).delete(await _db); | ||||
|     print("Deleted user profile <${profile.key}> - '${profile.name}'"); | ||||
|   } | ||||
| @@ -133,8 +131,6 @@ class UserProfileDBManager { | ||||
|  | ||||
|     final profiles = await store.find(await _db); | ||||
|  | ||||
|     List<UserProfile> profileList = []; | ||||
|  | ||||
|     for (int idx = 0; idx < profiles.length; idx++) { | ||||
|  | ||||
|       if (profiles[idx].key is int && profiles[idx].key == selected) { | ||||
|   | ||||
| @@ -126,7 +126,7 @@ class _CategoryDisplayState extends RefreshableState<CategoryDisplayWidget> { | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   _CategoryDisplayState(this.category) {} | ||||
|   _CategoryDisplayState(this.category); | ||||
|  | ||||
|   // The local InvenTreePartCategory object | ||||
|   final InvenTreePartCategory? category; | ||||
|   | ||||
| @@ -42,7 +42,7 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> { | ||||
|   void _saveCompany(Map<String, String> values) async { | ||||
|     Navigator.of(context).pop(); | ||||
|  | ||||
|     var response = await company.update(values: values); | ||||
|     await company.update(values: values); | ||||
|  | ||||
|     refresh(); | ||||
|   } | ||||
| @@ -57,13 +57,13 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> { | ||||
|     showFormDialog(L10().edit, | ||||
|         key: _editCompanyKey, | ||||
|         actions: <Widget>[ | ||||
|           FlatButton( | ||||
|           TextButton( | ||||
|             child: Text(L10().cancel), | ||||
|             onPressed: () { | ||||
|               Navigator.pop(context); | ||||
|             }, | ||||
|           ), | ||||
|           FlatButton( | ||||
|           TextButton( | ||||
|             child: Text(L10().save), | ||||
|             onPressed: () { | ||||
|               if (_editCompanyKey.currentState!.validate()) { | ||||
|   | ||||
| @@ -5,9 +5,7 @@ import 'package:flutter/material.dart'; | ||||
|  | ||||
| import 'package:InvenTree/api.dart'; | ||||
| import 'package:InvenTree/inventree/company.dart'; | ||||
| import 'package:InvenTree/widget/drawer.dart'; | ||||
| import 'package:InvenTree/widget/refreshable_state.dart'; | ||||
| import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||
|  | ||||
| abstract class CompanyListWidget extends StatefulWidget { | ||||
|  | ||||
| @@ -50,7 +48,7 @@ class _CompanyListState extends RefreshableState<CompanyListWidget> { | ||||
|  | ||||
|   Map<String, String> _filters = Map<String, String>(); | ||||
|  | ||||
|   _CompanyListState(this._title, this._filters) {} | ||||
|   _CompanyListState(this._title, this._filters); | ||||
|  | ||||
|   @override | ||||
|   Future<void> onBuild(BuildContext context) async { | ||||
|   | ||||
| @@ -163,10 +163,6 @@ Future<void> showStatusCodeError(int status, {int expected = 200}) async { | ||||
| } | ||||
|  | ||||
| Future<void> showTimeoutError() async { | ||||
|  | ||||
|   // Use OneContext as "sometimes" context is null here? | ||||
|   var ctx = OneContext().context; | ||||
|  | ||||
|   await showServerError(L10().timeout, L10().noResponse); | ||||
| } | ||||
|  | ||||
| @@ -174,8 +170,6 @@ void showFormDialog(String title, {String? acceptText, String? cancelText, Globa | ||||
|  | ||||
|   BuildContext? dialogContext; | ||||
|  | ||||
|   var ctx = OneContext().context; | ||||
|  | ||||
|   String _accept = acceptText ?? L10().save; | ||||
|   String _cancel = cancelText ?? L10().cancel; | ||||
|  | ||||
|   | ||||
| @@ -1,10 +1,7 @@ | ||||
|  | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:font_awesome_flutter/font_awesome_flutter.dart'; | ||||
| import 'package:image_picker/image_picker.dart'; | ||||
|  | ||||
| import 'package:one_context/one_context.dart'; | ||||
|  | ||||
| import 'package:InvenTree/l10.dart'; | ||||
|  | ||||
| import 'dart:async'; | ||||
|   | ||||
| @@ -1,5 +1,4 @@ | ||||
| import 'package:InvenTree/user_profile.dart'; | ||||
| import 'package:InvenTree/widget/starred_parts.dart'; | ||||
| import 'package:flutter/cupertino.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
|  | ||||
|   | ||||
| @@ -127,7 +127,7 @@ class _LocationDisplayState extends RefreshableState<LocationDisplayWidget> { | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   _LocationDisplayState(this.location) {} | ||||
|   _LocationDisplayState(this.location); | ||||
|  | ||||
|   List<InvenTreeStockLocation> _sublocations = []; | ||||
|  | ||||
|   | ||||
| @@ -2,8 +2,6 @@ import 'package:InvenTree/widget/drawer.dart'; | ||||
| import 'package:flutter/cupertino.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter/widgets.dart'; | ||||
| import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||||
| import 'package:InvenTree/widget/drawer.dart'; | ||||
|  | ||||
|  | ||||
| abstract class RefreshableState<T extends StatefulWidget> extends State<T> { | ||||
|   | ||||
| @@ -48,8 +48,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | ||||
|   final _moveStockKey = GlobalKey<FormState>(); | ||||
|   final _editStockKey = GlobalKey<FormState>(); | ||||
|  | ||||
|   _StockItemDisplayState(this.item) { | ||||
|   } | ||||
|   _StockItemDisplayState(this.item); | ||||
|  | ||||
|   @override | ||||
|   List<Widget> getAppBarActions(BuildContext context) { | ||||
|   | ||||
| @@ -5,11 +5,8 @@ | ||||
| // gestures. You can also use WidgetTester to find child widgets in the widget | ||||
| // tree, read text, and verify that the values of widget properties are correct. | ||||
|  | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_test/flutter_test.dart'; | ||||
|  | ||||
| //import 'package:inventree_app/main.dart'; | ||||
|  | ||||
| void main() { | ||||
|   testWidgets('Counter increments smoke test', (WidgetTester tester) async { | ||||
|   }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user