Skip to content

Commit 24ab7e7

Browse files
committed
Add missing progress code sample files
- progress_upload_basic.dart (referenced in Quick Example) - progress_multi_step.dart (referenced in Common Patterns) - progress_batch_processing.dart (referenced in Common Patterns) - progress_mock_testing.dart (referenced in Testing section) These files were created earlier but not committed.
1 parent 467bea3 commit 24ab7e7

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)