package com.community.pocket.ui.main.ui.share; import android.content.Context; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import com.community.pocket.R; import com.google.gson.Gson; /** * 响应体父类 * * @param */ public abstract class Response { //成功描述 @Nullable @Deprecated private Integer success; //失败描述 @Nullable @Deprecated private Integer error; //描述参数 private Object[] args; //响应体 private T body; //响应信息 private String message; private Result result; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Result getResult() { return result; } public void setResult(Result result) { this.result = result; } @Nullable @Deprecated // TODO 废弃 public Integer getSuccess() { return success; } @Deprecated // TODO 废弃 public void setSuccess(@Nullable @StringRes Integer success) { this.success = success; } @Nullable @Deprecated // TODO 废弃 public Integer getError() { return error; } @Deprecated // TODO 废弃 public void setError(@Nullable @StringRes Integer error) { this.error = error; } public T getBody() { return body; } public void setBody(T body) { this.body = body; } Object[] getArgs() { return args; } public void setArgs(Object[] args) { this.args = args; } public void ok(Context context) { if (success != null) { Toast.makeText(context, context.getString(success, args), Toast.LENGTH_SHORT).show(); } } public void fail(Context context) { if (error != null) { Toast.makeText(context, context.getString(error, args), Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, context.getString(R.string.unknow_error), Toast.LENGTH_LONG).show(); } } public enum Result { OK, FAIL } @NonNull @Override public String toString() { return new Gson().toJson(body); } }