Skip to content

Commit 2bc0364

Browse files
committed
Client:优化性能;解决不能删除评论;优化UserView
1 parent 94a773c commit 2bc0364

12 files changed

Lines changed: 212 additions & 135 deletions

File tree

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/MomentActivity.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ public void onCommentClick(CommentItem item, int position, int index, boolean is
511511
showShortToast("只能删除自己的或自己的动态下的评论");
512512
return;
513513
}
514+
toCommentItem = item;
514515
new AlertDialog(context, null, "删除这条评论?", true, DIALOG_DELETE_COMMENT, MomentActivity.this).show();
515516
} else {
516517
showInput(item, position, index);

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/MomentListFragment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void onDragBottom(boolean rightToLeft) {
307307
@Override
308308
public void onRefresh() {
309309
if (range == RANGE_ALL) {
310-
super.onRefresh();
310+
run();
311311
} else {
312312
loadAfterCorrect();
313313
}

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/UserActivity.java‎

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import zuo.biao.library.ui.EditTextInfoWindow;
3131
import zuo.biao.library.ui.SelectPictureActivity;
3232
import zuo.biao.library.ui.TextClearSuit;
33+
import zuo.biao.library.ui.WebViewActivity;
3334
import zuo.biao.library.util.CommonUtil;
3435
import zuo.biao.library.util.DataKeeper;
3536
import zuo.biao.library.util.JSON;
@@ -272,58 +273,66 @@ public void initEvent() {//必须调用
272273

273274
new TextClearSuit().addClearListener(etUserRemark, findViewById(R.id.ivUserRemarkClear));//清空备注按钮点击监听
274275

275-
userView.setOnDataChangedListener(new OnDataChangedListener() {
276-
277-
@Override
278-
public void onDataChanged() {
279-
user = userView.getData();
280-
}
281-
});
282276

283277
if (isOnEditMode) {
284278
userView.setOnDataChangedListener(new OnDataChangedListener() {
285279

286280
@Override
287281
public void onDataChanged() {
288-
isDataChanged = true;
289282
user = userView.getData();
283+
isDataChanged = true;
290284
}
291285
});
292-
293-
userView.setOnClickListener(new OnClickListener() {
286+
}
287+
userView.setOnClickListener(new OnClickListener() {
294288

295-
@Override
296-
public void onClick(View v) {
297-
if (user == null) {
298-
return;
299-
}
300-
switch (v.getId()) {
301-
case R.id.ivUserViewHead:
289+
@Override
290+
public void onClick(View v) {
291+
if (user == null) {
292+
return;
293+
}
294+
switch (v.getId()) {
295+
case R.id.ivUserViewHead:
296+
if (isOnEditMode) {
302297
toActivity(SelectPictureActivity.createIntent(context), REQUEST_TO_SELECT_PICTURE, false);
303-
break;
304-
case R.id.tvUserViewName:
298+
} else {
299+
toActivity(WebViewActivity.createIntent(context, user.getHead(), user.getHead()));
300+
}
301+
break;
302+
case R.id.tvUserViewName:
303+
if (isOnEditMode) {
305304
toActivity(EditTextInfoWindow.createIntent(context, "名字", user.getName())
306305
, REQUEST_TO_EDIT_TEXT_INFO_NAME, false);
307-
break;
308-
default:
309-
switch (v.getId()) {
310-
case R.id.tvUserViewSex:
306+
} else {
307+
CommonUtil.copyText(context, user.getName());
308+
}
309+
break;
310+
default:
311+
switch (v.getId()) {
312+
case R.id.tvUserViewSex:
313+
if (isOnEditMode) {
311314
user.setSex(user.getSex() == User.SEX_FEMALE ? User.SEX_MAIL : User.SEX_FEMALE);
312-
break;
313-
case R.id.tvUserViewPhone:
315+
}
316+
break;
317+
case R.id.tvUserViewPhone:
318+
if (isOnEditMode) {
314319
toActivity(EditTextInfoWindow.createIntent(context, EditTextInfoWindow.TYPE_PHONE
315320
, "手机", user.getPhone()), REQUEST_TO_EDIT_TEXT_INFO_PHONE, false);
316-
break;
317-
default:
318-
return;
321+
} else {
322+
CommonUtil.copyText(context, user.getPhone());
319323
}
324+
break;
325+
default:
326+
return;
327+
}
328+
if (isOnEditMode) {
320329
userView.bindView(user);
321330
isDataChanged = true;
322-
break;
323-
}
324-
}
325-
});
326-
}
331+
}
332+
break;
333+
}
334+
}
335+
});
327336

328337
if (bottomMenuView != null) {
329338
bottomMenuView.setOnMenuItemClickListener(this);//底部菜单点击监听

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/UserListFragment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public void onDragBottom(boolean rightToLeft) {
317317
@Override
318318
public void onRefresh() {
319319
if (range == RANGE_ALL) {
320-
super.onRefresh();
320+
run();
321321
} else {
322322
loadAfterCorrect();
323323
}

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/base/BaseActivity.java‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ protected void onCreate(Bundle savedInstanceState) {
3939
super.onCreate(savedInstanceState);
4040

4141
setCurrentUser();
42-
42+
4343
BaseBroadcastReceiver.register(context, receiver, ActionUtil.ACTION_USER_CHANGED);
4444
}
4545

4646
private void setCurrentUser() {
4747
currentUser = APIJSONApplication.getInstance().getCurrentUser();
4848
currentUserId = currentUser == null ? 0 : currentUser.getId();
49-
isLoggedIn = isCurrentUserCorrect();
49+
isLoggedIn = isCurrentUserCorrect();
5050
}
5151

5252
protected static boolean isCurrentUser(long userId) {
@@ -77,20 +77,39 @@ public void initData() {
7777
@Override
7878
public abstract void run();
7979

80+
private boolean isDataChanged = false;
8081
/**
8182
*/
8283
protected void invalidate() {
84+
if (isRunning() == false) {
85+
isDataChanged = true;
86+
Log.w(TAG, "invalidate isRunning() == false >> return;");
87+
return;
88+
}
89+
isDataChanged = false;
90+
8391
setCurrentUser();
8492
loadAfterCorrect();
8593
}
94+
95+
@Override
96+
protected void onResume() {
97+
super.onResume();
98+
if (isDataChanged) {
99+
Log.d(TAG, "onResume isDataChanged >> invalidate();");
100+
invalidate();
101+
}
102+
}
86103

87104
/**
88105
* @param runnable
89106
*/
90107
protected void loadAfterCorrect() {
91-
if (isCurrentUserCorrect()) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
92-
run();
108+
if (isCurrentUserCorrect() == false) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
109+
Log.e(TAG, "loadAfterCorrect isCurrentUserCorrect() == false >> return;");
110+
return;
93111
}
112+
run();
94113
}
95114

96115

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/base/BaseBottomTabActivity.java‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
4646
private void setCurrentUser() {
4747
currentUser = APIJSONApplication.getInstance().getCurrentUser();
4848
currentUserId = currentUser == null ? 0 : currentUser.getId();
49-
isLoggedIn = isCurrentUserCorrect();
49+
isLoggedIn = isCurrentUserCorrect();
5050
}
5151

5252
protected static boolean isCurrentUser(long userId) {
@@ -78,20 +78,39 @@ public void initData() {
7878
@Override
7979
public abstract void run();
8080

81+
private boolean isDataChanged = false;
8182
/**
8283
*/
8384
protected void invalidate() {
85+
if (isRunning() == false) {
86+
isDataChanged = true;
87+
Log.w(TAG, "invalidate isRunning() == false >> return;");
88+
return;
89+
}
90+
isDataChanged = false;
91+
8492
setCurrentUser();
8593
loadAfterCorrect();
8694
}
95+
96+
@Override
97+
protected void onResume() {
98+
super.onResume();
99+
if (isDataChanged) {
100+
Log.d(TAG, "onResume isDataChanged >> invalidate();");
101+
invalidate();
102+
}
103+
}
87104

88105
/**
89106
* @param runnable
90107
*/
91108
protected void loadAfterCorrect() {
92-
if (isCurrentUserCorrect()) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
93-
run();
109+
if (isCurrentUserCorrect() == false) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
110+
Log.e(TAG, "loadAfterCorrect isCurrentUserCorrect() == false >> return;");
111+
return;
94112
}
113+
run();
95114
}
96115

97116

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/base/BaseFragment.java‎

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414

1515
package apijson.demo.client.base;
1616

17-
import zuo.biao.apijson.JSON;
18-
import zuo.biao.apijson.JSONResponse;
1917
import zuo.biao.library.base.BaseBroadcastReceiver;
20-
import zuo.biao.library.interfaces.OnResultListener;
21-
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
2218
import zuo.biao.library.util.Log;
2319
import zuo.biao.library.util.StringUtil;
2420
import android.content.BroadcastReceiver;
@@ -34,7 +30,6 @@
3430
import apijson.demo.client.application.APIJSONApplication;
3531
import apijson.demo.client.model.User;
3632
import apijson.demo.client.util.ActionUtil;
37-
import apijson.demo.client.util.HttpRequest;
3833

3934
public abstract class BaseFragment extends zuo.biao.library.base.BaseFragment implements Runnable {
4035
private static final String TAG = "BaseFragment";
@@ -59,7 +54,7 @@ public View onCreateView(LayoutInflater inflater,
5954
private void setCurrentUser() {
6055
currentUser = APIJSONApplication.getInstance().getCurrentUser();
6156
currentUserId = currentUser == null ? 0 : currentUser.getId();
62-
isLoggedIn = isCurrentUserCorrect();
57+
isLoggedIn = isCurrentUserCorrect();
6358
}
6459

6560
protected static boolean isCurrentUser(long userId) {
@@ -90,30 +85,38 @@ public void initData() {
9085
@Override
9186
public abstract void run();
9287

88+
private boolean isDataChanged = false;
9389
/**
9490
*/
95-
protected void invalidate(User user) {
96-
currentUser = user;
91+
protected void invalidate() {
92+
if (isRunning() == false) {
93+
isDataChanged = true;
94+
Log.w(TAG, "invalidate isRunning() == false >> return;");
95+
return;
96+
}
97+
isDataChanged = false;
98+
99+
setCurrentUser();
97100
loadAfterCorrect();
98101
}
102+
103+
@Override
104+
public void onResume() {
105+
super.onResume();
106+
if (isDataChanged) {
107+
Log.d(TAG, "onResume isDataChanged >> invalidate();");
108+
invalidate();
109+
}
110+
}
99111

100112
/**
101113
* @param runnable
102114
*/
103115
protected void loadAfterCorrect() {
104-
if (isCurrentUserCorrect() == false) {
105-
loadCurrentUser(new OnResultListener<User>() {
106-
107-
@Override
108-
public void onResult(User result) {
109-
if (isCurrentUserCorrect()) {
110-
run();
111-
}
112-
}
113-
});
116+
if (isCurrentUserCorrect() == false) {//请求currentUser都统一交给MainTabActivity,避免同时多次相同请求
117+
Log.e(TAG, "loadAfterCorrect isCurrentUserCorrect() == false >> return;");
114118
return;
115119
}
116-
117120
run();
118121
}
119122

@@ -132,47 +135,6 @@ public boolean isUserCorrect(User user) {
132135
return user != null && user.getId() > 0;
133136
}
134137

135-
public static final int HTTP_GET_USER = -1;
136-
137-
/**
138-
* @param listener
139-
*/
140-
public void loadCurrentUser(final OnResultListener<User> listener) {
141-
loadUser(APIJSONApplication.getInstance().getCurrentUserId(), listener);
142-
}
143-
/**
144-
* @param id
145-
* @param listener
146-
*/
147-
public void loadUser(long id, final OnResultListener<User> listener) {
148-
HttpRequest.getUser(id, HTTP_GET_USER, new OnHttpResponseListener() {
149-
150-
@Override
151-
public void onHttpResponse(final int requestCode, final String resultJson, final Exception e) {
152-
final User user = new JSONResponse(resultJson).getObject(User.class);
153-
if (isUserCorrect(user) == false) {
154-
showShortToast(R.string.get_failed);
155-
// listener.onHttpResponse(requestCode, null, new NullPointerException("user == null"
156-
// + (e == null ? "" : " because " + e.getMessage())));
157-
listener.onResult(null);
158-
return;
159-
}
160-
if (APIJSONApplication.getInstance().isCurrentUser(user.getId())) {
161-
APIJSONApplication.getInstance().saveCurrentUser(user);
162-
currentUser = APIJSONApplication.getInstance().getCurrentUser();
163-
}
164-
165-
runUiThread(new Runnable() {
166-
167-
@Override
168-
public void run() {
169-
listener.onResult(user);
170-
}
171-
});
172-
}
173-
});
174-
}
175-
176138

177139
@Override
178140
public void onDestroy() {
@@ -193,10 +155,10 @@ public void onReceive(Context context, Intent intent) {
193155

194156
if (ActionUtil.ACTION_USER_CHANGED.equals(action)) {
195157
if (isCurrentUser(intent.getLongExtra(INTENT_ID, 0))) {
196-
invalidate(JSON.parseObject(intent.getStringExtra(ActionUtil.INTENT_USER), User.class));
158+
invalidate();
197159
}
198160
}
199161
}
200162
};
201163

202-
}
164+
}

0 commit comments

Comments
 (0)