From b4710b56e3537f66e70ed8a1038e678a17750633 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 20 Jan 2021 22:35:44 +1100 Subject: [PATCH] Fix network cache images - Changed from using flutter_advanced_networkimage to cached_network_image - flutter_advanced_networkimage is now outdated - cached_network_image is the pseudo official library --- lib/api.dart | 48 +++-- lib/widget/category_display.dart | 6 +- lib/widget/company_detail.dart | 5 +- lib/widget/company_list.dart | 5 +- lib/widget/location_display.dart | 5 +- lib/widget/part_detail.dart | 4 +- lib/widget/stock_detail.dart | 9 +- pubspec.lock | 292 +++++++++++++++++++++++-------- pubspec.yaml | 3 +- 9 files changed, 251 insertions(+), 126 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index c5178cb2..f22dd1f3 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -2,8 +2,9 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_advanced_networkimage/provider.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:image/image.dart'; import 'package:path/path.dart' as path; @@ -66,7 +67,6 @@ class InvenTreeAPI { } String makeApiUrl(String endpoint) { - return _makeUrl("/api/" + endpoint); } @@ -89,14 +89,13 @@ class InvenTreeAPI { * Useful as a precursor check before performing operations. */ bool checkConnection(BuildContext context) { - // Firstly, is the server connected? if (!isConnected()) { showDialog( context: context, child: new SimpleDialog( title: new Text("Not Connected"), - children: [ + children: [ ListTile( title: Text("Server not connected"), ) @@ -112,7 +111,6 @@ class InvenTreeAPI { // Finally return true; - } // Server instance information @@ -134,12 +132,13 @@ class InvenTreeAPI { // Ensure we only ever create a single instance of the API class static final InvenTreeAPI _api = new InvenTreeAPI._internal(); - factory InvenTreeAPI() { return _api; } + factory InvenTreeAPI() { + return _api; + } InvenTreeAPI._internal(); Future connect() async { - var prefs = await SharedPreferences.getInstance(); String server = prefs.getString("server"); @@ -149,7 +148,8 @@ class InvenTreeAPI { return connectToServer(server, username, password); } - Future connectToServer(String address, String username, String password) async { + Future connectToServer(String address, String username, + String password) async { /* Address is the base address for the InvenTree server, * e.g. http://127.0.0.1:8000 @@ -272,7 +272,6 @@ class InvenTreeAPI { // Perform a PATCH request Future patch(String url, {Map body}) async { - var _url = makeApiUrl(url); var _headers = defaultHeaders(); var _body = Map(); @@ -291,7 +290,8 @@ class InvenTreeAPI { /* * Upload a file to the given URL */ - Future uploadFile(String url, File f, {String name = "attachment", Map fields}) async { + Future uploadFile(String url, File f, + {String name = "attachment", Map fields}) async { var _url = makeApiUrl(url); var request = http.MultipartRequest('POST', Uri.parse(_url)); @@ -313,7 +313,6 @@ class InvenTreeAPI { // Perform a POST request Future post(String url, {Map body}) async { - var _url = makeApiUrl(url); var _headers = jsonHeaders(); @@ -329,7 +328,6 @@ class InvenTreeAPI { // Perform a GET request Future get(String url, {Map params}) async { - var _url = makeApiUrl(url); var _headers = defaultHeaders(); @@ -353,7 +351,6 @@ class InvenTreeAPI { } Map defaultHeaders() { - var headers = Map(); headers[HttpHeaders.authorizationHeader] = _authorizationHeader(); @@ -362,13 +359,12 @@ class InvenTreeAPI { } Map jsonHeaders() { - var headers = defaultHeaders(); headers['Content-Type'] = 'application/json'; return headers; } - String _authorizationHeader () { + String _authorizationHeader() { if (_token.isNotEmpty) { return "Token $_token"; } else { @@ -380,26 +376,22 @@ class InvenTreeAPI { static String get staticThumb => "/static/img/blank_image.thumbnail.png"; - /* - * Get an image from the server (or, from cache) + /** + * Load image from the InvenTree server, + * or from local cache (if it has been cached!) */ - AdvancedNetworkImage getImage(String imageUrl) { - + CachedNetworkImage getImage(String imageUrl) { if (imageUrl.isEmpty) { imageUrl = staticImage; } String url = makeUrl(imageUrl); - return new AdvancedNetworkImage(url, - header: defaultHeaders(), - useDiskCache: true, - //retryDuration: const Duration(seconds: 2), - //retryLimit: 3, - cacheRule: CacheRule(maxAge: const Duration(days: 1)), - loadFailedCallback: () { - DiskCache().evict(url); - } + return new CachedNetworkImage( + imageUrl: url, + placeholder: (context, url) => CircularProgressIndicator(), + errorWidget: (context, url, error) => Icon(FontAwesomeIcons.exclamation), + httpHeaders: defaultHeaders(), ); } } \ No newline at end of file diff --git a/lib/widget/category_display.dart b/lib/widget/category_display.dart index 6832b212..8fe87d09 100644 --- a/lib/widget/category_display.dart +++ b/lib/widget/category_display.dart @@ -11,7 +11,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_advanced_networkimage/provider.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; class CategoryDisplayWidget extends StatefulWidget { @@ -260,10 +259,7 @@ class PartList extends StatelessWidget { return ListTile( title: Text("${part.name}"), subtitle: Text("${part.description}"), - leading: Image( - image: InvenTreeAPI().getImage(part.thumbnail), - width: 48, - ), + leading: InvenTreeAPI().getImage(part.thumbnail), onTap: () { _openPart(context, part.pk); }, diff --git a/lib/widget/company_detail.dart b/lib/widget/company_detail.dart index c96b0e65..f98566c0 100644 --- a/lib/widget/company_detail.dart +++ b/lib/widget/company_detail.dart @@ -115,10 +115,7 @@ class _CompanyDetailState extends RefreshableState { child: ListTile( title: Text("${company.name}"), subtitle: Text("${company.description}"), - leading: Image( - image: InvenTreeAPI().getImage(company.image), - width: 48, - ), + leading: InvenTreeAPI().getImage(company.image), trailing: IconButton( icon: FaIcon(FontAwesomeIcons.edit), onPressed: editCompanyDialog, diff --git a/lib/widget/company_list.dart b/lib/widget/company_list.dart index fc021c51..8a44faa1 100644 --- a/lib/widget/company_list.dart +++ b/lib/widget/company_list.dart @@ -93,10 +93,7 @@ class _CompanyListState extends RefreshableState { return ListTile( title: Text("${company.name}"), subtitle: Text("${company.description}"), - leading: Image( - image: InvenTreeAPI().getImage(company.image), - width: 40, - ), + leading: InvenTreeAPI().getImage(company.image), onTap: () { if (company.pk > 0) { InvenTreeCompany().get(context, company.pk).then((var c) { diff --git a/lib/widget/location_display.dart b/lib/widget/location_display.dart index 2c5691f2..3610cf4e 100644 --- a/lib/widget/location_display.dart +++ b/lib/widget/location_display.dart @@ -243,10 +243,7 @@ class StockList extends StatelessWidget { return ListTile( title: Text("${item.partName}"), subtitle: Text("${item.partDescription}"), - leading: Image( - image: InvenTreeAPI().getImage(item.partThumbnail), - width: 48, - ), + leading: InvenTreeAPI().getImage(item.partThumbnail), trailing: Text("${item.displayQuantity}", style: TextStyle(fontWeight: FontWeight.bold), ), diff --git a/lib/widget/part_detail.dart b/lib/widget/part_detail.dart index 7a99fff2..5dec0c19 100644 --- a/lib/widget/part_detail.dart +++ b/lib/widget/part_detail.dart @@ -121,9 +121,7 @@ class _PartDisplayState extends RefreshableState { child: ListTile( title: Text("${part.fullname}"), subtitle: Text("${part.description}"), - leading: Image( - image: InvenTreeAPI().getImage(part.image) - ), + leading: InvenTreeAPI().getImage(part.image), trailing: IconButton( icon: FaIcon(FontAwesomeIcons.edit), onPressed: _editPartDialog, diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 062ece60..c0c9919e 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -331,9 +331,7 @@ class _StockItemDisplayState extends RefreshableState { child: ListTile( title: Text("${item.partName}"), subtitle: Text("${item.partDescription}"), - leading: Image( - image: InvenTreeAPI().getImage(item.partImage), - ), + leading: InvenTreeAPI().getImage(item.partImage), trailing: IconButton( icon: FaIcon(FontAwesomeIcons.edit), onPressed: _editStockItemDialog, @@ -413,10 +411,7 @@ class _StockItemDisplayState extends RefreshableState { title: Text("${item.supplierName}"), subtitle: Text("${item.supplierSKU}"), leading: FaIcon(FontAwesomeIcons.industry), - trailing: Image( - image: InvenTreeAPI().getImage(item.supplierImage), - height: 32, - ), + trailing: InvenTreeAPI().getImage(item.supplierImage), onTap: null, ) ); diff --git a/pubspec.lock b/pubspec.lock index 5a511e10..6cb40211 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -21,35 +21,63 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + cached_network_image: + dependency: "direct main" + description: + name: cached_network_image + url: "https://pub.dartlang.org" + source: hosted + version: "2.5.0" camera: dependency: "direct main" description: name: camera url: "https://pub.dartlang.org" source: hosted - version: "0.5.8+2" + version: "0.6.4+5" + camera_platform_interface: + dependency: transitive + description: + name: camera_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" + version: "1.2.0-nullsafety.1" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" + version: "1.15.0-nullsafety.3" convert: dependency: transitive description: @@ -57,13 +85,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + cross_file: + dependency: transitive + description: + name: cross_file + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" crypto: dependency: transitive description: name: crypto url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "2.1.5" cupertino_icons: dependency: "direct main" description: @@ -71,39 +106,81 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0-nullsafety.1" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "5.2.1" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" - flutter_advanced_networkimage: - dependency: "direct main" + flutter_blurhash: + dependency: transitive description: - name: flutter_advanced_networkimage + name: flutter_blurhash url: "https://pub.dartlang.org" source: hosted - version: "0.7.0" + version: "0.5.0" + flutter_cache_manager: + dependency: transitive + description: + name: flutter_cache_manager + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" flutter_keyboard_visibility: dependency: transitive description: name: flutter_keyboard_visibility url: "https://pub.dartlang.org" source: hosted - version: "0.7.0" + version: "4.0.2" + flutter_keyboard_visibility_platform_interface: + dependency: transitive + description: + name: flutter_keyboard_visibility_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + flutter_keyboard_visibility_web: + dependency: transitive + description: + name: flutter_keyboard_visibility_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" flutter_launcher_icons: dependency: "direct dev" description: name: flutter_launcher_icons url: "https://pub.dartlang.org" source: hosted - version: "0.7.5" + version: "0.8.1" flutter_plugin_android_lifecycle: dependency: transitive description: name: flutter_plugin_android_lifecycle url: "https://pub.dartlang.org" source: hosted - version: "1.0.8" + version: "1.0.11" flutter_speed_dial: dependency: "direct main" description: @@ -111,13 +188,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.5" - flutter_svg: - dependency: transitive - description: - name: flutter_svg - url: "https://pub.dartlang.org" - source: hosted - version: "0.17.4" flutter_test: dependency: "direct dev" description: flutter @@ -129,7 +199,7 @@ packages: name: flutter_typeahead url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.9.3" flutter_web_plugins: dependency: transitive description: flutter @@ -141,14 +211,14 @@ packages: name: font_awesome_flutter url: "https://pub.dartlang.org" source: hosted - version: "8.8.1" + version: "8.11.0" http: dependency: "direct main" description: name: http url: "https://pub.dartlang.org" source: hosted - version: "0.12.1" + version: "0.12.2" http_parser: dependency: transitive description: @@ -162,98 +232,112 @@ packages: name: image url: "https://pub.dartlang.org" source: hosted - version: "2.1.12" + version: "2.1.19" image_picker: dependency: "direct main" description: name: image_picker url: "https://pub.dartlang.org" source: hosted - version: "0.6.6+4" + version: "0.6.7+21" image_picker_platform_interface: dependency: transitive description: name: image_picker_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.1.1" + intl: + dependency: transitive + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.1" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" + octo_image: + dependency: transitive + description: + name: octo_image + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.0" package_info: dependency: "direct main" description: name: package_info url: "https://pub.dartlang.org" source: hosted - version: "0.4.0+18" + version: "0.4.3+2" path: dependency: "direct main" description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - path_drawing: - dependency: transitive - description: - name: path_drawing - url: "https://pub.dartlang.org" - source: hosted - version: "0.4.1" - path_parsing: - dependency: transitive - description: - name: path_parsing - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.4" + version: "1.8.0-nullsafety.1" path_provider: dependency: "direct main" description: name: path_provider url: "https://pub.dartlang.org" source: hosted - version: "1.6.9" + version: "1.6.27" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+2" path_provider_macos: dependency: transitive description: name: path_provider_macos url: "https://pub.dartlang.org" source: hosted - version: "0.0.4+3" + version: "0.0.4+8" path_provider_platform_interface: dependency: transitive description: name: path_provider_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.2" + version: "1.0.4" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.4+3" pedantic: dependency: transitive description: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.9.0" + version: "1.9.2" petitparser: dependency: transitive description: name: petitparser url: "https://pub.dartlang.org" source: hosted - version: "2.4.0" + version: "3.1.0" platform: dependency: transitive description: @@ -267,28 +351,35 @@ packages: name: plugin_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.2" + version: "1.0.3" preferences: dependency: "direct main" description: name: preferences url: "https://pub.dartlang.org" source: hosted - version: "5.2.0" + version: "5.2.1" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.13" qr_code_scanner: dependency: "direct main" description: name: qr_code_scanner url: "https://pub.dartlang.org" source: hosted - version: "0.0.13" - quiver: + version: "0.0.14" + rxdart: dependency: transitive description: - name: quiver + name: rxdart url: "https://pub.dartlang.org" source: hosted - version: "2.1.3" + version: "0.25.0" sentry: dependency: "direct main" description: @@ -302,14 +393,21 @@ packages: name: shared_preferences url: "https://pub.dartlang.org" source: hosted - version: "0.5.7+3" + version: "0.5.12+4" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2+4" shared_preferences_macos: dependency: transitive description: name: shared_preferences_macos url: "https://pub.dartlang.org" source: hosted - version: "0.0.1+9" + version: "0.0.1+11" shared_preferences_platform_interface: dependency: transitive description: @@ -324,6 +422,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2+7" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+3" sky_engine: dependency: transitive description: flutter @@ -335,70 +440,119 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" + sqflite: + dependency: transitive + description: + name: sqflite + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.2+2" + sqflite_common: + dependency: transitive + description: + name: sqflite_common + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + stream_transform: + dependency: transitive + description: + name: stream_transform + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" + synchronized: + dependency: transitive + description: + name: synchronized + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0+2" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" usage: dependency: transitive description: name: usage url: "https://pub.dartlang.org" source: hosted - version: "3.4.1" + version: "3.4.2" + uuid: + dependency: transitive + description: + name: uuid + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.2" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.3" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.4" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2" xml: dependency: transitive description: name: xml url: "https://pub.dartlang.org" source: hosted - version: "3.6.1" + version: "4.5.1" yaml: dependency: transitive description: @@ -407,5 +561,5 @@ packages: source: hosted version: "2.2.1" sdks: - dart: ">=2.6.0 <3.0.0" - flutter: ">=1.12.13+hotfix.5 <2.0.0" + dart: ">=2.10.2 <2.11.0" + flutter: ">=1.22.2 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index a08beee1..0aae04ad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,9 +26,8 @@ dependencies: http: ^0.12.1 shared_preferences: ^0.5.7 - flutter_advanced_networkimage: ^0.7.0 # Pull image from network or cache + cached_network_image: ^2.5.0 preferences: ^5.2.0 # Persistent settings storage - #barcode_scan: ^3.0.1 # Barcode / QR code scanning qr_code_scanner: ^0.0.13 package_info: ^0.4.0 # App information introspection font_awesome_flutter: ^8.8.1 # FontAwesome icon set