[03-Developer] #226 Catalog Gallery 구현
- Drift schema v2: Protocols.category CHECK 6→7 (light_circadian/sleep/movement/
nutrition/focus_cognition/recovery_stress/emotion_relationship). schemaVersion
1→2 + onUpgrade migrateV1ToV2 (DROP+CREATE+reseed flag 클리어).
- protocols.json 34 항목 v2 재분류 (1차 효과 기준). emotion_relationship 0 매핑.
- 도메인: DisplayCategory enum (8) + CatalogItem sealed (Protocol/Break/Diet).
- 데이터: CatalogRepository.all/byId/referencesByIds (3 source 통합).
- 상태: catalog_providers.dart (catalogItems / groupedByCategory / refsByIds).
- UI: ProtocolGalleryScreen (카테고리 칩 + 카드 그리드) + ProtocolPreviewScreen
(모든 필드 + reference 펼치기 + "내 습관으로" disabled placeholder) +
CatalogCard / CategoryChipRow / ReferenceExpandCard. HabitListScreen 빈
상태 CTA + AppBar 액션.
- 테스트: migration_v1_to_v2 3건 + display_category 5건 + catalog_repository
9건 + gallery widget 3건 + preview widget 3건 = 23 신규. 기존 88 회귀 0,
flutter analyze 0 issues. 110 passed / 1 skipped.
설계서: docs/design/226-catalog-gallery/{README, fn-catalog_repository,
fn-migration_v1_to_v2}.md + ADR-0004.
Refs #226
This commit is contained in:
133
app/lib/data/catalog/catalog_repository.dart
Normal file
133
app/lib/data/catalog/catalog_repository.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../domain/catalog/catalog_item.dart';
|
||||
import '../../domain/catalog/display_category.dart';
|
||||
import '../db/app_database.dart';
|
||||
|
||||
/// 3 source (Protocols / BreakProtocols / DietPatterns) → 단일 `List<CatalogItem>`.
|
||||
///
|
||||
/// 본 이슈 (#226) 의 핵심 변환 한 점. 본 함수는 fn-catalog_repository.md 의 알고리즘대로.
|
||||
class CatalogRepository {
|
||||
CatalogRepository(this._db);
|
||||
|
||||
final AppDatabase _db;
|
||||
|
||||
/// 47 항목 (protocols 34 + break 8 + diet 5) 을 displayCategory 기준 정렬해 반환.
|
||||
Future<List<CatalogItem>> all() async {
|
||||
final protocolRows = await _db.select(_db.protocols).get();
|
||||
final breakRows = await _db.select(_db.breakProtocols).get();
|
||||
final dietRows = await _db.select(_db.dietPatterns).get();
|
||||
|
||||
final items = <CatalogItem>[];
|
||||
|
||||
for (final p in protocolRows) {
|
||||
final dc = DisplayCategory.fromProtocolCategory(p.category);
|
||||
if (dc == null) {
|
||||
throw StateError(
|
||||
'unknown protocol category "${p.category}" for id=${p.id}');
|
||||
}
|
||||
items.add(ProtocolCatalogItem(
|
||||
id: p.id,
|
||||
title: p.title,
|
||||
titleEn: p.titleEn,
|
||||
summary: _summary(p.what, fallback: p.title),
|
||||
displayCategory: dc,
|
||||
evidenceStrength: p.evidenceStrength,
|
||||
referenceIds: _decodeIds(p.referenceIdsJson),
|
||||
what: p.what,
|
||||
whenText: p.whenText,
|
||||
dose: p.dose,
|
||||
why: p.why,
|
||||
how: _decodeList(p.howJson),
|
||||
checkText: p.checkText,
|
||||
caution: p.caution,
|
||||
defaultAnchor: _decodeAnchor(p.defaultAnchorJson),
|
||||
minDoseForStart: p.minDoseForStart,
|
||||
sourceDoc: p.sourceDoc,
|
||||
));
|
||||
}
|
||||
|
||||
for (final b in breakRows) {
|
||||
items.add(BreakCatalogItem(
|
||||
id: b.id,
|
||||
title: b.title,
|
||||
titleEn: null,
|
||||
summary: _summary(b.hubermanSummary, fallback: b.title),
|
||||
evidenceStrength: null,
|
||||
referenceIds: _decodeIds(b.referenceIdsJson),
|
||||
breakCategory: b.category,
|
||||
hubermanSummary: b.hubermanSummary,
|
||||
phases: _decodeList(b.phasesJson),
|
||||
defaultCommonFrames: _decodeList(b.defaultCommonFramesJson),
|
||||
tools: _decodeList(b.toolsJson),
|
||||
medicalWarning: b.medicalWarning,
|
||||
));
|
||||
}
|
||||
|
||||
for (final d in dietRows) {
|
||||
items.add(DietCatalogItem(
|
||||
id: d.id,
|
||||
title: d.name,
|
||||
titleEn: null,
|
||||
summary: _summary(d.core, fallback: d.name),
|
||||
evidenceStrength: d.evidenceStrength,
|
||||
referenceIds: _decodeIds(d.referenceIdsJson),
|
||||
name: d.name,
|
||||
core: d.core,
|
||||
strengths: _decodeList(d.strengthsJson),
|
||||
weaknesses: _decodeList(d.weaknessesJson),
|
||||
koreanContextFit: d.koreanContextFit,
|
||||
starterLevers: _decodeList(d.starterLeversJson),
|
||||
medicalWarning: d.medicalWarning,
|
||||
linkedProtocolIds: _decodeIds(d.linkedProtocolIdsJson),
|
||||
));
|
||||
}
|
||||
|
||||
items.sort((a, b) {
|
||||
final c = a.displayCategory.index - b.displayCategory.index;
|
||||
return c != 0 ? c : a.id.compareTo(b.id);
|
||||
});
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/// 단건 조회. Preview 화면 진입 시.
|
||||
Future<CatalogItem?> byId(String id) async {
|
||||
final all_ = await all();
|
||||
for (final item in all_) {
|
||||
if (item.id == id) return item;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// reference id 리스트 → References 테이블 매칭. 미매칭 항목은 결과에서 누락.
|
||||
Future<List<ReferenceRow>> referencesByIds(List<String> ids) async {
|
||||
if (ids.isEmpty) return const [];
|
||||
return (_db.select(_db.references)..where((t) => t.id.isIn(ids))).get();
|
||||
}
|
||||
}
|
||||
|
||||
/// `what` 의 첫 문장을 추출. 비어있으면 `fallback` 사용. 60자 초과 시 절단.
|
||||
String _summary(String what, {required String fallback, int max = 60}) {
|
||||
final firstSentence = what.split(RegExp(r'[.!?。!?]')).first.trim();
|
||||
final s = firstSentence.isEmpty ? fallback : firstSentence;
|
||||
return s.length <= max ? s : '${s.substring(0, max - 1)}…';
|
||||
}
|
||||
|
||||
List<String> _decodeIds(String? jsonStr) {
|
||||
if (jsonStr == null) return const [];
|
||||
final decoded = jsonDecode(jsonStr);
|
||||
return decoded is List ? decoded.cast<String>() : const [];
|
||||
}
|
||||
|
||||
List<String> _decodeList(String? jsonStr) {
|
||||
if (jsonStr == null) return const [];
|
||||
final decoded = jsonDecode(jsonStr);
|
||||
return decoded is List ? decoded.map((e) => e.toString()).toList() : const [];
|
||||
}
|
||||
|
||||
Map<String, dynamic>? _decodeAnchor(String? jsonStr) {
|
||||
if (jsonStr == null) return null;
|
||||
final decoded = jsonDecode(jsonStr);
|
||||
return decoded is Map<String, dynamic> ? decoded : null;
|
||||
}
|
||||
Reference in New Issue
Block a user