import 'package:uuid/uuid.dart';
import 'message.dart';
import '../database/db.dart' as db;
class User {
late final String _uid;
late final String _name;
late bool _isAuthenticated = false;
final List<Message> messagesSent = [];
User({ required String name }) : _uid = Uuid().v4(), _name = name {
_validateName(name);
db.users.add(this);
}
String get uid => _uid;
String get name => _name;
set name(String value) {
_name = value;
}
bool get isAuthenticated => _isAuthenticated;
set isAuthenticated(bool value) {
_isAuthenticated = value;
}
void _validateName(String name) {
if (name.trim().isEmpty) throw ArgumentError('User name cannot be null or empty.');
}
}
class Message {
final String senderName;
final String text;
static List<String> partingWords = [
'Goodbye',
'Farewell',
'See you later',
'Take care',
'Until next time',
'Adieu',
'Cheerio',
'Bye for now',
'Ciao',
'So long',
'Till we meet again',
'Catch you later',
'Have a great day',
'Signing off',
'Bye-bye',
'Later',
'Goodnight',
'Peace out',
'Stay safe',
'Adios'
];
Message({required this.senderName, required this.text});
}
import '../entities/user.dart';
class AuthService {
Future<void> authenticate(User user) async {
try {
print('Authenticating user ${user.uid}...');
await _simulateAsyncOperation();
if (await _isAuthenticated(user)) {
print('User ${user.uid} is already authenticated!');
return;
} else {
await _changeAuthenticationStatus(user);
}
print('User ${user.uid} authenticated successfully!');
} catch (e) {
print('Error during authentication: $e');
rethrow;
}
}
Future<void> _changeAuthenticationStatus(User user) async {
user.isAuthenticated = true;
}
Future<bool> _isAuthenticated(User user) async {
try {
print('Verifying if the user ${user.uid} is already authenticated....');
await _simulateAsyncOperation();
return user.isAuthenticated;
} catch (e) {
print('Error during isAuthenticated check: $e');
rethrow;
}
}
Future<void> _simulateAsyncOperation() async {
await Future.delayed(Duration(seconds: 3));
}
}
import 'dart:async';
import 'dart:io';
import '../auth/auth_service.dart';
import 'message.dart';
import 'user.dart';
class Chat {
late final User user1;
late final User user2;
final AuthService authService = AuthService();
Chat({required String name1, required String name2}) {
user1 = User(name: name1);
user2 = User(name: name2);
}
Future<void> startChat() async {
await _authenticateUsers();
print('Chat started between ${user1.name} and ${user2.name}.\n');
while (true) {
await _sendMessage(user1, user2);
if (_isPartingWords(user1.messagesSent.last.text)) {
print('${user1.name} has left the chat.');
break;
}
await _sendMessage(user2, user1);
if (_isPartingWords(user2.messagesSent.last.text)) {
print('${user2.name} has left the chat.');
break;
}
}
}
Future<void> _authenticateUsers() async {
try {
await authService.authenticate(user1);
await authService.authenticate(user2);
} catch (e) {
print('Error during authentication: $e');
rethrow;
}
}
Future<void> _sendMessage(User sender, User receiver) async {
stdout.write('${sender.name}: ');
String message = stdin.readLineSync()!;
sender.messagesSent.add(Message(senderName: sender.name, text: message));
print('Sending message...');
await Future.delayed(Duration(seconds: 2));
print('\n${receiver.name} received message: $message\n');
}
bool _isPartingWords(String message) {
return Message.partingWords
.any((partingWord) => message.toLowerCase().contains(partingWord.toLowerCase()));
}
}
class AuthenticationException implements Exception {
final String message;
AuthenticationException(this.message);
@override
String toString() => message;
}
class UserNotFoundException implements Exception {
final String message;
UserNotFoundException(this.message);
@override
String toString() => message;
}
import 'dart:io';
import 'entities/chat.dart';
void main() async {
print('Enter the name for User 1:');
String name1 = stdin.readLineSync()!;
print('Enter the name for User 2:');
String name2 = stdin.readLineSync()!;
Chat chat = Chat(name1: name1, name2: name2);
await chat.startChat();
}