Files

526 lines
17 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:social_app/app/di/injection.dart';
import 'package:social_app/core/theme/design_tokens.dart';
import 'package:social_app/core/l10n/l10n.dart';
import 'package:social_app/app/router/app_routes.dart';
import 'package:social_app/shared/widgets/app_loading_indicator.dart';
import 'package:social_app/shared/widgets/app_pressable.dart';
import '../widgets/settings_page_scaffold.dart';
import '../../data/models/memory_models.dart';
import '../../data/services/memory_service.dart';
class MemoryScreen extends StatefulWidget {
const MemoryScreen({super.key});
@override
State<MemoryScreen> createState() => _MemoryScreenState();
}
class _MemoryScreenState extends State<MemoryScreen> {
final MemoryService _memoryService = sl<MemoryService>();
MemoryListResponse? _memoryData;
bool _isLoading = true;
String? _error;
ColorScheme get _colorScheme => Theme.of(context).colorScheme;
@override
void initState() {
super.initState();
_loadMemories();
}
Future<void> _loadMemories() async {
if (!mounted) return;
setState(() {
_isLoading = true;
_error = null;
});
try {
final data = await _memoryService.getAllMemories();
if (!mounted) return;
setState(() {
_memoryData = data;
_isLoading = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_error = L10n.current.memoryLoadFailedRetry;
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return SettingsPageScaffold(
title: context.l10n.memoryTitle,
onBack: () => context.pop(),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildToggleCard(),
const SizedBox(height: AppSpacing.lg),
if (_isLoading) ...[
const SizedBox(height: AppSpacing.xxl),
_buildLoadingState(),
] else if (_error != null) ...[
const SizedBox(height: AppSpacing.xxl),
_buildErrorState(),
] else ...[
const SizedBox(height: AppSpacing.sm),
_buildMemoryCards(),
],
],
),
);
}
Widget _buildToggleCard() {
return Container(
padding: const EdgeInsets.all(AppSpacing.lg),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [_colorScheme.surface, _colorScheme.surfaceContainerLow],
),
borderRadius: BorderRadius.circular(AppRadius.xl),
border: Border.all(color: _colorScheme.outlineVariant),
boxShadow: [
BoxShadow(
color: _colorScheme.shadow.withValues(alpha: 0.2),
blurRadius: 14,
offset: const Offset(0, 4),
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
_colorScheme.primaryContainer,
_colorScheme.surfaceContainerLow,
],
),
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: _colorScheme.primary.withValues(alpha: 0.2),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Icon(
Icons.auto_awesome,
size: 22,
color: _colorScheme.primary,
),
),
const SizedBox(width: AppSpacing.md),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.memorySmartTitle,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurface,
),
),
const SizedBox(height: 2),
Text(
context.l10n.memorySmartDesc,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: _colorScheme.onSurfaceVariant,
),
),
],
),
),
],
),
);
}
Widget _buildLoadingState() {
return Center(
child: Container(
padding: const EdgeInsets.all(AppSpacing.xxl),
child: const AppLoadingIndicator(size: 32),
),
);
}
Widget _buildErrorState() {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.error_outline,
size: 48,
color: _colorScheme.onSurfaceVariant,
),
const SizedBox(height: AppSpacing.md),
Text(
_error ?? context.l10n.memoryLoadFailedRetry,
style: TextStyle(
fontSize: 14,
color: _colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: AppSpacing.lg),
AppPressable(
onTap: _loadMemories,
borderRadius: BorderRadius.circular(AppRadius.md),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.lg,
vertical: AppSpacing.sm,
),
decoration: BoxDecoration(
color: _colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(AppRadius.md),
border: Border.all(color: _colorScheme.outlineVariant),
),
child: Text(
context.l10n.memoryReload,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: _colorScheme.primary,
),
),
),
),
],
),
);
}
Widget _buildMemoryCards() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildSectionLabel(context.l10n.memorySectionUser),
const SizedBox(height: AppSpacing.sm),
_buildUserMemoryCard(),
const SizedBox(height: AppSpacing.md),
_buildSectionLabel(context.l10n.memorySectionWork),
const SizedBox(height: AppSpacing.sm),
_buildWorkMemoryCard(),
],
);
}
Widget _buildSectionLabel(String label) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.xs),
child: Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurfaceVariant,
),
),
);
}
Widget _buildUserMemoryCard() {
final userMemory = _memoryData?.userMemory;
final hasData = userMemory != null;
return AppPressable(
onTap: () => context.push(AppRoutes.settingsMemoryUser),
borderRadius: BorderRadius.circular(AppRadius.xl),
child: Container(
padding: const EdgeInsets.all(AppSpacing.lg),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [_colorScheme.surface, _colorScheme.surfaceContainerLow],
),
borderRadius: BorderRadius.circular(AppRadius.xl),
border: Border.all(color: _colorScheme.outlineVariant),
boxShadow: [
BoxShadow(
color: _colorScheme.shadow.withValues(alpha: 0.15),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
_colorScheme.primaryContainer.withValues(alpha: 0.8),
_colorScheme.surfaceContainerLow.withValues(alpha: 0.8),
],
),
borderRadius: BorderRadius.circular(10),
),
child: Icon(
Icons.person,
size: 20,
color: _colorScheme.primary,
),
),
const SizedBox(width: AppSpacing.md),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.memoryUserProfile,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurface,
),
),
const SizedBox(height: 2),
Text(
hasData
? userMemory.summary
: context.l10n.memoryNoInfo,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: _colorScheme.onSurfaceVariant,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
Icon(
Icons.chevron_right,
size: 20,
color: _colorScheme.onSurfaceVariant,
),
],
),
if (hasData) ...[
const SizedBox(height: AppSpacing.md),
_buildMemoryStatsRow(
icons: [
Icons.people_outline,
Icons.place_outlined,
Icons.interests_outlined,
Icons.schedule_outlined,
],
values: [
'${userMemory.people.length}',
'${userMemory.places.length}',
'${userMemory.interests.length}',
'${userMemory.recurringRoutines.length}',
],
labels: [
context.l10n.memoryStatContacts,
context.l10n.memoryStatPlaces,
context.l10n.memoryStatInterests,
context.l10n.memoryStatSchedule,
],
),
],
],
),
),
);
}
Widget _buildWorkMemoryCard() {
final workMemory = _memoryData?.workMemory;
final hasData = workMemory != null;
return AppPressable(
onTap: () => context.push(AppRoutes.settingsMemoryWork),
borderRadius: BorderRadius.circular(AppRadius.xl),
child: Container(
padding: const EdgeInsets.all(AppSpacing.lg),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [_colorScheme.surface, _colorScheme.surfaceContainerLow],
),
borderRadius: BorderRadius.circular(AppRadius.xl),
border: Border.all(color: _colorScheme.outlineVariant),
boxShadow: [
BoxShadow(
color: _colorScheme.shadow.withValues(alpha: 0.15),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
_colorScheme.tertiary.withValues(alpha: 0.15),
_colorScheme.tertiary.withValues(alpha: 0.05),
],
),
borderRadius: BorderRadius.circular(10),
),
child: Icon(
Icons.work_outline,
size: 20,
color: _colorScheme.tertiary,
),
),
const SizedBox(width: AppSpacing.md),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.memoryWorkProfile,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurface,
),
),
const SizedBox(height: 2),
Text(
hasData
? workMemory.summary
: context.l10n.memoryNoInfo,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: _colorScheme.onSurfaceVariant,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
Icon(
Icons.chevron_right,
size: 20,
color: _colorScheme.onSurfaceVariant,
),
],
),
if (hasData) ...[
const SizedBox(height: AppSpacing.md),
_buildMemoryStatsRow(
icons: [
Icons.psychology_outlined,
Icons.build_outlined,
Icons.folder_outlined,
Icons.groups_outlined,
],
values: [
'${workMemory.expertise.length}',
'${workMemory.preferredTools.length}',
'${workMemory.currentProjects.length}',
'${workMemory.teamMembers.length}',
],
labels: [
context.l10n.memoryStatExpertise,
context.l10n.memoryStatTools,
context.l10n.memoryStatProjects,
context.l10n.memoryStatTeam,
],
),
],
],
),
),
);
}
Widget _buildMemoryStatsRow({
required List<IconData> icons,
required List<String> values,
required List<String> labels,
}) {
return Row(
children: List.generate(icons.length, (index) {
return Expanded(
child: Container(
padding: const EdgeInsets.symmetric(vertical: AppSpacing.sm),
margin: EdgeInsets.only(
right: index < icons.length - 1 ? AppSpacing.sm : 0,
),
decoration: BoxDecoration(
color: _colorScheme.surfaceContainerLow,
borderRadius: BorderRadius.circular(AppRadius.md),
),
child: Column(
children: [
Icon(
icons[index],
size: 16,
color: _colorScheme.onSurfaceVariant,
),
const SizedBox(height: 4),
Text(
values[index],
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurface,
),
),
const SizedBox(height: 2),
Text(
labels[index],
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w500,
color: _colorScheme.onSurfaceVariant,
),
),
],
),
),
);
}),
);
}
}