Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

2 changes: 2 additions & 0 deletions lib/classes/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Global extends ChangeNotifier {
static Map<String, dynamic> cache = {};
static final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

static var profileNameStream;


void sentToConversations(Msg msg, String converser, {bool addToTable = true}) {

Expand Down
147 changes: 79 additions & 68 deletions lib/pages/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ import '../components/view_file.dart';
import '../encyption/rsa.dart';

class ChatPage extends StatefulWidget {
const ChatPage({Key? key, required this.converser}) : super(key: key);
String converser;
ChatPage({Key? key, required this.converser}) : super(key: key);


final String converser;

@override
ChatPageState createState() => ChatPageState();
_ChatPageState createState() => _ChatPageState();
}

class ChatPageState extends State<ChatPage> {
class _ChatPageState extends State<ChatPage> {
List<Msg> messageList = [];
TextEditingController myController = TextEditingController();

@override
void initState() {
super.initState();
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
// Adding listener to listen for profile name updates
Global.profileNameStream.listen((updatedName) {
setState(() {
if (widget.converser == Global.myName) {
// Update the converser name if it matches the updated profile name
widget.converser = updatedName;
}
});
});
}

final ScrollController _scrollController = ScrollController();
Expand Down Expand Up @@ -74,72 +79,78 @@ class ChatPageState extends State<ChatPage> {
Expanded(
child: messageList.isEmpty
? const Center(
child: Text('No messages yet'),
)
child: Text('No messages yet'),
)
: ListView.builder(
controller: _scrollController,
padding: const EdgeInsets.all(8),
itemCount: groupedMessages.keys.length,
itemBuilder: (BuildContext context, int index) {
String date = groupedMessages.keys.elementAt(index);
return Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
date,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
),
...groupedMessages[date]!.map((msg) {
String displayMessage = msg.message;
if (Global.myPrivateKey != null) {
RSAPrivateKey privateKey = Global.myPrivateKey!;
dynamic data = jsonDecode(msg.message);
if (data['type'] == 'text') {
Uint8List encryptedBytes = base64Decode(data['data']);
Uint8List decryptedBytes = rsaDecrypt(privateKey, encryptedBytes);
displayMessage = utf8.decode(decryptedBytes);
}
}
controller: _scrollController,
padding: const EdgeInsets.all(8),
itemCount: groupedMessages.keys.length,
itemBuilder: (BuildContext context, int index) {
String date = groupedMessages.keys.elementAt(index);
return Column(
crossAxisAlignment: msg.msgtype == 'sent' ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: [
Align(
alignment: msg.msgtype == 'sent' ? Alignment.centerRight : Alignment.centerLeft,
child: Bubble(
padding: const BubbleEdges.all(12),
margin: const BubbleEdges.only(top: 10),
//add shadow
style: BubbleStyle(
elevation: 3,
shadowColor: Colors.black.withOpacity(0.5),
),
// nip: msg.msgtype == 'sent' ? BubbleNip.rightTop : BubbleNip.leftTop,
radius: const Radius.circular(10),
color: msg.msgtype == 'sent' ? const Color(0xffd1c4e9) : const Color(0xff80DEEA),
child: msg.message.contains('file') ? _buildFileBubble(msg) : Text(
displayMessage,
style: const TextStyle(color: Colors.black87),
Center(
child: Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
date,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 2, bottom: 10),
child: Text(
dateFormatter(timeStamp: msg.timestamp),
style: const TextStyle(color: Colors.black54, fontSize: 10),
),
),
...groupedMessages[date]!.map((msg) {
String displayMessage = msg.message;
if (Global.myPrivateKey != null) {
RSAPrivateKey privateKey = Global.myPrivateKey!;
dynamic data = jsonDecode(msg.message);
if (data['type'] == 'text') {
Uint8List encryptedBytes = base64Decode(data['data']);
Uint8List decryptedBytes = rsaDecrypt(privateKey, encryptedBytes);
displayMessage = utf8.decode(decryptedBytes);
}
}
return Column(
crossAxisAlignment: msg.msgtype == 'sent'
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
Align(
alignment: msg.msgtype == 'sent'
? Alignment.centerRight
: Alignment.centerLeft,
child: Bubble(
padding: const BubbleEdges.all(12),
margin: const BubbleEdges.only(top: 10),
style: BubbleStyle(
elevation: 3,
shadowColor: Colors.black.withOpacity(0.5),
),
radius: const Radius.circular(10),
color: msg.msgtype == 'sent'
? const Color(0xffd1c4e9)
: const Color(0xff80DEEA),
child: msg.message.contains('file')
? _buildFileBubble(msg)
: Text(
displayMessage,
style: const TextStyle(color: Colors.black87),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 2, bottom: 10),
child: Text(
dateFormatter(timeStamp: msg.timestamp),
style: const TextStyle(color: Colors.black54, fontSize: 10),
),
),
],
);
}),
],
);
}),
],
);
},
),
},
),
),
MessagePanel(converser: widget.converser),
],
Expand All @@ -161,7 +172,6 @@ class ChatPageState extends State<ChatPage> {
color: Colors.black87,
),
overflow: TextOverflow.visible,

),
),
IconButton(
Expand All @@ -180,3 +190,4 @@ String dateFormatter({required String timeStamp}) {
String formattedTime = DateFormat('hh:mm aa').format(dateTime);
return formattedTime;
}

4 changes: 2 additions & 2 deletions lib/pages/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'home_screen.dart';
import 'package:nanoid/nanoid.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../classes/global.dart';

import '../services/communication_service.dart';
class Profile extends StatefulWidget {
final bool onLogin;

Expand Down Expand Up @@ -113,7 +113,7 @@ class _ProfileState extends State<Profile> {
// saving the name and id to shared preferences
prefs.setString('p_name', myName.text);
prefs.setString('p_id', customLengthId);

CommunicationService.broadcastProfileUpdate(customLengthId, myName.text);
// On pressing, move to the home screen
navigateToHomeScreen();
},
Expand Down
32 changes: 32 additions & 0 deletions lib/services/communication_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:convert';
// ignore: depend_on_referenced_packages
import 'package:web_socket_channel/web_socket_channel.dart';

class CommunicationService {
static final WebSocketChannel _channel =
WebSocketChannel.connect(Uri.parse('ws://your-websocket-server-url'));

/// Broadcasts a profile update to all connected peers
static void broadcastProfileUpdate(String userId, String newName) {
final message = {
'type': 'profile_update',
'userId': userId,
'newName': newName,
};

_channel.sink.add(jsonEncode(message));
}

/// Listens for incoming messages
static void listen(void Function(Map<String, dynamic>) onMessage) {
_channel.stream.listen((data) {
final decodedData = jsonDecode(data);
onMessage(decodedData);
});
}

/// Closes the WebSocket connection
static void closeConnection() {
_channel.sink.close();
}
}
2 changes: 1 addition & 1 deletion macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import flutter_secure_storage_macos
import local_auth_darwin
import path_provider_foundation
import shared_preferences_foundation
import sqflite
import sqflite_darwin

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
open_filex: ^4.5.0
permission_handler: ^11.3.1
path_provider: ^2.1.4
web_socket_channel: ^3.0.1

dev_dependencies:
flutter_lints:
Expand Down