2022-10-15 02:25:15 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2023-05-01 02:50:42 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-10-29 02:47:14 +01:00
|
|
|
import 'package:adguard_home_manager/functions/desktop_mode.dart';
|
2022-10-15 02:25:15 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:animations/animations.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/screens/settings/dhcp/delete_static_lease_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/screens/settings/dhcp/add_static_lease_modal.dart';
|
|
|
|
|
2023-05-24 18:22:13 +02:00
|
|
|
import 'package:adguard_home_manager/providers/dhcp_provider.dart';
|
2022-10-15 02:25:15 +02:00
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/models/dhcp.dart';
|
|
|
|
|
2022-10-24 01:30:56 +02:00
|
|
|
class DhcpLeases extends StatelessWidget {
|
2022-10-15 02:25:15 +02:00
|
|
|
final List<Lease> items;
|
2022-10-24 01:30:56 +02:00
|
|
|
final bool staticLeases;
|
2022-10-15 02:25:15 +02:00
|
|
|
|
2022-10-24 01:30:56 +02:00
|
|
|
const DhcpLeases({
|
2022-10-15 02:25:15 +02:00
|
|
|
Key? key,
|
|
|
|
required this.items,
|
2022-10-24 01:30:56 +02:00
|
|
|
required this.staticLeases,
|
2022-10-15 02:25:15 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-24 18:22:13 +02:00
|
|
|
final dhcpProvider = Provider.of<DhcpProvider>(context);
|
2022-10-15 02:25:15 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
2023-05-01 02:50:42 +02:00
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
|
2022-10-15 02:25:15 +02:00
|
|
|
void deleteLease(Lease lease) async {
|
2023-11-20 15:16:20 +01:00
|
|
|
ProcessModal processModal = ProcessModal();
|
2022-10-15 02:25:15 +02:00
|
|
|
processModal.open(AppLocalizations.of(context)!.deleting);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await dhcpProvider.deleteLease(lease);
|
2022-10-15 02:25:15 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result == true) {
|
2022-10-15 02:25:15 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.staticLeaseDeleted,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.staticLeaseNotDeleted,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void createLease(Lease lease) async {
|
2023-11-20 15:16:20 +01:00
|
|
|
ProcessModal processModal = ProcessModal();
|
2022-10-15 02:25:15 +02:00
|
|
|
processModal.open(AppLocalizations.of(context)!.creating);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await dhcpProvider.createLease(lease);
|
2022-10-15 02:25:15 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-11-19 22:52:40 +01:00
|
|
|
if (result.successful == true) {
|
2022-10-15 02:25:15 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.staticLeaseCreated,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
2023-11-19 22:52:40 +01:00
|
|
|
else if (result.successful == false && result.content == "already_exists") {
|
2022-10-15 02:25:15 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.staticLeaseExists,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
2023-11-19 22:52:40 +01:00
|
|
|
else if (result.successful == false && result.content == "server_not_configured") {
|
2022-10-15 02:29:08 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.serverNotConfigured,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
2022-10-15 02:25:15 +02:00
|
|
|
else {
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.staticLeaseNotCreated,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void openAddStaticLease() {
|
2023-05-01 02:50:42 +02:00
|
|
|
if (width > 900 || !(Platform.isAndroid || Platform.isIOS)) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AddStaticLeaseModal(
|
|
|
|
onConfirm: createLease,
|
|
|
|
dialog: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
2023-10-29 02:47:14 +01:00
|
|
|
useRootNavigator: true,
|
2023-05-01 02:50:42 +02:00
|
|
|
builder: (context) => AddStaticLeaseModal(
|
|
|
|
onConfirm: createLease,
|
|
|
|
dialog: false,
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true
|
|
|
|
);
|
|
|
|
}
|
2022-10-15 02:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-10-29 02:47:14 +01:00
|
|
|
surfaceTintColor: isDesktop(width) ? Colors.transparent : null,
|
2022-10-24 01:30:56 +02:00
|
|
|
title: Text(
|
|
|
|
staticLeases == true
|
|
|
|
? AppLocalizations.of(context)!.dhcpStatic
|
|
|
|
: AppLocalizations.of(context)!.dhcpLeases,
|
|
|
|
),
|
2022-10-15 02:25:15 +02:00
|
|
|
),
|
|
|
|
body: items.isNotEmpty
|
|
|
|
? ListView.builder(
|
|
|
|
padding: const EdgeInsets.only(top: 0),
|
|
|
|
itemCount: items.length,
|
|
|
|
itemBuilder: (context, index) => ListTile(
|
|
|
|
isThreeLine: true,
|
|
|
|
title: Text(items[index].ip),
|
|
|
|
subtitle: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(items[index].mac),
|
|
|
|
Text(items[index].hostname),
|
|
|
|
],
|
|
|
|
),
|
2022-10-24 01:30:56 +02:00
|
|
|
trailing: staticLeases == true
|
|
|
|
? IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => DeleteStaticLeaseModal(
|
|
|
|
onConfirm: () => deleteLease(items[index])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.delete)
|
|
|
|
)
|
|
|
|
: null,
|
2022-10-15 02:25:15 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
: Center(
|
2022-10-24 01:30:56 +02:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Text(
|
|
|
|
staticLeases == true
|
|
|
|
? AppLocalizations.of(context)!.noDhcpStaticLeases
|
|
|
|
: AppLocalizations.of(context)!.noLeases,
|
|
|
|
textAlign: TextAlign.center,
|
2023-01-29 21:52:37 +01:00
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
2022-10-24 01:30:56 +02:00
|
|
|
fontSize: 22
|
|
|
|
),
|
2022-10-15 02:25:15 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-10-24 01:30:56 +02:00
|
|
|
floatingActionButton: staticLeases == true
|
|
|
|
? FloatingActionButton(
|
|
|
|
onPressed: openAddStaticLease,
|
2023-01-25 19:55:34 +01:00
|
|
|
child: const Icon(Icons.add),
|
2022-10-24 01:30:56 +02:00
|
|
|
)
|
|
|
|
: null,
|
2022-10-15 02:25:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|