464 lines
16 KiB
Dart
464 lines
16 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
import 'package:worldhopper/l10n/app_localizations.dart';
|
|
import 'package:worldhopper/models/opds_server.dart';
|
|
import 'package:worldhopper/helpers/snackbar_helper.dart';
|
|
import 'package:worldhopper/providers/server_provider.dart';
|
|
import 'package:worldhopper/services/koreader_sync_service.dart';
|
|
|
|
/// Screen for adding a new OPDS server
|
|
class AddServerScreen extends ConsumerStatefulWidget {
|
|
const AddServerScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<AddServerScreen> createState() => _AddServerScreenState();
|
|
}
|
|
|
|
class _AddServerScreenState extends ConsumerState<AddServerScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _nameController = TextEditingController();
|
|
final _urlController = TextEditingController();
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _requiresAuth = false;
|
|
bool _obscurePassword = true;
|
|
bool _isLoading = false;
|
|
|
|
// KOReader sync fields
|
|
bool _koreaderSyncEnabled = false;
|
|
final _koreaderSyncUrlController = TextEditingController();
|
|
final _koreaderSyncUsernameController = TextEditingController();
|
|
final _koreaderSyncPasswordController = TextEditingController();
|
|
bool _obscureKoreaderPassword = true;
|
|
bool _isTestingKoreaderConnection = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_urlController.dispose();
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
_koreaderSyncUrlController.dispose();
|
|
_koreaderSyncUsernameController.dispose();
|
|
_koreaderSyncPasswordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(l10n.addServerTitle),
|
|
),
|
|
body: Form(
|
|
key: _formKey,
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
// Name field
|
|
TextFormField(
|
|
controller: _nameController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.addServerNameLabel,
|
|
hintText: l10n.addServerNameHint,
|
|
prefixIcon: const Icon(Icons.label),
|
|
),
|
|
textInputAction: TextInputAction.next,
|
|
validator: (value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return l10n.addServerValidationNameRequired;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// URL field
|
|
TextFormField(
|
|
controller: _urlController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.addServerUrlLabel,
|
|
hintText: l10n.addServerUrlHint,
|
|
prefixIcon: const Icon(Icons.link),
|
|
),
|
|
keyboardType: TextInputType.url,
|
|
textInputAction: TextInputAction.next,
|
|
validator: (value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return l10n.addServerValidationUrlRequired;
|
|
}
|
|
final uri = Uri.tryParse(value.trim());
|
|
if (uri == null || !uri.hasScheme || !uri.hasAuthority) {
|
|
return l10n.addServerValidationUrlInvalid;
|
|
}
|
|
if (uri.scheme != 'http' && uri.scheme != 'https') {
|
|
return l10n.addServerValidationUrlScheme;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
// Authentication toggle
|
|
SwitchListTile(
|
|
title: Text(l10n.addServerRequiresAuth),
|
|
subtitle: Text(l10n.addServerRequiresAuthSubtitle),
|
|
value: _requiresAuth,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_requiresAuth = value;
|
|
if (!value) {
|
|
_usernameController.clear();
|
|
_passwordController.clear();
|
|
}
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Authentication fields (conditionally shown)
|
|
if (_requiresAuth) ...[
|
|
TextFormField(
|
|
controller: _usernameController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.addServerUsername,
|
|
prefixIcon: const Icon(Icons.person),
|
|
),
|
|
textInputAction: TextInputAction.next,
|
|
validator: (value) {
|
|
if (_requiresAuth &&
|
|
(value == null || value.trim().isEmpty)) {
|
|
return l10n.addServerValidationUsernameRequired;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.addServerPassword,
|
|
prefixIcon: const Icon(Icons.lock),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscurePassword
|
|
? Icons.visibility
|
|
: Icons.visibility_off,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscurePassword = !_obscurePassword;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
obscureText: _obscurePassword,
|
|
textInputAction: TextInputAction.done,
|
|
validator: (value) {
|
|
if (_requiresAuth && (value == null || value.isEmpty)) {
|
|
return l10n.addServerValidationPasswordRequired;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 24),
|
|
],
|
|
|
|
// KOReader Sync toggle
|
|
SwitchListTile(
|
|
title: Text(l10n.koreaderSyncToggle),
|
|
subtitle: Text(l10n.koreaderSyncToggleSubtitle),
|
|
value: _koreaderSyncEnabled,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_koreaderSyncEnabled = value;
|
|
if (!value) {
|
|
_koreaderSyncUrlController.clear();
|
|
_koreaderSyncUsernameController.clear();
|
|
_koreaderSyncPasswordController.clear();
|
|
}
|
|
});
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// KOReader sync fields (conditionally shown)
|
|
if (_koreaderSyncEnabled) ...[
|
|
TextFormField(
|
|
controller: _koreaderSyncUrlController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.koreaderSyncUrl,
|
|
hintText: l10n.koreaderSyncUrlHint,
|
|
prefixIcon: const Icon(Icons.sync),
|
|
),
|
|
keyboardType: TextInputType.url,
|
|
textInputAction: TextInputAction.next,
|
|
validator: (value) {
|
|
if (_koreaderSyncEnabled &&
|
|
(value == null || value.trim().isEmpty)) {
|
|
return l10n.koreaderSyncValidationUrlRequired;
|
|
}
|
|
if (value != null && value.trim().isNotEmpty) {
|
|
final uri = Uri.tryParse(value.trim());
|
|
if (uri == null || !uri.hasScheme || !uri.hasAuthority) {
|
|
return l10n.koreaderSyncValidationUrlInvalid;
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _koreaderSyncUsernameController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.koreaderSyncUsername,
|
|
prefixIcon: const Icon(Icons.person_outline),
|
|
),
|
|
textInputAction: TextInputAction.next,
|
|
validator: (value) {
|
|
if (_koreaderSyncEnabled &&
|
|
(value == null || value.trim().isEmpty)) {
|
|
return l10n.koreaderSyncValidationUsernameRequired;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _koreaderSyncPasswordController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.koreaderSyncPassword,
|
|
prefixIcon: const Icon(Icons.lock_outline),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_obscureKoreaderPassword
|
|
? Icons.visibility
|
|
: Icons.visibility_off,
|
|
),
|
|
onPressed: () {
|
|
setState(() {
|
|
_obscureKoreaderPassword = !_obscureKoreaderPassword;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
obscureText: _obscureKoreaderPassword,
|
|
textInputAction: TextInputAction.done,
|
|
validator: (value) {
|
|
if (_koreaderSyncEnabled &&
|
|
(value == null || value.isEmpty)) {
|
|
return l10n.koreaderSyncValidationPasswordRequired;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
// Test connection button
|
|
OutlinedButton.icon(
|
|
onPressed: _isTestingKoreaderConnection
|
|
? null
|
|
: _testKoreaderConnection,
|
|
icon: _isTestingKoreaderConnection
|
|
? const SizedBox(
|
|
height: 16,
|
|
width: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.wifi_tethering),
|
|
label: Text(l10n.koreaderSyncTestConnection),
|
|
),
|
|
const SizedBox(height: 16),
|
|
// KOReader sync help text
|
|
Card(
|
|
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Icon(
|
|
Icons.info_outline,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Text(
|
|
l10n.koreaderSyncHelp,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
],
|
|
|
|
// Help text
|
|
Card(
|
|
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Icon(
|
|
Icons.info_outline,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Text(
|
|
l10n.addServerHelp,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
|
|
// Save button
|
|
FilledButton(
|
|
onPressed: _isLoading ? null : _saveServer,
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: Text(l10n.addServerButton),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _testKoreaderConnection() async {
|
|
// Validate KOReader fields before testing
|
|
final url = _koreaderSyncUrlController.text.trim();
|
|
final username = _koreaderSyncUsernameController.text.trim();
|
|
final password = _koreaderSyncPasswordController.text;
|
|
|
|
if (url.isEmpty || username.isEmpty || password.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isTestingKoreaderConnection = true;
|
|
});
|
|
|
|
try {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
// Create a temporary server to test with
|
|
final testServer = OPDSServer(
|
|
id: 'test',
|
|
name: 'test',
|
|
url: '',
|
|
createdAt: DateTime.now(),
|
|
koreaderSyncEnabled: true,
|
|
koreaderSyncUrl: url,
|
|
koreaderSyncUsername: username,
|
|
koreaderSyncPassword: password,
|
|
);
|
|
|
|
final syncService = KoreaderSyncService(testServer);
|
|
|
|
// Test authentication (also verifies server is reachable)
|
|
final authenticated = await syncService.authenticate();
|
|
if (mounted) {
|
|
if (authenticated) {
|
|
context.showSuccessSnackBar(l10n.koreaderSyncTestSuccess(username));
|
|
} else {
|
|
context
|
|
.showErrorSnackBar(l10n.koreaderSyncTestFailed('Unauthorized'));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
final l10n = AppLocalizations.of(context);
|
|
context.showErrorSnackBar(l10n.koreaderSyncTestFailed(e.toString()));
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isTestingKoreaderConnection = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _saveServer() async {
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final l10n = AppLocalizations.of(context);
|
|
final url = _urlController.text.trim();
|
|
|
|
// Check if server already exists
|
|
final exists =
|
|
await ref.read(serverNotifierProvider.notifier).serverExists(url);
|
|
|
|
if (exists) {
|
|
if (mounted) {
|
|
context.showWarningSnackBar(l10n.addServerDuplicate);
|
|
}
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Create new server
|
|
final server = OPDSServer(
|
|
id: const Uuid().v4(),
|
|
name: _nameController.text.trim(),
|
|
url: url,
|
|
username: _requiresAuth ? _usernameController.text.trim() : null,
|
|
password: _requiresAuth ? _passwordController.text : null,
|
|
createdAt: DateTime.now(),
|
|
koreaderSyncEnabled: _koreaderSyncEnabled,
|
|
koreaderSyncUrl: _koreaderSyncEnabled
|
|
? _koreaderSyncUrlController.text.trim()
|
|
: null,
|
|
koreaderSyncUsername: _koreaderSyncEnabled
|
|
? _koreaderSyncUsernameController.text.trim()
|
|
: null,
|
|
koreaderSyncPassword:
|
|
_koreaderSyncEnabled ? _koreaderSyncPasswordController.text : null,
|
|
);
|
|
|
|
// Save server
|
|
await ref.read(serverNotifierProvider.notifier).addServer(server);
|
|
|
|
if (mounted) {
|
|
context.showSuccessSnackBar(l10n.addServerSuccess(server.name));
|
|
context.pop();
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
final l10n = AppLocalizations.of(context);
|
|
context.showErrorSnackBar(l10n.addServerError(e.toString()));
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|