package com.gh.gamecenter.common.mvvm;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import retrofit2.HttpException;

/**
 * Created by khy on 31/01/18.
 * 包含加载状态的数据类
 */

public class Resource<T> {

    @NonNull
    public final Status status;

    @Nullable
    public final HttpException exception;

    @Nullable
    public final T data;

    public Resource(@NonNull Status status, @Nullable T data, @Nullable HttpException exception) {
        this.status = status;
        this.data = data;
        this.exception = exception;
    }

    public static <T> Resource<T> success(@Nullable T data) {
        return new Resource<>(Status.SUCCESS, data, null);
    }

    public static <T> Resource<T> error(HttpException msg) {
        return new Resource<>(Status.ERROR, null, msg);
    }

    public static <T> Resource<T> loading() {
        return new Resource<>(Status.LOADING, null, null);
    }

    @Override
    public String toString() {
        return "Resource{" +
                "status=" + status +
                ", exception='" + exception + '\'' +
                ", data=" + data +
                '}';
    }
}
