diff --git a/crdt_sync_flutter/README.md b/crdt_sync_flutter/README.md index d2da0bf..2ce3a69 100644 --- a/crdt_sync_flutter/README.md +++ b/crdt_sync_flutter/README.md @@ -208,7 +208,8 @@ errors.connect( `installAppErrors` chains `FlutterError.onError` and `PlatformDispatcher.onError`. Reports are deduplicated on the device (30 s window, 20 a minute), redacted, queued in `SharedPreferences` (so offline reports survive), and uploaded to -RTDB `//errors//` with `repo`, `build` and `version`. A spinner +RTDB `//errors//` with `repo`, `build` and `version`. Without +`APP_BUILD` the installed versionCode (`package_info_plus`) is the build. A spinner inside `WaitWatch` that stays over 1 s becomes `perf.slow`. Nothing shows on screen. diff --git a/crdt_sync_flutter/lib/src/errors/app_errors.dart b/crdt_sync_flutter/lib/src/errors/app_errors.dart index da8c5d7..e56dfc5 100644 --- a/crdt_sync_flutter/lib/src/errors/app_errors.dart +++ b/crdt_sync_flutter/lib/src/errors/app_errors.dart @@ -12,6 +12,7 @@ import 'package:crdt_sync_flutter/src/errors/error_upload.dart'; import 'package:crdt_sync_flutter/src/errors/slow_wait.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; +import 'package:package_info_plus/package_info_plus.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// The installed reporting: [reporter] for errors, [waits] for slow waits, @@ -54,14 +55,22 @@ class AppErrors { /// `WidgetsFlutterBinding.ensureInitialized`: uncaught framework errors /// (`uncaught.flutter`) and async errors (`uncaught.async`) are queued, then /// handed to the handlers installed before. Shows nothing on screen. +/// +/// A [config] with build 0 (no `--dart-define=APP_BUILD`) takes the installed +/// versionCode from [buildNumber] (default [platformBuildNumber]), so every +/// report names the build it came from; an unreadable one stays 0. Future installAppErrors( AppErrorsConfig config, { SharedPreferences? preferences, + Future Function() buildNumber = platformBuildNumber, }) async { + final stamped = config.build != 0 + ? config + : config.withBuild(await _installedBuild(buildNumber)); final queue = await ErrorQueue.open( preferences ?? await SharedPreferences.getInstance(), ); - final errors = AppErrors.instance = AppErrors.forTest(config, queue); + final errors = AppErrors.instance = AppErrors.forTest(stamped, queue); final reporter = errors.reporter; final framework = FlutterError.onError; FlutterError.onError = (details) { @@ -83,6 +92,18 @@ Future installAppErrors( return errors; } +/// The installed app's build number (Android versionCode). +Future platformBuildNumber() async => + (await PackageInfo.fromPlatform()).buildNumber; + +Future _installedBuild(Future Function() read) async { + try { + return int.tryParse(await read() ?? '') ?? 0; + } on Object { + return 0; + } +} + /// Keeps the reporter's route current, so every report names its screen. /// Add it to the app's `navigatorObservers`. Route names only: give routes /// patterns (`/list/:id`), never ids or titles. diff --git a/crdt_sync_flutter/lib/src/errors/app_errors_config.dart b/crdt_sync_flutter/lib/src/errors/app_errors_config.dart index bf4f14e..b235540 100644 --- a/crdt_sync_flutter/lib/src/errors/app_errors_config.dart +++ b/crdt_sync_flutter/lib/src/errors/app_errors_config.dart @@ -28,6 +28,15 @@ class AppErrorsConfig { /// The version name (`1.2.3`). final String? version; + /// This config with [build] replaced. + AppErrorsConfig withBuild(int build) => AppErrorsConfig( + app: app, + repo: repo, + package: package, + build: build, + version: version, + ); + /// What marks the app's own stack frames (`package:todo/`). String get mark => 'package:${package ?? app.replaceAll('-', '_')}/'; } diff --git a/crdt_sync_flutter/pubspec.lock b/crdt_sync_flutter/pubspec.lock index 23bd91d..6a55506 100644 --- a/crdt_sync_flutter/pubspec.lock +++ b/crdt_sync_flutter/pubspec.lock @@ -351,6 +351,22 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.0" + package_info_plus: + dependency: "direct main" + description: + name: package_info_plus + sha256: "73ef2cd5c11a7548f9b7f6fc2a4f6113de2e64aa2a70de63feede95548822908" + url: "https://pub.dev" + source: hosted + version: "10.2.2" + package_info_plus_platform_interface: + dependency: transitive + description: + name: package_info_plus_platform_interface + sha256: db762cb2f4f25ee60fb6359773861b0f199e00b90d237bd85a76a1e806b46ef4 + url: "https://pub.dev" + source: hosted + version: "4.1.0" path: dependency: transitive description: diff --git a/crdt_sync_flutter/pubspec.yaml b/crdt_sync_flutter/pubspec.yaml index e05b7ac..b837462 100644 --- a/crdt_sync_flutter/pubspec.yaml +++ b/crdt_sync_flutter/pubspec.yaml @@ -4,7 +4,7 @@ description: >- persisted per-install device id, and a one-call bootstrap. Removes the ~150 lines of firebase_backend.dart every consuming app was copying. publish_to: 'none' -version: 0.9.0 +version: 0.9.1 environment: sdk: ^3.12.2 @@ -36,6 +36,9 @@ dependencies: # Flutter 3.50 deprecates package:flutter/material.dart. An app on # material_ui gives flutter/material dialogs no MaterialLocalizations. material_ui: 1.11.1 + # The installed versionCode, stamped on error reports when the build did + # not pass --dart-define=APP_BUILD (phone_deploy and local builds). + package_info_plus: 10.2.2 shared_preferences: 2.5.6 # Opens the system browser for the desktop loopback sign-in. url_launcher: 6.3.3 diff --git a/crdt_sync_flutter/test/app_errors_build_test.dart b/crdt_sync_flutter/test/app_errors_build_test.dart new file mode 100644 index 0000000..0c01e79 --- /dev/null +++ b/crdt_sync_flutter/test/app_errors_build_test.dart @@ -0,0 +1,66 @@ +import 'dart:ui' show PlatformDispatcher; + +import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:package_info_plus/package_info_plus.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'app_errors_support.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() => SharedPreferences.setMockInitialValues({})); + + group('build number fallback', () { + const bare = AppErrorsConfig(app: 'todo', repo: 'kuhyx/todo'); + + Future install( + AppErrorsConfig c, + Future Function() buildNumber, + ) async { + final saved = (FlutterError.onError, PlatformDispatcher.instance.onError); + final errors = await installAppErrors( + c, + preferences: await SharedPreferences.getInstance(), + buildNumber: buildNumber, + ); + FlutterError.onError = saved.$1; + PlatformDispatcher.instance.onError = saved.$2; + AppErrors.instance = null; + return errors; + } + + test('no APP_BUILD: the installed versionCode is used', () async { + final errors = await install(bare, () async => '173'); + expect(errors.config.build, 173); + expect(errors.config.app, 'todo'); + }); + + test('an explicit build wins over the installed one', () async { + final errors = await install(config, () async => '173'); + expect(errors.config.build, 42); + }); + + test('an unreadable versionCode stays 0', () async { + expect((await install(bare, () async => 'x')).config.build, 0); + expect((await install(bare, () async => null)).config.build, 0); + expect( + (await install(bare, () async => throw StateError('no'))).config.build, + 0, + ); + }); + }); + + test('the default versionCode comes from the platform', () async { + PackageInfo.setMockInitialValues( + appName: 'todo', + packageName: 'com.kuhy.todo', + version: '1.0.0', + buildNumber: '9', + buildSignature: '', + ); + expect(await platformBuildNumber(), '9'); + }); +}