improve hit result handling
This commit is contained in:
@@ -37,6 +37,26 @@ extension HitResultJson on HitResult {
|
||||
|
||||
extension HitResultX on HitResult {
|
||||
Uri? tryGetLink() {
|
||||
const maxTitleLength = 2500;
|
||||
|
||||
final uri = switch (this) {
|
||||
UnknownHitResult(src: final src) => src,
|
||||
ImageHitResult(src: final src, title: final title) =>
|
||||
title.isEmpty ? (src.length > maxTitleLength ? 'image' : src) : title!,
|
||||
VideoHitResult(src: final src, title: final title) =>
|
||||
title.isEmpty ? src : title!,
|
||||
AudioHitResult(src: final src, title: final title) =>
|
||||
title.isEmpty ? src : title!,
|
||||
ImageSrcHitResult(uri: final uri) => uri,
|
||||
PhoneHitResult() => 'about:blank',
|
||||
EmailHitResult() => 'about:blank',
|
||||
GeoHitResult() => 'about:blank',
|
||||
};
|
||||
|
||||
return Uri.tryParse(uri);
|
||||
}
|
||||
|
||||
Uri? tryGetSource() {
|
||||
final uri = switch (this) {
|
||||
UnknownHitResult(src: final src) => src,
|
||||
ImageHitResult(src: final src) => src,
|
||||
@@ -67,7 +87,9 @@ extension HitResultX on HitResult {
|
||||
};
|
||||
}
|
||||
|
||||
bool get hasSrc => tryGetLink() != null;
|
||||
bool get hasSrc => tryGetSource() != null;
|
||||
|
||||
bool get hasLink => tryGetLink() != null;
|
||||
|
||||
bool isImage() {
|
||||
return (this is ImageHitResult || this is ImageSrcHitResult) && hasSrc;
|
||||
@@ -82,7 +104,8 @@ extension HitResultX on HitResult {
|
||||
}
|
||||
|
||||
bool isUri() {
|
||||
return (this is UnknownHitResult && hasSrc) || this is ImageSrcHitResult;
|
||||
return (this is UnknownHitResult && hasLink) ||
|
||||
(this is ImageSrcHitResult && hasLink);
|
||||
}
|
||||
|
||||
bool isHttpLink() {
|
||||
@@ -96,13 +119,13 @@ extension HitResultX on HitResult {
|
||||
|
||||
bool isIntent() {
|
||||
return this is UnknownHitResult &&
|
||||
hasSrc &&
|
||||
hasLink &&
|
||||
tryGetLink()?.scheme == 'intent';
|
||||
}
|
||||
|
||||
bool isMailto() {
|
||||
return this is UnknownHitResult &&
|
||||
hasSrc &&
|
||||
hasLink &&
|
||||
tryGetLink()?.scheme == 'mailto';
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class CopyImage extends HookConsumerWidget {
|
||||
title: const Text('Copy image'),
|
||||
onTap: () async {
|
||||
final currentTab = ref.read(selectedTabStateProvider);
|
||||
final url = hitResult.tryGetLink();
|
||||
final url = hitResult.tryGetSource();
|
||||
|
||||
if (currentTab != null && url != null) {
|
||||
await GeckoDownloadsService().copyInternetResource(
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class CopyImageLocation extends HookConsumerWidget {
|
||||
leading: const Icon(MdiIcons.imageMarker),
|
||||
title: const Text('Copy image location'),
|
||||
onTap: () async {
|
||||
await hitResult.tryGetLink().mapNotNull((link) async {
|
||||
await hitResult.tryGetSource().mapNotNull((link) async {
|
||||
await Clipboard.setData(ClipboardData(text: link.toString()));
|
||||
|
||||
if (context.mounted) {
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ class OpenImageInNewTab extends HookConsumerWidget {
|
||||
final tabId = await ref
|
||||
.read(tabRepositoryProvider.notifier)
|
||||
.addTab(
|
||||
url: hitResult.tryGetLink(),
|
||||
url: hitResult.tryGetSource(),
|
||||
parentId: currentTab?.id,
|
||||
selectTab: false,
|
||||
private: isPrivate,
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class SaveImage extends HookConsumerWidget {
|
||||
title: const Text('Save image'),
|
||||
onTap: () async {
|
||||
final currentTab = ref.read(selectedTabStateProvider);
|
||||
final url = hitResult.tryGetLink();
|
||||
final url = hitResult.tryGetSource();
|
||||
|
||||
if (currentTab != null && url != null) {
|
||||
await GeckoDownloadsService().requestDownload(
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class ShareImage extends HookConsumerWidget {
|
||||
title: const Text('Share image'),
|
||||
onTap: () async {
|
||||
final currentTab = ref.read(selectedTabStateProvider);
|
||||
final url = hitResult.tryGetLink();
|
||||
final url = hitResult.tryGetSource();
|
||||
|
||||
if (currentTab != null && url != null) {
|
||||
await GeckoDownloadsService().shareInternetResource(
|
||||
|
||||
Reference in New Issue
Block a user