diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 95cfd47a..da1f3add 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -4,6 +4,7 @@ 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'; @@ -14,6 +15,29 @@ import 'package:path/path.dart' as path; import 'package:http/http.dart' as http; +// Paginated response object +class InvenTreePageResponse { + + InvenTreePageResponse() { + results = []; + } + + void addResult(InvenTreeModel item) { + results.add(item); + } + + // Total number of results in the dataset + int _count = 0; + + void set count(int v) { _count = v; } + + int get count => _count; + + int get length => results?.length ?? 0; + + List results = []; +} + /** * The InvenTreeModel class provides a base-level object * for interacting with InvenTree data. @@ -317,6 +341,65 @@ class InvenTreeModel { return _model; } + Future listPaginated(int limit, int offset, {Map filters}) async { + var params = defaultListFilters(); + + if (filters != null) { + for (String key in filters.keys) { + params[key] = filters[key]; + } + } + + params["limit"] = "${limit}"; + params["offset"] = "${offset}"; + + var response = await api.get(URL, params: params) + .timeout(Duration(seconds: 10)) + .catchError((error) { + if (error is SocketException) { + showServerError( + I18N + .of(OneContext().context) + .connectionRefused, + error.toString() + ); + } + + return null; + }); + + if (response == null) { + return null; + } + + if (response.statusCode != 200) { + showStatusCodeError(response.statusCode); + return null; + } + + // Construct the response + InvenTreePageResponse page = new InvenTreePageResponse(); + + final data = json.decode(response.body); + + if (data.containsKey("count") && data.containsKey("results")) { + page.count = data["count"] as int; + + page.results = []; + + for (var result in data["results"]) { + page.addResult(createFromJson(result)); + } + + return page; + + } else { + // Inavlid response + print("Invalid!"); + return null; + } + } + // Return list of objects from the database, with optional filters Future> list(BuildContext context, {Map filters}) async { diff --git a/lib/l10n b/lib/l10n index 654b12d7..77f9c0b0 160000 --- a/lib/l10n +++ b/lib/l10n @@ -1 +1 @@ -Subproject commit 654b12d727c5c045c6586b0a13bfcf7293f112f8 +Subproject commit 77f9c0b023ca0622a60b6e4ee4a44d9c02dc4a03