mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
adds syncing
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../models/supplement.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../models/ingredient.dart';
|
||||
import '../models/supplement.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
|
||||
// Helper class to manage ingredient text controllers
|
||||
@@ -22,6 +24,8 @@ class IngredientController {
|
||||
name: nameController.text.trim(),
|
||||
amount: double.tryParse(amountController.text) ?? 0.0,
|
||||
unit: selectedUnit,
|
||||
syncId: const Uuid().v4(),
|
||||
lastModified: DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -46,10 +50,10 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
final _brandController = TextEditingController();
|
||||
final _numberOfUnitsController = TextEditingController();
|
||||
final _notesController = TextEditingController();
|
||||
|
||||
|
||||
// Multi-ingredient support with persistent controllers
|
||||
List<IngredientController> _ingredientControllers = [];
|
||||
|
||||
|
||||
String _selectedUnitType = 'capsules';
|
||||
int _frequencyPerDay = 1;
|
||||
List<String> _reminderTimes = ['08:00'];
|
||||
@@ -195,7 +199,7 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
_selectedUnitType = supplement.unitType;
|
||||
_frequencyPerDay = supplement.frequencyPerDay;
|
||||
_reminderTimes = List.from(supplement.reminderTimes);
|
||||
|
||||
|
||||
// Initialize ingredient controllers from existing ingredients
|
||||
_ingredientControllers.clear();
|
||||
if (supplement.ingredients.isEmpty) {
|
||||
@@ -556,13 +560,15 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
void _saveSupplement() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// Validate that we have at least one ingredient with name and amount
|
||||
final validIngredients = _ingredientControllers.where((controller) =>
|
||||
controller.nameController.text.trim().isNotEmpty &&
|
||||
final validIngredients = _ingredientControllers.where((controller) =>
|
||||
controller.nameController.text.trim().isNotEmpty &&
|
||||
(double.tryParse(controller.amountController.text) ?? 0) > 0
|
||||
).map((controller) => Ingredient(
|
||||
name: controller.nameController.text.trim(),
|
||||
amount: double.tryParse(controller.amountController.text) ?? 0,
|
||||
amount: double.tryParse(controller.amountController.text) ?? 0.0,
|
||||
unit: controller.selectedUnit,
|
||||
syncId: const Uuid().v4(),
|
||||
lastModified: DateTime.now(),
|
||||
)).toList();
|
||||
|
||||
if (validIngredients.isEmpty) {
|
||||
@@ -588,7 +594,7 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
);
|
||||
|
||||
final provider = context.read<SupplementProvider>();
|
||||
|
||||
|
||||
try {
|
||||
if (widget.supplement != null) {
|
||||
await provider.updateSupplement(supplement);
|
||||
@@ -598,10 +604,10 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(widget.supplement != null
|
||||
content: Text(widget.supplement != null
|
||||
? 'Supplement updated successfully!'
|
||||
: 'Supplement added successfully!'),
|
||||
backgroundColor: Colors.green,
|
||||
@@ -627,12 +633,12 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
||||
_brandController.dispose();
|
||||
_numberOfUnitsController.dispose();
|
||||
_notesController.dispose();
|
||||
|
||||
|
||||
// Dispose all ingredient controllers
|
||||
for (final controller in _ingredientControllers) {
|
||||
controller.dispose();
|
||||
}
|
||||
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
|
||||
import '../models/supplement.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../providers/sync_provider.dart';
|
||||
|
||||
class ArchivedSupplementsScreen extends StatefulWidget {
|
||||
const ArchivedSupplementsScreen({super.key});
|
||||
@@ -25,9 +27,35 @@ class _ArchivedSupplementsScreenState extends State<ArchivedSupplementsScreen> {
|
||||
appBar: AppBar(
|
||||
title: const Text('Archived Supplements'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
actions: [
|
||||
Consumer<SyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
if (!syncProvider.isConfigured) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return IconButton(
|
||||
icon: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: syncProvider.status.name == 'success' &&
|
||||
DateTime.now().difference(syncProvider.lastSyncTime ?? DateTime.now()).inSeconds < 5
|
||||
? const Icon(Icons.check, color: Colors.green)
|
||||
: const Icon(Icons.sync),
|
||||
onPressed: syncProvider.isSyncing ? null : () {
|
||||
syncProvider.performManualSync();
|
||||
},
|
||||
tooltip: syncProvider.isSyncing ? 'Syncing...' : 'Force Sync',
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Consumer<SupplementProvider>(
|
||||
builder: (context, provider, child) {
|
||||
body: Consumer2<SupplementProvider, SyncProvider>(
|
||||
builder: (context, provider, syncProvider, child) {
|
||||
if (provider.archivedSupplements.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
@@ -254,7 +282,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
|
||||
// Supplement details in a muted style
|
||||
if (supplement.ingredients.isNotEmpty) ...[
|
||||
Container(
|
||||
@@ -301,7 +329,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
|
||||
|
||||
// Dosage info
|
||||
Row(
|
||||
children: [
|
||||
@@ -318,7 +346,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
|
||||
if (supplement.reminderTimes.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
_InfoChip(
|
||||
|
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../providers/sync_provider.dart';
|
||||
|
||||
class HistoryScreen extends StatefulWidget {
|
||||
const HistoryScreen({super.key});
|
||||
@@ -30,6 +32,32 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
appBar: AppBar(
|
||||
title: const Text('Intake History'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
actions: [
|
||||
Consumer<SyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
if (!syncProvider.isConfigured) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return IconButton(
|
||||
icon: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: syncProvider.status.name == 'success' &&
|
||||
DateTime.now().difference(syncProvider.lastSyncTime ?? DateTime.now()).inSeconds < 5
|
||||
? const Icon(Icons.check, color: Colors.green)
|
||||
: const Icon(Icons.sync),
|
||||
onPressed: syncProvider.isSyncing ? null : () {
|
||||
syncProvider.performManualSync();
|
||||
},
|
||||
tooltip: syncProvider.isSyncing ? 'Syncing...' : 'Force Sync',
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _buildCalendarView(),
|
||||
);
|
||||
@@ -106,7 +134,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWideScreen = constraints.maxWidth > 800;
|
||||
|
||||
|
||||
if (isWideScreen) {
|
||||
// Desktop/tablet layout: side-by-side
|
||||
return Row(
|
||||
@@ -177,7 +205,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
if (picked != null) {
|
||||
setState(() {
|
||||
_selectedMonth = picked.month;
|
||||
@@ -203,13 +231,13 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
onPressed: () async {
|
||||
await context.read<SupplementProvider>().deleteIntake(intakeId);
|
||||
Navigator.of(context).pop();
|
||||
|
||||
|
||||
// Force refresh of the UI
|
||||
setState(() {});
|
||||
|
||||
|
||||
// Force refresh of the current view data
|
||||
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
|
||||
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('$supplementName intake deleted'),
|
||||
@@ -230,11 +258,11 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
final lastDayOfMonth = DateTime(_selectedYear, _selectedMonth + 1, 0);
|
||||
final firstWeekday = firstDayOfMonth.weekday;
|
||||
final daysInMonth = lastDayOfMonth.day;
|
||||
|
||||
|
||||
// Calculate how many cells we need (including empty ones for alignment)
|
||||
final totalCells = ((daysInMonth + firstWeekday - 1) / 7).ceil() * 7;
|
||||
final weeks = (totalCells / 7).ceil();
|
||||
|
||||
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWideScreen = constraints.maxWidth > 800;
|
||||
@@ -242,7 +270,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
final cellHeight = isWideScreen ? 56.0 : 48.0;
|
||||
final calendarContentHeight = (weeks * cellHeight) + 60; // +60 for headers and padding
|
||||
final calendarHeight = isWideScreen ? 400.0 : calendarContentHeight;
|
||||
|
||||
|
||||
return Card(
|
||||
child: Container(
|
||||
height: calendarHeight,
|
||||
@@ -283,11 +311,11 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
itemCount: totalCells,
|
||||
itemBuilder: (context, index) {
|
||||
final dayNumber = index - firstWeekday + 2;
|
||||
|
||||
|
||||
if (dayNumber < 1 || dayNumber > daysInMonth) {
|
||||
return const SizedBox(); // Empty cell
|
||||
}
|
||||
|
||||
|
||||
final date = DateTime(_selectedYear, _selectedMonth, dayNumber);
|
||||
final dateKey = DateFormat('yyyy-MM-dd').format(date);
|
||||
final hasIntakes = groupedIntakes.containsKey(dateKey);
|
||||
@@ -295,7 +323,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
final isSelected = _selectedDay != null &&
|
||||
DateFormat('yyyy-MM-dd').format(_selectedDay!) == dateKey;
|
||||
final isToday = DateFormat('yyyy-MM-dd').format(DateTime.now()) == dateKey;
|
||||
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
@@ -305,12 +333,12 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(1),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: hasIntakes
|
||||
: hasIntakes
|
||||
? Theme.of(context).colorScheme.primaryContainer
|
||||
: null,
|
||||
border: isToday
|
||||
border: isToday
|
||||
? Border.all(color: Theme.of(context).colorScheme.secondary, width: 2)
|
||||
: null,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
@@ -321,9 +349,9 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
child: Text(
|
||||
'$dayNumber',
|
||||
style: TextStyle(
|
||||
color: isSelected
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.onPrimary
|
||||
: hasIntakes
|
||||
: hasIntakes
|
||||
? Theme.of(context).colorScheme.onPrimaryContainer
|
||||
: Theme.of(context).colorScheme.onSurface,
|
||||
fontWeight: isToday ? FontWeight.bold : FontWeight.normal,
|
||||
@@ -338,7 +366,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(isWideScreen ? 3 : 2),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.onPrimary
|
||||
: Theme.of(context).colorScheme.primary,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
@@ -350,7 +378,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
child: Text(
|
||||
'$intakeCount',
|
||||
style: TextStyle(
|
||||
color: isSelected
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Theme.of(context).colorScheme.onPrimary,
|
||||
fontSize: isWideScreen ? 11 : 10,
|
||||
@@ -380,7 +408,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWideScreen = constraints.maxWidth > 600;
|
||||
|
||||
|
||||
if (_selectedDay == null) {
|
||||
return Card(
|
||||
child: Center(
|
||||
|
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../providers/settings_provider.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../services/notification_service.dart';
|
||||
import 'pending_notifications_screen.dart';
|
||||
import 'sync_settings_screen.dart';
|
||||
|
||||
class SettingsScreen extends StatelessWidget {
|
||||
const SettingsScreen({super.key});
|
||||
@@ -68,6 +70,22 @@ class SettingsScreen extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.cloud_sync),
|
||||
title: const Text('Cloud Sync'),
|
||||
subtitle: const Text('Configure WebDAV sync settings'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const SyncSettingsScreen(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
@@ -365,7 +383,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Pending Notifications'),
|
||||
content: pending.isEmpty
|
||||
content: pending.isEmpty
|
||||
? const Text('No pending notifications')
|
||||
: SizedBox(
|
||||
width: double.maxFinite,
|
||||
@@ -376,7 +394,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
itemCount: pending.length,
|
||||
itemBuilder: (context, index) {
|
||||
final notification = pending[index];
|
||||
|
||||
|
||||
// Calculate scheduled time inline
|
||||
String scheduledTime = '';
|
||||
try {
|
||||
@@ -389,22 +407,22 @@ class SettingsScreen extends StatelessWidget {
|
||||
} else {
|
||||
final supplementId = notificationId ~/ 100;
|
||||
final reminderIndex = notificationId % 100;
|
||||
|
||||
|
||||
final supplement = provider.supplements.firstWhere(
|
||||
(s) => s.id == supplementId,
|
||||
orElse: () => provider.supplements.first,
|
||||
);
|
||||
|
||||
|
||||
if (reminderIndex < supplement.reminderTimes.length) {
|
||||
final reminderTime = supplement.reminderTimes[reminderIndex];
|
||||
final now = DateTime.now();
|
||||
final timeParts = reminderTime.split(':');
|
||||
final hour = int.parse(timeParts[0]);
|
||||
final minute = int.parse(timeParts[1]);
|
||||
|
||||
|
||||
final today = DateTime(now.year, now.month, now.day, hour, minute);
|
||||
final isToday = today.isAfter(now);
|
||||
|
||||
|
||||
scheduledTime = '${isToday ? 'Today' : 'Tomorrow'} at $reminderTime';
|
||||
} else {
|
||||
scheduledTime = 'Unknown time';
|
||||
@@ -413,7 +431,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
} catch (e) {
|
||||
scheduledTime = 'ID: ${notification.id}';
|
||||
}
|
||||
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: ListTile(
|
||||
|
@@ -1,8 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../providers/settings_provider.dart';
|
||||
|
||||
import '../models/supplement.dart';
|
||||
import '../providers/settings_provider.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../providers/sync_provider.dart';
|
||||
import '../widgets/supplement_card.dart';
|
||||
import 'add_supplement_screen.dart';
|
||||
import 'archived_supplements_screen.dart';
|
||||
@@ -17,6 +19,30 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
title: const Text('My Supplements'),
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
actions: [
|
||||
Consumer<SyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
if (!syncProvider.isConfigured) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return IconButton(
|
||||
icon: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: syncProvider.status.name == 'success' &&
|
||||
DateTime.now().difference(syncProvider.lastSyncTime ?? DateTime.now()).inSeconds < 5
|
||||
? const Icon(Icons.check, color: Colors.green)
|
||||
: const Icon(Icons.sync),
|
||||
onPressed: syncProvider.isSyncing ? null : () {
|
||||
syncProvider.performManualSync();
|
||||
},
|
||||
tooltip: syncProvider.isSyncing ? 'Syncing...' : 'Force Sync',
|
||||
);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.archive),
|
||||
onPressed: () {
|
||||
@@ -30,8 +56,8 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Consumer2<SupplementProvider, SettingsProvider>(
|
||||
builder: (context, provider, settingsProvider, child) {
|
||||
body: Consumer3<SupplementProvider, SettingsProvider, SyncProvider>(
|
||||
builder: (context, provider, settingsProvider, syncProvider, child) {
|
||||
if (provider.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
@@ -80,13 +106,13 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
|
||||
Widget _buildGroupedSupplementsList(BuildContext context, List<Supplement> supplements, SettingsProvider settingsProvider) {
|
||||
final groupedSupplements = _groupSupplementsByTimeOfDay(supplements, settingsProvider);
|
||||
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
if (groupedSupplements['morning']!.isNotEmpty) ...[
|
||||
_buildSectionHeader('Morning (${settingsProvider.morningRange})', Icons.wb_sunny, Colors.orange, groupedSupplements['morning']!.length),
|
||||
...groupedSupplements['morning']!.map((supplement) =>
|
||||
...groupedSupplements['morning']!.map((supplement) =>
|
||||
SupplementCard(
|
||||
supplement: supplement,
|
||||
onTake: () => _showTakeDialog(context, supplement),
|
||||
@@ -97,10 +123,10 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
|
||||
if (groupedSupplements['afternoon']!.isNotEmpty) ...[
|
||||
_buildSectionHeader('Afternoon (${settingsProvider.afternoonRange})', Icons.light_mode, Colors.blue, groupedSupplements['afternoon']!.length),
|
||||
...groupedSupplements['afternoon']!.map((supplement) =>
|
||||
...groupedSupplements['afternoon']!.map((supplement) =>
|
||||
SupplementCard(
|
||||
supplement: supplement,
|
||||
onTake: () => _showTakeDialog(context, supplement),
|
||||
@@ -111,10 +137,10 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
|
||||
if (groupedSupplements['evening']!.isNotEmpty) ...[
|
||||
_buildSectionHeader('Evening (${settingsProvider.eveningRange})', Icons.nightlight_round, Colors.indigo, groupedSupplements['evening']!.length),
|
||||
...groupedSupplements['evening']!.map((supplement) =>
|
||||
...groupedSupplements['evening']!.map((supplement) =>
|
||||
SupplementCard(
|
||||
supplement: supplement,
|
||||
onTake: () => _showTakeDialog(context, supplement),
|
||||
@@ -125,10 +151,10 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
|
||||
if (groupedSupplements['night']!.isNotEmpty) ...[
|
||||
_buildSectionHeader('Night (${settingsProvider.nightRange})', Icons.bedtime, Colors.purple, groupedSupplements['night']!.length),
|
||||
...groupedSupplements['night']!.map((supplement) =>
|
||||
...groupedSupplements['night']!.map((supplement) =>
|
||||
SupplementCard(
|
||||
supplement: supplement,
|
||||
onTake: () => _showTakeDialog(context, supplement),
|
||||
@@ -139,10 +165,10 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
|
||||
if (groupedSupplements['anytime']!.isNotEmpty) ...[
|
||||
_buildSectionHeader('Anytime', Icons.schedule, Colors.grey, groupedSupplements['anytime']!.length),
|
||||
...groupedSupplements['anytime']!.map((supplement) =>
|
||||
...groupedSupplements['anytime']!.map((supplement) =>
|
||||
SupplementCard(
|
||||
supplement: supplement,
|
||||
onTake: () => _showTakeDialog(context, supplement),
|
||||
@@ -305,7 +331,7 @@ class SupplementsListScreen extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
|
||||
// Time selection section
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
|
782
lib/screens/sync_settings_screen.dart
Normal file
782
lib/screens/sync_settings_screen.dart
Normal file
@@ -0,0 +1,782 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/sync_enums.dart';
|
||||
import '../providers/sync_provider.dart';
|
||||
|
||||
/// Screen for configuring WebDAV sync settings
|
||||
class SyncSettingsScreen extends StatefulWidget {
|
||||
const SyncSettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<SyncSettingsScreen> createState() => _SyncSettingsScreenState();
|
||||
}
|
||||
|
||||
class _SyncSettingsScreenState extends State<SyncSettingsScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _serverUrlController = TextEditingController();
|
||||
final _usernameController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
final _deviceNameController = TextEditingController();
|
||||
final _syncFolderController = TextEditingController();
|
||||
|
||||
bool _isPasswordVisible = false;
|
||||
bool _isTestingConnection = false;
|
||||
bool _isConfiguring = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadCurrentSettings();
|
||||
}
|
||||
|
||||
void _loadCurrentSettings() {
|
||||
final syncProvider = context.read<SyncProvider>();
|
||||
_serverUrlController.text = syncProvider.serverUrl ?? '';
|
||||
_usernameController.text = syncProvider.username ?? '';
|
||||
_syncFolderController.text = syncProvider.syncFolderName ?? 'Supplements';
|
||||
// Note: We don't load the password for security reasons
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_serverUrlController.dispose();
|
||||
_usernameController.dispose();
|
||||
_passwordController.dispose();
|
||||
_deviceNameController.dispose();
|
||||
_syncFolderController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Cloud Sync Settings'),
|
||||
actions: [
|
||||
Consumer<SyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
if (!syncProvider.isConfigured) return const SizedBox.shrink();
|
||||
|
||||
return PopupMenuButton<String>(
|
||||
onSelected: (value) {
|
||||
switch (value) {
|
||||
case 'test':
|
||||
_testConnection();
|
||||
break;
|
||||
case 'sync':
|
||||
_performSync();
|
||||
break;
|
||||
case 'clear':
|
||||
_showClearConfigDialog();
|
||||
break;
|
||||
}
|
||||
},
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem(
|
||||
value: 'test',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.wifi_protected_setup),
|
||||
title: Text('Test Connection'),
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'sync',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.sync),
|
||||
title: Text('Sync Now'),
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'clear',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.clear),
|
||||
title: Text('Clear Configuration'),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Consumer<SyncProvider>(
|
||||
builder: (context, syncProvider, child) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildStatusCard(syncProvider),
|
||||
const SizedBox(height: 24),
|
||||
_buildDeviceInfoSection(syncProvider),
|
||||
const SizedBox(height: 24),
|
||||
_buildConfigurationSection(syncProvider),
|
||||
const SizedBox(height: 24),
|
||||
_buildSyncSettingsSection(syncProvider),
|
||||
if (syncProvider.hasPendingConflicts) ...[
|
||||
const SizedBox(height: 24),
|
||||
_buildConflictsSection(syncProvider),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusCard(SyncProvider syncProvider) {
|
||||
Color statusColor;
|
||||
IconData statusIcon;
|
||||
|
||||
switch (syncProvider.status) {
|
||||
case SyncOperationStatus.success:
|
||||
statusColor = Colors.green;
|
||||
statusIcon = Icons.check_circle;
|
||||
break;
|
||||
case SyncOperationStatus.syncing:
|
||||
statusColor = Colors.blue;
|
||||
statusIcon = Icons.sync;
|
||||
break;
|
||||
case SyncOperationStatus.networkError:
|
||||
case SyncOperationStatus.authenticationError:
|
||||
case SyncOperationStatus.serverError:
|
||||
statusColor = Colors.red;
|
||||
statusIcon = Icons.error;
|
||||
break;
|
||||
case SyncOperationStatus.conflictsDetected:
|
||||
statusColor = Colors.orange;
|
||||
statusIcon = Icons.warning;
|
||||
break;
|
||||
default:
|
||||
statusColor = Colors.grey;
|
||||
statusIcon = Icons.cloud_off;
|
||||
}
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(statusIcon, color: statusColor, size: 24),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
syncProvider.statusMessage,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (syncProvider.lastSyncTime != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Last sync: ${syncProvider.formattedLastSyncTime}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
if (syncProvider.isConfigured && syncProvider.detectedServerType != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.check_circle, size: 16, color: Colors.green),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'Detected: ${syncProvider.detectedServerType}',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Colors.green[700],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
if (syncProvider.hasError) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
syncProvider.currentError!,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
],
|
||||
if (syncProvider.isSyncing) ...[
|
||||
const SizedBox(height: 12),
|
||||
LinearProgressIndicator(
|
||||
value: syncProvider.syncProgress,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildConfigurationSection(SyncProvider syncProvider) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'WebDAV Configuration',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _serverUrlController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server URL',
|
||||
hintText: 'cloud.example.com or drive.mydomain.com',
|
||||
prefixIcon: Icon(Icons.cloud),
|
||||
helperText: 'Just enter your server domain - we\'ll auto-detect the rest!',
|
||||
helperMaxLines: 2,
|
||||
),
|
||||
validator: (value) {
|
||||
if (value?.isEmpty ?? true) {
|
||||
return 'Please enter server URL';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.blue.withOpacity(0.3)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.lightbulb_outline, size: 16, color: Colors.blue[700]),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Smart URL Detection',
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
color: Colors.blue[700],
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'You can enter simple URLs like:',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'• cloud.example.com\n'
|
||||
'• drive.mydomain.com\n'
|
||||
'• nextcloud.company.org\n'
|
||||
'• my-server.duckdns.org:8080',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'We\'ll automatically detect if it\'s Nextcloud, ownCloud, or generic WebDAV and build the correct URL for you!',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _usernameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Username',
|
||||
prefixIcon: Icon(Icons.person),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value?.isEmpty ?? true) {
|
||||
return 'Please enter username';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
obscureText: !_isPasswordVisible,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password / App Password',
|
||||
prefixIcon: const Icon(Icons.lock),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
_isPasswordVisible ? Icons.visibility : Icons.visibility_off,
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_isPasswordVisible = !_isPasswordVisible;
|
||||
});
|
||||
},
|
||||
),
|
||||
helperText: 'Use app passwords for better security',
|
||||
),
|
||||
validator: (value) {
|
||||
if (value?.isEmpty ?? true) {
|
||||
return 'Please enter password';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _syncFolderController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Sync Folder Name',
|
||||
prefixIcon: Icon(Icons.folder),
|
||||
hintText: 'Supplements',
|
||||
helperText: 'Folder name on your cloud server for syncing data',
|
||||
),
|
||||
validator: (value) {
|
||||
if (value?.isEmpty ?? true) {
|
||||
return 'Please enter folder name';
|
||||
}
|
||||
if (value!.contains('/') || value.contains('\\')) {
|
||||
return 'Folder name cannot contain slashes';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _deviceNameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Device Name (Optional)',
|
||||
prefixIcon: Icon(Icons.phone_android),
|
||||
hintText: 'My Phone',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: _isConfiguring || syncProvider.isSyncing
|
||||
? null
|
||||
: () => _configureWebDAV(syncProvider),
|
||||
icon: _isConfiguring
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.save),
|
||||
label: Text(syncProvider.isConfigured ? 'Update' : 'Configure'),
|
||||
),
|
||||
),
|
||||
if (syncProvider.isConfigured) ...[
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _isTestingConnection || syncProvider.isSyncing
|
||||
? null
|
||||
: _testConnection,
|
||||
icon: _isTestingConnection
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.wifi_protected_setup),
|
||||
label: const Text('Test'),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSyncSettingsSection(SyncProvider syncProvider) {
|
||||
if (!syncProvider.isConfigured) return const SizedBox.shrink();
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Sync Settings',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SwitchListTile(
|
||||
title: const Text('Auto Sync on Data Changes'),
|
||||
subtitle: const Text('Automatically sync when you add, modify, or take supplements'),
|
||||
value: syncProvider.autoSyncOnDataChanges,
|
||||
onChanged: syncProvider.setAutoSyncOnDataChanges,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ListTile(
|
||||
title: const Text('Conflict Resolution'),
|
||||
subtitle: Text(_getConflictStrategyDescription(syncProvider.conflictStrategy)),
|
||||
trailing: DropdownButton<ConflictResolutionStrategy>(
|
||||
value: syncProvider.conflictStrategy,
|
||||
onChanged: (strategy) {
|
||||
if (strategy != null) {
|
||||
syncProvider.setConflictResolutionStrategy(strategy);
|
||||
}
|
||||
},
|
||||
items: ConflictResolutionStrategy.values
|
||||
.map((strategy) => DropdownMenuItem(
|
||||
value: strategy,
|
||||
child: Text(_getConflictStrategyName(strategy)),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: syncProvider.isSyncing ? null : _performSync,
|
||||
icon: syncProvider.isSyncing
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.sync),
|
||||
label: const Text('Sync Now'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildConflictsSection(SyncProvider syncProvider) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.warning, color: Colors.orange),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'Sync Conflicts',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'${syncProvider.pendingConflicts.length} conflicts need your attention',
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: () => _showConflictsDialog(syncProvider),
|
||||
child: const Text('Review Conflicts'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: syncProvider.resolveAllConflicts,
|
||||
child: const Text('Auto Resolve'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeviceInfoSection(SyncProvider syncProvider) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Connection Information',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FutureBuilder<Map<String, String?>>(
|
||||
future: syncProvider.getDeviceInfo(),
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final deviceInfo = snapshot.data!;
|
||||
return Column(
|
||||
children: [
|
||||
if (syncProvider.detectedServerType != null)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.cloud_done),
|
||||
title: const Text('Server Type'),
|
||||
subtitle: Text(syncProvider.detectedServerType!),
|
||||
),
|
||||
if (syncProvider.finalWebdavUrl != null)
|
||||
ListTile(
|
||||
leading: const Icon(Icons.link),
|
||||
title: const Text('WebDAV URL'),
|
||||
subtitle: Text(
|
||||
syncProvider.finalWebdavUrl!,
|
||||
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.fingerprint),
|
||||
title: const Text('Device ID'),
|
||||
subtitle: Text(deviceInfo['deviceId'] ?? 'Unknown'),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.devices),
|
||||
title: const Text('Device Name'),
|
||||
subtitle: Text(deviceInfo['deviceName'] ?? 'Unknown'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getConflictStrategyName(ConflictResolutionStrategy strategy) {
|
||||
switch (strategy) {
|
||||
case ConflictResolutionStrategy.preferLocal:
|
||||
return 'Prefer Local';
|
||||
case ConflictResolutionStrategy.preferRemote:
|
||||
return 'Prefer Remote';
|
||||
case ConflictResolutionStrategy.preferNewer:
|
||||
return 'Prefer Newer';
|
||||
case ConflictResolutionStrategy.manual:
|
||||
return 'Manual';
|
||||
}
|
||||
}
|
||||
|
||||
String _getConflictStrategyDescription(ConflictResolutionStrategy strategy) {
|
||||
switch (strategy) {
|
||||
case ConflictResolutionStrategy.preferLocal:
|
||||
return 'Always keep local changes';
|
||||
case ConflictResolutionStrategy.preferRemote:
|
||||
return 'Always keep remote changes';
|
||||
case ConflictResolutionStrategy.preferNewer:
|
||||
return 'Keep most recent changes';
|
||||
case ConflictResolutionStrategy.manual:
|
||||
return 'Review each conflict manually';
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _configureWebDAV(SyncProvider syncProvider) async {
|
||||
// For updates, allow empty password to keep existing one
|
||||
if (syncProvider.isConfigured && _passwordController.text.isEmpty) {
|
||||
// Skip validation for password on updates
|
||||
if (_serverUrlController.text.trim().isEmpty || _usernameController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please fill in server URL and username'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
} else if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _isConfiguring = true);
|
||||
|
||||
final success = await syncProvider.configure(
|
||||
serverUrl: _serverUrlController.text.trim(),
|
||||
username: _usernameController.text.trim(),
|
||||
password: _passwordController.text.isEmpty ? null : _passwordController.text,
|
||||
deviceName: _deviceNameController.text.trim().isEmpty
|
||||
? null
|
||||
: _deviceNameController.text.trim(),
|
||||
syncFolderName: _syncFolderController.text.trim().isEmpty
|
||||
? 'Supplements'
|
||||
: _syncFolderController.text.trim(),
|
||||
);
|
||||
|
||||
setState(() => _isConfiguring = false);
|
||||
|
||||
if (mounted) {
|
||||
final message = success
|
||||
? 'WebDAV configured successfully!'
|
||||
: 'Failed to configure WebDAV';
|
||||
|
||||
final detectionInfo = success && syncProvider.detectedServerType != null
|
||||
? '\nDetected: ${syncProvider.detectedServerType}'
|
||||
: '';
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('$message$detectionInfo'),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
duration: Duration(seconds: success ? 4 : 3),
|
||||
),
|
||||
);
|
||||
|
||||
if (success) {
|
||||
_passwordController.clear(); // Clear password for security
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _testConnection() async {
|
||||
setState(() => _isTestingConnection = true);
|
||||
|
||||
final syncProvider = context.read<SyncProvider>();
|
||||
final success = await syncProvider.testConnection();
|
||||
|
||||
setState(() => _isTestingConnection = false);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success
|
||||
? 'Connection test successful!'
|
||||
: 'Connection test failed'),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _performSync() async {
|
||||
final syncProvider = context.read<SyncProvider>();
|
||||
final success = await syncProvider.performManualSync();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(success
|
||||
? 'Sync completed successfully!'
|
||||
: 'Sync failed'),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showClearConfigDialog() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Clear Configuration'),
|
||||
content: const Text(
|
||||
'Are you sure you want to clear the WebDAV configuration? '
|
||||
'This will disable cloud sync.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Clear'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true) {
|
||||
final syncProvider = context.read<SyncProvider>();
|
||||
await syncProvider.clearConfiguration();
|
||||
_serverUrlController.clear();
|
||||
_usernameController.clear();
|
||||
_passwordController.clear();
|
||||
_deviceNameController.clear();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Configuration cleared'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showConflictsDialog(SyncProvider syncProvider) async {
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Sync Conflicts'),
|
||||
content: SizedBox(
|
||||
width: double.maxFinite,
|
||||
height: 300,
|
||||
child: ListView.builder(
|
||||
itemCount: syncProvider.pendingConflicts.length,
|
||||
itemBuilder: (context, index) {
|
||||
final conflict = syncProvider.pendingConflicts[index];
|
||||
return Card(
|
||||
child: ListTile(
|
||||
title: Text(conflict.type.name),
|
||||
subtitle: Text(conflict.description),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
syncProvider.resolveConflict(
|
||||
conflict.syncId,
|
||||
ConflictResolution.useLocal,
|
||||
);
|
||||
},
|
||||
child: const Text('Local'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
syncProvider.resolveConflict(
|
||||
conflict.syncId,
|
||||
ConflictResolution.useRemote,
|
||||
);
|
||||
},
|
||||
child: const Text('Remote'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user