-
Notifications
You must be signed in to change notification settings - Fork 19
/
external_share_service_impl.dart
138 lines (123 loc) · 3.83 KB
/
external_share_service_impl.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import 'package:easy_dispose/easy_dispose.dart';
import 'package:fedi/app/share/external/external_share_model.dart';
import 'package:fedi/app/share/external/external_share_service.dart';
import 'package:fedi/file/temp/temp_file_helper.dart';
import 'package:fedi/file/temp/temp_file_model.dart';
import 'package:mime_type/mime_type.dart';
import 'package:share_plus/share_plus.dart';
class ExternalShareService extends DisposableOwner
implements IExternalShareService {
@override
Future share({
required String popupTitle,
required String? text,
required List<ShareUrlFile>? urlFiles,
}) async {
assert(text?.isNotEmpty == true || urlFiles?.isNotEmpty == true);
if (urlFiles?.isNotEmpty == true) {
if (text?.isNotEmpty == true) {
shareSeveralUrlFilesAsLinksWithText(
popupTitle: popupTitle,
text: text,
urlFiles: urlFiles!,
);
} else {
if (urlFiles!.length == 1) {
await shareSingleUrlFileAsBytesWithoutText(
popupTitle: popupTitle,
urlFile: urlFiles[0],
);
} else {
await shareSeveralUrlFilesAsBytesWithoutText(
urlFiles,
text,
popupTitle,
);
}
}
} else {
shareTextOnly(popupTitle, text!);
}
}
Future<String?> shareSeveralUrlFilesAsBytesWithoutText(List<ShareUrlFile> urlFiles,
String? text,
String popupTitle,) async {
var urlFilesPossibleToShareAsBytes =
urlFiles.where((urlFile) => isPossibleToShareAsBytes(urlFile.url));
text = text ?? '';
// several files sharing not supported with text
if (urlFilesPossibleToShareAsBytes.length == 1) {
var firstUrlFileToShareAsFiles = urlFilesPossibleToShareAsBytes.first;
var url = firstUrlFileToShareAsFiles.url;
var nonFirstUrlFileToShareAsBytes = urlFiles
.where((attachment) => attachment != firstUrlFileToShareAsFiles);
if (nonFirstUrlFileToShareAsBytes.isNotEmpty) {
text += '[${nonFirstUrlFileToShareAsBytes.map(
(attachment) => attachment.url,
).join(', ')}]';
}
var file = await TempFileHelper.downloadFileToTempFolder(
request: DownloadTempFileRequest(
url: url,
filenameWithExtension:
firstUrlFileToShareAsFiles.filenameWithExtension,
),
);
await Share.shareFiles(
[
file.path,
],
subject: popupTitle,
text: text,
);
await file.delete();
} else {
// share everything as text
text +=
'[${urlFilesPossibleToShareAsBytes.map((urlFile) => urlFile.url).join(', ')}]';
}
return text;
}
void shareTextOnly(String popupTitle, String text) {
Share.share(
text,
);
}
Future shareSingleUrlFileAsBytesWithoutText({
required String popupTitle,
required ShareUrlFile urlFile,
}) async {
var url = urlFile.url;
var filename = urlFile.filenameWithExtension;
// other types too big, for example video
// todo: improve sharing other media types
if (isPossibleToShareAsBytes(url)) {
var file = await TempFileHelper.downloadFileToTempFolder(
request: DownloadTempFileRequest(
url: url,
filenameWithExtension: filename,
),
);
await Share.shareFiles(
[
file.path,
],
subject: popupTitle,
);
await file.delete();
} else {
shareTextOnly(popupTitle, url);
}
}
void shareSeveralUrlFilesAsLinksWithText({
required String popupTitle,
required String? text,
required List<ShareUrlFile> urlFiles,
}) {
shareTextOnly(
popupTitle,
'${text ?? ''} [${urlFiles.map((urlFile) => urlFile.url).join(', ')}]',
);
}
bool isPossibleToShareAsBytes(String? url) => mime(url)!.contains('image');
}