From cd4731833d87bc169b3a8b12a610ee01c95bd619 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 5 Apr 2020 00:34:08 +1100 Subject: [PATCH] Driving the refactor tractor - InvenTreeAPI() object now has a method to return a cache-backed image from the server --- lib/api.dart | 18 ++++++++++++++++++ lib/inventree/part.dart | 17 ++++++----------- lib/widget/category_display.dart | 7 +------ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 73b2a6d2..acd5a2ba 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:flutter/material.dart'; +import 'package:flutter_advanced_networkimage/provider.dart'; import 'package:image/image.dart'; import 'package:path/path.dart' as path; @@ -287,4 +289,20 @@ class InvenTreeAPI { return "Basic " + base64Encode(utf8.encode('$_username:$_password')); } } + + static String get staticImage => "/static/img/blank_image.png"; + + static String get staticThumb => "/static/img/blank_image.thumbnail.png"; + + /* + * Get an image from the server (or, from cache) + */ + AdvancedNetworkImage getImage(String imageUrl) { + + return new AdvancedNetworkImage(makeUrl(imageUrl), + header: defaultHeaders(), + useDiskCache: true, + cacheRule: CacheRule(maxAge: const Duration(days: 5)), + ); + } } \ No newline at end of file diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index 2d334c30..5c9403c2 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -42,25 +42,20 @@ class InvenTreePart extends InvenTreeModel { String get _thumbnail => jsondata['thumbnail'] ?? ''; - // Return a fully-qualified path to the image for this Part + // Return a path to the image for this Part String get image { + // Use thumbnail as a backup String img = _image.isNotEmpty ? _image : _thumbnail; - if (img.isEmpty) { - return InvenTreeAPI().makeUrl('/static/img/blank_image.png'); - } else { - return InvenTreeAPI().makeUrl(img); - } + return img.isNotEmpty ? img : InvenTreeAPI.staticImage; } + // Return a path to the thumbnail for this part String get thumbnail { + // Use image as a backup String img = _thumbnail.isNotEmpty ? _thumbnail : _image; - if (img.isEmpty) { - return InvenTreeAPI().makeUrl('/static/img/blank_image.thumbnail.png'); - } else { - return InvenTreeAPI().makeUrl(img); - } + return img.isNotEmpty ? img : InvenTreeAPI.staticThumb; } InvenTreePart() : super(); diff --git a/lib/widget/category_display.dart b/lib/widget/category_display.dart index a610f2bb..7a30fd91 100644 --- a/lib/widget/category_display.dart +++ b/lib/widget/category_display.dart @@ -180,12 +180,7 @@ class PartList extends StatelessWidget { title: Text("${part.name}"), subtitle: Text("${part.description}"), leading: Image( - image: AdvancedNetworkImage( - part.thumbnail, - header: InvenTreeAPI().defaultHeaders(), - useDiskCache: true, - cacheRule: CacheRule(maxAge: const Duration(days: 1)), - ), + image: InvenTreeAPI().getImage(part.thumbnail), width: 48, ), onTap: () {