Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit a10ac2e

Browse files
[camera] Switch platform interface to new analysis options (#4613)
Switches camera_platform_interface from the legacy analysis options to the new ones, fixing all violations they flag. Part of flutter/flutter#76229
1 parent 6a9de4f commit a10ac2e

25 files changed

+572
-476
lines changed

packages/camera/camera_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.2
2+
3+
* Adopts new analysis options and fixes all violations.
4+
15
## 2.1.1
26

37
* Add web-relevant docs to platform interface code.

packages/camera/camera_platform_interface/analysis_options.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/camera/camera_platform_interface/lib/camera_platform_interface.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
/// Expose XFile
6+
export 'package:cross_file/cross_file.dart';
7+
58
export 'src/events/camera_event.dart';
69
export 'src/events/device_event.dart';
710
export 'src/platform_interface/camera_platform.dart';
811
export 'src/types/types.dart';
9-
10-
/// Expose XFile
11-
export 'package:cross_file/cross_file.dart';

packages/camera/camera_platform_interface/lib/src/events/camera_event.dart

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:camera_platform_interface/src/types/focus_mode.dart';
6+
import 'package:meta/meta.dart';
67

78
import '../../camera_platform_interface.dart';
89

@@ -22,14 +23,15 @@ import '../../camera_platform_interface.dart';
2223
/// See below for examples: `CameraClosingEvent`, `CameraErrorEvent`...
2324
/// These events are more semantic and more pleasant to use than raw generics.
2425
/// They can be (and in fact, are) filtered by the `instanceof`-operator.
26+
@immutable
2527
abstract class CameraEvent {
26-
/// The ID of the Camera this event is associated to.
27-
final int cameraId;
28-
2928
/// Build a Camera Event, that relates a `cameraId`.
3029
///
3130
/// The `cameraId` is the ID of the camera that triggered the event.
32-
CameraEvent(this.cameraId) : assert(cameraId != null);
31+
const CameraEvent(this.cameraId) : assert(cameraId != null);
32+
33+
/// The ID of the Camera this event is associated to.
34+
final int cameraId;
3335

3436
@override
3537
bool operator ==(Object other) =>
@@ -44,30 +46,12 @@ abstract class CameraEvent {
4446

4547
/// An event fired when the camera has finished initializing.
4648
class CameraInitializedEvent extends CameraEvent {
47-
/// The width of the preview in pixels.
48-
final double previewWidth;
49-
50-
/// The height of the preview in pixels.
51-
final double previewHeight;
52-
53-
/// The default exposure mode
54-
final ExposureMode exposureMode;
55-
56-
/// The default focus mode
57-
final FocusMode focusMode;
58-
59-
/// Whether setting exposure points is supported.
60-
final bool exposurePointSupported;
61-
62-
/// Whether setting focus points is supported.
63-
final bool focusPointSupported;
64-
6549
/// Build a CameraInitialized event triggered from the camera represented by
6650
/// `cameraId`.
6751
///
6852
/// The `previewWidth` represents the width of the generated preview in pixels.
6953
/// The `previewHeight` represents the height of the generated preview in pixels.
70-
CameraInitializedEvent(
54+
const CameraInitializedEvent(
7155
int cameraId,
7256
this.previewWidth,
7357
this.previewHeight,
@@ -80,17 +64,36 @@ class CameraInitializedEvent extends CameraEvent {
8064
/// Converts the supplied [Map] to an instance of the [CameraInitializedEvent]
8165
/// class.
8266
CameraInitializedEvent.fromJson(Map<String, dynamic> json)
83-
: previewWidth = json['previewWidth'],
84-
previewHeight = json['previewHeight'],
85-
exposureMode = deserializeExposureMode(json['exposureMode']),
86-
exposurePointSupported = json['exposurePointSupported'] ?? false,
87-
focusMode = deserializeFocusMode(json['focusMode']),
88-
focusPointSupported = json['focusPointSupported'] ?? false,
89-
super(json['cameraId']);
67+
: previewWidth = json['previewWidth']! as double,
68+
previewHeight = json['previewHeight']! as double,
69+
exposureMode = deserializeExposureMode(json['exposureMode']! as String),
70+
exposurePointSupported =
71+
(json['exposurePointSupported'] as bool?) ?? false,
72+
focusMode = deserializeFocusMode(json['focusMode']! as String),
73+
focusPointSupported = (json['focusPointSupported'] as bool?) ?? false,
74+
super(json['cameraId']! as int);
75+
76+
/// The width of the preview in pixels.
77+
final double previewWidth;
78+
79+
/// The height of the preview in pixels.
80+
final double previewHeight;
81+
82+
/// The default exposure mode
83+
final ExposureMode exposureMode;
84+
85+
/// The default focus mode
86+
final FocusMode focusMode;
87+
88+
/// Whether setting exposure points is supported.
89+
final bool exposurePointSupported;
90+
91+
/// Whether setting focus points is supported.
92+
final bool focusPointSupported;
9093

9194
/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
9295
/// can be serialized to JSON.
93-
Map<String, dynamic> toJson() => {
96+
Map<String, dynamic> toJson() => <String, Object>{
9497
'cameraId': cameraId,
9598
'previewWidth': previewWidth,
9699
'previewHeight': previewHeight,
@@ -126,18 +129,12 @@ class CameraInitializedEvent extends CameraEvent {
126129

127130
/// An event fired when the resolution preset of the camera has changed.
128131
class CameraResolutionChangedEvent extends CameraEvent {
129-
/// The capture width in pixels.
130-
final double captureWidth;
131-
132-
/// The capture height in pixels.
133-
final double captureHeight;
134-
135132
/// Build a CameraResolutionChanged event triggered from the camera
136133
/// represented by `cameraId`.
137134
///
138135
/// The `captureWidth` represents the width of the resulting image in pixels.
139136
/// The `captureHeight` represents the height of the resulting image in pixels.
140-
CameraResolutionChangedEvent(
137+
const CameraResolutionChangedEvent(
141138
int cameraId,
142139
this.captureWidth,
143140
this.captureHeight,
@@ -146,13 +143,19 @@ class CameraResolutionChangedEvent extends CameraEvent {
146143
/// Converts the supplied [Map] to an instance of the
147144
/// [CameraResolutionChangedEvent] class.
148145
CameraResolutionChangedEvent.fromJson(Map<String, dynamic> json)
149-
: captureWidth = json['captureWidth'],
150-
captureHeight = json['captureHeight'],
151-
super(json['cameraId']);
146+
: captureWidth = json['captureWidth']! as double,
147+
captureHeight = json['captureHeight']! as double,
148+
super(json['cameraId']! as int);
149+
150+
/// The capture width in pixels.
151+
final double captureWidth;
152+
153+
/// The capture height in pixels.
154+
final double captureHeight;
152155

153156
/// Converts the [CameraResolutionChangedEvent] instance into a [Map] instance
154157
/// that can be serialized to JSON.
155-
Map<String, dynamic> toJson() => {
158+
Map<String, dynamic> toJson() => <String, Object>{
156159
'cameraId': cameraId,
157160
'captureWidth': captureWidth,
158161
'captureHeight': captureHeight,
@@ -162,7 +165,7 @@ class CameraResolutionChangedEvent extends CameraEvent {
162165
bool operator ==(Object other) =>
163166
identical(this, other) ||
164167
other is CameraResolutionChangedEvent &&
165-
super == (other) &&
168+
super == other &&
166169
runtimeType == other.runtimeType &&
167170
captureWidth == other.captureWidth &&
168171
captureHeight == other.captureHeight;
@@ -176,58 +179,61 @@ class CameraResolutionChangedEvent extends CameraEvent {
176179
class CameraClosingEvent extends CameraEvent {
177180
/// Build a CameraClosing event triggered from the camera represented by
178181
/// `cameraId`.
179-
CameraClosingEvent(int cameraId) : super(cameraId);
182+
const CameraClosingEvent(int cameraId) : super(cameraId);
180183

181184
/// Converts the supplied [Map] to an instance of the [CameraClosingEvent]
182185
/// class.
183186
CameraClosingEvent.fromJson(Map<String, dynamic> json)
184-
: super(json['cameraId']);
187+
: super(json['cameraId']! as int);
185188

186189
/// Converts the [CameraClosingEvent] instance into a [Map] instance that can
187190
/// be serialized to JSON.
188-
Map<String, dynamic> toJson() => {
191+
Map<String, dynamic> toJson() => <String, Object>{
189192
'cameraId': cameraId,
190193
};
191194

192195
@override
193196
bool operator ==(Object other) =>
194197
identical(this, other) ||
195-
super == (other) &&
198+
super == other &&
196199
other is CameraClosingEvent &&
197200
runtimeType == other.runtimeType;
198201

199202
@override
203+
// This is here even though it just calls super to make it less likely that
204+
// operator== would be changed without changing `hashCode`.
205+
// ignore: unnecessary_overrides
200206
int get hashCode => super.hashCode;
201207
}
202208

203209
/// An event fired when an error occured while operating the camera.
204210
class CameraErrorEvent extends CameraEvent {
205-
/// Description of the error.
206-
final String description;
207-
208211
/// Build a CameraError event triggered from the camera represented by
209212
/// `cameraId`.
210213
///
211214
/// The `description` represents the error occured on the camera.
212-
CameraErrorEvent(int cameraId, this.description) : super(cameraId);
215+
const CameraErrorEvent(int cameraId, this.description) : super(cameraId);
213216

214217
/// Converts the supplied [Map] to an instance of the [CameraErrorEvent]
215218
/// class.
216219
CameraErrorEvent.fromJson(Map<String, dynamic> json)
217-
: description = json['description'],
218-
super(json['cameraId']);
220+
: description = json['description']! as String,
221+
super(json['cameraId']! as int);
222+
223+
/// Description of the error.
224+
final String description;
219225

220226
/// Converts the [CameraErrorEvent] instance into a [Map] instance that can be
221227
/// serialized to JSON.
222-
Map<String, dynamic> toJson() => {
228+
Map<String, dynamic> toJson() => <String, Object>{
223229
'cameraId': cameraId,
224230
'description': description,
225231
};
226232

227233
@override
228234
bool operator ==(Object other) =>
229235
identical(this, other) ||
230-
super == (other) &&
236+
super == other &&
231237
other is CameraErrorEvent &&
232238
runtimeType == other.runtimeType &&
233239
description == other.description;
@@ -238,32 +244,32 @@ class CameraErrorEvent extends CameraEvent {
238244

239245
/// An event fired when a video has finished recording.
240246
class VideoRecordedEvent extends CameraEvent {
241-
/// XFile of the recorded video.
242-
final XFile file;
243-
244-
/// Maximum duration of the recorded video.
245-
final Duration? maxVideoDuration;
246-
247247
/// Build a VideoRecordedEvent triggered from the camera with the `cameraId`.
248248
///
249249
/// The `file` represents the file of the video.
250250
/// The `maxVideoDuration` shows if a maxVideoDuration shows if a maximum
251251
/// video duration was set.
252-
VideoRecordedEvent(int cameraId, this.file, this.maxVideoDuration)
252+
const VideoRecordedEvent(int cameraId, this.file, this.maxVideoDuration)
253253
: super(cameraId);
254254

255255
/// Converts the supplied [Map] to an instance of the [VideoRecordedEvent]
256256
/// class.
257257
VideoRecordedEvent.fromJson(Map<String, dynamic> json)
258-
: file = XFile(json['path']),
258+
: file = XFile(json['path']! as String),
259259
maxVideoDuration = json['maxVideoDuration'] != null
260260
? Duration(milliseconds: json['maxVideoDuration'] as int)
261261
: null,
262-
super(json['cameraId']);
262+
super(json['cameraId']! as int);
263+
264+
/// XFile of the recorded video.
265+
final XFile file;
266+
267+
/// Maximum duration of the recorded video.
268+
final Duration? maxVideoDuration;
263269

264270
/// Converts the [VideoRecordedEvent] instance into a [Map] instance that can be
265271
/// serialized to JSON.
266-
Map<String, dynamic> toJson() => {
272+
Map<String, dynamic> toJson() => <String, Object?>{
267273
'cameraId': cameraId,
268274
'path': file.path,
269275
'maxVideoDuration': maxVideoDuration?.inMilliseconds

packages/camera/camera_platform_interface/lib/src/events/device_event.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:camera_platform_interface/src/utils/utils.dart';
66
import 'package:flutter/services.dart';
7+
import 'package:meta/meta.dart';
78

89
/// Generic Event coming from the native side of Camera,
910
/// not related to a specific camera module.
@@ -18,24 +19,29 @@ import 'package:flutter/services.dart';
1819
/// See below for examples: `DeviceOrientationChangedEvent`...
1920
/// These events are more semantic and more pleasant to use than raw generics.
2021
/// They can be (and in fact, are) filtered by the `instanceof`-operator.
21-
abstract class DeviceEvent {}
22+
@immutable
23+
abstract class DeviceEvent {
24+
/// Creates a new device event.
25+
const DeviceEvent();
26+
}
2227

2328
/// The [DeviceOrientationChangedEvent] is fired every time the orientation of the device UI changes.
2429
class DeviceOrientationChangedEvent extends DeviceEvent {
25-
/// The new orientation of the device
26-
final DeviceOrientation orientation;
27-
2830
/// Build a new orientation changed event.
29-
DeviceOrientationChangedEvent(this.orientation);
31+
const DeviceOrientationChangedEvent(this.orientation);
3032

3133
/// Converts the supplied [Map] to an instance of the [DeviceOrientationChangedEvent]
3234
/// class.
3335
DeviceOrientationChangedEvent.fromJson(Map<String, dynamic> json)
34-
: orientation = deserializeDeviceOrientation(json['orientation']);
36+
: orientation =
37+
deserializeDeviceOrientation(json['orientation']! as String);
38+
39+
/// The new orientation of the device
40+
final DeviceOrientation orientation;
3541

3642
/// Converts the [DeviceOrientationChangedEvent] instance into a [Map] instance that
3743
/// can be serialized to JSON.
38-
Map<String, dynamic> toJson() => {
44+
Map<String, dynamic> toJson() => <String, Object>{
3945
'orientation': serializeDeviceOrientation(orientation),
4046
};
4147

0 commit comments

Comments
 (0)