observation_page.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../../../../data/models/analysis_models.dart';
  5. import '../../../../services/analysis_providers.dart';
  6. import '../../../shared/presentation/widgets/async_value_view.dart';
  7. import '../../../shared/presentation/widgets/lab_section_scaffold.dart';
  8. class ObservationPage extends ConsumerWidget {
  9. const ObservationPage({super.key});
  10. @override
  11. Widget build(BuildContext context, WidgetRef ref) {
  12. final snapshot = ref.watch(selectedSessionSnapshotProvider);
  13. final observation = ref.watch(selectedObservationProvider);
  14. return LabSectionScaffold(
  15. eyebrow: 'Observation',
  16. title: 'Inspect sample metadata and probe evidence.',
  17. description:
  18. 'This page will show the captured audio summary, waveform slices, probe output, and sample tags before deeper analysis begins.',
  19. children: [
  20. AsyncValueView(
  21. value: snapshot,
  22. loadingMessage: 'Loading observation snapshot...',
  23. data: (session) {
  24. if (session != null) {
  25. return _ObservationSessionView(session: session);
  26. }
  27. return AsyncValueView(
  28. value: observation,
  29. loadingMessage: 'Loading observation details...',
  30. data: (selectedObservation) {
  31. if (selectedObservation == null) {
  32. return const _EmptySelectionCard();
  33. }
  34. return _ObservationOnlyView(observation: selectedObservation);
  35. },
  36. );
  37. },
  38. ),
  39. ],
  40. );
  41. }
  42. }
  43. class _ObservationSessionView extends StatelessWidget {
  44. const _ObservationSessionView({required this.session});
  45. final AnalysisSessionSnapshot session;
  46. @override
  47. Widget build(BuildContext context) {
  48. return Column(
  49. children: [
  50. _ObservationDetailView(
  51. observation: session.observation,
  52. headerLine: 'Mode: ${session.mode} | Status: ${session.status}',
  53. ),
  54. const SizedBox(height: 16),
  55. Card(
  56. child: Padding(
  57. padding: const EdgeInsets.all(20),
  58. child: Column(
  59. crossAxisAlignment: CrossAxisAlignment.start,
  60. children: [
  61. Text(
  62. 'Probe Evidence',
  63. style: Theme.of(context).textTheme.titleLarge,
  64. ),
  65. const SizedBox(height: 12),
  66. ...session.probeEvidence.map(
  67. (evidence) => ListTile(
  68. contentPadding: EdgeInsets.zero,
  69. title: Text(
  70. '${evidence.category} | ${evidence.producerModuleId ?? 'unknown'}',
  71. ),
  72. subtitle: Text(
  73. evidence.values.entries
  74. .map((entry) => '${entry.key}: ${entry.value}')
  75. .join(' | '),
  76. ),
  77. trailing: Text(evidence.confidence.toStringAsFixed(2)),
  78. ),
  79. ),
  80. ],
  81. ),
  82. ),
  83. ),
  84. const SizedBox(height: 16),
  85. FilledButton(
  86. onPressed: () => context.goNamed('experiment'),
  87. child: const Text('Open Experiment Timeline'),
  88. ),
  89. ],
  90. );
  91. }
  92. }
  93. class _ObservationDetailView extends StatelessWidget {
  94. const _ObservationDetailView({
  95. required this.observation,
  96. this.headerLine,
  97. });
  98. final ObservationSummary observation;
  99. final String? headerLine;
  100. @override
  101. Widget build(BuildContext context) {
  102. final theme = Theme.of(context);
  103. return Card(
  104. child: Padding(
  105. padding: const EdgeInsets.all(20),
  106. child: Column(
  107. crossAxisAlignment: CrossAxisAlignment.start,
  108. children: [
  109. Text(
  110. 'Observation ${observation.id}',
  111. style: theme.textTheme.titleLarge,
  112. ),
  113. if (headerLine != null) ...[
  114. const SizedBox(height: 10),
  115. Text(headerLine!, style: theme.textTheme.bodyMedium),
  116. ],
  117. const SizedBox(height: 14),
  118. Text(
  119. 'Duration ${observation.durationMs} ms | ${observation.sampleRate} Hz | ${observation.channels} channel',
  120. style: theme.textTheme.bodyLarge,
  121. ),
  122. const SizedBox(height: 12),
  123. if (observation.tags.isEmpty)
  124. Text('No tags attached yet.', style: theme.textTheme.bodyMedium)
  125. else
  126. Wrap(
  127. spacing: 8,
  128. runSpacing: 8,
  129. children: observation.tags
  130. .map((tag) => Chip(label: Text(tag)))
  131. .toList(),
  132. ),
  133. if (observation.captureMetadata.isNotEmpty) ...[
  134. const SizedBox(height: 14),
  135. ...observation.captureMetadata.entries.map(
  136. (entry) => Padding(
  137. padding: const EdgeInsets.only(bottom: 4),
  138. child: Text('${entry.key}: ${entry.value}'),
  139. ),
  140. ),
  141. ],
  142. ],
  143. ),
  144. ),
  145. );
  146. }
  147. }
  148. class _ObservationOnlyView extends ConsumerStatefulWidget {
  149. const _ObservationOnlyView({required this.observation});
  150. final ObservationSummary observation;
  151. @override
  152. ConsumerState<_ObservationOnlyView> createState() =>
  153. _ObservationOnlyViewState();
  154. }
  155. class _ObservationOnlyViewState extends ConsumerState<_ObservationOnlyView> {
  156. bool _creatingSession = false;
  157. String? _errorMessage;
  158. @override
  159. Widget build(BuildContext context) {
  160. return Column(
  161. children: [
  162. _ObservationDetailView(observation: widget.observation),
  163. const SizedBox(height: 16),
  164. if (_errorMessage != null)
  165. Card(
  166. child: Padding(
  167. padding: const EdgeInsets.all(20),
  168. child: Text(_errorMessage!),
  169. ),
  170. ),
  171. if (_errorMessage != null) const SizedBox(height: 16),
  172. FilledButton(
  173. onPressed: _creatingSession ? null : _createSession,
  174. child: Text(
  175. _creatingSession ? 'Creating Analysis Session...' : 'Analyze This Observation',
  176. ),
  177. ),
  178. ],
  179. );
  180. }
  181. Future<void> _createSession() async {
  182. setState(() {
  183. _creatingSession = true;
  184. _errorMessage = null;
  185. });
  186. try {
  187. await ref
  188. .read(sessionActionsProvider)
  189. .createSessionForObservation(widget.observation.id);
  190. if (!mounted) return;
  191. context.goNamed('experiment');
  192. } catch (error) {
  193. if (!mounted) return;
  194. setState(() {
  195. _errorMessage = 'Failed to create analysis session: $error';
  196. });
  197. } finally {
  198. if (mounted) {
  199. setState(() {
  200. _creatingSession = false;
  201. });
  202. }
  203. }
  204. }
  205. }
  206. class _EmptySelectionCard extends StatelessWidget {
  207. const _EmptySelectionCard();
  208. @override
  209. Widget build(BuildContext context) {
  210. return const Card(
  211. child: Padding(
  212. padding: EdgeInsets.all(20),
  213. child: Text(
  214. 'No observation selected yet. Open one from History or create a new capture.',
  215. ),
  216. ),
  217. );
  218. }
  219. }