Skip to content

Commit 3d5cea1

Browse files
author
caiyg
committed
修复添加和删除好友bug
1 parent 00762e6 commit 3d5cea1

File tree

8 files changed

+89
-35
lines changed

8 files changed

+89
-35
lines changed

ReactJChat.iml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module external.linked.project.id="ReactJChat" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="android" name="Android">
5+
<configuration />
6+
</facet>
7+
</component>
38
<component name="NewModuleRootManager" inherit-compiler-output="false">
49
<output url="file://$MODULE_DIR$/build" />
510
<output-test url="file://$MODULE_DIR$/build" />
611
<exclude-output />
712
<content url="file://$MODULE_DIR$">
13+
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
814
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
915
<excludeFolder url="file://$MODULE_DIR$/build" />
1016
</content>
11-
<orderEntry type="inheritedJdk" />
17+
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
1218
<orderEntry type="sourceFolder" forTests="false" />
1319
</component>
1420
</module>

android/app/src/io/jchat/android/ConversationTOJSON.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public ConversationToJSON(Context context, Conversation conv) {
7575
lastMsg = ((TextContent) message.getContent()).getText();
7676
}
7777
}
78-
myConversation = new MyConversation(title, username, groupId, avatarPath, unreadMsgCnt, date, lastMsg);
78+
myConversation = new MyConversation(title, username, groupId, avatarPath, unreadMsgCnt,
79+
date, lastMsg, conv.getTargetAppKey());
7980
Gson gson = new Gson();
8081
mResult = gson.toJson(myConversation);
8182
}
@@ -134,7 +135,8 @@ public ConversationToJSON(Context context, List<Conversation> data) {
134135
lastMsg = ((TextContent) message.getContent()).getText();
135136
}
136137
}
137-
myConversation = new MyConversation(title, username, groupId, avatarPath, unreadMsgCnt, date, lastMsg);
138+
myConversation = new MyConversation(title, username, groupId, avatarPath, unreadMsgCnt,
139+
date, lastMsg, conv.getTargetAppKey());
138140
list.add(myConversation);
139141
}
140142
Gson gson = new Gson();

android/app/src/io/jchat/android/JMessageHelper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,17 @@ public void getConvList(Callback successCallback, Callback errorCallback) {
323323
* @param callback 回调
324324
*/
325325
@ReactMethod
326-
public void addFriend(final String username, final Callback callback) {
326+
public void addFriend(final String username, final Callback callback, final Callback failCallback) {
327327
mContext = getCurrentActivity();
328328
if (TextUtils.isEmpty(username)) {
329329
HandleResponseCode.onHandle(mContext, 802001, true);
330+
failCallback.invoke();
330331
} else if (username.equals(JMessageClient.getMyInfo().getUserName())) {
331332
HandleResponseCode.onHandle(mContext, 1003, true);
333+
failCallback.invoke();
332334
} else if (isExistConv(username)) {
333335
HandleResponseCode.onHandle(mContext, 810007, true);
336+
failCallback.invoke();
334337
} else {
335338
final ProgressDialog dialog = new ProgressDialog(mContext);
336339
dialog.setMessage(mContext.getString(R.string.adding_hint));
@@ -347,6 +350,7 @@ public void gotResult(int status, String desc, UserInfo userInfo) {
347350
callback.invoke(result);
348351
} else {
349352
HandleResponseCode.onHandle(mContext, status, false);
353+
failCallback.invoke();
350354
}
351355
}
352356
});
@@ -358,6 +362,17 @@ public boolean isExistConv(String username) {
358362
return conv != null;
359363
}
360364

365+
@ReactMethod
366+
public void deleteConversation(String username, int groupId, String appKey, Callback callback) {
367+
if (groupId != 0) {
368+
JMessageClient.deleteGroupConversation(groupId);
369+
callback.invoke();
370+
} else {
371+
JMessageClient.deleteSingleConversation(username, appKey);
372+
callback.invoke();
373+
}
374+
}
375+
361376
@Override
362377
public void onActivityResult(int requestCode, int resultCode, Intent data) {
363378
final Activity activity = getCurrentActivity();

android/app/src/io/jchat/android/MyConversation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ public class MyConversation {
1010
private int unreadMsgCnt;
1111
private String date;
1212
private String lastMsg;
13+
private String appKey;
1314

1415
public MyConversation(String title, String username, long groupId, String avatarPath,
15-
int unreadMsgCnt, String date, String lastMsg) {
16+
int unreadMsgCnt, String date, String lastMsg, String appKey) {
1617
this.title = title;
1718
this.username = username;
1819
this.groupId = groupId;
1920
this.avatarPath = avatarPath;
2021
this.unreadMsgCnt = unreadMsgCnt;
2122
this.date = date;
2223
this.lastMsg = lastMsg;
24+
this.appKey = appKey;
2325
}
2426
}

react-native-android/actions/ActionTypes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
export const INITIAL_CONVERSATION_LIST = 'INITIAL_CONVERSATION_LIST';
44
export const LOAD_CONVERSATIONS = 'LOAD_CONVERSATIONS';
55
export const SELECT_CONVERSATION = 'SELECT_CONVERSATION';
6-
export const ADDING_FRIEND = "ADDING_FRIEND";
7-
export const ADD_FRIEND_SUCCESS = "ADD_FRIEND_SUCCESS";
6+
export const ADDING_FRIEND = 'ADDING_FRIEND';
7+
export const ADD_FRIEND_SUCCESS = 'ADD_FRIEND_SUCCESS';
8+
export const ADD_FRIEND_ERROR = 'ADD_FRIEND_ERROR';
89
export const DELETE_CONVERSATION = 'DELETE_CONVERSATION';
910
export const LOAD_ERROR = 'LOAD_ERROR';

react-native-android/actions/conversationList.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ var {
77
} = React;
88
var JMessageHelper = NativeModules.JMessageHelper;
99

10-
11-
1210
export function loadConversations() {
1311
return dispatch => {
1412
type: types.INITIAL_CONVERSATION_LIST,
@@ -34,6 +32,22 @@ export function addFriend(username) {
3432
type: types.ADD_FRIEND_SUCCESS,
3533
conversation: JSON.parse(result)
3634
});
35+
}, () => {
36+
dispatch ({
37+
type: types.ADD_FRIEND_ERROR,
38+
});
39+
})
40+
}
41+
}
42+
43+
export function deleteConversation(conversation: Object, selected: number) {
44+
return dispatch => {
45+
JMessageHelper.deleteConversation(conversation.username, conversation.groupId, conversation.appKey, () => {
46+
dispatch ({
47+
type: types.DELETE_CONVERSATION,
48+
selected: selected,
49+
conversation: conversation
50+
});
3751
})
3852
}
3953
}

react-native-android/containers/conv_fragment.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import ChatActivity from './chat_activity';
2626
import Modal from 'react-native-root-modal';
2727
var JMessageHelper = NativeModules.JMessageHelper;
2828
var _convList = [];
29+
var convReducer;
2930

3031
export default class Conv extends Component {
3132

@@ -38,6 +39,7 @@ export default class Conv extends Component {
3839
showAddFriendDialog: false,
3940
showDelConvDialog: false,
4041
title: '',
42+
rowID: '',
4143
scaleAnimation: new Animated.Value(1),
4244
y: new Animated.Value(0),
4345
friendId: '',
@@ -54,6 +56,7 @@ export default class Conv extends Component {
5456
this.renderRow = this.renderRow.bind(this);
5557
this.longPressRow = this.longPressRow.bind(this);
5658
this.pressRow = this.pressRow.bind(this);
59+
this.delConvClick = this.delConvClick.bind(this);
5760
}
5861

5962
componentWillMount() {
@@ -64,16 +67,9 @@ export default class Conv extends Component {
6467

6568
componentDidMount() {
6669
const { loadConversations } = this.props.actions;
70+
const { conversationList } = this.props.state;
6771
loadConversations();
68-
// JMessageHelper.getConvList((result) => {
69-
// _convList = JSON.parse(result);
70-
// this.setState({
71-
// isLoading: false,
72-
// dataSource: this.getDataSource(_convList),
73-
// });
74-
// }, () => {
75-
// this.setState({isLoading: false});
76-
// });
72+
7773
JMessageHelper.checkNetwork((value) => {
7874
this.setState({ disconnected: value });
7975
});
@@ -156,6 +152,7 @@ export default class Conv extends Component {
156152
longPressRow(rowID: number) {
157153
if (!this.state.showDelConvDialog && !this.state.showAddFriendDialog) {
158154
console.log('rowID ' +rowID + ' long pressed!');
155+
this.setState({rowID: rowID});
159156
Animated.spring(this.state.scaleAnimation, {
160157
toValue: 1
161158
}).start( () => this.setState({
@@ -211,11 +208,14 @@ export default class Conv extends Component {
211208
addFriend() {
212209
const { addFriend } = this.props.actions;
213210
addFriend(this.state.friendId);
214-
const { fetching } = this.props.state;
215-
if (!fetching) {
211+
var adding = convReducer.adding;
212+
var error = convReducer.error;
213+
console.log('adding: ' + adding);
214+
if (adding !== undefined && !adding) {
216215
this.dismissAddFriendDialog();
216+
} else if (error !== undefined && error) {
217+
this.setState({friendId: ''});
217218
}
218-
219219
// JMessageHelper.addFriend(this.state.friendId, (result) => {
220220
// this.dismissAddFriendDialog();
221221
// var newDs = JSON.parse(result);
@@ -242,9 +242,11 @@ export default class Conv extends Component {
242242
});
243243
}
244244

245-
deleteConversation() {
246-
247-
this.dismissDelConvDialog();
245+
delConvClick() {
246+
const { deleteConversation } = this.props.actions;
247+
var conversation = _convList[this.state.rowID];
248+
deleteConversation(conversation, this.state.rowID);
249+
this.dismissDelConvDialog();
248250
}
249251

250252
dismissDelConvDialog() {
@@ -256,7 +258,6 @@ export default class Conv extends Component {
256258
render() {
257259
const { conversationList } = this.props.state;
258260
_convList = conversationList.convList;
259-
console.log('conversationList: ' + conversationList);
260261
var content = conversationList.dataSource.length === 0 ?
261262
<View style = { styles.container }>
262263
{ conversationList.fetching && <View style = { {alignItems: 'center', justifyContent: 'center'} }>
@@ -354,7 +355,7 @@ export default class Conv extends Component {
354355
<TouchableHighlight style = { {position: 'absolute', bottom: 0, left: 0, right: 0,
355356
paddingTop: 10, paddingBottom: 10, paddingLeft: 20} }
356357
underlayColor = { '#dddddd' }
357-
onPress = { this.deleteConversation }>
358+
onPress = { this.delConvClick }>
358359
<Text style = { {fontSize: 18, color: '#4e4e4e'}}>
359360
删除该聊天
360361
</Text>

react-native-android/reducers/conversationList.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
import React from 'react-native';
44
import * as types from '../actions/ActionTypes';
55
import { combineReducers } from 'redux';
6+
import Immutable from 'immutable';
67
var {
78
ListView,
89
NativeModules,
910
} = React;
1011
var JMessageHelper = NativeModules.JMessageHelper;
1112

13+
1214
export default function conversationList(state, action) {
1315
state = state || {
1416
type: types.INITIAL_CONVERSATION_LIST,
15-
convList: [],
1617
dataSource: [],
1718
fetching: true,
19+
adding: true,
20+
error: false,
1821
}
1922

2023
switch(action.type) {
@@ -25,7 +28,7 @@ export default function conversationList(state, action) {
2528
return {
2629
...state,
2730
...action,
28-
convList,
31+
convList: convList,
2932
dataSource,
3033
fetching: false
3134
}
@@ -41,30 +44,40 @@ export default function conversationList(state, action) {
4144
return {
4245
...state,
4346
...action,
44-
fetching: true
47+
adding: true
4548
}
4649
break;
4750
case types.ADD_FRIEND_SUCCESS:
4851
var convList = [...state.convList];
4952
convList.unshift(action.conversation);
5053
dataSource = state.dataSource.cloneWithRows(convList);
54+
console.log('convList: ' + convList);
5155
return {
5256
...state,
5357
...action,
54-
convList,
58+
convList: convList,
5559
dataSource,
56-
fetching: false
60+
adding: false
61+
}
62+
case types.ADD_FRIEND_ERROR:
63+
console.log('error: ');
64+
return {
65+
...state,
66+
...action,
67+
adding: true,
68+
error: true
5769
}
5870
case types.DELETE_CONVERSATION:
5971
var selected = action.selected;
6072
var convList = [...state.convList];
61-
var index = convList.indexOf(selected);
62-
convList = convList.splice(index, 1);
63-
dataSource = state.dataSource.cloneWithRows(convList);
73+
convList.splice(selected, 1);
74+
var newList = new Array();
75+
newList = convList;
76+
dataSource = state.dataSource.cloneWithRows(newList);
6477
return {
6578
...state,
6679
...action,
67-
convList,
80+
convList: newList,
6881
dataSource
6982
}
7083
default:

0 commit comments

Comments
 (0)