Skip to content

Commit ea4efc7

Browse files
rajveermalviyagnprice
authored andcommitted
binding: Add packageInfo getter
This is a preparatory commit for the work of embedding the package info in user-agent header.
1 parent 2064bb6 commit ea4efc7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/model/binding.dart

Lines changed: 50 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';
@@ -115,6 +116,22 @@ abstract class ZulipBinding {
115116
/// or null if that hasn't resolved yet.
116117
BaseDeviceInfo? get syncDeviceInfo;
117118

119+
/// Provides application package information,
120+
/// via package:package_info_plus.
121+
///
122+
/// The returned Future resolves to null if an error is
123+
/// encountered while fetching the data.
124+
///
125+
/// This wraps [package_info_plus.PackageInfo.fromPlatform].
126+
Future<PackageInfo?> get packageInfo;
127+
128+
/// Provides application package information,
129+
/// via package:package_info_plus.
130+
///
131+
/// This is the value [packageInfo] resolved to,
132+
/// or null if that hasn't resolved yet.
133+
PackageInfo? get syncPackageInfo;
134+
118135
/// Initialize Firebase, to use for notifications.
119136
///
120137
/// This wraps [firebase_core.Firebase.initializeApp].
@@ -163,6 +180,17 @@ class IosDeviceInfo extends BaseDeviceInfo {
163180
IosDeviceInfo({required this.systemVersion});
164181
}
165182

183+
/// Like [package_info_plus.PackageInfo], but without things we don't use.
184+
class PackageInfo {
185+
final String version;
186+
final String buildNumber;
187+
188+
PackageInfo({
189+
required this.version,
190+
required this.buildNumber,
191+
});
192+
}
193+
166194
/// A concrete binding for use in the live application.
167195
///
168196
/// The global store returned by [loadGlobalStore], and consequently by
@@ -174,6 +202,7 @@ class IosDeviceInfo extends BaseDeviceInfo {
174202
class LiveZulipBinding extends ZulipBinding {
175203
LiveZulipBinding() {
176204
_deviceInfo = _prefetchDeviceInfo();
205+
_packageInfo = _prefetchPackageInfo();
177206
}
178207

179208
/// Initialize the binding if necessary, and ensure it is a [LiveZulipBinding].
@@ -232,6 +261,27 @@ class LiveZulipBinding extends ZulipBinding {
232261
return _syncDeviceInfo;
233262
}
234263

264+
@override
265+
Future<PackageInfo?> get packageInfo => _packageInfo;
266+
late Future<PackageInfo?> _packageInfo;
267+
268+
@override
269+
PackageInfo? get syncPackageInfo => _syncPackageInfo;
270+
PackageInfo? _syncPackageInfo;
271+
272+
Future<PackageInfo?> _prefetchPackageInfo() async {
273+
try {
274+
final info = await package_info_plus.PackageInfo.fromPlatform();
275+
_syncPackageInfo = PackageInfo(
276+
version: info.version,
277+
buildNumber: info.buildNumber,
278+
);
279+
} catch (e, st) {
280+
assert(debugLog('Failed to prefetch package info: $e\n$st')); // TODO(log)
281+
}
282+
return _syncPackageInfo;
283+
}
284+
235285
@override
236286
Future<void> firebaseInitializeApp({
237287
required firebase_core.FirebaseOptions options}) {

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
@override
219220
BaseDeviceInfo? get syncDeviceInfo => deviceInfoResult;
220221

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+
230+
@override
231+
Future<PackageInfo?> get packageInfo async => packageInfoResult;
232+
233+
@override
234+
PackageInfo? get syncPackageInfo => packageInfoResult;
235+
221236
void _resetFirebase() {
222237
_firebaseInitialized = false;
223238
_firebaseMessaging = null;

0 commit comments

Comments
 (0)