package com.atmob.keyboard_android.mvvm.base

import com.atmob.keyboard_android.enums.ServerErrorCode
import com.atmob.keyboard_android.model.base.BaseResp
import retrofit2.HttpException
import java.net.ConnectException
import java.net.UnknownHostException

/**
 * Repository仓库基类
 */
open class BaseRepository {
    /**
     * 处理状态
     */
    fun <T> BaseResp<T>.handleState(): BaseResp<T> {
        try {
            when (this.code) {
                // 请求成功
                ServerErrorCode.SUCCESS.code -> {
                }

                ServerErrorCode.NO_LOGIN.code -> {
                    // 未登录，清除token，跳转去登录页
                }
            }
        } catch (e: Exception) {
            when (e) {
                is UnknownHostException,
                is HttpException,
                is ConnectException,
                    -> {
                    // 网络错误
                }

                else -> {
                }
            }
        }

        return this
    }
}