mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat: adds auto sync feature and fixes UI a bit up
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../services/database_sync_service.dart';
|
||||
import '../providers/settings_provider.dart';
|
||||
import '../providers/simple_sync_provider.dart';
|
||||
import '../services/database_sync_service.dart';
|
||||
|
||||
class SimpleSyncSettingsScreen extends StatefulWidget {
|
||||
const SimpleSyncSettingsScreen({super.key});
|
||||
@@ -17,7 +18,7 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
final _remotePathController = TextEditingController();
|
||||
|
||||
|
||||
String _previewUrl = '';
|
||||
|
||||
@override
|
||||
@@ -27,11 +28,11 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
_usernameController.addListener(_updatePreviewUrl);
|
||||
_loadSavedConfiguration();
|
||||
}
|
||||
|
||||
|
||||
void _loadSavedConfiguration() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
|
||||
if (syncProvider.serverUrl != null) {
|
||||
_serverUrlController.text = _extractHostnameFromUrl(syncProvider.serverUrl!);
|
||||
}
|
||||
@@ -44,11 +45,11 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
if (syncProvider.remotePath != null) {
|
||||
_remotePathController.text = syncProvider.remotePath!;
|
||||
}
|
||||
|
||||
|
||||
_updatePreviewUrl();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
String _extractHostnameFromUrl(String fullUrl) {
|
||||
try {
|
||||
final uri = Uri.parse(fullUrl);
|
||||
@@ -81,30 +82,35 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final syncProvider = context.watch<SimpleSyncProvider>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Database Sync Settings'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Save Configuration',
|
||||
onPressed: syncProvider.isSyncing ? null : _configureSync,
|
||||
icon: const Icon(Icons.save),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Consumer<SimpleSyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_buildStatusCard(syncProvider),
|
||||
const SizedBox(height: 20),
|
||||
_buildConfigurationSection(),
|
||||
const SizedBox(height: 20),
|
||||
_buildActionButtons(syncProvider),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_buildStatusCard(syncProvider),
|
||||
const SizedBox(height: 20),
|
||||
_buildConfigurationSection(syncProvider),
|
||||
const SizedBox(height: 20),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -116,17 +122,17 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
|
||||
switch (syncProvider.status) {
|
||||
case SyncStatus.idle:
|
||||
icon = Icons.sync;
|
||||
icon = syncProvider.isAutoSync ? Icons.sync_alt : Icons.sync;
|
||||
color = Colors.blue;
|
||||
break;
|
||||
case SyncStatus.downloading:
|
||||
case SyncStatus.merging:
|
||||
case SyncStatus.uploading:
|
||||
icon = Icons.sync;
|
||||
color = Colors.orange;
|
||||
icon = syncProvider.isAutoSync ? Icons.sync_alt : Icons.sync;
|
||||
color = syncProvider.isAutoSync ? Colors.deepOrange : Colors.orange;
|
||||
break;
|
||||
case SyncStatus.completed:
|
||||
icon = Icons.check_circle;
|
||||
icon = syncProvider.isAutoSync ? Icons.check_circle_outline : Icons.check_circle;
|
||||
color = Colors.green;
|
||||
break;
|
||||
case SyncStatus.error:
|
||||
@@ -152,8 +158,28 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
),
|
||||
),
|
||||
),
|
||||
// Sync action inside the status card
|
||||
if (syncProvider.isSyncing) ...[
|
||||
const SizedBox(width: 12),
|
||||
SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2.2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(color),
|
||||
),
|
||||
),
|
||||
] else ...[
|
||||
IconButton(
|
||||
tooltip: 'Sync Database',
|
||||
onPressed: (!syncProvider.isConfigured || syncProvider.isSyncing) ? null : _syncDatabase,
|
||||
icon: const Icon(Icons.sync),
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
_buildAutoSyncStatusIndicator(syncProvider),
|
||||
if (syncProvider.lastSyncTime != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
@@ -161,19 +187,73 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
if (syncProvider.lastError != null) ...[
|
||||
// Show auto-sync specific errors
|
||||
if (syncProvider.isAutoSyncDisabledDueToErrors) ...[
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.red.withValues(alpha: 0.3)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.warning, color: Colors.red, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Auto-sync disabled due to repeated failures',
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
color: Colors.red,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (syncProvider.autoSyncLastError != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
syncProvider.autoSyncLastError!,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Colors.red[700],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
onPressed: () => syncProvider.resetAutoSyncErrors(),
|
||||
icon: const Icon(Icons.refresh, size: 16),
|
||||
label: const Text('Reset & Re-enable'),
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
] else if (syncProvider.lastError != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.withOpacity(0.1),
|
||||
color: Colors.red.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
syncProvider.lastError!,
|
||||
_getErrorMessage(syncProvider),
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
@@ -191,7 +271,7 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildConfigurationSection() {
|
||||
Widget _buildConfigurationSection(SimpleSyncProvider syncProvider) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
@@ -199,10 +279,17 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'WebDAV Configuration',
|
||||
'Sync Configuration',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildAutoSyncSection(),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'WebDAV Settings',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _serverUrlController,
|
||||
decoration: const InputDecoration(
|
||||
@@ -237,10 +324,10 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest.withOpacity(0.3),
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.5),
|
||||
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
@@ -258,6 +345,21 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
onPressed: syncProvider.isSyncing ? null : _testConnection,
|
||||
icon: const Icon(Icons.link),
|
||||
label: const Text('Test'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
elevation: 0,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -292,40 +394,122 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionButtons(SimpleSyncProvider syncProvider) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: syncProvider.isSyncing ? null : _testConnection,
|
||||
child: const Text('Test Connection'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: syncProvider.isSyncing ? null : _configureSync,
|
||||
child: const Text('Save Configuration'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: (!syncProvider.isConfigured || syncProvider.isSyncing)
|
||||
? null
|
||||
: _syncDatabase,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
Widget _buildActionButtons() {
|
||||
// Buttons have been moved into the AppBar / cards. Keep a small spacer here for layout.
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
Widget _buildAutoSyncSection() {
|
||||
return Consumer<SettingsProvider>(
|
||||
builder: (context, settingsProvider, child) {
|
||||
return Consumer<SimpleSyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
child: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
title: Row(
|
||||
children: [
|
||||
const Text(
|
||||
'Auto-sync',
|
||||
style: TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildAutoSyncStatusBadge(settingsProvider, syncProvider),
|
||||
],
|
||||
),
|
||||
subtitle: Text(
|
||||
settingsProvider.autoSyncEnabled
|
||||
? 'Automatically sync when you make changes'
|
||||
: 'Sync manually using the sync button',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
value: settingsProvider.autoSyncEnabled,
|
||||
onChanged: (bool value) async {
|
||||
await settingsProvider.setAutoSyncEnabled(value);
|
||||
},
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
if (settingsProvider.autoSyncEnabled) ...[
|
||||
const SizedBox(height: 8),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: Text(
|
||||
'Changes are debounced for ${settingsProvider.autoSyncDebounceSeconds} seconds to prevent excessive syncing.',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
)
|
||||
: const Text('Sync Database'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Debounce timeout',
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SegmentedButton<int>(
|
||||
segments: const [
|
||||
ButtonSegment(value: 1, label: Text('1s')),
|
||||
ButtonSegment(value: 5, label: Text('5s')),
|
||||
ButtonSegment(value: 15, label: Text('15s')),
|
||||
ButtonSegment(value: 30, label: Text('30s')),
|
||||
],
|
||||
selected: {settingsProvider.autoSyncDebounceSeconds},
|
||||
onSelectionChanged: (values) {
|
||||
settingsProvider.setAutoSyncDebounceSeconds(values.first);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primaryContainer.withValues(alpha: 0.3),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.info_outline,
|
||||
size: 16,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Auto-sync triggers when you add, update, or delete supplements and intakes. Configure your WebDAV settings below to enable syncing.',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -333,31 +517,31 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
|
||||
try {
|
||||
// Construct the full WebDAV URL from the simple hostname
|
||||
final fullWebDAVUrl = _constructWebDAVUrl(
|
||||
_serverUrlController.text.trim(),
|
||||
_usernameController.text.trim(),
|
||||
);
|
||||
|
||||
|
||||
// Configure temporarily for testing
|
||||
await syncProvider.configure(
|
||||
serverUrl: fullWebDAVUrl,
|
||||
username: _usernameController.text.trim(),
|
||||
password: _passwordController.text.trim(),
|
||||
remotePath: _remotePathController.text.trim().isEmpty
|
||||
? 'Supplements'
|
||||
remotePath: _remotePathController.text.trim().isEmpty
|
||||
? 'Supplements'
|
||||
: _remotePathController.text.trim(),
|
||||
);
|
||||
|
||||
final success = await syncProvider.testConnection();
|
||||
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success
|
||||
? 'Connection successful!'
|
||||
content: Text(success
|
||||
? 'Connection successful!'
|
||||
: 'Connection failed. Check your settings.'),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
),
|
||||
@@ -379,20 +563,20 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
|
||||
try {
|
||||
// Construct the full WebDAV URL from the simple hostname
|
||||
final fullWebDAVUrl = _constructWebDAVUrl(
|
||||
_serverUrlController.text.trim(),
|
||||
_usernameController.text.trim(),
|
||||
);
|
||||
|
||||
|
||||
await syncProvider.configure(
|
||||
serverUrl: fullWebDAVUrl,
|
||||
username: _usernameController.text.trim(),
|
||||
password: _passwordController.text.trim(),
|
||||
remotePath: _remotePathController.text.trim().isEmpty
|
||||
? 'Supplements'
|
||||
remotePath: _remotePathController.text.trim().isEmpty
|
||||
? 'Supplements'
|
||||
: _remotePathController.text.trim(),
|
||||
);
|
||||
|
||||
@@ -418,14 +602,14 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
|
||||
Future<void> _syncDatabase() async {
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
|
||||
try {
|
||||
await syncProvider.syncDatabase();
|
||||
|
||||
await syncProvider.syncDatabase(isAutoSync: false);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Database sync completed!'),
|
||||
content: Text('Manual sync completed!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
@@ -434,7 +618,7 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Sync failed: $e'),
|
||||
content: Text('Manual sync failed: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
@@ -450,17 +634,200 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
} else if (cleanUrl.startsWith('https://')) {
|
||||
cleanUrl = cleanUrl.substring(8);
|
||||
}
|
||||
|
||||
|
||||
// Remove trailing slash if present
|
||||
if (cleanUrl.endsWith('/')) {
|
||||
cleanUrl = cleanUrl.substring(0, cleanUrl.length - 1);
|
||||
}
|
||||
|
||||
|
||||
// For Nextcloud instances, construct the standard WebDAV path
|
||||
// Default to HTTPS for security
|
||||
return 'https://$cleanUrl/remote.php/dav/files/$username/';
|
||||
}
|
||||
|
||||
Widget _buildAutoSyncStatusIndicator(SimpleSyncProvider syncProvider) {
|
||||
return Consumer<SettingsProvider>(
|
||||
builder: (context, settingsProvider, child) {
|
||||
// Only show auto-sync status if auto-sync is enabled
|
||||
if (!settingsProvider.autoSyncEnabled) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
// Check if auto-sync service has pending sync
|
||||
final autoSyncService = syncProvider.autoSyncService;
|
||||
if (autoSyncService == null) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
// Show pending auto-sync indicator
|
||||
if (autoSyncService.hasPendingSync && !syncProvider.isSyncing) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(top: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: Colors.blue.withValues(alpha: 0.3)),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 12,
|
||||
height: 12,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 1.5,
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Colors.blue),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Auto-sync pending...',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Colors.blue,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Show auto-sync active indicator (when sync is running and it's auto-triggered)
|
||||
if (syncProvider.isSyncing && syncProvider.isAutoSync) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(top: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.deepOrange.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: Colors.deepOrange.withValues(alpha: 0.3)),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.sync_alt,
|
||||
size: 12,
|
||||
color: Colors.deepOrange,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Auto-sync active',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Colors.deepOrange,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAutoSyncStatusBadge(SettingsProvider settingsProvider, SimpleSyncProvider syncProvider) {
|
||||
if (!settingsProvider.autoSyncEnabled) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'OFF',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Colors.grey[600],
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Check if auto-sync is disabled due to errors
|
||||
if (syncProvider.isAutoSyncDisabledDueToErrors) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'ERROR',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Colors.red[700],
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Check if sync is configured
|
||||
if (!syncProvider.isConfigured) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'NOT CONFIGURED',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Colors.orange[700],
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Check if there are recent failures but not disabled yet
|
||||
if (syncProvider.autoSyncConsecutiveFailures > 0) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'RETRYING',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Colors.orange[700],
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
'ACTIVE',
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: Colors.green[700],
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getErrorMessage(SimpleSyncProvider syncProvider) {
|
||||
final error = syncProvider.lastError ?? 'Unknown error';
|
||||
|
||||
// Add context for auto-sync errors
|
||||
if (syncProvider.isAutoSync) {
|
||||
return 'Auto-sync error: $error';
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime dateTime) {
|
||||
return '${dateTime.day}/${dateTime.month}/${dateTime.year} ${dateTime.hour}:${dateTime.minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
Reference in New Issue
Block a user