-
Notifications
You must be signed in to change notification settings - Fork 990
/
chat2.container.tsx
182 lines (155 loc) · 5.17 KB
/
chat2.container.tsx
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
import React from 'react';
import { NavigationStackScreenProps } from 'react-navigation-stack';
import * as MediaLibrary from 'expo-media-library';
import * as Permissions from 'expo-permissions';
import {
ChatFileMessageAppearance,
ChatHeader,
ChatHeaderNavigationStateParams,
} from '@src/components/messaging';
import {
Conversation,
FileType,
Message,
Profile,
} from '@src/core/model';
import { conversation5, conversation6 } from '@src/core/data/conversation';
import {
profile1,
profile2,
} from '@src/core/data/profile';
import { imageMessage1 } from '@src/assets/images';
import { Chat2 } from './chat2.component';
interface State {
newMessageText: string;
galleryFiles: MediaLibrary.Asset[];
fileSectionOpened: boolean;
conversation: Conversation;
}
export class Chat2Container extends React.Component<NavigationStackScreenProps, State> {
public state: State = {
newMessageText: '',
galleryFiles: [],
fileSectionOpened: false,
conversation: conversation6,
};
static navigationOptions = ({ navigation, screenProps }) => {
const headerProps: ChatHeaderNavigationStateParams = {
interlocutor: navigation.getParam('interlocutor', conversation5.interlocutor),
lastSeen: navigation.getParam('lastSeen', 'today'),
onBack: navigation.getParam('onBack'),
onProfile: navigation.getParam('onProfile'),
};
const header = (navigationProps: NavigationStackScreenProps) => {
return (
<ChatHeader
{...navigationProps}
{...headerProps}
/>
);
};
return { ...navigation, ...screenProps, header };
};
public componentWillMount(): void {
MediaLibrary.getAssetsAsync({ first: 6 })
.then(this.onMediaResponse);
this.props.navigation.setParams({
interlocutor: this.state.conversation.interlocutor,
lastSeen: this.state.conversation.lastSeen,
onBack: this.onBackPress,
onProfile: this.onProfilePress,
});
}
private onProfilePress = (profile: Profile): void => {
this.props.navigation.navigate('Test Profile');
};
private onMediaResponse = (data: MediaLibrary.PagedInfo<MediaLibrary.Asset>): void => {
this.setState({ galleryFiles: data.assets });
};
private onNewMessageChange = (newMessageText: string): void => {
this.setState({ newMessageText });
};
private onMessageAddPress = (): void => {
const profiles: Profile[] = [profile1, profile2];
const newMessage: Message = {
author: profiles[Math.floor(Math.random() * profiles.length)],
text: this.state.newMessageText,
date: '15:01 PM',
read: false,
delivered: false,
};
const conversationCopy: Conversation = this.state.conversation;
conversationCopy.messages.push(newMessage);
this.setState({
conversation: conversationCopy,
newMessageText: '',
});
};
private onCameraPermissionResponse = (result: Permissions.PermissionResponse): void => {
if (result.status === 'granted') {
this.setState({ fileSectionOpened: true });
}
};
private onAddButtonPress = (): void => {
Permissions.askAsync(Permissions.CAMERA_ROLL)
.then(this.onCameraPermissionResponse);
};
private onCancelButtonPress = (): void => {
this.setState({ fileSectionOpened: false });
};
private onPhotoOrVideoButtonPress = (): void => {
};
private onFileButtonPress = (): void => {
};
private onLocationButtonPress = (): void => {
};
private onContactButtonPress = (): void => {
};
private onTakePhotoButtonPress = (): void => {
};
private onBackPress = (): void => {
this.props.navigation.goBack(null);
};
private onGalleryItemPress = (item: MediaLibrary.Asset) => {
// TODO: resolve somehow the possibility of adding a real asset
const profiles: Profile[] = [profile1, profile2];
const newMessage: Message = {
file: {
type: item.mediaType as FileType,
preview: imageMessage1,
name: item.filename,
},
date: '15:01 PM',
read: false,
delivered: false,
author: profiles[Math.floor(Math.random() * profiles.length)],
};
const conversationCopy: Conversation = this.state.conversation;
conversationCopy.messages.push(newMessage);
this.setState({
conversation: conversationCopy,
fileSectionOpened: false,
});
};
public render(): React.ReactNode {
return (
<Chat2
conversation={this.state.conversation}
fileSectionOpened={this.state.fileSectionOpened}
newMessage={this.state.newMessageText}
galleryFiles={this.state.galleryFiles}
fileMessageAppearance={ChatFileMessageAppearance.full}
onNewMessageChange={this.onNewMessageChange}
onMessageAdd={this.onMessageAddPress}
onAddButtonPress={this.onAddButtonPress}
onCancelButtonPress={this.onCancelButtonPress}
onPhotoOrVideoButtonPress={this.onPhotoOrVideoButtonPress}
onFileButtonPress={this.onFileButtonPress}
onLocationButtonPress={this.onLocationButtonPress}
onContactButtonPress={this.onContactButtonPress}
onTakePhotoButtonPress={this.onTakePhotoButtonPress}
onGalleryItemPress={this.onGalleryItemPress}
/>
);
}
}