Skip to content

Commit df9464d

Browse files
authored
refactor: remove null check enforcements (#11)
2 parents b2f7793 + ecbc6e4 commit df9464d

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.2.1
2+
3+
* refactor: remove null check enforcements (#10)
4+
* docs: update `README.md`
5+
16
## 1.2.0
27

38
* feat: add volume set feature (#8)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies:
2323
video_player_service:
2424
git:
2525
url: https://github.com/BBKDevelopment/Video-Player-Service.git
26-
ref: v1.2.0
26+
ref: v1.2.1
2727
```
2828
2929
Install it:

lib/src/video_player_service.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class VideoPlayerService {
5757
/// Returns `false` if the video has not been initialized.
5858
bool get isPlaying {
5959
try {
60-
return _controller!.value.isPlaying;
60+
return _controller?.value.isPlaying ?? false;
6161
} catch (error, stackTrace) {
6262
log(
6363
'Could not get playing status!',
@@ -74,7 +74,7 @@ class VideoPlayerService {
7474
/// Returns [Duration.zero] if the video has not been initialized.
7575
Duration get position {
7676
try {
77-
return _controller!.value.position;
77+
return _controller?.value.position ?? Duration.zero;
7878
} catch (error, stackTrace) {
7979
log(
8080
'Could not get position!',
@@ -91,7 +91,7 @@ class VideoPlayerService {
9191
/// Returns [Duration.zero] if the video has not been initialized.
9292
Duration get duration {
9393
try {
94-
return _controller!.value.duration;
94+
return _controller?.value.duration ?? Duration.zero;
9595
} catch (error, stackTrace) {
9696
log(
9797
'Could not get duration!',
@@ -108,7 +108,7 @@ class VideoPlayerService {
108108
/// Returns `1` if the video has not been initialized.
109109
double get playbackSpeed {
110110
try {
111-
return _controller!.value.playbackSpeed;
111+
return _controller?.value.playbackSpeed ?? 1;
112112
} catch (error, stackTrace) {
113113
log(
114114
'Could not get playback speed!',
@@ -125,7 +125,7 @@ class VideoPlayerService {
125125
/// Returns `0` if the video has not been initialized.
126126
double get height {
127127
try {
128-
return _controller!.value.size.height;
128+
return _controller?.value.size.height ?? 0;
129129
} catch (error, stackTrace) {
130130
log(
131131
'Could not get height!',
@@ -142,7 +142,7 @@ class VideoPlayerService {
142142
/// Returns `0` if the video has not been initialized.
143143
double get width {
144144
try {
145-
return _controller!.value.size.width;
145+
return _controller?.value.size.width ?? 0;
146146
} catch (error, stackTrace) {
147147
log(
148148
'Could not get width!',
@@ -166,8 +166,8 @@ class VideoPlayerService {
166166
);
167167
}
168168

169-
await _controller!.initialize();
170-
await _controller!.setVolume(volume);
169+
await _controller?.initialize();
170+
await _controller?.setVolume(volume);
171171
} catch (error, stackTrace) {
172172
log(
173173
'Could not load file!',
@@ -198,7 +198,7 @@ class VideoPlayerService {
198198
/// Throws a [PlayVideoException] if the video fails to play.
199199
Future<void> play() async {
200200
try {
201-
await _controller!.play();
201+
await _controller?.play();
202202
} catch (error, stackTrace) {
203203
log(
204204
'Could not play the video!',
@@ -215,7 +215,7 @@ class VideoPlayerService {
215215
/// Throws a [PauseVideoException] if the video fails to pause.
216216
Future<void> pause() async {
217217
try {
218-
await _controller!.pause();
218+
await _controller?.pause();
219219
} catch (error, stackTrace) {
220220
log(
221221
'Could not pause the video!',
@@ -232,7 +232,7 @@ class VideoPlayerService {
232232
/// Throws a [SetVideoPlaybackSpeedException] if could not set playback speed.
233233
Future<void> setPlaybackSpeed(double speed) async {
234234
try {
235-
await _controller!.setPlaybackSpeed(speed);
235+
await _controller?.setPlaybackSpeed(speed);
236236
} catch (error, stackTrace) {
237237
log(
238238
'Could not set playback speed!',
@@ -249,7 +249,7 @@ class VideoPlayerService {
249249
/// Throws a [SetVolumeException] if the video fails to set the volume.
250250
Future<void> setVolume(double volume) async {
251251
try {
252-
await _controller!.setVolume(volume);
252+
await _controller?.setVolume(volume);
253253
} catch (error, stackTrace) {
254254
log(
255255
'Could not set volume!',
@@ -266,7 +266,7 @@ class VideoPlayerService {
266266
/// Throws a [SeekVideoPositionException] if the video fails to seek.
267267
Future<void> seekTo(Duration position) async {
268268
try {
269-
await _controller!.seekTo(position);
269+
await _controller?.seekTo(position);
270270
} catch (error, stackTrace) {
271271
log(
272272
'Could not seek to the position!',
@@ -281,7 +281,7 @@ class VideoPlayerService {
281281
/// Adds a listener to the video player.
282282
void addListener(VoidCallback listener) {
283283
try {
284-
_controller!.addListener(listener);
284+
_controller?.addListener(listener);
285285
} catch (error, stackTrace) {
286286
log(
287287
'Could not add listener!',

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: video_player_service
22
description: Video Player Service
3-
version: 1.2.0
3+
version: 1.2.1
44
publish_to: none
55

66
environment:

test/src/video_player_service_test.dart

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ class _MockVideoPlayerController extends Mock
1717

1818
void main() {
1919
late VideoPlayerService sut;
20+
late VideoPlayerController controller;
2021

2122
setUp(() {
22-
sut = VideoPlayerService(options: VideoPlayerOptions());
23+
controller = _MockVideoPlayerController();
24+
sut = VideoPlayerService.test(controller: controller);
25+
});
26+
27+
tearDown(() {
28+
sut.dispose();
2329
});
2430

2531
group('VideoPlayerService', () {
@@ -28,22 +34,25 @@ void main() {
2834
});
2935

3036
test('can provide a controller', () {
31-
expect(sut.controller, isNull);
37+
expect(sut.controller, isNotNull);
3238
});
3339

3440
test('can provide status of the video player', () {
35-
expect(sut.isReady, false);
41+
expect(sut.isReady, true);
3642
});
3743

3844
test('can provide current status of the video player', () {
45+
when(() => controller.value.isPlaying).thenThrow(Exception());
3946
expect(sut.isPlaying, false);
4047
});
4148

4249
test('can provide current position of the video', () {
50+
when(() => controller.value.position).thenThrow(Exception());
4351
expect(sut.position, Duration.zero);
4452
});
4553

4654
test('can provide duration of the video', () {
55+
when(() => controller.value.duration).thenThrow(Exception());
4756
expect(sut.duration, Duration.zero);
4857
});
4958

@@ -60,23 +69,20 @@ void main() {
6069
});
6170

6271
test('can load a video from file', () async {
63-
final controller = _MockVideoPlayerController();
64-
final videoPlayerService = VideoPlayerService.test(
65-
controller: controller,
66-
);
67-
68-
when(controller.initialize).thenAnswer((_) => Future.value());
69-
when(() => controller.setVolume(any())).thenAnswer((_) => Future.value());
70-
71-
await videoPlayerService.loadFile(File(''));
72-
72+
when(() => controller.initialize()).thenAnswer(Future.value);
73+
when(() => controller.setVolume(any())).thenAnswer(Future.value);
74+
await sut.loadFile(File(''));
7375
verify(controller.initialize).called(1);
7476
verify(() => controller.setVolume(any())).called(1);
7577
});
7678

7779
test('can throw LoadVideoException while loading a video from file', () {
80+
final videoPlayerService = VideoPlayerService(
81+
options: VideoPlayerOptions(),
82+
);
83+
7884
expect(
79-
sut.loadFile(File('')),
85+
videoPlayerService.loadFile(File('')),
8086
throwsA(isA<LoadVideoException>()),
8187
);
8288
});
@@ -87,13 +93,8 @@ void main() {
8793

8894
test('can log the issue when could not dispose the video player controller',
8995
() {
90-
final controller = _MockVideoPlayerController();
91-
final videoPlayerService = VideoPlayerService.test(
92-
controller: controller,
93-
);
94-
9596
when(controller.dispose).thenThrow(Exception());
96-
expect(videoPlayerService.dispose(), isA<Future<void>>());
97+
expect(sut.dispose(), isA<Future<void>>());
9798
});
9899

99100
test('can throw PlayVideoException while trying to play a video', () {

0 commit comments

Comments
 (0)