Flutter高仿微信-第59篇-同步数据

 Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

 详情请查看

实现代码:

//服务器同步数据
Future syncFirst(String account, String packageName) async {
  Response result = await HttpUtils.getInstance().syncFirst(account, packageName);
  LogUtils.d("syncFirst返回数据:${result.toString()}");
  final responseResult = jsonDecode(result.toString());
  final baseResult = BaseResult.fromJson(responseResult);
  //LogUtils.d("syncFirst返回baseResult数据:${baseResult}");
  final syncFirstJson = jsonDecode(baseResult.data.toString());
  SyncFirstBean syncFirstBean = SyncFirstBean.fromJson(syncFirstJson);
  List syncUser = syncFirstBean.syncUser;
  List syncContacts = syncFirstBean.syncContacts;
  List syncChat = syncFirstBean.syncChat;
  List syncGroup = syncFirstBean.syncGroup;
  List syncGroupUser = syncFirstBean.syncGroupUser;

  processSyncUser(syncUser);
  processSyncContact(syncContacts);
  processSyncChat(syncChat);
  processSyncGroup(syncGroup);
  processSyncGroupUser(syncGroupUser);
  return true;
}

//处理登录账户
void processSyncUser(List syncUser) async{
  List resultUserList = [];
  for(int i = 0; i < syncUser.length; i++){
    UserBean userBean = syncUser[i];
    //LogUtils.d("第一次同步用户:${userBean.account}, ${userBean.nickName} , ${userBean.address},  ${userBean.note}");
    UserBean? localUserBean = await userDao?.findUserByAccount(userBean.account!);
    if(localUserBean == null){
      resultUserList.add(userBean);
    }
  }
  userDao?.insertUserList(resultUserList);
}

//处理联系人
void processSyncContact(List syncContacts) async{
  /*syncContacts.forEach((contactsBean) {
    LogUtils.d("第一次同步联系人:${contactsBean.fromAccount}, ${contactsBean.toAccount} , ${contactsBean.type}");
  });*/

  List resultContactsList = [];
  for(int i = 0; i < syncContacts.length; i++){
    ContactsBean contactsBean = syncContacts[i];
    ContactsBean? localContactsBean = await contactsDao?.findContactByFromOrToAccount(contactsBean.fromAccount??"", contactsBean.toAccount??"");
    //如果已经存在联系人, 不需要重复插入
    if(localContactsBean == null){
      resultContactsList.add(contactsBean);
    }
  }

  contactsDao?.insertContactList(resultContactsList);
}

//处理聊天信息
void processSyncChat(List syncChat){
  /*syncChat.forEach((chatBean) {
    LogUtils.d("第一次同步聊天信息:${chatBean.fromAccount}, ${chatBean.toAccount} ${chatBean.content}");
  });*/
  ChatDao? chatDao = DBUtils.getInstance().getDatabase()?.chatDao;
  chatDao?.insertChatList(syncChat);
}

//处理群名信息
void processSyncGroup(List syncGroup){
  GroupRepository.getInstance().insertGroupList(syncGroup);
}

//处理群成员信息
void processSyncGroupUser(List syncGroupUser){
  GroupUserRepository.getInstance().insertGroupUserList(syncGroupUser);
}

你可能感兴趣的:(Flutter高仿微信,flutter,微信)