games/packages/feature_common/lib/screens/game_completion_screen.dart

123 lines
3.6 KiB
Dart
Raw Normal View History

2025-11-14 18:03:50 +09:00
import 'package:flutter/material.dart';
2025-11-17 18:21:49 +09:00
import 'package:provider/provider.dart';
2025-11-14 18:03:50 +09:00
import 'package:service_api/service_api.dart';
2025-12-15 18:18:17 +09:00
import 'base_game_screen.dart'; // GameResultArgs import
2025-11-14 18:03:50 +09:00
class GameCompletionScreen extends StatefulWidget {
final GameResultArgs args;
2025-12-15 18:18:17 +09:00
final bool isDailyCourse;
2025-11-14 18:03:50 +09:00
2025-12-15 18:18:17 +09:00
const GameCompletionScreen({
super.key,
required this.args,
this.isDailyCourse = false,
});
2025-11-14 18:03:50 +09:00
@override
State<GameCompletionScreen> createState() => _GameCompletionScreenState();
}
class _GameCompletionScreenState extends State<GameCompletionScreen> {
2025-12-15 18:18:17 +09:00
late TextEditingController _nameController;
late IdentityService _identityService;
bool _isSaving = false;
String? _userName;
2025-11-14 18:03:50 +09:00
@override
void initState() {
super.initState();
2025-12-15 18:18:17 +09:00
_identityService = context.read<IdentityService>();
// [Fix] args.userName 사용 가능
String? initialName = widget.args.userName;
_nameController = TextEditingController(text: initialName ?? '');
_loadUserInfo();
2025-11-14 18:03:50 +09:00
}
2025-12-15 18:18:17 +09:00
Future<void> _loadUserInfo() async {
final session = await _identityService.getUserSession();
String? name = session?.userName ?? await _identityService.getUserName();
2025-11-14 18:03:50 +09:00
2025-12-15 18:18:17 +09:00
// 만약 args에 이름이 없고 저장된 이름이 있다면 불러옴
if (widget.args.userName == null && name != null) {
if (mounted) {
setState(() {
_nameController.text = name;
_userName = name;
});
2025-11-14 18:03:50 +09:00
}
}
2025-12-15 18:18:17 +09:00
if (session != null && !session.isGuest) {
_saveProgress(name ?? "Unknown");
}
}
2025-11-14 18:03:50 +09:00
2025-12-15 18:18:17 +09:00
Future<void> _saveProgress(String playerName) async {
if (_isSaving) return;
setState(() => _isSaving = true);
2025-11-14 18:03:50 +09:00
try {
2025-12-15 18:18:17 +09:00
await _identityService.saveUserName(playerName);
2025-11-14 18:03:50 +09:00
2025-12-15 18:18:17 +09:00
// [Fix] 타입 호환성 수정: String 인자 전달
if (widget.args.onProgressSave != null) {
await widget.args.onProgressSave!(playerName);
}
if (widget.args.stars != null && widget.args.levelIndex != null) {
await _identityService.submitGameResult(
gameType: widget.args.gameType,
level: widget.args.levelIndex!,
stars: widget.args.stars!
);
2025-11-14 18:03:50 +09:00
}
} catch (e) {
2025-12-15 18:18:17 +09:00
debugPrint("Error saving progress: $e");
} finally {
if (mounted) setState(() => _isSaving = false);
2025-11-14 18:03:50 +09:00
}
}
2025-11-19 17:00:33 +09:00
@override
Widget build(BuildContext context) {
2025-11-14 18:03:50 +09:00
return Scaffold(
2025-12-15 18:18:17 +09:00
appBar: AppBar(title: const Text('결과')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
2025-11-19 17:00:33 +09:00
child: Column(
children: [
2025-12-15 18:18:17 +09:00
const Icon(Icons.emoji_events, size: 80, color: Colors.orange),
const SizedBox(height: 24),
Text(
"점수: ${widget.args.primaryScore}",
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 32),
TextField(
controller: _nameController,
decoration: const InputDecoration(
labelText: '이름을 입력하세요',
border: OutlineInputBorder(),
2025-11-19 17:00:33 +09:00
),
2025-12-15 18:18:17 +09:00
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () async {
await _saveProgress(_nameController.text);
if (!mounted) return;
if (widget.args.onNextGame != null) {
widget.args.onNextGame!();
} else {
Navigator.pop(context);
}
},
child: const Text('확인'),
)
2025-11-19 17:00:33 +09:00
],
2025-11-14 18:03:50 +09:00
),
),
);
}
}