You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.8 KiB
118 lines
3.8 KiB
import 'package:flutter/material.dart';
|
|
import '../../core/service_locator.dart';
|
|
import '../../data/local/models/item.dart';
|
|
import '../shared/primary_app_bar.dart';
|
|
|
|
/// Home screen showing local storage and cached content.
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
List<Item> _items = [];
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadItems();
|
|
}
|
|
|
|
Future<void> _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: PrimaryAppBar(title: '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,
|
|
);
|
|
}
|
|
}
|
|
|