File tree Expand file tree Collapse file tree
code_samples/lib/command_it Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import 'package:command_it/command_it.dart' ;
2+ import '_shared/stubs.dart' ;
3+
4+ // #region example
5+ final batchCommand = Command .createAsyncWithProgress <List <Item >, String >(
6+ (items, handle) async {
7+ final total = items.length;
8+ int processed = 0 ;
9+
10+ for (final item in items) {
11+ if (handle.isCanceled.value) {
12+ return 'Canceled ($processed /$total processed)' ;
13+ }
14+
15+ await processItem (item);
16+ processed++ ;
17+
18+ handle.updateProgress (processed / total);
19+ handle.updateStatusMessage ('Processed $processed of $total items' );
20+ }
21+
22+ return 'Processed $total items' ;
23+ },
24+ initialValue: '' ,
25+ );
26+ // #endregion example
27+
28+ void main () {
29+ // Example usage
30+ final items = [Item (), Item (), Item ()];
31+ batchCommand.run (items);
32+ }
Original file line number Diff line number Diff line change 1+ import 'package:command_it/command_it.dart' ;
2+ import 'dart:io' ;
3+
4+ // #region example
5+ void testProgressUpdates () {
6+ final mockCommand = MockCommand <File , String >(
7+ initialValue: '' ,
8+ withProgressHandle: true , // Enable progress simulation
9+ );
10+
11+ // Simulate progress updates
12+ mockCommand.updateMockProgress (0.0 );
13+ mockCommand.updateMockStatusMessage ('Starting upload...' );
14+ assert (mockCommand.progress.value == 0.0 );
15+ assert (mockCommand.statusMessage.value == 'Starting upload...' );
16+
17+ mockCommand.updateMockProgress (0.5 );
18+ mockCommand.updateMockStatusMessage ('Uploading...' );
19+ assert (mockCommand.progress.value == 0.5 );
20+
21+ mockCommand.mockCancel ();
22+ assert (mockCommand.isCanceled.value == true );
23+
24+ mockCommand.dispose ();
25+ }
26+ // #endregion example
27+
28+ void main () {
29+ testProgressUpdates ();
30+ }
Original file line number Diff line number Diff line change 1+ import 'package:command_it/command_it.dart' ;
2+ import '_shared/stubs.dart' ;
3+
4+ // #region example
5+ final multiStepCommand = Command .createAsyncWithProgress <void , String >(
6+ (_, handle) async {
7+ // Step 1: Download (0-40%)
8+ handle.updateStatusMessage ('Downloading data...' );
9+ await downloadData ();
10+ handle.updateProgress (0.4 );
11+
12+ // Step 2: Process (40-80%)
13+ handle.updateStatusMessage ('Processing data...' );
14+ await processData ();
15+ handle.updateProgress (0.8 );
16+
17+ // Step 3: Save (80-100%)
18+ handle.updateStatusMessage ('Saving results...' );
19+ await saveResults ();
20+ handle.updateProgress (1.0 );
21+
22+ return 'Complete' ;
23+ },
24+ initialValue: '' ,
25+ );
26+ // #endregion example
27+
28+ void main () {
29+ // Example usage
30+ multiStepCommand.run ();
31+ }
Original file line number Diff line number Diff line change 1+ import 'package:command_it/command_it.dart' ;
2+ import 'dart:io' ;
3+ import '_shared/stubs.dart' ;
4+
5+ // #region example
6+ final uploadCommand = Command .createAsyncWithProgress <File , String >(
7+ (file, handle) async {
8+ for (int i = 0 ; i <= 100 ; i += 10 ) {
9+ if (handle.isCanceled.value) return 'Canceled' ;
10+
11+ await uploadChunk (file, i);
12+ handle.updateProgress (i / 100.0 );
13+ handle.updateStatusMessage ('Uploading: $i %' );
14+ }
15+ return 'Complete' ;
16+ },
17+ initialValue: '' ,
18+ );
19+ // #endregion example
20+
21+ void main () {
22+ // Example usage
23+ uploadCommand.run (File ('example.txt' ));
24+ }
You can’t perform that action at this time.
0 commit comments