Skip to content

Commit f9ce9e5

Browse files
binding: Add packageInfo getter
This is a preparatory commit for the work of embedding the device info in user-agent header.
1 parent 7be37dd commit f9ce9e5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

lib/model/binding.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:firebase_core/firebase_core.dart' as firebase_core;
33
import 'package:firebase_messaging/firebase_messaging.dart' as firebase_messaging;
44
import 'package:flutter/foundation.dart';
55
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
6+
import 'package:package_info_plus/package_info_plus.dart' as package_info_plus;
67
import 'package:url_launcher/url_launcher.dart' as url_launcher;
78

89
import '../host/android_notifications.dart';
@@ -81,6 +82,20 @@ abstract class ZulipBinding {
8182
/// or an error occured while fetching the data.
8283
BaseDeviceInfo? get maybeDeviceInfo;
8384

85+
/// Provides application package information,
86+
/// via package:package_info_plus.
87+
///
88+
/// The returned Future resolves to null if an error is
89+
/// encountered while fetching the data.
90+
Future<PackageInfo?> get packageInfo;
91+
92+
/// Provides application package information,
93+
/// via package:package_info_plus.
94+
///
95+
/// May return null if prefetching hasn't completed yet,
96+
/// or an error occured while fetching the data.
97+
PackageInfo? get maybePackageInfo;
98+
8499
/// Prepare the app's [GlobalStore], loading the necessary data.
85100
///
86101
/// Generally the app should call this function only once.
@@ -161,6 +176,17 @@ class IosDeviceInfo extends BaseDeviceInfo {
161176
IosDeviceInfo({required this.systemVersion});
162177
}
163178

179+
/// Like [package_info_plus.PackageInfo], but without things we don't use.
180+
class PackageInfo {
181+
final String version;
182+
final String buildNumber;
183+
184+
PackageInfo({
185+
required this.version,
186+
required this.buildNumber,
187+
});
188+
}
189+
164190
/// A concrete binding for use in the live application.
165191
///
166192
/// The global store returned by [loadGlobalStore], and consequently by
@@ -175,6 +201,7 @@ class LiveZulipBinding extends ZulipBinding {
175201
if (ZulipBinding._instance == null) {
176202
final binding = LiveZulipBinding();
177203
binding._deviceInfo = binding._prefetchDeviceInfo();
204+
binding._packageInfo = binding._prefetchPackageInfo();
178205
}
179206
return ZulipBinding.instance as LiveZulipBinding;
180207
}
@@ -187,6 +214,14 @@ class LiveZulipBinding extends ZulipBinding {
187214
BaseDeviceInfo? get maybeDeviceInfo => _maybeDeviceInfo;
188215
BaseDeviceInfo? _maybeDeviceInfo;
189216

217+
@override
218+
Future<PackageInfo?> get packageInfo => _packageInfo;
219+
late Future<PackageInfo?> _packageInfo;
220+
221+
@override
222+
PackageInfo? get maybePackageInfo => _maybePackageInfo;
223+
PackageInfo? _maybePackageInfo;
224+
190225
Future<BaseDeviceInfo?> _prefetchDeviceInfo() async {
191226
try {
192227
final info = await device_info_plus.DeviceInfoPlugin().deviceInfo;
@@ -202,6 +237,20 @@ class LiveZulipBinding extends ZulipBinding {
202237
return _maybeDeviceInfo;
203238
}
204239

240+
Future<PackageInfo?> _prefetchPackageInfo() async {
241+
try {
242+
final info = await package_info_plus.PackageInfo.fromPlatform();
243+
_maybePackageInfo = PackageInfo(
244+
version: info.version,
245+
buildNumber: info.buildNumber,
246+
);
247+
} catch (e) {
248+
assert(debugLog('Failed to prefetch package info: $e'));
249+
// TODO(log)
250+
}
251+
return _maybePackageInfo;
252+
}
253+
205254
@override
206255
Future<GlobalStore> loadGlobalStore() {
207256
return LiveGlobalStore.load();

test/model/binding.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestZulipBinding extends ZulipBinding {
7070
_resetLaunchUrl();
7171
_resetCloseInAppWebView();
7272
_resetDeviceInfo();
73+
_resetPackageInfo();
7374
_resetFirebase();
7475
_resetNotifications();
7576
}
@@ -218,6 +219,20 @@ class TestZulipBinding extends ZulipBinding {
218219
deviceInfoResult = _defaultDeviceInfoResult;
219220
}
220221

222+
@override
223+
Future<PackageInfo?> get packageInfo async => packageInfoResult;
224+
225+
@override
226+
PackageInfo? get maybePackageInfo => packageInfoResult;
227+
228+
/// The value that `ZulipBinding.instance.packageInfo` should return.
229+
PackageInfo packageInfoResult = _defaultPackageInfo;
230+
static final _defaultPackageInfo = PackageInfo(version: '0.0.1', buildNumber: '1');
231+
232+
void _resetPackageInfo() {
233+
packageInfoResult = _defaultPackageInfo;
234+
}
235+
221236
void _resetFirebase() {
222237
_firebaseInitialized = false;
223238
_firebaseMessaging = null;

0 commit comments

Comments
 (0)