package com.renrenliao.im.viewmodel

import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.model.repository.LocalDataSource
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.MapUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 **@ClassName UserInfoViewModel
 **@Description
 **@Author gan
 **@Date 2022/5/30 12:42
 **/
class UserInfoViewModel(private val remoteDataSource: RemoteDataSource,private val localDataSource: LocalDataSource):BaseViewModel() {

    val userInfo = LocalDataSource.readOnlyUserFlow


    fun updateUserInfo(map: HashMap<String,String>){

        launchUI({
            val map = MapUtils.getComParam(map)
            val result = withContext(Dispatchers.IO){
                remoteDataSource.updateUserInfo(map)
            }
            result.checkSuccess {
                LogUtils.error(it)
                localDataSource.updateUserInfo(it)
            }
        })
    }
}