From d2ce3fadf1e718312df7e9cab6610775bb9f2185 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 15 Jul 2021 11:27:19 +1000 Subject: [PATCH] Code cleanup --- lib/api.dart | 48 +++++++++++++++---------------- lib/barcode.dart | 2 -- lib/inventree/model.dart | 8 ------ lib/inventree/part.dart | 8 ++---- lib/inventree/stock.dart | 6 ++-- lib/settings/app_settings.dart | 3 +- lib/settings/login.dart | 5 +--- lib/settings/release.dart | 1 - lib/user_profile.dart | 4 --- lib/widget/category_display.dart | 2 +- lib/widget/company_detail.dart | 6 ++-- lib/widget/company_list.dart | 4 +-- lib/widget/dialogs.dart | 6 ---- lib/widget/fields.dart | 3 -- lib/widget/home.dart | 1 - lib/widget/location_display.dart | 2 +- lib/widget/refreshable_state.dart | 2 -- lib/widget/stock_detail.dart | 3 +- test/widget_test.dart | 3 -- 19 files changed, 36 insertions(+), 81 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 30abfefd..39d4113d 100644 --- a/lib/api.dart +++ b/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 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) { diff --git a/lib/barcode.dart b/lib/barcode.dart index e0dbbf4b..100c0d54 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -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), diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 678739a6..9953c659 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -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 diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index 4cc4b127..4cf593aa 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -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; diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 827553b6..da2999fb 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -35,8 +35,7 @@ class InvenTreeStockItemTestResult extends InvenTreeModel { InvenTreeStockItemTestResult() : super(); - InvenTreeStockItemTestResult.fromJson(Map json) : super.fromJson(json) { - } + InvenTreeStockItemTestResult.fromJson(Map json) : super.fromJson(json); @override InvenTreeStockItemTestResult createFromJson(Map 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'] ?? ''; } diff --git a/lib/settings/app_settings.dart b/lib/settings/app_settings.dart index f5910dd0..d7754cbc 100644 --- a/lib/settings/app_settings.dart +++ b/lib/settings/app_settings.dart @@ -17,8 +17,7 @@ class _InvenTreeAppSettingsState extends State { final GlobalKey<_InvenTreeAppSettingsState> _settingsKey = GlobalKey<_InvenTreeAppSettingsState>(); - _InvenTreeAppSettingsState() { - } + _InvenTreeAppSettingsState(); bool barcodeSounds = true; bool serverSounds = true; diff --git a/lib/settings/login.dart b/lib/settings/login.dart index 8dbaf324..4ecb26d9 100644 --- a/lib/settings/login.dart +++ b/lib/settings/login.dart @@ -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 { final GlobalKey<_InvenTreeLoginSettingsState> _loginKey = GlobalKey<_InvenTreeLoginSettingsState>(); - final GlobalKey _formKey = new GlobalKey(); - final GlobalKey _addProfileKey = new GlobalKey(); List profiles = []; @@ -250,7 +247,7 @@ class _InvenTreeLoginSettingsState extends State { List children = []; - if (profiles != null && profiles.length > 0) { + if (profiles.length > 0) { for (int idx = 0; idx < profiles.length; idx++) { UserProfile profile = profiles[idx]; diff --git a/lib/settings/release.dart b/lib/settings/release.dart index d15b4f92..5664ea2c 100644 --- a/lib/settings/release.dart +++ b/lib/settings/release.dart @@ -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'; diff --git a/lib/user_profile.dart b/lib/user_profile.dart index efbd81ca..3b6bf8f2 100644 --- a/lib/user_profile.dart +++ b/lib/user_profile.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 profileList = []; - for (int idx = 0; idx < profiles.length; idx++) { if (profiles[idx].key is int && profiles[idx].key == selected) { diff --git a/lib/widget/category_display.dart b/lib/widget/category_display.dart index 3f4533d3..4abdccf8 100644 --- a/lib/widget/category_display.dart +++ b/lib/widget/category_display.dart @@ -126,7 +126,7 @@ class _CategoryDisplayState extends RefreshableState { ); } - _CategoryDisplayState(this.category) {} + _CategoryDisplayState(this.category); // The local InvenTreePartCategory object final InvenTreePartCategory? category; diff --git a/lib/widget/company_detail.dart b/lib/widget/company_detail.dart index 04134783..c5741fe7 100644 --- a/lib/widget/company_detail.dart +++ b/lib/widget/company_detail.dart @@ -42,7 +42,7 @@ class _CompanyDetailState extends RefreshableState { void _saveCompany(Map 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 { showFormDialog(L10().edit, key: _editCompanyKey, actions: [ - FlatButton( + TextButton( child: Text(L10().cancel), onPressed: () { Navigator.pop(context); }, ), - FlatButton( + TextButton( child: Text(L10().save), onPressed: () { if (_editCompanyKey.currentState!.validate()) { diff --git a/lib/widget/company_list.dart b/lib/widget/company_list.dart index 56fc76af..f3cb207a 100644 --- a/lib/widget/company_list.dart +++ b/lib/widget/company_list.dart @@ -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 { Map _filters = Map(); - _CompanyListState(this._title, this._filters) {} + _CompanyListState(this._title, this._filters); @override Future onBuild(BuildContext context) async { diff --git a/lib/widget/dialogs.dart b/lib/widget/dialogs.dart index 8cf13f6d..725b5e52 100644 --- a/lib/widget/dialogs.dart +++ b/lib/widget/dialogs.dart @@ -163,10 +163,6 @@ Future showStatusCodeError(int status, {int expected = 200}) async { } Future 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; diff --git a/lib/widget/fields.dart b/lib/widget/fields.dart index 0572f2b6..87a62477 100644 --- a/lib/widget/fields.dart +++ b/lib/widget/fields.dart @@ -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'; diff --git a/lib/widget/home.dart b/lib/widget/home.dart index f85c32bd..dd466447 100644 --- a/lib/widget/home.dart +++ b/lib/widget/home.dart @@ -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'; diff --git a/lib/widget/location_display.dart b/lib/widget/location_display.dart index 8cc11536..ed053cd2 100644 --- a/lib/widget/location_display.dart +++ b/lib/widget/location_display.dart @@ -127,7 +127,7 @@ class _LocationDisplayState extends RefreshableState { ); } - _LocationDisplayState(this.location) {} + _LocationDisplayState(this.location); List _sublocations = []; diff --git a/lib/widget/refreshable_state.dart b/lib/widget/refreshable_state.dart index d981e6a0..e6589959 100644 --- a/lib/widget/refreshable_state.dart +++ b/lib/widget/refreshable_state.dart @@ -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 extends State { diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 1682968b..2517c9dc 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -48,8 +48,7 @@ class _StockItemDisplayState extends RefreshableState { final _moveStockKey = GlobalKey(); final _editStockKey = GlobalKey(); - _StockItemDisplayState(this.item) { - } + _StockItemDisplayState(this.item); @override List getAppBarActions(BuildContext context) { diff --git a/test/widget_test.dart b/test/widget_test.dart index 3f5ec9ac..661b665f 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -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 { });