Skip to content

Commit

Permalink
sending links is now available
Browse files Browse the repository at this point in the history
  • Loading branch information
RamyBouchareb25 committed Sep 12, 2023
1 parent 11842a1 commit 7a0989d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
76 changes: 67 additions & 9 deletions lib/Pages/conversation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:chat_app/models/user_model.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_functions/cloud_functions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:chat_app/models/global.dart';
import 'package:get/get.dart';
Expand All @@ -18,6 +19,7 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';
import 'package:image_picker_plus/image_picker_plus.dart';
import 'package:video_player/video_player.dart';
import 'package:url_launcher/url_launcher.dart';

class Conversation extends StatefulWidget {
const Conversation(
Expand Down Expand Up @@ -58,6 +60,13 @@ class _ConversationState extends State<Conversation> {
return await resp;
}

Future<void> _launchURL(String url) async {
var parsedUrl = Uri.parse(url);
if (!await launchUrl(parsedUrl)) {
throw Exception('Could not launch $parsedUrl');
}
}

Future<void> sendImage(ImageSource source) async {
ImagePickerPlus picker = ImagePickerPlus(context);
SelectedImagesDetails? images = await picker.pickBoth(
Expand Down Expand Up @@ -104,7 +113,63 @@ class _ConversationState extends State<Conversation> {
.update({"LastMsgTime": date});
}

List<TextSpan> extractURLAndText(
{required String text, required Color color}) {
List<TextSpan> spans = [];
const urlPattern = r'(https?|ftp):\/\/[^\s/$.?#].[^\s]*$';
final regex = RegExp(urlPattern, caseSensitive: false);

String url = '';
String linkText = text;
int currentIndex = 0;
for (var match in regex.allMatches(text)) {
final preMatch = text.substring(currentIndex, match.start);
if (preMatch.isNotEmpty) {
spans.add(
TextSpan(
text: preMatch,
style: TextStyle(color: color, fontWeight: FontWeight.bold)),
);
}
// Extract the URL and text
final url = match.group(0)!;
final linkText = url;

// Create a clickable span
spans.add(
TextSpan(
text: linkText,
style: TextStyle(
color: color,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
_launchURL(url);
},
),
);

currentIndex = match.end;
}
// Add any remaining non-URL text
final remainingText = text.substring(currentIndex);
if (remainingText.isNotEmpty) {
spans.add(
TextSpan(
text: remainingText,
style: TextStyle(color: color, fontWeight: FontWeight.bold)),
);
}

return spans;
}

Widget renderText(MessageData data, BuildContext context) {
var spans = extractURLAndText(
text: data.message!,
color: data.senderId != Auth().currentUser!.uid ? black : Colors.white,
);
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
decoration: BoxDecoration(
Expand All @@ -118,14 +183,7 @@ class _ConversationState extends State<Conversation> {
bottomLeft: const Radius.circular(20),
bottomRight: const Radius.circular(20),
)),
child: Text(
data.message ?? "error fetching message",
style: TextStyle(
fontWeight: FontWeight.bold,
color:
data.senderId == Auth().currentUser!.uid ? Colors.white : black,
),
),
child: RichText(text: TextSpan(children: spans)),
);
}

Expand All @@ -145,7 +203,7 @@ class _ConversationState extends State<Conversation> {
if (!controller!.value.isInitialized) {
controller.initialize().then((_) {
if (context.mounted) {
setState(() {});
setState(() {});
}
});
controller.setLooping(true);
Expand Down
6 changes: 6 additions & 0 deletions lib/Pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class _HomeState extends State<Home> {
.get();
}

Future<void> constantRefresh() async {
await _onRefresh();
constantRefresh();
}

Stream<QuerySnapshot<Map<String, dynamic>>> _getMessages(
{required dynamic docId, required bool isEmpty}) {
return isEmpty
Expand Down Expand Up @@ -94,6 +99,7 @@ class _HomeState extends State<Home> {
List<MessageData> lastMessages = [];
@override
void initState() {
constantRefresh();
_getUsers().then((value) {
_getRooms().then((snapshot) {
for (var index = 0; index < snapshot.docs.length; index++) {
Expand Down

0 comments on commit 7a0989d

Please sign in to comment.