• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inventree / inventree-app / 12326848637

14 Dec 2024 04:24AM UTC coverage: 8.386% (-0.1%) from 8.518%
12326848637

push

github

web-flow
[refactor] Scan improvements (#577)

* Handle error on unexpected barcode response

* Add ManufacturerPart detail view

* Support barcode scanning for manufacturer part

* Refactoring for null checks

* Ignore selected errors in sentry

* Fix API implementation for ManufacturerPart

* Update release notes

* More error handling

* Decode quantity betterer

* Refactoring

* Add option to confirm checkin details

* Improve response handlign

* Cleanup

* Remove unused imports

* Fix async function

* Fix for assigning custom barcode

* Handle barcode scan result for company

* Fix

* Adjust scan priority

* Refactoring MODEL_TYPE

- Use instead of duplicated const strings

* @override fix

4 of 241 new or added lines in 17 files covered. (1.66%)

12 existing lines in 9 files now uncovered.

723 of 8621 relevant lines covered (8.39%)

0.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.05
/lib/barcode/handler.dart
1

2
import "package:flutter/material.dart";
3
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
4

5
import "package:inventree/api.dart";
6
import "package:inventree/helpers.dart";
7
import "package:inventree/l10.dart";
8

9
import "package:inventree/barcode/tones.dart";
10

11
import "package:inventree/inventree/sentry.dart";
12

13
import "package:inventree/widget/dialogs.dart";
14
import "package:inventree/widget/snacks.dart";
15

16

17
/* Generic class which "handles" a barcode, by communicating with the InvenTree server,
18
 * and handling match / unknown / error cases.
19
 *
20
 * Override functionality of this class to perform custom actions,
21
 * based on the response returned from the InvenTree server
22
 */
23
class BarcodeHandler {
24

25
  BarcodeHandler();
2✔
26

27
  // Return the text to display on the barcode overlay
28
  // Note: Will be overridden by child classes
29
  String getOverlayText(BuildContext context) => "Barcode Overlay";
×
30

31
  // Called when the server "matches" a barcode
32
  Future<void> onBarcodeMatched(Map<String, dynamic> data) async {
×
33
    // Override this function
34
  }
35

36
  // Called when the server does not know about a barcode
37
  Future<void> onBarcodeUnknown(Map<String, dynamic> data) async {
×
38
    // Override this function
39

40
    barcodeFailureTone();
×
41

42
    showSnackIcon(
×
43
      (data["error"] ?? L10().barcodeNoMatch) as String,
×
44
      success: false,
45
      icon: Icons.qr_code,
46
    );
47
  }
48

49
  // Called when the server returns an unhandled response
50
  Future<void> onBarcodeUnhandled(Map<String, dynamic> data) async {
×
51
    barcodeFailureTone();
×
52
    showServerError("barcode/", L10().responseUnknown, data.toString());
×
53
  }
54

55
  /*
56
    * Base function to capture and process barcode data.
57
    *
58
    * Returns true only if the barcode scanner should remain open
59
    */
60
  Future<void> processBarcode(String barcode,
2✔
61
      {String url = "barcode/",
62
      Map<String, dynamic> extra_data = const {}}) async {
63

64
    debug("Scanned barcode data: '${barcode}'");
4✔
65

66
    barcode = barcode.trim();
2✔
67

68
    // Empty barcode is invalid
69
    if (barcode.isEmpty) {
2✔
70

71
      barcodeFailureTone();
1✔
72

73
      showSnackIcon(
1✔
74
        L10().barcodeError,
2✔
75
        icon: TablerIcons.exclamation_circle,
76
        success: false
77
      );
78

79
      return;
80
    }
81

82
    APIResponse? response;
83

84
    try {
85
      response = await InvenTreeAPI().post(
4✔
86
        url,
87
        body: {
2✔
88
          "barcode": barcode,
2✔
89
          ...extra_data,
2✔
90
        },
91
        expectedStatusCode: null, // Do not show an error on "unexpected code"
92
      );
93
    } catch (error, stackTrace) {
NEW
94
      sentryReportError("Barcode.processBarcode", error, stackTrace);
×
95
      response = null;
96
    }
97

98
    if (response == null) {
NEW
99
      barcodeFailureTone();
×
NEW
100
      showSnackIcon(L10().barcodeError, success: false);
×
101
      return;
102
    }
103

104
    debug("Barcode scan response" + response.data.toString());
8✔
105

106
    Map<String, dynamic> data = response.asMap();
2✔
107

108
    // Handle strange response from the server
109
    if (!response.isValid() || !response.isMap()) {
3✔
110
      await onBarcodeUnknown({});
2✔
111

112
      showSnackIcon(L10().serverError, success: false);
3✔
113

114
      // We want to know about this one!
115
      await sentryReportMessage(
1✔
116
          "BarcodeHandler.processBarcode returned unexpected value",
117
          context: {
1✔
118
            "data": response.data?.toString() ?? "null",
2✔
119
            "barcode": barcode,
120
            "url": url,
121
            "statusCode": response.statusCode.toString(),
2✔
122
            "valid": response.isValid().toString(),
2✔
123
            "error": response.error,
1✔
124
            "errorDetail": response.errorDetail,
1✔
125
            "className": "${this}",
1✔
126
          }
127
      );
128
    } else if (data.containsKey("success")) {
1✔
129
      await onBarcodeMatched(data);
1✔
130
    } else if ((response.statusCode >= 400) || data.containsKey("error")) {
2✔
131
      await onBarcodeUnknown(data);
1✔
132
    } else {
133
      await onBarcodeUnhandled(data);
×
134
    }
135
  }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc