Repository navigation
Expand file tree
/
Copy pathBaseFragment.java
More file actions
183 lines (156 loc) · 4.94 KB
/
Copy pathBaseFragment.java
File metadata and controls
183 lines (156 loc) · 4.94 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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.client.base;
import zuo.biao.apijson.JSONResponse;
import zuo.biao.library.base.BaseBroadcastReceiver;
import zuo.biao.library.base.BaseView.OnDataChangedListener;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import apijson.demo.client.R;
import apijson.demo.client.activity_fragment.LoginActivity;
import apijson.demo.client.application.APIJSONApplication;
import apijson.demo.client.model.User;
import apijson.demo.client.util.ActionUtil;
public abstract class BaseFragment extends zuo.biao.library.base.BaseFragment {
private static final String TAG = "BaseFragment";
protected User currentUser;
protected long currentUserId;
protected boolean isLoggedIn;
@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setCurrentUser();
return view;
}
private void setCurrentUser() {
currentUser = APIJSONApplication.getInstance().getCurrentUser();
currentUserId = currentUser == null ? 0 : currentUser.getId();
isLoggedIn = isCurrentUserCorrect();
}
protected boolean verifyHttpLogin(int code) {
if (isAlive() == false) {
return true;
}
if (code == JSONResponse.CODE_NOT_LOGGED_IN) {
APIJSONApplication.getInstance().logout();
setCurrentUser();
}
return verifyLogin();
}
protected static boolean isCurrentUser(long userId) {
return APIJSONApplication.getInstance().isCurrentUser(userId);
}
/**未登录会toLoginActivity();
* @return isLoggedIn
*/
protected boolean verifyLogin() {
if (isLoggedIn == false) {
showShortToast("请先登录");
toLoginActivity();
}
return isLoggedIn;
}
protected void toLoginActivity() {
startActivity(LoginActivity.createIntent(context));
context.overridePendingTransition(R.anim.bottom_push_in, R.anim.hold);
}
@Override
public void initData() {
loadAfterCorrect();
}
private boolean isDataChanged = false;
/**
*/
protected void invalidate() {
if (isRunning() == false) {
isDataChanged = true;
Log.w(TAG, "onDataChanged isRunning() == false >> return;");
return;
}
isDataChanged = false;
setCurrentUser();
loadAfterCorrect();
}
@Override
public void onResume() {
super.onResume();
if (isDataChanged) {
if (onDataChangedListener != null) {
Log.d(TAG, "onResume isDataChanged >> onDataChangedListener.onDataChanged();");
onDataChangedListener.onDataChanged();
}
}
}
/**
* @param runnable
*/
protected void loadAfterCorrect() {
if (isCurrentUserCorrect() == false) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
Log.e(TAG, "loadAfterCorrect isCurrentUserCorrect() == false >> return;");
return;
}
if (onDataChangedListener != null) {
onDataChangedListener.onDataChanged();
}
}
/**
* @return
*/
public boolean isCurrentUserCorrect() {
return isUserCorrect(currentUser);
}
/**
* @param user
* @return
*/
public boolean isUserCorrect(User user) {
return user != null && user.getId() > 0;
}
@Override
public void onDestroy() {
unregisterObserver();
super.onDestroy();
}
private OnDataChangedListener onDataChangedListener;
protected void registerObserver(OnDataChangedListener onDataChangedListener) {
this.onDataChangedListener = onDataChangedListener;
BaseBroadcastReceiver.register(context, receiver, ActionUtil.ACTION_USER_CHANGED);
}
protected void unregisterObserver() {
onDataChangedListener = null;
BaseBroadcastReceiver.unregister(context, receiver);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent == null ? null : intent.getAction();
if (isAlive() == false || StringUtil.isNotEmpty(action, true) == false) {
Log.e(TAG, "receiver.onReceive isAlive() == false" +
" || StringUtil.isNotEmpty(action, true) == false >> return;");
return;
}
if (ActionUtil.ACTION_USER_CHANGED.equals(action)) {
if (isCurrentUser(intent.getLongExtra(INTENT_ID, 0))) {
invalidate();
}
}
}
};
}