2022-10-07 01:12:48 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2023-05-01 21:34:00 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-09 17:45:18 +02:00
|
|
|
import 'package:animations/animations.dart';
|
2022-10-08 22:16:35 +02:00
|
|
|
import 'package:flutter/rendering.dart';
|
2023-05-01 21:34:00 +02:00
|
|
|
import 'package:flutter_split_view/flutter_split_view.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2022-11-04 01:15:45 +01:00
|
|
|
import 'package:adguard_home_manager/screens/clients/client_screen.dart';
|
2023-05-01 21:34:00 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/added_client_tile.dart';
|
2022-10-08 23:04:28 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/remove_client_modal.dart';
|
2022-10-07 01:22:18 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/fab.dart';
|
2022-10-07 18:16:22 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/options_modal.dart';
|
2023-04-06 18:10:23 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/tab_content_list.dart';
|
2022-10-07 01:22:18 +02:00
|
|
|
|
2022-10-09 20:03:17 +02:00
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
2023-05-24 13:51:22 +02:00
|
|
|
import 'package:adguard_home_manager/providers/status_provider.dart';
|
2023-05-24 14:16:53 +02:00
|
|
|
import 'package:adguard_home_manager/providers/clients_provider.dart';
|
2023-04-06 18:10:23 +02:00
|
|
|
import 'package:adguard_home_manager/constants/enums.dart';
|
2022-10-07 17:08:23 +02:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:adguard_home_manager/models/clients.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
class AddedList extends StatefulWidget {
|
|
|
|
final ScrollController scrollController;
|
2022-10-07 01:12:48 +02:00
|
|
|
final List<Client> data;
|
2023-05-01 21:34:00 +02:00
|
|
|
final void Function(Client) onClientSelected;
|
|
|
|
final Client? selectedClient;
|
|
|
|
final bool splitView;
|
2022-10-07 01:12:48 +02:00
|
|
|
|
|
|
|
const AddedList({
|
|
|
|
Key? key,
|
2022-10-08 22:16:35 +02:00
|
|
|
required this.scrollController,
|
2022-10-07 01:12:48 +02:00
|
|
|
required this.data,
|
2023-05-01 21:34:00 +02:00
|
|
|
required this.onClientSelected,
|
|
|
|
this.selectedClient,
|
|
|
|
required this.splitView
|
2022-10-07 01:12:48 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
@override
|
|
|
|
State<AddedList> createState() => _AddedListState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddedListState extends State<AddedList> {
|
|
|
|
late bool isVisible;
|
|
|
|
|
|
|
|
@override
|
|
|
|
initState(){
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
isVisible = true;
|
|
|
|
widget.scrollController.addListener(() {
|
|
|
|
if (widget.scrollController.position.userScrollDirection == ScrollDirection.reverse) {
|
|
|
|
if (mounted && isVisible == true) {
|
|
|
|
setState(() => isVisible = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (widget.scrollController.position.userScrollDirection == ScrollDirection.forward) {
|
|
|
|
if (mounted && isVisible == false) {
|
|
|
|
setState(() => isVisible = true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-07 01:12:48 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-24 13:51:22 +02:00
|
|
|
final statusProvider = Provider.of<StatusProvider>(context);
|
2023-05-24 14:16:53 +02:00
|
|
|
final clientsProvider = Provider.of<ClientsProvider>(context);
|
2022-10-07 01:12:48 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
2023-05-01 21:34:00 +02:00
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
void confirmEditClient(Client client) async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.addingClient);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await clientsProvider.editClient(client);
|
2022-10-07 17:08:23 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result == true) {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientUpdatedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-07 17:08:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientNotUpdated,
|
|
|
|
color: Colors.red
|
2022-10-07 17:08:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
void deleteClient(Client client) async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.removingClient);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await clientsProvider.deleteClient(client);
|
2022-10-07 18:16:22 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result == true) {
|
2023-05-01 21:34:00 +02:00
|
|
|
if (widget.splitView == true) {
|
|
|
|
SplitView.of(context).popUntil(0);
|
|
|
|
}
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientDeletedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-07 18:16:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientNotDeleted,
|
|
|
|
color: Colors.red
|
2022-10-07 18:16:22 +02:00
|
|
|
);
|
|
|
|
}
|
2023-05-25 15:06:21 +02:00
|
|
|
}
|
2022-10-07 18:16:22 +02:00
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
void openClientModal(Client client) {
|
2023-05-01 21:34:00 +02:00
|
|
|
if (width > 900 || !(Platform.isAndroid | Platform.isIOS)) {
|
|
|
|
showDialog(
|
|
|
|
barrierDismissible: false,
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) => ClientScreen(
|
|
|
|
onConfirm: confirmEditClient,
|
2023-05-24 13:51:22 +02:00
|
|
|
serverVersion: statusProvider.serverStatus!.serverVersion,
|
2023-05-01 21:34:00 +02:00
|
|
|
onDelete: deleteClient,
|
|
|
|
client: client,
|
|
|
|
dialog: true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Navigator.push(context, MaterialPageRoute(
|
|
|
|
fullscreenDialog: true,
|
|
|
|
builder: (BuildContext context) => ClientScreen(
|
|
|
|
onConfirm: confirmEditClient,
|
2023-05-24 13:51:22 +02:00
|
|
|
serverVersion: statusProvider.serverStatus!.serverVersion,
|
2023-05-01 21:34:00 +02:00
|
|
|
onDelete: deleteClient,
|
|
|
|
client: client,
|
|
|
|
dialog: false,
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
2022-10-07 17:08:23 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
void openDeleteModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
2022-10-08 23:04:28 +02:00
|
|
|
builder: (ctx) => RemoveClientModal(
|
2022-10-07 18:16:22 +02:00
|
|
|
onConfirm: () => deleteClient(client)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void openOptionsModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
|
|
|
builder: (ctx) => OptionsModal(
|
|
|
|
onDelete: () => openDeleteModal(client),
|
|
|
|
onEdit: () => openClientModal(client),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:29:46 +02:00
|
|
|
return CustomTabContentList(
|
2023-05-01 21:34:00 +02:00
|
|
|
noSliver: !(Platform.isAndroid || Platform.isIOS),
|
|
|
|
listPadding: widget.splitView == true
|
|
|
|
? const EdgeInsets.only(top: 8)
|
|
|
|
: null,
|
2023-04-06 18:29:46 +02:00
|
|
|
loadingGenerator: () => SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.loadingStatus,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
itemsCount: widget.data.length,
|
2023-05-01 21:34:00 +02:00
|
|
|
contentWidget: (index) => AddedClientTile(
|
|
|
|
selectedClient: widget.selectedClient,
|
|
|
|
client: widget.data[index],
|
|
|
|
onTap: widget.onClientSelected,
|
|
|
|
onLongPress: openOptionsModal,
|
|
|
|
onEdit: openClientModal,
|
|
|
|
splitView: widget.splitView,
|
2023-05-24 13:51:22 +02:00
|
|
|
serverVersion: statusProvider.serverStatus!.serverVersion,
|
2023-05-01 21:34:00 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
noData: SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2023-05-04 01:45:08 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.noClientsList,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
),
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(height: 30),
|
|
|
|
TextButton.icon(
|
2023-05-25 15:06:21 +02:00
|
|
|
onPressed: () => clientsProvider.fetchClients(updateLoading: true),
|
2023-04-06 18:29:46 +02:00
|
|
|
icon: const Icon(Icons.refresh_rounded),
|
|
|
|
label: Text(AppLocalizations.of(context)!.refresh),
|
|
|
|
)
|
|
|
|
],
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
),
|
|
|
|
errorGenerator: () => SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Icon(
|
|
|
|
Icons.error,
|
|
|
|
color: Colors.red,
|
|
|
|
size: 50,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.errorLoadServerStatus,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2023-05-25 15:06:21 +02:00
|
|
|
loadStatus: statusProvider.loadStatus == LoadStatus.loading || clientsProvider.loadStatus == LoadStatus.loading
|
|
|
|
? LoadStatus.loading
|
|
|
|
: clientsProvider.loadStatus,
|
|
|
|
onRefresh: () => clientsProvider.fetchClients(updateLoading: false),
|
2023-04-06 18:29:46 +02:00
|
|
|
fab: const ClientsFab(),
|
|
|
|
fabVisible: isVisible,
|
2023-04-06 18:10:23 +02:00
|
|
|
);
|
2022-10-07 01:12:48 +02:00
|
|
|
}
|
|
|
|
}
|