Files
Libretranslate-app/lib/screens/history_screen.dart
T
2026-07-14 17:37:17 +03:00

118 lines
3.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import '../models/history_item.dart';
import '../services/history_service.dart';
class HistoryScreen extends StatefulWidget {
const HistoryScreen({super.key});
@override
State<HistoryScreen> createState() => _HistoryScreenState();
}
class _HistoryScreenState extends State<HistoryScreen> {
final _historyService = HistoryService();
List<HistoryItem> _items = [];
bool _loading = true;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final items = await _historyService.getAll();
if (!mounted) return;
setState(() {
_items = items;
_loading = false;
});
}
Future<void> _clear() async {
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Geçmişi Sil'),
content: const Text('Tüm çeviri geçmişi silinsin mi? Bu işlem geri alınamaz.'),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Vazgeç')),
FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Sil')),
],
),
);
if (confirmed == true) {
await _historyService.clear();
_load();
}
}
String _formatTime(DateTime dt) {
final now = DateTime.now();
final sameDay = dt.year == now.year && dt.month == now.month && dt.day == now.day;
final time = '${dt.hour.toString().padLeft(2, '0')}:${dt.minute.toString().padLeft(2, '0')}';
if (sameDay) return time;
return '${dt.day}.${dt.month}.${dt.year} $time';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Geçmiş'),
actions: [
if (_items.isNotEmpty)
IconButton(
icon: const Icon(Icons.delete_outline),
tooltip: 'Geçmişi temizle',
onPressed: _clear,
),
],
),
body: _loading
? const Center(child: CircularProgressIndicator())
: _items.isEmpty
? const Center(child: Text('Henüz çeviri geçmişi yok'))
: ListView.separated(
padding: const EdgeInsets.all(12),
itemCount: _items.length,
separatorBuilder: (_, __) => const SizedBox(height: 8),
itemBuilder: (ctx, i) {
final item = _items[i];
return Card(
margin: EdgeInsets.zero,
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
'${item.sourceLang.toUpperCase()}${item.targetLang.toUpperCase()}',
style: Theme.of(context).textTheme.labelMedium,
),
const Spacer(),
Text(
_formatTime(item.timestamp),
style: Theme.of(context).textTheme.labelSmall,
),
],
),
const SizedBox(height: 6),
Text(item.sourceText),
const Divider(height: 16),
Text(
item.translatedText,
style: TextStyle(color: Theme.of(context).colorScheme.primary),
),
],
),
),
);
},
),
);
}
}