Skip to content

Commit 2f12fc4

Browse files
binding: Support more device/os variants for deviceInfo getter
1 parent ea3f531 commit 2f12fc4

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
@@ -169,13 +169,20 @@ abstract class BaseDeviceInfo {
169169

170170
/// Like [device_info_plus.AndroidDeviceInfo], but without things we don't use.
171171
class AndroidDeviceInfo extends BaseDeviceInfo {
172+
/// The user-visible version string.
173+
///
174+
/// E.g., "1.0" or "3.4b5" or "bananas". This field is an opaque string.
175+
/// Do not assume that its value has any particular structure or that
176+
/// values of RELEASE from different releases can be somehow ordered.
177+
final String release;
178+
172179
/// The Android SDK version.
173180
///
174181
/// Possible values are defined in:
175182
/// https://developer.android.com/reference/android/os/Build.VERSION_CODES.html
176183
final int sdkInt;
177184

178-
AndroidDeviceInfo({required this.sdkInt});
185+
AndroidDeviceInfo({required this.release, required this.sdkInt});
179186
}
180187

181188
/// Like [device_info_plus.IosDeviceInfo], but without things we don't use.
@@ -188,6 +195,56 @@ class IosDeviceInfo extends BaseDeviceInfo {
188195
IosDeviceInfo({required this.systemVersion});
189196
}
190197

198+
/// Like [device_info_plus.MacOsDeviceInfo], but without things we don't use.
199+
class MacOsDeviceInfo extends BaseDeviceInfo {
200+
/// The major release number, such as 10 in version 10.9.3.
201+
final int majorVersion;
202+
203+
/// The minor release number, such as 9 in version 10.9.3.
204+
final int minorVersion;
205+
206+
/// The update release number, such as 3 in version 10.9.3.
207+
final int patchVersion;
208+
209+
MacOsDeviceInfo({
210+
required this.majorVersion,
211+
required this.minorVersion,
212+
required this.patchVersion,
213+
});
214+
}
215+
216+
/// Like [device_info_plus.WindowsDeviceInfo], currently only used to
217+
/// determine if we're on Windows.
218+
class WindowsDeviceInfo implements BaseDeviceInfo {}
219+
220+
/// Like [device_info_plus.LinuxDeviceInfo], but without things we don't use.
221+
///
222+
/// See:
223+
/// https://www.freedesktop.org/software/systemd/man/os-release.html
224+
class LinuxDeviceInfo implements BaseDeviceInfo {
225+
/// A string identifying the operating system, without a version component,
226+
/// and suitable for presentation to the user.
227+
///
228+
/// Examples: 'Fedora', 'Debian GNU/Linux'.
229+
///
230+
/// If not set, defaults to 'Linux'.
231+
final String name;
232+
233+
/// A lower-case string identifying the operating system version, excluding
234+
/// any OS name information or release code name, and suitable for processing
235+
/// by scripts or usage in generated filenames.
236+
///
237+
/// The version is mostly numeric, and contains no spaces or other characters
238+
/// outside of 0–9, a–z, '.', '_' and '-'.
239+
///
240+
/// Examples: '17', '11.04'.
241+
///
242+
/// This field is optional and may be null on some systems.
243+
final String? versionId;
244+
245+
LinuxDeviceInfo({required this.name, required this.versionId});
246+
}
247+
191248
/// Like [package_info_plus.PackageInfo], but without things we don't use.
192249
class PackageInfo {
193250
final String version;
@@ -238,9 +295,16 @@ class LiveZulipBinding extends ZulipBinding {
238295
try {
239296
final info = await device_info_plus.DeviceInfoPlugin().deviceInfo;
240297
_maybeDeviceInfo = switch (info) {
241-
device_info_plus.AndroidDeviceInfo(:var version) => AndroidDeviceInfo(sdkInt: version.sdkInt),
242-
device_info_plus.IosDeviceInfo(:var systemVersion) => IosDeviceInfo(systemVersion: systemVersion),
243-
_ => throw UnimplementedError(),
298+
device_info_plus.AndroidDeviceInfo() => AndroidDeviceInfo(release: info.version.release,
299+
sdkInt: info.version.sdkInt),
300+
device_info_plus.IosDeviceInfo() => IosDeviceInfo(systemVersion: info.systemVersion),
301+
device_info_plus.MacOsDeviceInfo() => MacOsDeviceInfo(majorVersion: info.majorVersion,
302+
minorVersion: info.minorVersion,
303+
patchVersion: info.patchVersion),
304+
device_info_plus.WindowsDeviceInfo() => WindowsDeviceInfo(),
305+
device_info_plus.LinuxDeviceInfo() => LinuxDeviceInfo(name: info.name,
306+
versionId: info.versionId),
307+
_ => throw UnimplementedError(),
244308
};
245309
} catch (e) {
246310
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)