2024-04-14 07:58:27 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math' as math;
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
2024-04-14 07:58:27 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-04-19 16:01:48 +00:00
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-04-14 07:58:27 +00:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/models/post.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-03 08:16:42 +00:00
|
|
|
import 'package:solian/utils/file.dart';
|
2024-05-10 15:17:01 +00:00
|
|
|
import 'package:solian/utils/services_url.dart';
|
2024-04-14 07:58:27 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-29 12:22:06 +00:00
|
|
|
import 'package:solian/widgets/exts.dart';
|
2024-04-14 07:58:27 +00:00
|
|
|
|
|
|
|
class AttachmentEditor extends StatefulWidget {
|
2024-04-19 11:36:03 +00:00
|
|
|
final String provider;
|
2024-04-14 07:58:27 +00:00
|
|
|
final List<Attachment> current;
|
|
|
|
final void Function(List<Attachment> data) onUpdate;
|
|
|
|
|
2024-04-19 11:36:03 +00:00
|
|
|
const AttachmentEditor({
|
|
|
|
super.key,
|
|
|
|
required this.provider,
|
|
|
|
required this.current,
|
|
|
|
required this.onUpdate,
|
|
|
|
});
|
2024-04-14 07:58:27 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<AttachmentEditor> createState() => _AttachmentEditorState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AttachmentEditorState extends State<AttachmentEditor> {
|
|
|
|
final _imagePicker = ImagePicker();
|
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
bool _isSubmitting = false;
|
2024-04-14 07:58:27 +00:00
|
|
|
|
|
|
|
List<Attachment> _attachments = List.empty(growable: true);
|
|
|
|
|
|
|
|
void viewAttachMethods(BuildContext context) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AttachmentEditorMethodPopup(
|
2024-05-02 16:09:33 +00:00
|
|
|
pickMedia: () => pickMediaToUpload(context),
|
|
|
|
pickFile: () => pickFileToUpload(context),
|
|
|
|
takeImage: () => takeMediaToUpload(context, false),
|
|
|
|
takeVideo: () => takeMediaToUpload(context, true),
|
2024-04-14 07:58:27 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
Future<void> pickMediaToUpload(BuildContext context) async {
|
2024-04-14 07:58:27 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
final medias = await _imagePicker.pickMultipleMedia();
|
|
|
|
if (medias.isEmpty) return;
|
2024-04-16 15:19:53 +00:00
|
|
|
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
bool isPopped = false;
|
|
|
|
for (final media in medias) {
|
|
|
|
final file = File(media.path);
|
2024-05-03 08:16:42 +00:00
|
|
|
final hashcode = await calculateFileSha256(file);
|
2024-05-02 16:09:33 +00:00
|
|
|
try {
|
|
|
|
await uploadAttachment(file, hashcode);
|
|
|
|
} catch (err) {
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
}
|
|
|
|
if (!isPopped && Navigator.canPop(context)) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
isPopped = true;
|
|
|
|
}
|
2024-04-16 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> pickFileToUpload(BuildContext context) async {
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);
|
|
|
|
if (result == null) return;
|
|
|
|
|
|
|
|
List<File> files = result.paths.map((path) => File(path!)).toList();
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
|
|
|
bool isPopped = false;
|
|
|
|
for (final file in files) {
|
2024-05-03 08:16:42 +00:00
|
|
|
final hashcode = await calculateFileSha256(file);
|
2024-05-02 16:09:33 +00:00
|
|
|
try {
|
|
|
|
await uploadAttachment(file, hashcode);
|
|
|
|
} catch (err) {
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
}
|
|
|
|
if (!isPopped && Navigator.canPop(context)) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
isPopped = true;
|
|
|
|
}
|
2024-04-16 15:19:53 +00:00
|
|
|
}
|
2024-05-02 16:09:33 +00:00
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
2024-04-16 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
Future<void> takeMediaToUpload(BuildContext context, bool isVideo) async {
|
2024-04-16 15:19:53 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
XFile? media;
|
|
|
|
if (isVideo) {
|
|
|
|
media = await _imagePicker.pickVideo(source: ImageSource.camera);
|
|
|
|
} else {
|
|
|
|
media = await _imagePicker.pickImage(source: ImageSource.camera);
|
|
|
|
}
|
|
|
|
if (media == null) return;
|
2024-04-14 07:58:27 +00:00
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
setState(() => _isSubmitting = true);
|
2024-04-14 07:58:27 +00:00
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
final file = File(media.path);
|
2024-05-03 08:16:42 +00:00
|
|
|
final hashcode = await calculateFileSha256(file);
|
2024-04-14 07:58:27 +00:00
|
|
|
|
|
|
|
if (Navigator.canPop(context)) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await uploadAttachment(file, hashcode);
|
|
|
|
} catch (err) {
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-04-14 07:58:27 +00:00
|
|
|
}
|
2024-05-02 16:09:33 +00:00
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
2024-04-14 07:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> uploadAttachment(File file, String hashcode) async {
|
|
|
|
final auth = context.read<AuthProvider>();
|
2024-04-19 11:36:03 +00:00
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
final req = MultipartRequest('POST', getRequestUri(widget.provider, '/api/attachments'));
|
2024-04-14 07:58:27 +00:00
|
|
|
req.files.add(await MultipartFile.fromPath('attachment', file.path));
|
|
|
|
req.fields['hashcode'] = hashcode;
|
|
|
|
|
|
|
|
var res = await auth.client!.send(req);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
var result = Attachment.fromJson(
|
2024-05-01 16:49:38 +00:00
|
|
|
jsonDecode(utf8.decode(await res.stream.toBytes()))['info'],
|
2024-04-14 07:58:27 +00:00
|
|
|
);
|
|
|
|
setState(() => _attachments.add(result));
|
|
|
|
widget.onUpdate(_attachments);
|
|
|
|
} else {
|
|
|
|
throw Exception(utf8.decode(await res.stream.toBytes()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
Future<void> disposeAttachment(BuildContext context, Attachment item, int index) async {
|
2024-04-14 07:58:27 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
|
2024-05-02 16:09:33 +00:00
|
|
|
final req = MultipartRequest('DELETE', getRequestUri(widget.provider, '/api/attachments/${item.id}'));
|
2024-04-14 07:58:27 +00:00
|
|
|
|
2024-04-15 15:08:32 +00:00
|
|
|
setState(() => _isSubmitting = true);
|
2024-04-14 07:58:27 +00:00
|
|
|
var res = await auth.client!.send(req);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
setState(() => _attachments.removeAt(index));
|
|
|
|
widget.onUpdate(_attachments);
|
|
|
|
} else {
|
|
|
|
final err = utf8.decode(await res.stream.toBytes());
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-04-14 07:58:27 +00:00
|
|
|
}
|
2024-04-15 15:08:32 +00:00
|
|
|
setState(() => _isSubmitting = false);
|
2024-04-14 07:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getFileName(Attachment item) {
|
|
|
|
return item.filename.replaceAll(RegExp(r'\.[^/.]+$'), '');
|
|
|
|
}
|
|
|
|
|
|
|
|
String getFileType(Attachment item) {
|
|
|
|
switch (item.type) {
|
|
|
|
case 1:
|
|
|
|
return 'Photo';
|
|
|
|
case 2:
|
|
|
|
return 'Video';
|
|
|
|
case 3:
|
|
|
|
return 'Audio';
|
|
|
|
default:
|
|
|
|
return 'Others';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String formatBytes(int bytes, {int decimals = 2}) {
|
|
|
|
if (bytes == 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
|
|
final dm = decimals < 0 ? 0 : decimals;
|
2024-05-02 16:09:33 +00:00
|
|
|
final sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
2024-04-14 07:58:27 +00:00
|
|
|
final i = (math.log(bytes) / math.log(k)).floor().toInt();
|
|
|
|
return '${(bytes / math.pow(k, i)).toStringAsFixed(dm)} ${sizes[i]}';
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_attachments = widget.current;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Container(
|
2024-05-02 16:09:33 +00:00
|
|
|
padding: const EdgeInsets.only(left: 8, right: 8, top: 20, bottom: 12),
|
2024-04-14 07:58:27 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
2024-04-14 10:38:44 +00:00
|
|
|
horizontal: 8,
|
|
|
|
vertical: 12,
|
2024-04-14 07:58:27 +00:00
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.attachment,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
FutureBuilder(
|
|
|
|
future: auth.isAuthorized(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData && snapshot.data == true) {
|
|
|
|
return TextButton(
|
2024-05-02 16:09:33 +00:00
|
|
|
onPressed: _isSubmitting ? null : () => viewAttachMethods(context),
|
2024-04-14 07:58:27 +00:00
|
|
|
style: TextButton.styleFrom(shape: const CircleBorder()),
|
|
|
|
child: const Icon(Icons.add_circle),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-05-02 16:09:33 +00:00
|
|
|
_isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
2024-04-14 07:58:27 +00:00
|
|
|
Expanded(
|
2024-05-02 16:09:33 +00:00
|
|
|
child: ListView.builder(
|
2024-04-14 07:58:27 +00:00
|
|
|
itemCount: _attachments.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
var element = _attachments[index];
|
|
|
|
return Container(
|
2024-05-02 16:09:33 +00:00
|
|
|
padding: const EdgeInsets.only(left: 16, right: 8, bottom: 16),
|
2024-04-14 07:58:27 +00:00
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
getFileName(element),
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
|
|
),
|
|
|
|
Text(
|
2024-05-01 16:49:38 +00:00
|
|
|
'${getFileType(element)} · ${formatBytes(element.filesize)}',
|
2024-04-14 07:58:27 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
shape: const CircleBorder(),
|
|
|
|
foregroundColor: Colors.red,
|
|
|
|
),
|
|
|
|
child: const Icon(Icons.delete),
|
2024-05-02 16:09:33 +00:00
|
|
|
onPressed: () => disposeAttachment(context, element, index),
|
2024-04-14 07:58:27 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AttachmentEditorMethodPopup extends StatelessWidget {
|
2024-05-02 16:09:33 +00:00
|
|
|
final Function pickMedia;
|
|
|
|
final Function pickFile;
|
2024-04-16 15:19:53 +00:00
|
|
|
final Function takeImage;
|
|
|
|
final Function takeVideo;
|
2024-04-14 07:58:27 +00:00
|
|
|
|
2024-04-16 15:19:53 +00:00
|
|
|
const AttachmentEditorMethodPopup({
|
|
|
|
super.key,
|
2024-05-02 16:09:33 +00:00
|
|
|
required this.pickMedia,
|
|
|
|
required this.pickFile,
|
2024-04-16 15:19:53 +00:00
|
|
|
required this.takeImage,
|
|
|
|
required this.takeVideo,
|
|
|
|
});
|
2024-04-14 07:58:27 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
|
|
|
height: 320,
|
|
|
|
padding: const EdgeInsets.only(left: 8, right: 8, top: 20),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 8.0,
|
|
|
|
vertical: 12.0,
|
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.attachmentAdd,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
2024-04-19 11:36:03 +00:00
|
|
|
child: GridView.count(
|
|
|
|
primary: false,
|
|
|
|
crossAxisSpacing: 10,
|
|
|
|
mainAxisSpacing: 10,
|
|
|
|
crossAxisCount: 4,
|
2024-04-14 07:58:27 +00:00
|
|
|
children: [
|
|
|
|
InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
2024-05-02 16:09:33 +00:00
|
|
|
onTap: () => pickMedia(),
|
2024-04-19 11:36:03 +00:00
|
|
|
child: Center(
|
2024-04-14 07:58:27 +00:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-05-02 16:09:33 +00:00
|
|
|
const Icon(Icons.photo_library, color: Colors.indigo),
|
2024-04-14 07:58:27 +00:00
|
|
|
const SizedBox(height: 8),
|
2024-05-02 16:09:33 +00:00
|
|
|
Text(AppLocalizations.of(context)!.pickMedia),
|
2024-04-14 07:58:27 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-16 15:19:53 +00:00
|
|
|
InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
2024-05-02 16:09:33 +00:00
|
|
|
onTap: () => pickFile(),
|
2024-04-19 11:36:03 +00:00
|
|
|
child: Center(
|
2024-04-16 15:19:53 +00:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-05-02 16:09:33 +00:00
|
|
|
const Icon(Icons.file_present, color: Colors.amber),
|
2024-04-16 15:19:53 +00:00
|
|
|
const SizedBox(height: 8),
|
2024-05-02 16:09:33 +00:00
|
|
|
Text(AppLocalizations.of(context)!.pickFile),
|
2024-04-16 15:19:53 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
2024-05-02 16:09:33 +00:00
|
|
|
onTap: () => takeImage(),
|
2024-04-19 11:36:03 +00:00
|
|
|
child: Center(
|
2024-04-16 15:19:53 +00:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-05-02 16:09:33 +00:00
|
|
|
const Icon(Icons.camera_alt, color: Colors.teal),
|
2024-04-16 15:19:53 +00:00
|
|
|
const SizedBox(height: 8),
|
2024-05-02 16:09:33 +00:00
|
|
|
Text(AppLocalizations.of(context)!.takePhoto),
|
2024-04-16 15:19:53 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
onTap: () => takeVideo(),
|
2024-04-19 11:36:03 +00:00
|
|
|
child: Center(
|
2024-04-16 15:19:53 +00:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-05-02 16:09:33 +00:00
|
|
|
const Icon(Icons.movie, color: Colors.blue),
|
2024-04-16 15:19:53 +00:00
|
|
|
const SizedBox(height: 8),
|
|
|
|
Text(AppLocalizations.of(context)!.takeVideo),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-14 07:58:27 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|