Browse Source

发消息修改

wr_202303
zsw 4 months ago
parent
commit
afa45d7c95
  1. 46
      lib/constant.dart
  2. 3
      lib/im/chat_details_page.dart
  3. 22
      lib/im/database/hx_database.dart
  4. 1
      lib/im/database/message.dart
  5. 10
      lib/im/im_view/im_page.dart

46
lib/constant.dart

@ -0,0 +1,46 @@
Map<S, List<T>> groupBy<S, T>(Iterable<T> values, S Function(T) key) {
var map = <S, List<T>>{};
for (var element in values) {
(map[key(element)] ??= []).add(element);
}
return map;
}
Map<String, int> groupCount<S, T>(Map<S, List<T>> values) {
var map = <String, int>{};
for (var element in values.keys) {
map["$element"] = values[element]?.length ?? 0;
}
return map;
}
Map<String, T> groupItem<S, T>(Map<S, List<T>> values) {
var map = <String, T>{};
for (var element in values.keys) {
if (values[element] == null) {
continue;
}
map["$element"] = values[element].first;
}
return map;
}
extension ListExtension<S, T> on Iterable<T> {
Map<S, List<T>> lGroupBy(S Function(T) key) {
return groupBy(this, key);
}
}
extension MapExtension<S, T> on Map<S, List<T>> {
Map<String, int> get mGroupCount => groupCount(this);
Map<String, T> get mGroupItem => groupItem(this);
}

3
lib/im/chat_details_page.dart

@ -64,13 +64,12 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
loadMessageList() async {
selfUserId = (await SharedPreferences.getInstance()).getString("userId");
toUserId = widget.arguments["toId"];
await hxDatabase.readMessage(selfUserId, toUserId);
messages = await hxDatabase.queryUList(toUserId);
socketClient.addCallback(toUserId, (Message message) {
messages.add(message);
refreshState();
});
refreshState();

22
lib/im/database/hx_database.dart

@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:huixiang/constant.dart';
import 'package:huixiang/im/database/contact.dart';
import 'package:huixiang/im/database/message.dart';
import 'package:huixiang/im/database/migration.dart';
@ -86,6 +87,19 @@ class HxDatabase {
});
}
Future<Map<String, int>> messageUnreadCount(List<String> userIds, String selfUserId) async {
if (db == null) {
return Future.value({});
}
String sql = 'SELECT * FROM Message WHERE fromId IN (?) AND toId = ? AND state = 0 AND isDelete = 0';
List<Message> messages = await db.rawQuery(sql, [userIds, selfUserId]).then((value) {
return value.map((e) => Message.fromJson(e)).toList();
}, onError: (error) {
debugPrint("Message-error: $error");
});
return messages.lGroupBy((p)=> p.fromId).mGroupCount;
}
Future<List<Map>> queryListAll() {
if (db == null) {
return Future.value();
@ -114,6 +128,14 @@ class HxDatabase {
return db.insert("Message", message);
}
/// update message read state
readMessage(String selfUserId, String userId) {
if (db == null) {
return Future.value(<Message>[]);
}
db.update("Message", {"state": 1}, where: "fromId = ? AND toId = ? AND state = 0 AND isDelete = 0", whereArgs: [userId, selfUserId]);
}
Future<int> insertContact(Map contactMap) async {
if (db == null) {
return Future.value(0);

1
lib/im/database/message.dart

@ -16,6 +16,7 @@ class Message {
String time;
/// 0 unread, 1 read, 2
int state;
int isDelete;

10
lib/im/im_view/im_page.dart

@ -4,6 +4,7 @@ import 'dart:collection';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:huixiang/constant.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/im/database/contact.dart';
import 'package:huixiang/im/database/message.dart';
@ -81,6 +82,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
List<String> userIds = [];
Map<String, Contact> contactMap = {};
Map<String, Message> lastMessageMap = {};
Map<String, int> unreadCountMap = {};
Stream streamSubscription ;
loadMessageList() async {
@ -115,7 +117,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
.toSet().where((element) => element != userId)
.toList();
lastMessageMap = groupBy(messages, (p0) => p0.toId != userId ? p0.toId : p0.fromId);
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId);
lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem;
List<Contact> contacts = await hxDatabase.queryContact(userIds);
if (contacts?.isEmpty ?? false) {
@ -192,8 +195,9 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
token: value.getString("token"),
);
}
BaseData<List<MsgStats>> baseData =
await apiService.stats().catchError((onError) {});
BaseData<List<MsgStats>> baseData = await apiService.stats().catchError((onError) {
debugPrint("stats.error: $onError");
});
if (baseData != null && baseData.isSuccess) {
setState(() {
msgNumber.forEach((key, value) {

Loading…
Cancel
Save