Kotlin高仿微信-第55篇-同步数据

实现代码:

/**
 * Author : wangning
 * Email : maoning20080809@163.com
 * Date : 2022/6/27 14:41
 * Description : 同步信息
 */
object SyncRepository {

    //第一次安装、或者卸载后安装,同步服务器信息
    fun syncFirst (account : String, packageName : String, iCommonCallback: ICommonCallback) {
        var isFirstLogin = DataStoreUtils.get(DataStoreParams.User.DS_IS_FIRST_LOGIN, true) as Boolean
        //首次安装
        if(!isFirstLogin){
            return
        }
        CoroutineScope(Dispatchers.IO).launch {
            var result = ApiResult.create {SyncService.getApi().syncFirst(account, packageName)}
            if(result.isSuccess){
                var data = result.data
                var syncFirstBean = Gson().fromJson(data, SyncFirstBean::class.java)
                syncFirstBean?.let {
                    processBaseSystem(it.sync_base_system)
                    processUserList(it.sync_user)
                    processContactsList(it.sync_contacts)
                    processChatList(it.sync_chat)
                    processVipBean(it.sync_vip)
                    //如果同步成功,设置登陆标识符
                    DataStoreUtils.put(DataStoreParams.User.DS_IS_FIRST_LOGIN, false)
                }
                iCommonCallback?.onSuccess(0)
            } else {
                iCommonCallback?.onFailure(0)
            }
        }
    }

    //每次进入主页面,要查询服务器重要数据
    fun syncEnterMain(account: String){
        syncVip(account)
    }

    //获取服务器vip信息
    fun syncVip(account: String){
        if(TextUtils.isEmpty(account)){
            return
        }
        CoroutineScope(Dispatchers.IO).launch {
            var vipBean = VipManagerRepository.getVipLocal(account)
            if(vipBean != null && CommonUtils.Vip.isVip(vipBean)){
                //如果还是vip,不需要加载。
                return@launch
            }
            var result = VipManagerRepository.getVipServer(account)
            if(result.isSuccess){
                var vipBean = Gson().fromJson(result.data, VipBean::class.java)
                VipManagerRepository.insertVipLocal(vipBean)
            }
        }
    }

    /**
     * 同步系统基本信息
     */
    private fun processBaseSystem(baseSystemBean: BaseSystemBean){
        baseSystemBean?.let {
            BaseSystemRepository.insertBaseSystem(it)
        }
    }

    /**
     * 同步vip信息
     */
    private fun processVipBean(vipBean: VipBean){
        vipBean?.let {
            VipManagerRepository.insertVipLocal(vipBean)
        }
    }

    /**
     * 同步账户信息
     */
    private fun processUserList(userListServer : ArrayList<UserBean>){
        userListServer?.let {
            UserRepository.syncUserFirst(it)
        }
    }

    /**
     * 同步聊天信息
     */
    private fun processChatList(userListServer : ArrayList<ChatBean>){
        userListServer?.let {
            ChatRepository.syncInsertChatListLocal(it)
        }
    }

    /**
     * 同步联系人
     */
    private fun processContactsList(contactsListServer : ArrayList<ContactsBean>){
        contactsListServer?.let {
            ContactsRepository.syncInsertContactsListLocal(it)
        }
    }

}
经验分享 程序员 微信小程序 职场和发展