Remove flutter chat #26
3 changed files with 66 additions and 43 deletions
custom link previews
commit
881c76359b
|
|
@ -4,13 +4,14 @@ import "package:http/http.dart";
|
|||
import "package:nexus/controllers/client_state_controller.dart";
|
||||
import "package:nexus/controllers/header_controller.dart";
|
||||
import "package:nexus/helpers/extensions/mxc_to_https.dart";
|
||||
import "package:nexus/models/open_graph_data.dart";
|
||||
|
||||
class UrlPreviewController extends AsyncNotifier<LinkPreviewData?> {
|
||||
class UrlPreviewController extends AsyncNotifier<OpenGraphData?> {
|
||||
final String link;
|
||||
UrlPreviewController(this.link);
|
||||
|
||||
@override
|
||||
Future<LinkPreviewData?> build() async {
|
||||
Future<OpenGraphData?> build() async {
|
||||
final homeserver = ref.watch(ClientStateController.provider)?.homeserverUrl;
|
||||
|
||||
if (homeserver != null && !link.contains("matrix.to")) {
|
||||
|
|
@ -23,28 +24,15 @@ class UrlPreviewController extends AsyncNotifier<LinkPreviewData?> {
|
|||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final decodedValue = json.decode(response.body);
|
||||
final mxc = decodedValue["og:image"];
|
||||
final decodedValue = json.decode(response.body) as Map?;
|
||||
if (decodedValue?.isNotEmpty == true) return null;
|
||||
|
||||
final mxc = decodedValue!["og:image"];
|
||||
final image = mxc == null
|
||||
? null
|
||||
: Uri.tryParse(mxc)?.mxcToHttps(homeserver);
|
||||
|
||||
return LinkPreviewData(
|
||||
link: link,
|
||||
title: decodedValue["og:title"],
|
||||
description: decodedValue["og:description"],
|
||||
image: image == null
|
||||
? null
|
||||
: ImagePreviewData(
|
||||
url: image.toString(),
|
||||
width:
|
||||
(decodedValue["og:image:width"] as int?)?.toDouble() ??
|
||||
0,
|
||||
height:
|
||||
(decodedValue["og:image:height"] as int?)?.toDouble() ??
|
||||
0,
|
||||
),
|
||||
);
|
||||
return OpenGraphData.fromJson({...decodedValue, "og:image": image});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -53,7 +41,7 @@ class UrlPreviewController extends AsyncNotifier<LinkPreviewData?> {
|
|||
}
|
||||
|
||||
static final provider = AsyncNotifierProvider.autoDispose
|
||||
.family<UrlPreviewController, LinkPreviewData?, String>(
|
||||
.family<UrlPreviewController, OpenGraphData?, String>(
|
||||
UrlPreviewController.new,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
17
lib/models/open_graph_data.dart
Normal file
17
lib/models/open_graph_data.dart
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import "package:freezed_annotation/freezed_annotation.dart";
|
||||
part "open_graph_data.freezed.dart";
|
||||
part "open_graph_data.g.dart";
|
||||
|
||||
@freezed
|
||||
abstract class OpenGraphData with _$OpenGraphData {
|
||||
const factory OpenGraphData({
|
||||
@JsonKey(name: "og:title") required String? title,
|
||||
@JsonKey(name: "og:description") required String? description,
|
||||
@JsonKey(name: "og:image") required String? imageUrl,
|
||||
@JsonKey(name: "og:image:width") required double? width,
|
||||
@JsonKey(name: "og:image:height") required double? height,
|
||||
}) = _OpenGraphData;
|
||||
|
||||
factory OpenGraphData.fromJson(Map<String, Object?> json) =>
|
||||
_$OpenGraphDataFromJson(json);
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
import "package:cross_cache/cross_cache.dart";
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_link_previewer/flutter_link_previewer.dart";
|
||||
import "package:flutter_linkify/flutter_linkify.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/cross_cache_controller.dart";
|
||||
|
|
@ -109,30 +108,49 @@ class TextMessageWrapper extends ConsumerWidget {
|
|||
loading: SizedBox.shrink,
|
||||
data: (preview) => preview == null
|
||||
? SizedBox.shrink()
|
||||
: LinkPreview(
|
||||
onTap: (url) => ref
|
||||
: InkWell(
|
||||
onTap: () => ref
|
||||
.watch(LaunchHelper.provider)
|
||||
.launchUrl(Uri.parse(url)),
|
||||
imageBuilder: (url) => Image(
|
||||
image: CachedNetworkImage(
|
||||
url,
|
||||
ref.watch(CrossCacheController.provider),
|
||||
headers: ref.headers,
|
||||
.launchUrl(Uri.parse(link)),
|
||||
child: Card(
|
||||
child: Column(
|
||||
children: [
|
||||
if (preview.title != null)
|
||||
Text(
|
||||
preview.title!,
|
||||
style: theme.textTheme.labelLarge,
|
||||
),
|
||||
if (preview.description != null)
|
||||
Text(preview.description!),
|
||||
if (preview.imageUrl != null)
|
||||
Image(
|
||||
errorBuilder: (_, _, _) =>
|
||||
SizedBox.shrink(),
|
||||
width: preview.width,
|
||||
height: preview.height,
|
||||
image: CachedNetworkImage(
|
||||
preview.imageUrl!,
|
||||
ref.watch(
|
||||
CrossCacheController.provider,
|
||||
),
|
||||
headers: ref.headers,
|
||||
),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
],
|
||||
),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, _, _) => SizedBox.shrink(),
|
||||
// text: link,
|
||||
// backgroundColor: isSentByMe
|
||||
// ? colorScheme.inversePrimary
|
||||
// : colorScheme.surfaceContainerLow,
|
||||
// outsidePadding: EdgeInsets.only(top: 4),
|
||||
// insidePadding: EdgeInsets.symmetric(
|
||||
// vertical: 8,
|
||||
// horizontal: 16,
|
||||
// ),
|
||||
// linkPreviewData: preview,
|
||||
// onLinkPreviewDataFetched: (_) => null,
|
||||
),
|
||||
text: link,
|
||||
backgroundColor: isSentByMe
|
||||
? colorScheme.inversePrimary
|
||||
: colorScheme.surfaceContainerLow,
|
||||
outsidePadding: EdgeInsets.only(top: 4),
|
||||
insidePadding: EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
linkPreviewData: preview,
|
||||
onLinkPreviewDataFetched: (_) => null,
|
||||
),
|
||||
),
|
||||
?extra,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue