2024-07-26 08:05:22 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-06-07 17:22:55 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/models/models.dart';
|
2024-07-26 08:05:22 +08:00
|
|
|
import 'package:fl_clash/plugins/app.dart';
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
2024-07-26 08:05:22 +08:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
|
|
|
|
class EditProfile extends StatefulWidget {
|
|
|
|
|
final Profile profile;
|
|
|
|
|
final BuildContext context;
|
|
|
|
|
|
|
|
|
|
const EditProfile({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.context,
|
|
|
|
|
required this.profile,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<EditProfile> createState() => _EditProfileState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _EditProfileState extends State<EditProfile> {
|
|
|
|
|
late TextEditingController labelController;
|
|
|
|
|
late TextEditingController urlController;
|
|
|
|
|
late TextEditingController autoUpdateDurationController;
|
|
|
|
|
late bool autoUpdate;
|
|
|
|
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
2024-07-26 08:05:22 +08:00
|
|
|
final fileInfoNotifier = ValueNotifier<FileInfo?>(null);
|
|
|
|
|
Uint8List? fileData;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
labelController = TextEditingController(text: widget.profile.label);
|
|
|
|
|
urlController = TextEditingController(text: widget.profile.url);
|
|
|
|
|
autoUpdate = widget.profile.autoUpdate;
|
|
|
|
|
autoUpdateDurationController = TextEditingController(
|
|
|
|
|
text: widget.profile.autoUpdateDuration.inMinutes.toString(),
|
|
|
|
|
);
|
2024-07-26 08:05:22 +08:00
|
|
|
appPath.getProfilePath(widget.profile.id).then((path) async {
|
|
|
|
|
if (path == null) return;
|
|
|
|
|
fileInfoNotifier.value = await _getFileInfo(path);
|
|
|
|
|
});
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-26 08:05:22 +08:00
|
|
|
_handleConfirm() async {
|
2024-04-30 23:38:49 +08:00
|
|
|
if (!_formKey.currentState!.validate()) return;
|
2024-05-11 17:02:34 +08:00
|
|
|
final config = widget.context.read<Config>();
|
2024-07-26 08:05:22 +08:00
|
|
|
var profile = widget.profile.copyWith(
|
2024-06-19 13:13:31 +08:00
|
|
|
url: urlController.text,
|
|
|
|
|
label: labelController.text,
|
|
|
|
|
autoUpdate: autoUpdate,
|
|
|
|
|
autoUpdateDuration: Duration(
|
|
|
|
|
minutes: int.parse(
|
|
|
|
|
autoUpdateDurationController.text,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
final hasUpdate = widget.profile.url != profile.url;
|
2024-07-26 08:05:22 +08:00
|
|
|
if (fileData != null) {
|
|
|
|
|
config.setProfile(await profile.saveFile(fileData!));
|
|
|
|
|
} else {
|
|
|
|
|
config.setProfile(profile);
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
if (hasUpdate) {
|
2024-06-19 13:13:31 +08:00
|
|
|
globalState.homeScaffoldKey.currentState?.loadingRun(
|
|
|
|
|
() async {
|
|
|
|
|
if (hasUpdate) {
|
|
|
|
|
await globalState.appController.updateProfile(profile);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-07-26 08:05:22 +08:00
|
|
|
if (mounted) {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_setAutoUpdate(bool value) {
|
|
|
|
|
if (autoUpdate == value) return;
|
|
|
|
|
setState(() {
|
|
|
|
|
autoUpdate = value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 23:51:00 +08:00
|
|
|
Future<FileInfo?> _getFileInfo(path) async {
|
2024-07-26 08:05:22 +08:00
|
|
|
final file = File(path);
|
2024-08-01 23:51:00 +08:00
|
|
|
if (!await file.exists()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-07-26 08:05:22 +08:00
|
|
|
final lastModified = await file.lastModified();
|
|
|
|
|
final size = await file.length();
|
|
|
|
|
return FileInfo(
|
|
|
|
|
size: size,
|
|
|
|
|
lastModified: lastModified,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_editProfileFile() async {
|
|
|
|
|
final profilePath = await appPath.getProfilePath(widget.profile.id);
|
|
|
|
|
if (profilePath == null) return;
|
|
|
|
|
globalState.safeRun(() async {
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
await app?.openFile(
|
|
|
|
|
profilePath,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await launchUrl(
|
|
|
|
|
Uri.file(
|
|
|
|
|
profilePath,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_uploadProfileFile() async {
|
2024-08-04 08:21:14 +08:00
|
|
|
final platformFile = await globalState.safeRun(picker.pickerFile);
|
2024-07-26 08:05:22 +08:00
|
|
|
if (platformFile?.bytes == null) return;
|
|
|
|
|
fileData = platformFile?.bytes;
|
|
|
|
|
fileInfoNotifier.value = fileInfoNotifier.value?.copyWith(
|
|
|
|
|
size: fileData?.length ?? 0,
|
|
|
|
|
lastModified: DateTime.now(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final items = [
|
|
|
|
|
ListItem(
|
|
|
|
|
title: TextFormField(
|
|
|
|
|
controller: labelController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
|
labelText: appLocalizations.name,
|
|
|
|
|
),
|
|
|
|
|
validator: (String? value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return appLocalizations.profileNameNullValidationDesc;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-06-19 13:13:31 +08:00
|
|
|
if (widget.profile.type == ProfileType.url) ...[
|
2024-04-30 23:38:49 +08:00
|
|
|
ListItem(
|
|
|
|
|
title: TextFormField(
|
|
|
|
|
controller: urlController,
|
2024-06-27 01:14:45 +08:00
|
|
|
maxLines: 5,
|
|
|
|
|
minLines: 1,
|
2024-04-30 23:38:49 +08:00
|
|
|
decoration: InputDecoration(
|
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
|
labelText: appLocalizations.url,
|
|
|
|
|
),
|
|
|
|
|
validator: (String? value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return appLocalizations.profileUrlNullValidationDesc;
|
|
|
|
|
}
|
|
|
|
|
if (!value.isUrl) {
|
|
|
|
|
return appLocalizations.profileUrlInvalidValidationDesc;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
ListItem.switchItem(
|
|
|
|
|
title: Text(appLocalizations.autoUpdate),
|
|
|
|
|
delegate: SwitchDelegate<bool>(
|
|
|
|
|
value: autoUpdate,
|
|
|
|
|
onChanged: _setAutoUpdate,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (autoUpdate)
|
|
|
|
|
ListItem(
|
|
|
|
|
title: TextFormField(
|
|
|
|
|
controller: autoUpdateDurationController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
|
labelText: appLocalizations.autoUpdateInterval,
|
|
|
|
|
),
|
|
|
|
|
validator: (String? value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return appLocalizations
|
|
|
|
|
.profileAutoUpdateIntervalNullValidationDesc;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
int.parse(value);
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return appLocalizations
|
|
|
|
|
.profileAutoUpdateIntervalInvalidValidationDesc;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-07-26 08:05:22 +08:00
|
|
|
],
|
2024-08-01 23:51:00 +08:00
|
|
|
ValueListenableBuilder<FileInfo?>(
|
|
|
|
|
valueListenable: fileInfoNotifier,
|
|
|
|
|
builder: (_, fileInfo, __) {
|
|
|
|
|
return FadeBox(
|
|
|
|
|
child: fileInfo == null
|
|
|
|
|
? Container()
|
|
|
|
|
: ListItem(
|
|
|
|
|
title: Text(
|
|
|
|
|
appLocalizations.profile,
|
|
|
|
|
),
|
|
|
|
|
subtitle: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 4,
|
|
|
|
|
),
|
|
|
|
|
Text(
|
|
|
|
|
fileInfo.desc,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 8,
|
|
|
|
|
),
|
|
|
|
|
Wrap(
|
|
|
|
|
runSpacing: 6,
|
|
|
|
|
spacing: 12,
|
|
|
|
|
children: [
|
|
|
|
|
CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.edit),
|
|
|
|
|
label: appLocalizations.edit,
|
|
|
|
|
onPressed: _editProfileFile,
|
|
|
|
|
),
|
|
|
|
|
CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.upload),
|
|
|
|
|
label: appLocalizations.upload,
|
|
|
|
|
onPressed: _uploadProfileFile,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-07-26 08:05:22 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
];
|
|
|
|
|
return FloatLayout(
|
|
|
|
|
floatingWidget: FloatWrapper(
|
|
|
|
|
child: FloatingActionButton.extended(
|
|
|
|
|
heroTag: null,
|
|
|
|
|
onPressed: _handleConfirm,
|
|
|
|
|
label: Text(appLocalizations.save),
|
|
|
|
|
icon: const Icon(Icons.save),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Form(
|
|
|
|
|
key: _formKey,
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
vertical: 16,
|
|
|
|
|
),
|
2024-08-04 08:21:14 +08:00
|
|
|
child: ListView.separated(
|
|
|
|
|
padding: kMaterialListPadding.copyWith(
|
|
|
|
|
bottom: 72,
|
|
|
|
|
),
|
|
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
return items[index];
|
|
|
|
|
},
|
|
|
|
|
separatorBuilder: (_, __) {
|
|
|
|
|
return const SizedBox(
|
|
|
|
|
height: 24,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
},
|
2024-08-04 08:21:14 +08:00
|
|
|
itemCount: items.length,
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|