fix(flow): guard stepper and reset state on image-less project load
This commit is contained in:
@@ -205,6 +205,31 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
return File('${projectsDir.path}/latest_project.json');
|
return File('${projectsDir.path}/latest_project.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get _hasSourceImage => _sourceImageBytes != null;
|
||||||
|
|
||||||
|
bool get _canGenerate => _hasSourceImage && !_isGenerating;
|
||||||
|
|
||||||
|
int get _maxAccessibleStepIndex =>
|
||||||
|
_hasSourceImage ? MosaicFlowStep.result.index : MosaicFlowStep.image.index;
|
||||||
|
|
||||||
|
List<MosaicPaletteSnapshotEntry> _catalogSnapshotFromCurrentCatalog() {
|
||||||
|
return _catalog
|
||||||
|
.map((entry) => MosaicPaletteSnapshotEntry(
|
||||||
|
name: entry.name,
|
||||||
|
colorValue: entry.colorValue,
|
||||||
|
))
|
||||||
|
.toList(growable: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _showMissingImageHint() {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text('Bitte zuerst ein Bild auswählen.'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
MosaicProjectData _buildProjectData() {
|
MosaicProjectData _buildProjectData() {
|
||||||
return MosaicProjectData(
|
return MosaicProjectData(
|
||||||
useCapSize: _useCapSize,
|
useCapSize: _useCapSize,
|
||||||
@@ -217,6 +242,7 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
colorVariation: _colorVariation,
|
colorVariation: _colorVariation,
|
||||||
selectedPreset: _selectedPreset.name,
|
selectedPreset: _selectedPreset.name,
|
||||||
sourceImageBytes: _sourceImageBytes,
|
sourceImageBytes: _sourceImageBytes,
|
||||||
|
catalogSnapshot: _catalogSnapshotFromCurrentCatalog(),
|
||||||
savedAt: DateTime.now(),
|
savedAt: DateTime.now(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -288,6 +314,7 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
final data = MosaicProjectData.fromJson(
|
final data = MosaicProjectData.fromJson(
|
||||||
jsonDecode(await file.readAsString()) as Map<String, dynamic>,
|
jsonDecode(await file.readAsString()) as Map<String, dynamic>,
|
||||||
);
|
);
|
||||||
|
final hasCatalogSnapshot = data.catalogSnapshot.isNotEmpty;
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -305,18 +332,22 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
orElse: () => StylePreset.ausgewogen,
|
orElse: () => StylePreset.ausgewogen,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (data.sourceImageBytes != null) {
|
|
||||||
_sourceImageBytes = data.sourceImageBytes;
|
_sourceImageBytes = data.sourceImageBytes;
|
||||||
_result = null;
|
_result = null;
|
||||||
_currentFlowStep = MosaicFlowStep.result;
|
_currentFlowStep =
|
||||||
}
|
_sourceImageBytes == null ? MosaicFlowStep.image : MosaicFlowStep.result;
|
||||||
_activeSection = HomeSection.mosaic;
|
_activeSection = HomeSection.mosaic;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (data.sourceImageBytes != null) await _generate();
|
if (data.sourceImageBytes != null) {
|
||||||
|
await _generate(catalogSnapshotOverride: hasCatalogSnapshot ? data.catalogSnapshot : null);
|
||||||
|
}
|
||||||
if (!silent && mounted) {
|
if (!silent && mounted) {
|
||||||
|
final message = hasCatalogSnapshot
|
||||||
|
? 'Projekt geladen ✅ (mit gespeichertem Katalog-Snapshot)'
|
||||||
|
: 'Projekt geladen ✅ (ohne Snapshot: aktueller Katalog aktiv)';
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(content: Text('Projekt geladen ✅')),
|
SnackBar(content: Text(message)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -879,8 +910,24 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
_scheduleRegenerate();
|
_scheduleRegenerate();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _generate() async {
|
Future<void> _generate({List<MosaicPaletteSnapshotEntry>? catalogSnapshotOverride}) async {
|
||||||
if (_sourceImageBytes == null || _catalog.isEmpty) return;
|
if (_sourceImageBytes == null) {
|
||||||
|
_showMissingImageHint();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final paletteSource = catalogSnapshotOverride != null && catalogSnapshotOverride.isNotEmpty
|
||||||
|
? catalogSnapshotOverride
|
||||||
|
.map((entry) => <String, dynamic>{
|
||||||
|
'name': entry.name,
|
||||||
|
'value': entry.colorValue,
|
||||||
|
})
|
||||||
|
.toList(growable: false)
|
||||||
|
: _catalog
|
||||||
|
.map((p) => <String, dynamic>{'name': p.name, 'value': p.colorValue})
|
||||||
|
.toList(growable: false);
|
||||||
|
|
||||||
|
if (paletteSource.isEmpty) return;
|
||||||
|
|
||||||
final int gridW = math.max(1, int.tryParse(_gridWidthCtrl.text) ?? 40);
|
final int gridW = math.max(1, int.tryParse(_gridWidthCtrl.text) ?? 40);
|
||||||
final int gridH = math.max(1, int.tryParse(_gridHeightCtrl.text) ?? 30);
|
final int gridH = math.max(1, int.tryParse(_gridHeightCtrl.text) ?? 30);
|
||||||
@@ -899,9 +946,7 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
'ditheringStrength': _ditheringStrength,
|
'ditheringStrength': _ditheringStrength,
|
||||||
'edgeEmphasis': _edgeEmphasis,
|
'edgeEmphasis': _edgeEmphasis,
|
||||||
'colorVariation': _colorVariation,
|
'colorVariation': _colorVariation,
|
||||||
'palette': _catalog
|
'palette': paletteSource,
|
||||||
.map((p) => <String, dynamic>{'name': p.name, 'value': p.colorValue})
|
|
||||||
.toList(growable: false),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
final out = await compute(_generateMosaicIsolate, payload);
|
final out = await compute(_generateMosaicIsolate, payload);
|
||||||
@@ -952,7 +997,7 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
? FloatingActionButton.extended(
|
? FloatingActionButton.extended(
|
||||||
backgroundColor: Colors.white.withValues(alpha: 0.85),
|
backgroundColor: Colors.white.withValues(alpha: 0.85),
|
||||||
foregroundColor: Theme.of(context).colorScheme.primary,
|
foregroundColor: Theme.of(context).colorScheme.primary,
|
||||||
onPressed: _isGenerating ? null : _generate,
|
onPressed: _canGenerate ? _generate : null,
|
||||||
icon: _isGenerating
|
icon: _isGenerating
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
width: 18,
|
width: 18,
|
||||||
@@ -1085,8 +1130,12 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
onStepContinue: () {
|
onStepContinue: () {
|
||||||
setState(() {
|
|
||||||
final next = _currentFlowStep.index + 1;
|
final next = _currentFlowStep.index + 1;
|
||||||
|
if (next > _maxAccessibleStepIndex) {
|
||||||
|
_showMissingImageHint();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
if (next <= MosaicFlowStep.result.index) {
|
if (next <= MosaicFlowStep.result.index) {
|
||||||
_currentFlowStep = MosaicFlowStep.values[next];
|
_currentFlowStep = MosaicFlowStep.values[next];
|
||||||
}
|
}
|
||||||
@@ -1101,6 +1150,10 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
onStepTapped: (index) {
|
onStepTapped: (index) {
|
||||||
|
if (index > _maxAccessibleStepIndex) {
|
||||||
|
_showMissingImageHint();
|
||||||
|
return;
|
||||||
|
}
|
||||||
setState(() => _currentFlowStep = MosaicFlowStep.values[index]);
|
setState(() => _currentFlowStep = MosaicFlowStep.values[index]);
|
||||||
},
|
},
|
||||||
steps: [
|
steps: [
|
||||||
@@ -1250,7 +1303,7 @@ class _MosaicHomePageState extends State<MosaicHomePage>
|
|||||||
children: [
|
children: [
|
||||||
FilledButton.icon(
|
FilledButton.icon(
|
||||||
key: const Key('generate-btn'),
|
key: const Key('generate-btn'),
|
||||||
onPressed: _isGenerating ? null : _generate,
|
onPressed: _canGenerate ? _generate : null,
|
||||||
icon: const Icon(Icons.auto_fix_high_rounded),
|
icon: const Icon(Icons.auto_fix_high_rounded),
|
||||||
label: const Text('Generate Mosaic'),
|
label: const Text('Generate Mosaic'),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:korken_mosaic/main.dart';
|
import 'package:korken_mosaic/main.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('shows 4-step workflow and can navigate to result step',
|
testWidgets('stepper blocks forward navigation without image and shows hint',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
await tester.pumpWidget(const KorkenMosaicApp());
|
await tester.pumpWidget(const KorkenMosaicApp());
|
||||||
|
|
||||||
@@ -12,13 +13,23 @@ void main() {
|
|||||||
expect(find.text('4) Ergebnis'), findsOneWidget);
|
expect(find.text('4) Ergebnis'), findsOneWidget);
|
||||||
|
|
||||||
await tester.tap(find.text('Weiter'));
|
await tester.tap(find.text('Weiter'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pump();
|
||||||
await tester.tap(find.text('Weiter'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Weiter'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
expect(find.byKey(const Key('generate-btn')), findsOneWidget);
|
// Stays on step 1 because no source image is available yet.
|
||||||
expect(find.text('Export JSON'), findsOneWidget);
|
expect(find.text('Import target image'), findsOneWidget);
|
||||||
|
expect(find.text('Bitte zuerst ein Bild auswählen.'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('generate actions are disabled without image',
|
||||||
|
(WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(const KorkenMosaicApp());
|
||||||
|
|
||||||
|
final fab = tester.widget<FloatingActionButton>(
|
||||||
|
find.byType(FloatingActionButton),
|
||||||
|
);
|
||||||
|
expect(fab.onPressed, isNull);
|
||||||
|
|
||||||
|
// Result-step action is not reachable before image selection.
|
||||||
|
expect(find.byKey(const Key('generate-btn')), findsNothing);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user