Toast升级

This commit is contained in:
itgaojian 2023-07-28 14:49:15 +08:00
parent 849bcbe83c
commit 3db012d979
6 changed files with 28 additions and 278 deletions

View File

@ -9,11 +9,11 @@ import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.hjq.toast.Toaster;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sucstepsoft.cm_utils.core.widget.refresh.CustomRefreshHeader;
import com.sucstepsoft.cm_utils.utils.UserLgUtils;
import com.sucstepsoft.cm_utils.utils.toast.ToastUtils;
import com.tengshisoft.cultural.R;
import cn.jiguang.share.android.api.JShareInterface;
@ -46,8 +46,8 @@ public class BaseMyApplication extends Application {
initShare();
initBaiduMap();
ToastUtils.init(this);
ToastUtils.setGravity(Gravity.TOP, 0, 80);
Toaster.init(this);
Toaster.setGravity(Gravity.TOP, 0, 100);
getConfigCityCode();
}

View File

@ -76,4 +76,6 @@ dependencies {
implementation 'com.gyf.immersionbar:immersionbar-components:3.0.0'
//
api 'com.github.mmin18:realtimeblurview:1.2.1'
api 'com.github.getActivity:Toaster:12.3'
}

View File

@ -29,19 +29,23 @@ public class ExceptionHandler {
if (!TextUtils.isEmpty(errorBean.getMsg())) {
ToastUtils.showShort(errorBean.getMsg());
} else {
if (!TextUtils.isEmpty(errorBean.getMessage())) {
ToastUtils.showShort(errorBean.getMessage());
}
}
} else {
ToastUtils.showShort("网络错误(500)");
}
} catch (Exception e1) {
ToastUtils.showShort("网络错误,请稍后重试");
ToastUtils.showShort("网络错误(500)");
e1.printStackTrace();
}
} else if (throwable instanceof JsonParseException) {
ToastUtils.showShort("解析错误");
ToastUtils.showShort("解析错误(451)");
} else if (throwable instanceof SocketTimeoutException) {
ToastUtils.showShort("网络连接超时");
} else {
ToastUtils.showShort("网络错误,请稍后重试");
ToastUtils.showShort("网络错误(500)");
}
}

View File

@ -50,7 +50,7 @@ public class ToastLogInterceptor implements IToastInterceptor {
}
protected boolean isLogEnable() {
return ToastUtils.isDebugMode();
return false;
}
protected void printLog(String msg) {

View File

@ -5,6 +5,7 @@ import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.widget.Toast;
import com.hjq.toast.Toaster;
import com.sucstepsoft.cm_utils.utils.toast.config.IToastInterceptor;
import com.sucstepsoft.cm_utils.utils.toast.config.IToastStrategy;
import com.sucstepsoft.cm_utils.utils.toast.config.IToastStyle;
@ -23,141 +24,20 @@ import com.sucstepsoft.cm_utils.utils.toast.style.WhiteToastStyle;
@SuppressWarnings("unused")
public final class ToastUtils {
/**
* Application 对象
*/
private static Application sApplication;
/**
* Toast 处理策略
*/
private static IToastStrategy sToastStrategy;
/**
* Toast 样式
*/
private static IToastStyle<?> sToastStyle;
/**
* Toast 拦截器可空
*/
private static IToastInterceptor sToastInterceptor;
/**
* 调试模式
*/
private static Boolean sDebugMode;
/**
* 不允许被外部实例化
*/
private ToastUtils() {
}
/**
* 初始化 Toast需要在 Application.create 中初始化
*
* @param application 应用的上下文
*/
public static void init(Application application) {
init(application, sToastStyle);
}
public static void init(Application application, IToastStrategy strategy) {
init(application, strategy, null);
}
public static void init(Application application, IToastStyle<?> style) {
init(application, null, style);
}
/**
* 初始化 Toast
*
* @param application 应用的上下文
* @param strategy Toast 策略
* @param style Toast 样式
*/
public static void init(Application application, IToastStrategy strategy, IToastStyle<?> style) {
sApplication = application;
// 初始化 Toast 策略
if (strategy == null) {
strategy = new ToastStrategy();
}
setStrategy(strategy);
// 设置 Toast 样式
if (style == null) {
style = new BlackToastStyle();
}
setStyle(style);
}
/**
* 判断当前框架是否已经初始化
*/
public static boolean isInit() {
return sApplication != null && sToastStrategy != null && sToastStyle != null;
}
/**
* 延迟显示 Toast
*/
public static void delayedShow(int id, long delayMillis) {
delayedShow(stringIdToCharSequence(id), delayMillis);
}
public static void delayedShow(Object object, long delayMillis) {
delayedShow(objectToCharSequence(object), delayMillis);
}
public static void delayedShow(CharSequence text, long delayMillis) {
ToastParams params = new ToastParams();
params.text = text;
params.delayMillis = delayMillis;
show(params);
}
/**
* debug 模式下显示 Toast
*/
public static void debugShow(int id) {
debugShow(stringIdToCharSequence(id));
}
public static void debugShow(Object object) {
debugShow(objectToCharSequence(object));
}
public static void debugShow(CharSequence text) {
if (!isDebugMode()) {
return;
}
ToastParams params = new ToastParams();
params.text = text;
show(params);
}
/**
* 显示一个短 Toast
*/
public static void showShort(int id) {
showShort(stringIdToCharSequence(id));
Toaster.showLong(id);
}
public static void showShort(Object object) {
showShort(objectToCharSequence(object));
Toaster.showLong(object);
}
public static void showShort(CharSequence text) {
ToastParams params = new ToastParams();
params.text = text;
params.duration = Toast.LENGTH_SHORT;
show(params);
Toaster.showShort(text);
}
/**
@ -165,18 +45,15 @@ public final class ToastUtils {
*/
public static void showLong(int id) {
showLong(stringIdToCharSequence(id));
Toaster.showLong(id);
}
public static void showLong(Object object) {
showLong(objectToCharSequence(object));
Toaster.showLong(object);
}
public static void showLong(CharSequence text) {
ToastParams params = new ToastParams();
params.text = text;
params.duration = Toast.LENGTH_LONG;
show(params);
Toaster.showLong(text);
}
/**
@ -184,151 +61,14 @@ public final class ToastUtils {
*/
public static void show(int id) {
show(stringIdToCharSequence(id));
Toaster.show(id);
}
public static void show(Object object) {
show(objectToCharSequence(object));
Toaster.show(object);
}
public static void show(CharSequence text) {
ToastParams params = new ToastParams();
params.text = text;
show(params);
}
public static void show(ToastParams params) {
// 如果是空对象或者空文本就不显示
if (params.text == null || params.text.length() == 0) {
return;
}
if (params.strategy == null) {
params.strategy = sToastStrategy;
}
if (params.interceptor == null) {
if (sToastInterceptor == null) {
sToastInterceptor = new ToastLogInterceptor();
}
params.interceptor = sToastInterceptor;
}
if (params.style == null) {
params.style = sToastStyle;
}
if (params.interceptor.intercept(params)) {
return;
}
if (params.duration == -1) {
params.duration = params.text.length() > 20 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
}
params.strategy.showToast(params);
}
/**
* 取消吐司的显示
*/
public static void cancel() {
sToastStrategy.cancelToast();
}
/**
* 设置吐司的位置
*
* @param gravity 重心
*/
public static void setGravity(int gravity) {
setGravity(gravity, 0, 0);
}
public static void setGravity(int gravity, int xOffset, int yOffset) {
setGravity(gravity, xOffset, yOffset, 0, 0);
}
public static void setGravity(int gravity, int xOffset, int yOffset, float horizontalMargin, float verticalMargin) {
sToastStyle = new LocationToastStyle(sToastStyle, gravity, xOffset, yOffset, horizontalMargin, verticalMargin);
}
/**
* 给当前 Toast 设置新的布局
*/
public static void setView(int id) {
if (id <= 0) {
return;
}
setStyle(new CustomViewToastStyle(id, sToastStyle.getGravity(),
sToastStyle.getXOffset(), sToastStyle.getYOffset(),
sToastStyle.getHorizontalMargin(), sToastStyle.getVerticalMargin()));
}
/**
* 初始化全局的 Toast 样式
*
* @param style 样式实现类框架已经实现两种不同的样式
* 黑色样式{@link BlackToastStyle}
* 白色样式{@link WhiteToastStyle}
*/
public static void setStyle(IToastStyle<?> style) {
sToastStyle = style;
}
public static IToastStyle<?> getStyle() {
return sToastStyle;
}
/**
* 设置 Toast 显示策略
*/
public static void setStrategy(IToastStrategy strategy) {
sToastStrategy = strategy;
sToastStrategy.registerStrategy(sApplication);
}
public static IToastStrategy getStrategy() {
return sToastStrategy;
}
/**
* 设置 Toast 拦截器可以根据显示的内容决定是否拦截这个Toast
* 场景打印 Toast 内容日志根据 Toast 内容是否包含敏感字来动态切换其他方式显示这里可以使用我的另外一套框架 XToast
*/
public static void setInterceptor(IToastInterceptor interceptor) {
sToastInterceptor = interceptor;
}
public static IToastInterceptor getInterceptor() {
return sToastInterceptor;
}
/**
* 是否为调试模式
*/
public static void setDebugMode(boolean debug) {
sDebugMode = debug;
}
static boolean isDebugMode() {
if (sDebugMode == null) {
sDebugMode = (sApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
}
return sDebugMode;
}
private static CharSequence stringIdToCharSequence(int id) {
try {
// 如果这是一个资源 id
return sApplication.getResources().getText(id);
} catch (Resources.NotFoundException ignored) {
// 如果这是一个 int 整数
return String.valueOf(id);
}
}
private static CharSequence objectToCharSequence(Object object) {
return object != null ? object.toString() : "null";
Toaster.show(text);
}
}

View File

@ -35,6 +35,7 @@ import com.sucstepsoft.cm_utils.core.retrofit_net.bean.VersionBean;
import com.sucstepsoft.cm_utils.core.widget.base.BaseActivity;
import com.sucstepsoft.cm_utils.core.widget.base.FragmentUtils;
import com.sucstepsoft.cm_utils.core.widget.views.CenterDialogUpdateView;
import com.sucstepsoft.cm_utils.utils.ExceptionHandler;
import com.sucstepsoft.cm_utils.utils.FileUtils;
import com.sucstepsoft.cm_utils.utils.LogUtils;
import com.sucstepsoft.cm_utils.utils.PermissionUtils;
@ -295,11 +296,14 @@ public class MainActivity extends BaseActivity {
@Override
public void onNext(Boolean baseSuccessBean) {
LogUtils.e(baseSuccessBean);
UserLgUtils.putIsPublicCode(baseSuccessBean);
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
ExceptionHandler.handleException(e);
}
@Override