AppActivity.java
6.01 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.studymachine.www.app;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.TitleBar;
import com.hjq.base.BaseActivity;
import com.hjq.base.BaseDialog;
import com.studymachine.www.R;
import com.studymachine.www.action.TitleBarAction;
import com.studymachine.www.action.ToastAction;
import com.studymachine.www.http.model.HttpData;
import com.studymachine.www.manager.EventBusManager;
import com.studymachine.www.other.Tool;
import com.studymachine.www.ui.activity.HomeActivity;
import com.studymachine.www.ui.dialog.WaitDialog;
import com.hjq.http.listener.OnHttpListener;
import org.greenrobot.eventbus.EventBus;
import okhttp3.Call;
/**
* time : 2018/10/18
* desc : Activity 业务基类
*/
public abstract class AppActivity extends BaseActivity
implements ToastAction, TitleBarAction, OnHttpListener<Object> {
/**
* 标题栏对象
*/
private TitleBar mTitleBar;
/**
* 状态栏沉浸
*/
private ImmersionBar mImmersionBar;
/**
* 加载对话框
*/
private BaseDialog mDialog;
/**
* 对话框数量
*/
private int mDialogCount;
/**
* 当前加载对话框是否在显示中
*/
public boolean isShowDialog() {
return mDialog != null && mDialog.isShowing();
}
/**
* 显示加载对话框
*/
public void showDialog() {
if (isFinishing() || isDestroyed()) {
return;
}
mDialogCount++;
postDelayed(() -> {
if (mDialogCount <= 0 || isFinishing() || isDestroyed()) {
return;
}
if (mDialog == null) {
mDialog = new WaitDialog.Builder(this)
.setCancelable(false)
.create();
}
if (!mDialog.isShowing()) {
mDialog.show();
}
}, 300);
}
/**
* 隐藏加载对话框
*/
public void hideDialog() {
if (isFinishing() || isDestroyed()) {
return;
}
if (mDialogCount > 0) {
mDialogCount--;
}
if (mDialogCount != 0 || mDialog == null || !mDialog.isShowing()) {
return;
}
mDialog.dismiss();
}
@Override
protected void initLayout() {
super.initLayout();
if (getTitleBar() != null) {
getTitleBar().setOnTitleBarListener(this);
}
// 初始化沉浸式状态栏
if (isStatusBarEnabled()) {
getStatusBarConfig().init();
// 设置标题栏沉浸
if (getTitleBar() != null) {
ImmersionBar.setTitleBar(this, getTitleBar());
}
}
//保持常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
EventBusManager.register(this);
}
/**
* 是否使用沉浸式状态栏
*/
protected boolean isStatusBarEnabled() {
return true;
}
/**
* 状态栏字体深色模式
*/
protected boolean isStatusBarDarkFont() {
return true;
}
/**
* 获取状态栏沉浸的配置对象
*/
@NonNull
public ImmersionBar getStatusBarConfig() {
if (mImmersionBar == null) {
mImmersionBar = createStatusBarConfig();
}
return mImmersionBar;
}
/**
* 初始化沉浸式状态栏
*/
@NonNull
protected ImmersionBar createStatusBarConfig() {
return ImmersionBar.with(this)
// 隐藏状态栏和导航栏
.hideBar(BarHide.FLAG_HIDE_BAR);
// return ImmersionBar.with(this)
// // 默认状态栏字体颜色为黑色
// .statusBarDarkFont(isStatusBarDarkFont())
// // 指定导航栏背景颜色
// .navigationBarColor(R.color.white)
// // 状态栏字体和导航栏内容自动变色,必须指定状态栏颜色和导航栏颜色才可以自动变色
// .autoDarkModeEnable(true, 0.2f);
}
/**
* 设置标题栏的标题
*/
@Override
public void setTitle(@StringRes int id) {
setTitle(getString(id));
}
/**
* 设置标题栏的标题
*/
@Override
public void setTitle(CharSequence title) {
super.setTitle(title);
if (getTitleBar() != null) {
getTitleBar().setTitle(title);
}
}
@Override
@Nullable
public TitleBar getTitleBar() {
if (mTitleBar == null) {
mTitleBar = obtainTitleBar(getContentView());
}
return mTitleBar;
}
@Override
public void onLeftClick(View view) {
onBackPressed();
}
@Override
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
super.startActivityForResult(intent, requestCode, options);
overridePendingTransition(R.anim.right_in_activity, R.anim.right_out_activity);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.left_in_activity, R.anim.left_out_activity);
}
/**
* {@link OnHttpListener}
*/
@Override
public void onStart(Call call) {
showDialog();
}
@Override
public void onSucceed(Object result) {
if (result instanceof HttpData) {
toast(((HttpData<?>) result).getMessage());
}
}
@Override
public void onFail(Exception e) {
toast(e.getMessage());
}
@Override
public void onEnd(Call call) {
hideDialog();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isShowDialog()) {
hideDialog();
}
mDialog = null;
EventBusManager.unregister(this);
}
}