StatusAction.java
2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.studymachine.www.action;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.annotation.DrawableRes;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import com.studymachine.www.R;
import com.studymachine.www.widget.StatusLayout;
/**
* time : 2019/12/08
* desc : 状态布局意图
*/
public interface StatusAction {
/**
* 获取状态布局
*/
StatusLayout getStatusLayout();
/**
* 显示加载中
*/
default void showLoading() {
showLoading(R.raw.loading);
}
default void showLoading(@RawRes int id) {
StatusLayout layout = getStatusLayout();
layout.show();
layout.setAnimResource(id);
layout.setHint("");
layout.setOnRetryListener(null);
}
/**
* 显示加载完成
*/
default void showComplete() {
StatusLayout layout = getStatusLayout();
if (layout == null || !layout.isShow()) {
return;
}
layout.hide();
}
/**
* 显示空提示
*/
default void showEmpty() {
showLayout(R.drawable.common_status_empty, R.string.status_layout_no_data, null);
}
/**
* 显示错误提示
*/
default void showError(StatusLayout.OnRetryListener listener) {
StatusLayout layout = getStatusLayout();
Context context = layout.getContext();
ConnectivityManager manager = ContextCompat.getSystemService(context, ConnectivityManager.class);
if (manager != null) {
NetworkInfo info = manager.getActiveNetworkInfo();
// 判断网络是否连接
if (info == null || !info.isConnected()) {
showLayout(R.drawable.status_network_ic, R.string.status_layout_error_network, listener);
return;
}
}
showLayout(R.drawable.status_error_ic, R.string.status_layout_error_request, listener);
}
/**
* 显示自定义提示
*/
default void showLayout(@DrawableRes int drawableId, @StringRes int stringId, StatusLayout.OnRetryListener listener) {
StatusLayout layout = getStatusLayout();
Context context = layout.getContext();
showLayout(ContextCompat.getDrawable(context, drawableId), context.getString(stringId), listener);
}
default void showLayout(Drawable drawable, CharSequence hint, StatusLayout.OnRetryListener listener) {
StatusLayout layout = getStatusLayout();
layout.show();
layout.setIcon(drawable);
layout.setHint(hint);
layout.setOnRetryListener(listener);
}
}