설계서대로 구현. flutter_gemma 실제 통합은 OQ-1 (모델 URL+SHA) 확정 후.
v1은 LlmService 추상 + ModelLifecycle (다운로드/SHA/purge) + Riverpod
providers + 다이얼로그 + Settings 화면까지. main.dart 가 MockLlmService 를
override 해 모든 경로가 graceful (suggest 결과는 빈 리스트).
추가:
- lib/data/ai/{llm_service,gemma_llm_service,model_lifecycle}.dart
- lib/domain/ai/{frame_candidate,few_shot_builder,parse_response,suggest_frame}.dart
- lib/state/ai_providers.dart (aiSettings + modelAvailability + frameSuggestions)
- lib/ui/screens/settings_screen.dart (opt-in 토글 + 모델 상태 표시)
- lib/ui/widgets/frame_suggestion_dialog.dart (후보 3개 카드 + 다시 시도)
- HabitCreateScreen: "AI 제안" 버튼 (opt-in + ready 일 때만 노출)
- MetaDao.remove(key) 추가 (purge 용)
테스트 31개 신규 추가 (총 62개 통과):
- test/domain/ai/{suggest_frame, few_shot_builder, parse_response}_test.dart
- test/data/ai/model_lifecycle_test.dart (download/SHA/purge/availability)
flutter analyze 0 issue, flutter build apk --debug 통과.
Refs #215
209 lines
7.1 KiB
Dart
209 lines
7.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../core/constants.dart';
|
|
import '../../core/time.dart';
|
|
import '../../data/ai/model_lifecycle.dart';
|
|
import '../../data/db/daos/habit_dao.dart';
|
|
import '../../domain/ai/frame_candidate.dart';
|
|
import '../../domain/models/habit.dart';
|
|
import '../../domain/rules/active_habit_quota.dart';
|
|
import '../../state/ai_providers.dart';
|
|
import '../../state/providers.dart';
|
|
import '../widgets/frame_suggestion_dialog.dart';
|
|
|
|
class HabitCreateScreen extends ConsumerStatefulWidget {
|
|
const HabitCreateScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<HabitCreateScreen> createState() => _HabitCreateScreenState();
|
|
}
|
|
|
|
class _HabitCreateScreenState extends ConsumerState<HabitCreateScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _titleCtrl = TextEditingController();
|
|
final _framedCtrl = TextEditingController();
|
|
HabitType _type = HabitType.build;
|
|
FrameLevel _level = FrameLevel.l2;
|
|
bool _saving = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_titleCtrl.dispose();
|
|
_framedCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
setState(() => _saving = true);
|
|
try {
|
|
final dao = ref.read(habitDaoProvider);
|
|
final count = await dao.countActive(
|
|
userId: kLocalDefaultUserId,
|
|
type: _type,
|
|
);
|
|
final quota = judgeActiveHabitQuota(type: _type, currentActiveCount: count);
|
|
if (!quota.allowed) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(quota.reason)),
|
|
);
|
|
return;
|
|
}
|
|
await dao.insertWithVariants(HabitDraft(
|
|
userId: kLocalDefaultUserId,
|
|
type: _type,
|
|
title: _titleCtrl.text.trim(),
|
|
// Placeholder: vertical-slice uses the first seeded protocol.
|
|
protocolId: _type == HabitType.build ? 'morning_sunlight' : null,
|
|
breakProtocolId: _type == HabitType.breakHabit ? 'alcohol' : null,
|
|
frameLevel: _level,
|
|
frameFramedText: _framedCtrl.text.trim(),
|
|
startedAt: _ymd(nowKst()),
|
|
));
|
|
if (!mounted) return;
|
|
Navigator.of(context).pop();
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('저장 실패: $e')),
|
|
);
|
|
} finally {
|
|
if (mounted) setState(() => _saving = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('새 습관')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: ListView(
|
|
children: [
|
|
TextFormField(
|
|
controller: _titleCtrl,
|
|
decoration: const InputDecoration(labelText: '제목'),
|
|
validator: (v) =>
|
|
(v == null || v.trim().isEmpty) ? '제목을 입력하세요' : null,
|
|
),
|
|
const SizedBox(height: 16),
|
|
DropdownButtonFormField<HabitType>(
|
|
initialValue: _type,
|
|
items: const [
|
|
DropdownMenuItem(value: HabitType.build, child: Text('만들기 (build)')),
|
|
DropdownMenuItem(
|
|
value: HabitType.breakHabit, child: Text('없애기 (break)')),
|
|
],
|
|
onChanged: (v) => setState(() => _type = v ?? HabitType.build),
|
|
decoration: const InputDecoration(labelText: '타입'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
DropdownButtonFormField<FrameLevel>(
|
|
initialValue: _level,
|
|
items: const [
|
|
DropdownMenuItem(value: FrameLevel.l2, child: Text('L2 · 조건부 긍정')),
|
|
DropdownMenuItem(value: FrameLevel.l3, child: Text('L3 · 정체성')),
|
|
],
|
|
onChanged: (v) => setState(() => _level = v ?? FrameLevel.l2),
|
|
decoration: const InputDecoration(labelText: '프레임 레벨'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _framedCtrl,
|
|
decoration: const InputDecoration(
|
|
labelText: '프레임 문구',
|
|
hintText: '예: 아침 햇빛을 10분 받는다',
|
|
),
|
|
maxLines: 2,
|
|
validator: (v) =>
|
|
(v == null || v.trim().isEmpty) ? '프레임 문구를 입력하세요' : null,
|
|
),
|
|
const SizedBox(height: 8),
|
|
_AiSuggestButton(
|
|
titleCtrl: _titleCtrl,
|
|
framedCtrl: _framedCtrl,
|
|
habitType: _type,
|
|
onSelectLevel: (level) => setState(() => _level = level),
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(
|
|
onPressed: _saving ? null : _save,
|
|
child: Text(_saving ? '저장 중...' : '저장'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
String _ymd(DateTime d) =>
|
|
'${d.year.toString().padLeft(4, '0')}-'
|
|
'${d.month.toString().padLeft(2, '0')}-'
|
|
'${d.day.toString().padLeft(2, '0')}';
|
|
|
|
/// "AI 제안" button — only visible when opt-in is ON and model is ready.
|
|
/// Graceful: hidden otherwise (AC-5, AC-9).
|
|
class _AiSuggestButton extends ConsumerWidget {
|
|
final TextEditingController titleCtrl;
|
|
final TextEditingController framedCtrl;
|
|
final HabitType habitType;
|
|
final ValueChanged<FrameLevel> onSelectLevel;
|
|
const _AiSuggestButton({
|
|
required this.titleCtrl,
|
|
required this.framedCtrl,
|
|
required this.habitType,
|
|
required this.onSelectLevel,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final settings = ref.watch(aiSettingsProvider);
|
|
final availability = ref.watch(modelAvailabilityProvider);
|
|
final visible = settings.maybeWhen(
|
|
data: (v) => v,
|
|
orElse: () => false,
|
|
) &&
|
|
availability.maybeWhen(
|
|
data: (a) => a == ModelAvailability.ready,
|
|
orElse: () => false,
|
|
);
|
|
if (!visible) return const SizedBox.shrink();
|
|
return Align(
|
|
alignment: Alignment.centerRight,
|
|
child: TextButton.icon(
|
|
icon: const Icon(Icons.auto_awesome),
|
|
label: const Text('AI 제안'),
|
|
onPressed: () async {
|
|
final raw = titleCtrl.text.trim();
|
|
if (raw.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('먼저 제목을 입력해주세요')),
|
|
);
|
|
return;
|
|
}
|
|
final picked = await FrameSuggestionDialog.show(
|
|
context,
|
|
input: SuggestFrameInput(
|
|
rawText: raw,
|
|
habitType: habitType,
|
|
),
|
|
);
|
|
if (picked != null) {
|
|
framedCtrl.text = picked;
|
|
// Heuristic: identity-style ("나는") → L3, else L2.
|
|
onSelectLevel(
|
|
picked.startsWith('나는') ? FrameLevel.l3 : FrameLevel.l2,
|
|
);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|