Skip to content

Commit 027545c

Browse files
binding: Add packageInfo getter
1 parent 3a6d22e commit 027545c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/model/binding.dart

Lines changed: 34 additions & 1 deletion
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';
@@ -72,6 +73,12 @@ abstract class ZulipBinding {
7273
/// This wraps [device_info_plus.DeviceInfoPlugin.deviceInfo].
7374
BaseDeviceInfo get deviceInfo;
7475

76+
/// Provides application package information,
77+
/// via package:package_info_plus.
78+
///
79+
/// This wraps [package_info_plus.PackageInfo.fromPlatform].
80+
PackageInfo get packageInfo;
81+
7582
/// Prepare the app's [GlobalStore], loading the necessary data.
7683
///
7784
/// Generally the app should call this function only once.
@@ -152,6 +159,17 @@ class IosDeviceInfo extends BaseDeviceInfo {
152159
IosDeviceInfo({required this.systemVersion});
153160
}
154161

162+
/// Like [package_info_plus.PackageInfo], but without things we don't use.
163+
class PackageInfo {
164+
final String version;
165+
final String buildNumber;
166+
167+
PackageInfo({
168+
required this.version,
169+
required this.buildNumber,
170+
});
171+
}
172+
155173
/// A concrete binding for use in the live application.
156174
///
157175
/// The global store returned by [loadGlobalStore], and consequently by
@@ -165,7 +183,10 @@ class LiveZulipBinding extends ZulipBinding {
165183
static Future<LiveZulipBinding> ensureInitialized() async {
166184
if (ZulipBinding._instance == null) {
167185
final binding = LiveZulipBinding();
168-
await binding._prefetchDeviceInfo();
186+
await Future.wait(<Future<void>>[
187+
binding._prefetchDeviceInfo(),
188+
binding._prefetchPackageInfo(),
189+
]);
169190
}
170191
return ZulipBinding.instance as LiveZulipBinding;
171192
}
@@ -174,6 +195,10 @@ class LiveZulipBinding extends ZulipBinding {
174195
BaseDeviceInfo get deviceInfo => _deviceInfo!;
175196
BaseDeviceInfo? _deviceInfo;
176197

198+
@override
199+
PackageInfo get packageInfo => _packageInfo!;
200+
PackageInfo? _packageInfo;
201+
177202
Future<void> _prefetchDeviceInfo() async {
178203
final info = await device_info_plus.DeviceInfoPlugin().deviceInfo;
179204
_deviceInfo = switch (info) {
@@ -183,6 +208,14 @@ class LiveZulipBinding extends ZulipBinding {
183208
};
184209
}
185210

211+
Future<void> _prefetchPackageInfo() async {
212+
final info = await package_info_plus.PackageInfo.fromPlatform();
213+
_packageInfo = PackageInfo(
214+
version: info.version,
215+
buildNumber: info.buildNumber,
216+
);
217+
}
218+
186219
@override
187220
Future<GlobalStore> loadGlobalStore() {
188221
return LiveGlobalStore.load();

test/model/binding.dart

Lines changed: 12 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
}
@@ -215,6 +216,17 @@ class TestZulipBinding extends ZulipBinding {
215216
deviceInfoResult = _defaultDeviceInfoResult;
216217
}
217218

219+
@override
220+
PackageInfo get packageInfo => packageInfoResult;
221+
222+
/// The value that `ZulipBinding.instance.packageInfo` should return.
223+
PackageInfo packageInfoResult = _defaultPackageInfo;
224+
static final _defaultPackageInfo = PackageInfo(version: '0.0.1', buildNumber: '1');
225+
226+
void _resetPackageInfo() {
227+
packageInfoResult = _defaultPackageInfo;
228+
}
229+
218230
void _resetFirebase() {
219231
_firebaseInitialized = false;
220232
_firebaseMessaging = null;

0 commit comments

Comments
 (0)