|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 3 | +import 'package:firebase_auth/firebase_auth.dart'; |
3 | 4 | import 'package:animeishi/ui/home/view/home_page.dart'; |
4 | 5 | import 'package:animeishi/ui/camera/services/anime_analysis_service.dart'; |
5 | 6 | import 'package:animeishi/ui/camera/services/scan_data_service.dart'; |
6 | 7 | import '../controllers/scan_result_animation_controller.dart'; |
7 | | -import '../services/scan_data_service.dart'; |
8 | 8 | import '../components/scan_result_widgets.dart'; |
9 | 9 | import '../components/scan_result_painters.dart'; |
10 | 10 |
|
@@ -60,6 +60,23 @@ class _ScanDataWidgetState extends State<ScanDataWidget> |
60 | 60 | // データ取得 |
61 | 61 | try { |
62 | 62 | final result = await ScanDataService.fetchUserData(_userId!); |
| 63 | + |
| 64 | + // 既存の分析結果を取得 |
| 65 | + final currentUserId = FirebaseAuth.instance.currentUser?.uid; |
| 66 | + if (currentUserId != null && result.success) { |
| 67 | + final savedComment = await ScanDataService.getAnalysisComment( |
| 68 | + currentUserId, |
| 69 | + _userId!, |
| 70 | + ); |
| 71 | + if (savedComment != null) { |
| 72 | + if (!mounted) return; |
| 73 | + setState(() { |
| 74 | + analysisComment = savedComment; |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (!mounted) return; |
63 | 80 | setState(() { |
64 | 81 | _scanResult = result; |
65 | 82 | _isLoading = false; |
@@ -94,11 +111,28 @@ class _ScanDataWidgetState extends State<ScanDataWidget> |
94 | 111 | animeList, |
95 | 112 | username: username, |
96 | 113 | ); |
| 114 | + |
| 115 | + // 分析結果をFirestoreに保存 |
| 116 | + final currentUserId = FirebaseAuth.instance.currentUser?.uid; |
| 117 | + if (currentUserId != null) { |
| 118 | + try { |
| 119 | + await ScanDataService.saveAnalysisComment( |
| 120 | + currentUserId, |
| 121 | + userId, |
| 122 | + comment, |
| 123 | + ); |
| 124 | + } catch (e) { |
| 125 | + debugPrint('analysisComment保存失敗: $e'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (!mounted) return; |
97 | 130 | setState(() { |
98 | 131 | analysisComment = comment; |
99 | 132 | isAnalyzing = false; |
100 | 133 | }); |
101 | 134 | } catch (e) { |
| 135 | + if (!mounted) return; |
102 | 136 | setState(() { |
103 | 137 | analysisComment = 'AI分析に失敗しました: $e'; |
104 | 138 | isAnalyzing = false; |
@@ -166,10 +200,7 @@ class _ScanDataWidgetState extends State<ScanDataWidget> |
166 | 200 | (route) => false, |
167 | 201 | ); |
168 | 202 | }, |
169 | | - icon: const Icon( |
170 | | - Icons.arrow_back, |
171 | | - color: Color(0xFF667EEA), |
172 | | - ), |
| 203 | + icon: const Icon(Icons.arrow_back, color: Color(0xFF667EEA)), |
173 | 204 | ), |
174 | 205 | ), |
175 | 206 | ), |
@@ -225,30 +256,13 @@ class _ScanDataWidgetState extends State<ScanDataWidget> |
225 | 256 | ScanResultWidgets.buildAnimeListCard(animeList), |
226 | 257 |
|
227 | 258 | const SizedBox(height: 24), |
228 | | - ElevatedButton.icon( |
229 | | - icon: const Icon(Icons.insights), |
230 | | - label: const Text('AIで視聴傾向を分析'), |
231 | | - onPressed: isAnalyzing ? null : () => _runAnalysis(_userId!), |
232 | | - style: ElevatedButton.styleFrom( |
233 | | - backgroundColor: const Color(0xFF667EEA), |
234 | | - foregroundColor: Colors.white, |
235 | | - minimumSize: const Size(double.infinity, 48), |
236 | | - ), |
237 | | - ), |
238 | | - const SizedBox(height: 16), |
239 | | - if (isAnalyzing) |
240 | | - Row( |
241 | | - children: [ |
242 | | - const CircularProgressIndicator(), |
243 | | - const SizedBox(width: 12), |
244 | | - const Text('分析中...'), |
245 | | - ], |
246 | | - ), |
247 | | - if (analysisComment != null) |
| 259 | + |
| 260 | + // 分析結果表示(既存の場合) |
| 261 | + if (analysisComment != null && !isAnalyzing) |
248 | 262 | Container( |
249 | 263 | width: double.infinity, |
250 | 264 | padding: const EdgeInsets.all(16), |
251 | | - margin: const EdgeInsets.only(top: 8), |
| 265 | + margin: const EdgeInsets.only(bottom: 16), |
252 | 266 | decoration: BoxDecoration( |
253 | 267 | color: Colors.white, |
254 | 268 | borderRadius: BorderRadius.circular(15), |
@@ -282,6 +296,32 @@ class _ScanDataWidgetState extends State<ScanDataWidget> |
282 | 296 | ], |
283 | 297 | ), |
284 | 298 | ), |
| 299 | + |
| 300 | + // 分析ボタン |
| 301 | + ElevatedButton.icon( |
| 302 | + icon: const Icon(Icons.insights), |
| 303 | + label: Text(analysisComment != null ? 'AIで再分析' : 'AIで視聴傾向を分析'), |
| 304 | + onPressed: isAnalyzing ? null : () => _runAnalysis(_userId!), |
| 305 | + style: ElevatedButton.styleFrom( |
| 306 | + backgroundColor: const Color(0xFF667EEA), |
| 307 | + foregroundColor: Colors.white, |
| 308 | + minimumSize: const Size(double.infinity, 48), |
| 309 | + ), |
| 310 | + ), |
| 311 | + |
| 312 | + // 分析中表示 |
| 313 | + if (isAnalyzing) |
| 314 | + const Padding( |
| 315 | + padding: EdgeInsets.only(top: 16), |
| 316 | + child: Row( |
| 317 | + mainAxisAlignment: MainAxisAlignment.center, |
| 318 | + children: [ |
| 319 | + CircularProgressIndicator(), |
| 320 | + SizedBox(width: 12), |
| 321 | + Text('分析中...'), |
| 322 | + ], |
| 323 | + ), |
| 324 | + ), |
285 | 325 | ], |
286 | 326 | ), |
287 | 327 | ), |
|
0 commit comments