mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat adds proper syncing feature
Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
467
lib/screens/simple_sync_settings_screen.dart
Normal file
467
lib/screens/simple_sync_settings_screen.dart
Normal file
@@ -0,0 +1,467 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../services/database_sync_service.dart';
|
||||
import '../providers/simple_sync_provider.dart';
|
||||
|
||||
class SimpleSyncSettingsScreen extends StatefulWidget {
|
||||
const SimpleSyncSettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<SimpleSyncSettingsScreen> createState() => _SimpleSyncSettingsScreenState();
|
||||
}
|
||||
|
||||
class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _serverUrlController = TextEditingController();
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
final _remotePathController = TextEditingController();
|
||||
|
||||
String _previewUrl = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_serverUrlController.addListener(_updatePreviewUrl);
|
||||
_usernameController.addListener(_updatePreviewUrl);
|
||||
_loadSavedConfiguration();
|
||||
}
|
||||
|
||||
void _loadSavedConfiguration() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
if (syncProvider.serverUrl != null) {
|
||||
_serverUrlController.text = _extractHostnameFromUrl(syncProvider.serverUrl!);
|
||||
}
|
||||
if (syncProvider.username != null) {
|
||||
_usernameController.text = syncProvider.username!;
|
||||
}
|
||||
if (syncProvider.password != null) {
|
||||
_passwordController.text = syncProvider.password!;
|
||||
}
|
||||
if (syncProvider.remotePath != null) {
|
||||
_remotePathController.text = syncProvider.remotePath!;
|
||||
}
|
||||
|
||||
_updatePreviewUrl();
|
||||
});
|
||||
}
|
||||
|
||||
String _extractHostnameFromUrl(String fullUrl) {
|
||||
try {
|
||||
final uri = Uri.parse(fullUrl);
|
||||
return uri.host;
|
||||
} catch (e) {
|
||||
return fullUrl; // Return as-is if parsing fails
|
||||
}
|
||||
}
|
||||
|
||||
void _updatePreviewUrl() {
|
||||
setState(() {
|
||||
if (_serverUrlController.text.isNotEmpty && _usernameController.text.isNotEmpty) {
|
||||
_previewUrl = _constructWebDAVUrl(_serverUrlController.text, _usernameController.text);
|
||||
} else {
|
||||
_previewUrl = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_serverUrlController.removeListener(_updatePreviewUrl);
|
||||
_usernameController.removeListener(_updatePreviewUrl);
|
||||
_serverUrlController.dispose();
|
||||
_usernameController.dispose();
|
||||
_passwordController.dispose();
|
||||
_remotePathController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Database Sync Settings'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
),
|
||||
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),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusCard(SimpleSyncProvider syncProvider) {
|
||||
IconData icon;
|
||||
Color color;
|
||||
String statusText = syncProvider.getStatusText();
|
||||
|
||||
switch (syncProvider.status) {
|
||||
case SyncStatus.idle:
|
||||
icon = Icons.sync;
|
||||
color = Colors.blue;
|
||||
break;
|
||||
case SyncStatus.downloading:
|
||||
case SyncStatus.merging:
|
||||
case SyncStatus.uploading:
|
||||
icon = Icons.sync;
|
||||
color = Colors.orange;
|
||||
break;
|
||||
case SyncStatus.completed:
|
||||
icon = Icons.check_circle;
|
||||
color = Colors.green;
|
||||
break;
|
||||
case SyncStatus.error:
|
||||
icon = Icons.error;
|
||||
color = Colors.red;
|
||||
break;
|
||||
}
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, color: color, size: 24),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
statusText,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (syncProvider.lastSyncTime != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Last sync: ${_formatDateTime(syncProvider.lastSyncTime!)}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
if (syncProvider.lastError != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
syncProvider.lastError!,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.red),
|
||||
onPressed: () => syncProvider.clearError(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildConfigurationSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'WebDAV Configuration',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _serverUrlController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server URL',
|
||||
hintText: 'your-nextcloud.com',
|
||||
helperText: 'Enter just the hostname. We\'ll auto-detect the full WebDAV path.',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a server URL';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _usernameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Username',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a username';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
if (_previewUrl.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest.withOpacity(0.3),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'WebDAV URL Preview:',
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
SelectableText(
|
||||
_previewUrl,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Password',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
obscureText: true,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a password';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _remotePathController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Remote Path (optional)',
|
||||
hintText: 'Supplements/',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
),
|
||||
child: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
),
|
||||
)
|
||||
: const Text('Sync Database'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _testConnection() async {
|
||||
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'
|
||||
: _remotePathController.text.trim(),
|
||||
);
|
||||
|
||||
final success = await syncProvider.testConnection();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success
|
||||
? 'Connection successful!'
|
||||
: 'Connection failed. Check your settings.'),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Connection test failed: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _configureSync() async {
|
||||
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'
|
||||
: _remotePathController.text.trim(),
|
||||
);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Configuration saved successfully!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Failed to save configuration: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _syncDatabase() async {
|
||||
final syncProvider = context.read<SimpleSyncProvider>();
|
||||
|
||||
try {
|
||||
await syncProvider.syncDatabase();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Database sync completed!'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Sync failed: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _constructWebDAVUrl(String serverUrl, String username) {
|
||||
// Remove any protocol prefix if present
|
||||
String cleanUrl = serverUrl.trim();
|
||||
if (cleanUrl.startsWith('http://')) {
|
||||
cleanUrl = cleanUrl.substring(7);
|
||||
} 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/';
|
||||
}
|
||||
|
||||
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