import 'package:flutter/material.dart'; import '../../core/service_locator.dart'; import '../../data/local/models/item.dart'; /// Home screen showing local storage and cached content. class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { List _items = []; bool _isLoading = true; @override void initState() { super.initState(); _loadItems(); } Future _loadItems() async { try { final localStorageService = ServiceLocator.instance.localStorageService; if (localStorageService == null) { setState(() { _isLoading = false; }); return; } final items = await 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 { final localStorageService = ServiceLocator.instance.localStorageService; await localStorageService?.deleteItem(item.id); _loadItems(); }, ), ); }, ), ), floatingActionButton: ServiceLocator.instance.localStorageService != null ? FloatingActionButton( onPressed: () async { final localStorageService = ServiceLocator.instance.localStorageService; final item = Item( id: 'item-${DateTime.now().millisecondsSinceEpoch}', data: { 'name': 'New Item', 'timestamp': DateTime.now().toIso8601String(), }, ); await localStorageService!.insertItem(item); _loadItems(); }, child: const Icon(Icons.add), ) : null, ); } }