Skip to content

Commit

Permalink
Merge pull request #10 from habyaad/dev
Browse files Browse the repository at this point in the history
Release v2.0.3
  • Loading branch information
habyaad authored Jun 24, 2024
2 parents 782db20 + 1ce61f7 commit 9d8da18
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/flutter-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v2.0.2
release_name: Release v2.0.2
tag_name: v2.0.3
release_name: Release v2.0.3
draft: false
prerelease: false

Expand Down
4 changes: 4 additions & 0 deletions lib/models/conversation_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ class Conversation {
final DateTime lastUpdatedAt;
final List<String> participantIds;
final ChatModel sender;
final bool read;
final ChatModel receiver;

Conversation({
required this.lastMessage,
required this.lastUpdatedAt,
required this.participantIds,
required this.sender,
required this.read,
required this.receiver,
});

factory Conversation.fromJson(Map<String, dynamic> json) => Conversation(
sender: ChatModel.fromJson(json["sender"]),
receiver: ChatModel.fromJson(json["receiver"]),
lastMessage: json["lastMessage"],
read: json["read"],
lastUpdatedAt: json["lastUpdatedAt"] is Timestamp
? (json["lastUpdatedAt"] as Timestamp).toDate()
: DateTime.parse(json["lastUpdatedAt"]),
Expand All @@ -39,6 +42,7 @@ class Conversation {
"sender": sender.toJson(),
"receiver": receiver.toJson(),
"lastMessage": lastMessage,
"read": read,
"lastUpdatedAt": lastUpdatedAt.toIso8601String(),
"participantIds": List<dynamic>.from(participantIds.map((x) => x)),
};
Expand Down
17 changes: 17 additions & 0 deletions lib/services/message_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MessageService {
'messages/${getConversationID(message.receiver.uid, senderId: senderId)}/chat')
.add(message.toJson());
}

// Retrieve messages between two users

Stream<QuerySnapshot<Object?>> getMessagesBetweenUsers(String friendUid) {
Expand All @@ -67,8 +68,24 @@ class MessageService {
"lastMessage": message.content,
"lastUpdatedAt": message.timestamp,
"participantIds": [message.sender.uid, message.receiver.uid],
"read": false,
"sender": message.sender.toJson(),
"receiver": message.receiver.toJson()
}).onError((e, _) => log("Error writing document: $e"));
}

markChatAsRead(receiverUid) async {
DocumentSnapshot doc = await _databaseService.store
.collection('messages')
.doc(getConversationID(receiverUid))
.get();
if (doc.get("sender") == receiverUid) {
await _databaseService.store
.collection('messages')
.doc(getConversationID(receiverUid))
.set({
"read": true,
}).onError((e, _) => log("Error writing document: $e"));
}
}
}
24 changes: 23 additions & 1 deletion lib/ui/views/ai_chat/ai_chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shimmer_animation/shimmer_animation.dart';
import 'package:stacked/stacked.dart';
import '../../../models/message_model.dart';
import '../../../utils/app_colors.dart';
Expand Down Expand Up @@ -67,7 +68,28 @@ class AiChatView extends StackedView<AiChatViewModel> {
child: StreamBuilder(
stream: viewModel.getMessagesStream(viewModel.AI_ID),
builder: (context, snapshot) {
if (!snapshot.hasData) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Shimmer(
duration: const Duration(seconds: 4),
//Default value
interval: const Duration(seconds: 1),
//Default value: Duration(seconds: 0)
color: Colors.grey,
//Default value
colorOpacity: 0.01,
//Default value
enabled: true,
//Default value
direction: const ShimmerDirection.fromLTRB(),
child: Container(
decoration: BoxDecoration(
color: Colors.grey.withOpacity(.01),
borderRadius: BorderRadius.circular(8)),
width: double.infinity,
height: 100,
),
);
} else if (!snapshot.hasData) {
log("no messages");

return const Center(
Expand Down
30 changes: 29 additions & 1 deletion lib/ui/views/chat/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shimmer_animation/shimmer_animation.dart';
import 'package:soro_soke/ui/common/custom_text_form_field.dart';
import 'package:soro_soke/ui/common/string_utils.dart';
import 'package:soro_soke/ui/views/chat/widgets/message_box.dart';
Expand Down Expand Up @@ -62,7 +63,28 @@ class ChatView extends StackedView<ChatViewModel> {
child: StreamBuilder(
stream: viewModel.getMessagesStream(friend.uid),
builder: (context, snapshot) {
if (!snapshot.hasData) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Shimmer(
duration: const Duration(seconds: 4),
//Default value
interval: const Duration(seconds: 1),
//Default value: Duration(seconds: 0)
color: Colors.grey,
//Default value
colorOpacity: 0.01,
//Default value
enabled: true,
//Default value
direction: const ShimmerDirection.fromLTRB(),
child: Container(
decoration: BoxDecoration(
color: Colors.grey.withOpacity(.01),
borderRadius: BorderRadius.circular(8)),
width: double.infinity,
height: 100,
),
);
} else if (!snapshot.hasData) {
log("no messages");

return const Center(
Expand Down Expand Up @@ -170,6 +192,12 @@ class ChatView extends StackedView<ChatViewModel> {
viewModel.initializeUser();
}

@override
void onDispose(ChatViewModel viewModel) {
super.onDispose(viewModel);
viewModel.markAsRead(friend.uid);
}

@override
ChatViewModel viewModelBuilder(
BuildContext context,
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/views/chat/chat_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class ChatViewModel extends BaseViewModel {
return _messageService.getMessagesBetweenUsers(friendID);
}

void markAsRead(String friendID) {
_messageService.markChatAsRead(friendID);
}

void goBack() {
_navigationService.back();
}
Expand Down
36 changes: 30 additions & 6 deletions lib/ui/views/home/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class HomeView extends StackedView<HomeViewModel> {
borderRadius: BorderRadius.circular(10)),
tileColor: const Color(0xFF261C2C),
onTap: () {
sender == true
? null
: viewModel.markChatAsRead(receiver.uid);
viewModel.goToChat(receiver);
},
leading: CircleAvatar(
Expand All @@ -158,12 +161,33 @@ class HomeView extends StackedView<HomeViewModel> {
fontStyle: FontStyle.italic,
overflow: TextOverflow.ellipsis),
),
trailing: Text(
"${convos[index].lastUpdatedAt.hour}: ${convos[index].lastUpdatedAt.minute}",
style: const TextStyle(
fontSize: 10,
color: Colors.white70,
overflow: TextOverflow.ellipsis),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"${convos[index].lastUpdatedAt.hour}: ${convos[index].lastUpdatedAt.minute}",
style: const TextStyle(
fontSize: 10,
color: Colors.white70,
overflow: TextOverflow.ellipsis),
),
horizontalSpaceSmall,
Visibility(
visible: sender == false,
replacement: const SizedBox(
width: 6,
),
child: Container(
width: 6,
height: 6,
decoration: BoxDecoration(
color: convos[index].read == false
? Colors.pinkAccent
: Colors.transparent,
shape: BoxShape.circle),
),
)
],
),
);
},
Expand Down
6 changes: 4 additions & 2 deletions lib/ui/views/home/home_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import 'package:soro_soke/services/message_service.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import '../../../models/chat_model.dart';
import '../../../services/friend_service.dart';
import '../../../services/user_service.dart';

class HomeViewModel extends BaseViewModel {
final _userService = locator<UserService>();
final _friendService = locator<FriendService>();
final _messageService = locator<MessageService>();
final _navigationService = locator<NavigationService>();

Expand All @@ -27,6 +25,10 @@ class HomeViewModel extends BaseViewModel {
rebuildUi();
}

markChatAsRead(receiverUid) async {
_messageService.markChatAsRead(receiverUid);
}

void goToChat(ChatModel user) {
_navigationService.navigateToChatView(friend: user);
}
Expand Down

0 comments on commit 9d8da18

Please sign in to comment.