import 'package:flutter/material.dart'; import '../../data/local/local_storage_service.dart'; import '../../data/local/models/item.dart'; /// Home screen showing local storage and cached content. class HomeScreen extends StatefulWidget { final LocalStorageService? localStorageService; const HomeScreen({ super.key, this.localStorageService, }); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { List _items = []; bool _isLoading = true; @override void initState() { super.initState(); _loadItems(); } Future _loadItems() async { if (widget.localStorageService == null) { setState(() { _isLoading = false; }); return; } try { final items = await widget.localStorageService!.getAllItems(); setState(() { _items = items; _isLoading = false; }); } catch (e) { setState(() { _isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Home'), ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : RefreshIndicator( onRefresh: _loadItems, child: _items.isEmpty ? const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.storage_outlined, size: 64, color: Colors.grey, ), SizedBox(height: 16), Text( 'No items in local storage', style: TextStyle( fontSize: 16, color: Colors.grey, ), ), ], ), ) : ListView.builder( itemCount: _items.length, itemBuilder: (context, index) { final item = _items[index]; return ListTile( leading: const Icon(Icons.data_object), title: Text(item.id), subtitle: Text( 'Created: ${DateTime.fromMillisecondsSinceEpoch(item.createdAt).toString().split('.')[0]}', ), trailing: IconButton( icon: const Icon(Icons.delete_outline), onPressed: () async { await widget.localStorageService?.deleteItem(item.id); _loadItems(); }, ), ); }, ), ), floatingActionButton: widget.localStorageService != null ? FloatingActionButton( onPressed: () async { final item = Item( id: 'item-${DateTime.now().millisecondsSinceEpoch}', data: { 'name': 'New Item', 'timestamp': DateTime.now().toIso8601String(), }, ); await widget.localStorageService!.insertItem(item); _loadItems(); }, child: const Icon(Icons.add), ) : null, ); } }