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