From e74005fa176def406a828aba02e5884b16e04f89 Mon Sep 17 00:00:00 2001 From: opjoobe Date: Wed, 3 Aug 2022 19:26:52 +0900 Subject: [PATCH] =?UTF-8?q?#66=20=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=8B=A4?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=20=EC=8A=A4=ED=8A=B8=EB=A6=AC=EB=B0=8D(?= =?UTF-8?q?=EC=8B=A4=EC=8B=9C=EA=B0=84=EC=9C=BC=EB=A1=9C=20=EC=9D=BD?= =?UTF-8?q?=EA=B8=B0=20=ED=99=95=EC=9D=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/screens/message/chat_detail_page.dart | 46 ++++++++++++++-------- lib/screens/mypage/profile_page.dart | 1 - lib/widgets/message/conversation_list.dart | 1 - 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/screens/message/chat_detail_page.dart b/lib/screens/message/chat_detail_page.dart index 73113ed..c581a92 100644 --- a/lib/screens/message/chat_detail_page.dart +++ b/lib/screens/message/chat_detail_page.dart @@ -13,7 +13,6 @@ class ChatScreen extends StatefulWidget { int friendID; String friendName; String friendImage; - int notReadCnt; ChatScreen( {Key? key, @@ -21,8 +20,7 @@ class ChatScreen extends StatefulWidget { required this.loginID, required this.friendID, required this.friendName, - required this.friendImage, - required this.notReadCnt}) + required this.friendImage}) : super(key: key); @override @@ -37,6 +35,30 @@ class _ChatScreenState extends State { int? myConnId; int? friendConnId; StreamController _streamController = StreamController(); + int? notReadCnt; + + getChatReadCnts() async { + try { + var dio = Dio(); + Response response = await dio.get( + 'http://34.64.217.3:3000/api/chatroom/readcnts?id_1=${widget.loginID}&id_2=${widget.friendID}'); + if (response.statusCode == 200) { + final jsonData = response.data; + int tempCnt = int.parse(jsonData['notreadcnt']); + setState(() { + notReadCnt = tempCnt; + }); + return await getChatMessages(tempCnt); + } else { + print('error'); + return false; + } + } on DioError catch (e) { + final errorjson = jsonDecode(e.response.toString()); + print(errorjson); + return false; + } + } getChatConns() async { try { @@ -45,9 +67,6 @@ class _ChatScreenState extends State { 'http://34.64.217.3:3000/api/chatroom/conns?id_1=${widget.loginID}&id_2=${widget.friendID}'); if (response.statusCode == 200) { final jsonData = response.data; - print(jsonData[0].runtimeType); - print(jsonData[1].runtimeType); - print(jsonData.runtimeType); setState(() { myConnId = jsonData[0]; friendConnId = jsonData[1]; @@ -69,11 +88,11 @@ class _ChatScreenState extends State { } } - getChatMessages(int chatroomid) async { + getChatMessages(int notReadCnt) async { try { var dio = Dio(); Response response = await dio.get( - 'http://34.64.217.3:3000/api/chatroom/message?room_id=$chatroomid'); + 'http://34.64.217.3:3000/api/chatroom/message?room_id=${widget.chatroomID}'); if (response.statusCode == 200) { final jsonData = response.data; List temp = []; @@ -82,16 +101,11 @@ class _ChatScreenState extends State { int i = totalSends; jsonData.forEach((e) { var nowtime = DateTime.parse(e['sendat']); - print(nowtime.runtimeType); var realnow = DateTime.now(); - print(realnow.runtimeType); Duration diff = realnow.difference(nowtime); - print(diff.inMinutes); - print(diff.inHours); - print(diff.inDays); bool nowisread = true; if (e['sender'] == widget.loginID) { - if (i <= widget.notReadCnt) { + if (i <= notReadCnt) { nowisread = false; } i -= 1; @@ -109,7 +123,7 @@ class _ChatScreenState extends State { _messages = temp; _streamController.add(_messages); }); - print('접속 성공!'); + return true; } else { print('error'); return false; @@ -219,8 +233,8 @@ class _ChatScreenState extends State { @override void initState() { + getChatReadCnts(); getChatConns(); - getChatMessages(widget.chatroomID); super.initState(); // enterChatRoom(); initializeSocket(); diff --git a/lib/screens/mypage/profile_page.dart b/lib/screens/mypage/profile_page.dart index d59dd84..6b163e2 100644 --- a/lib/screens/mypage/profile_page.dart +++ b/lib/screens/mypage/profile_page.dart @@ -274,7 +274,6 @@ class _ProfilePageState extends State { friendID: widget.friendId!, // not isMe friendName: user.nickname, friendImage: user.imagePath, - notReadCnt: notReadCnt, ); })); } else { diff --git a/lib/widgets/message/conversation_list.dart b/lib/widgets/message/conversation_list.dart index 4e1590f..c4f70cb 100644 --- a/lib/widgets/message/conversation_list.dart +++ b/lib/widgets/message/conversation_list.dart @@ -37,7 +37,6 @@ class _ConversationListState extends State { friendID: widget.friendID, friendName: widget.friendName, friendImage: widget.friendImage, - notReadCnt: widget.notReadCnt, ); })); },