Skip to content

Commit 6a0fa87

Browse files
binding: Support more device/os variants for deviceInfo getter
1 parent 662346e commit 6a0fa87

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

lib/model/binding.dart

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,20 @@ abstract class BaseDeviceInfo {
157157

158158
/// Like [device_info_plus.AndroidDeviceInfo], but without things we don't use.
159159
class AndroidDeviceInfo extends BaseDeviceInfo {
160+
/// The user-visible version string.
161+
///
162+
/// E.g., "1.0" or "3.4b5" or "bananas". This field is an opaque string.
163+
/// Do not assume that its value has any particular structure or that
164+
/// values of RELEASE from different releases can be somehow ordered.
165+
final String release;
166+
160167
/// The Android SDK version.
161168
///
162169
/// Possible values are defined in:
163170
/// https://developer.android.com/reference/android/os/Build.VERSION_CODES.html
164171
final int sdkInt;
165172

166-
AndroidDeviceInfo({required this.sdkInt});
173+
AndroidDeviceInfo({required this.release, required this.sdkInt});
167174
}
168175

169176
/// Like [device_info_plus.IosDeviceInfo], but without things we don't use.
@@ -176,6 +183,56 @@ class IosDeviceInfo extends BaseDeviceInfo {
176183
IosDeviceInfo({required this.systemVersion});
177184
}
178185

186+
/// Like [device_info_plus.MacOsDeviceInfo], but without things we don't use.
187+
class MacOsDeviceInfo extends BaseDeviceInfo {
188+
/// The major release number, such as 10 in version 10.9.3.
189+
final int majorVersion;
190+
191+
/// The minor release number, such as 9 in version 10.9.3.
192+
final int minorVersion;
193+
194+
/// The update release number, such as 3 in version 10.9.3.
195+
final int patchVersion;
196+
197+
MacOsDeviceInfo({
198+
required this.majorVersion,
199+
required this.minorVersion,
200+
required this.patchVersion,
201+
});
202+
}
203+
204+
/// Like [device_info_plus.WindowsDeviceInfo], currently only used to
205+
/// determine if we're on Windows.
206+
class WindowsDeviceInfo implements BaseDeviceInfo {}
207+
208+
/// Like [device_info_plus.LinuxDeviceInfo], but without things we don't use.
209+
///
210+
/// See:
211+
/// https://www.freedesktop.org/software/systemd/man/os-release.html
212+
class LinuxDeviceInfo implements BaseDeviceInfo {
213+
/// A string identifying the operating system, without a version component,
214+
/// and suitable for presentation to the user.
215+
///
216+
/// Examples: 'Fedora', 'Debian GNU/Linux'.
217+
///
218+
/// If not set, defaults to 'Linux'.
219+
final String name;
220+
221+
/// A lower-case string identifying the operating system version, excluding
222+
/// any OS name information or release code name, and suitable for processing
223+
/// by scripts or usage in generated filenames.
224+
///
225+
/// The version is mostly numeric, and contains no spaces or other characters
226+
/// outside of 0–9, a–z, '.', '_' and '-'.
227+
///
228+
/// Examples: '17', '11.04'.
229+
///
230+
/// This field is optional and may be null on some systems.
231+
final String? versionId;
232+
233+
LinuxDeviceInfo({required this.name, required this.versionId});
234+
}
235+
179236
/// Like [package_info_plus.PackageInfo], but without things we don't use.
180237
class PackageInfo {
181238
final String version;
@@ -226,9 +283,16 @@ class LiveZulipBinding extends ZulipBinding {
226283
try {
227284
final info = await device_info_plus.DeviceInfoPlugin().deviceInfo;
228285
_maybeDeviceInfo = switch (info) {
229-
device_info_plus.AndroidDeviceInfo(:var version) => AndroidDeviceInfo(sdkInt: version.sdkInt),
230-
device_info_plus.IosDeviceInfo(:var systemVersion) => IosDeviceInfo(systemVersion: systemVersion),
231-
_ => throw UnimplementedError(),
286+
device_info_plus.AndroidDeviceInfo() => AndroidDeviceInfo(release: info.version.release,
287+
sdkInt: info.version.sdkInt),
288+
device_info_plus.IosDeviceInfo() => IosDeviceInfo(systemVersion: info.systemVersion),
289+
device_info_plus.MacOsDeviceInfo() => MacOsDeviceInfo(majorVersion: info.majorVersion,
290+
minorVersion: info.minorVersion,
291+
patchVersion: info.patchVersion),
292+
device_info_plus.WindowsDeviceInfo() => WindowsDeviceInfo(),
293+
device_info_plus.LinuxDeviceInfo() => LinuxDeviceInfo(name: info.name,
294+
versionId: info.versionId),
295+
_ => throw UnimplementedError(),
232296
};
233297
} catch (e) {
234298
assert(debugLog('Failed to prefetch device info: $e'));

test/model/binding.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class TestZulipBinding extends ZulipBinding {
213213

214214
/// The value that `ZulipBinding.instance.deviceInfo` should return.
215215
BaseDeviceInfo deviceInfoResult = _defaultDeviceInfoResult;
216-
static final _defaultDeviceInfoResult = AndroidDeviceInfo(sdkInt: 33);
216+
static final _defaultDeviceInfoResult = AndroidDeviceInfo(sdkInt: 33, release: '13');
217217

218218
void _resetDeviceInfo() {
219219
deviceInfoResult = _defaultDeviceInfoResult;

test/widgets/clipboard_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ void main() {
6565
});
6666

6767
testWidgets('Android', (WidgetTester tester) async {
68-
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 33);
68+
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 33, release: '13');
6969
await call(tester, text: 'asdf');
7070
await checkClipboardText('asdf');
7171
await checkSnackBar(tester, expected: false);
7272
});
7373

7474
testWidgets('Android <13', (WidgetTester tester) async {
75-
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 32);
75+
testBinding.deviceInfoResult = AndroidDeviceInfo(sdkInt: 32, release: '12L');
7676
await call(tester, text: 'asdf');
7777
await checkClipboardText('asdf');
7878
await checkSnackBar(tester, expected: true);

0 commit comments

Comments
 (0)