flutter_sudoku/lib/services/identity_service.dart
2025-11-11 14:38:15 +09:00

47 lines
1.7 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
// 앱-고유 ID와 사용자 이름, 레벨 진행 상황을 관리하는 서비스
class IdentityService {
static const String _userIdKey = 'app_user_id';
static const String _userNameKey = 'app_user_name';
static const String _maxLevelKey = 'max_unlocked_level'; // 👈 [추가]
// 1. 앱-고유 ID 가져오기 (없으면 생성)
Future<String> getOrCreateUserId() async {
final prefs = await SharedPreferences.getInstance();
String? userId = prefs.getString(_userIdKey);
if (userId == null) {
// ID가 없으면 V4 UUID 생성
userId = const Uuid().v4();
await prefs.setString(_userIdKey, userId);
}
return userId;
}
// 2. 랭킹에 등록한 사용자 이름 가져오기
Future<String?> getSavedUserName() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_userNameKey);
}
// 3. 랭킹 등록 성공 시, 사용자 이름 저장하기
Future<void> saveUserName(String name) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_userNameKey, name);
}
// 4. 🔽 [추가] 현재 잠금 해제된 최고 레벨 가져오기
Future<int> getMaxUnlockedLevel() async {
final prefs = await SharedPreferences.getInstance();
// 최초 실행 시 1 (L1) 반환, 9레벨 클리어 시 99 반환
return prefs.getInt(_maxLevelKey) ?? 1;
}
// 5. 🔽 [추가] 새 레벨 잠금 해제
Future<void> saveMaxUnlockedLevel(int level) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_maxLevelKey, level);
}
}