Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit 3fbb5b8

Browse files
undercaseasutula
authored andcommitted
Add ability to copy messages and comments (#1266)
* Add ability to copy messages and comments. Signed-off-by: Thomas Hobohm <public@thomashobohm.com> * Show action sheet even when messages aren't removable. Signed-off-by: Thomas Hobohm <public@thomashobohm.com>
1 parent c2e4128 commit 3fbb5b8

File tree

2 files changed

+99
-25
lines changed

2 files changed

+99
-25
lines changed

App/Containers/Comments.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react'
22
import { Dispatch } from 'redux'
33
import { connect } from 'react-redux'
4-
import { View, ScrollView, ViewStyle } from 'react-native'
4+
import { View, ScrollView, ViewStyle, Clipboard } from 'react-native'
55
import { NavigationActions, SafeAreaView } from 'react-navigation'
66
import Textile, { IComment } from '@textile/react-native-sdk'
77
import ActionSheet from 'react-native-actionsheet'
@@ -38,7 +38,11 @@ interface DispatchProps {
3838
interface ComponentState {
3939
submitting: boolean
4040
// The current selected block (message/photo). For use in the block action sheet
41-
selectedBlockId?: string
41+
selected?: {
42+
blockId: string
43+
canRemove: boolean
44+
text: string
45+
}
4246
}
4347

4448
type Props = StateProps & DispatchProps
@@ -112,7 +116,13 @@ class Comments extends Component<Props, ComponentState> {
112116

113117
render() {
114118
// Block action sheet
115-
const blockActionSheetOptions = ['Remove', 'Cancel']
119+
const blockActionSheetOptions = [
120+
...(this.state.selected && this.state.selected.canRemove
121+
? ['Remove']
122+
: []),
123+
'Copy',
124+
'Cancel'
125+
]
116126
const blockCancelButtonIndex = blockActionSheetOptions.indexOf('Cancel')
117127
const commentCardProps = this.props.comments
118128
.slice()
@@ -127,9 +137,8 @@ class Comments extends Component<Props, ComponentState> {
127137
comment: comment.body,
128138
date: Textile.util.timestampToDate(comment.date),
129139
isCaption: false,
130-
onLongPress: canRemove
131-
? () => this.showBlockActionSheet(comment.id)
132-
: undefined
140+
onLongPress: () =>
141+
this.showBlockActionSheet(comment.id, canRemove, comment.body)
133142
}
134143
return props
135144
})
@@ -169,18 +178,33 @@ class Comments extends Component<Props, ComponentState> {
169178
)
170179
}
171180

172-
showBlockActionSheet = (blockId: string) => {
181+
showBlockActionSheet = (
182+
blockId: string,
183+
canRemove: boolean,
184+
text: string
185+
) => {
173186
this.setState({
174-
selectedBlockId: blockId
187+
selected: {
188+
blockId,
189+
canRemove,
190+
text
191+
}
175192
})
176193
this.blockActionSheet.show()
177194
}
178195

179196
handleBlockActionSheetResponse = (index: number) => {
180197
const actions = [
181-
() =>
182-
this.state.selectedBlockId &&
183-
this.props.remove(this.state.selectedBlockId)
198+
...(this.state.selected && this.state.selected.canRemove
199+
? [
200+
() => {
201+
if (this.state.selected) {
202+
this.props.remove(this.state.selected.blockId)
203+
}
204+
}
205+
]
206+
: []),
207+
() => this.state.selected && Clipboard.setString(this.state.selected.text)
184208
]
185209
if (index < actions.length) {
186210
actions[index]()

App/screens/group/group.tsx

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
ListRenderItemInfo,
88
Dimensions,
99
TouchableWithoutFeedback,
10-
View
10+
View,
11+
Clipboard
1112
} from 'react-native'
1213
import { NavigationScreenProps, SafeAreaView } from 'react-navigation'
1314
import uuid from 'uuid/v4'
@@ -84,7 +85,12 @@ interface State {
8485
showInviteContactModal: boolean
8586
showRenameGroupModal: boolean
8687
// The current selected block (message/photo). For use in the block action sheet
87-
selectedBlockId?: string
88+
selected?: {
89+
blockId: string
90+
isCopyable: boolean
91+
canRemove: boolean
92+
text?: string
93+
}
8894
}
8995

9096
class Group extends React.PureComponent<Props, State> {
@@ -157,7 +163,15 @@ class Group extends React.PureComponent<Props, State> {
157163
]
158164
const threadCancelButtonIndex = threadActionSheetOptions.indexOf('Cancel')
159165
// Block action sheet
160-
const blockActionSheetOptions = ['Remove', 'Cancel']
166+
const blockActionSheetOptions = [
167+
...(this.state.selected && this.state.selected.canRemove
168+
? ['Remove']
169+
: []),
170+
...(this.state.selected && this.state.selected.isCopyable
171+
? ['Copy']
172+
: []),
173+
'Cancel'
174+
]
161175
const blockCancelButtonIndex = blockActionSheetOptions.indexOf('Cancel')
162176
return (
163177
<SafeAreaView style={{ flex: 1, flexGrow: 1 }}>
@@ -265,9 +279,13 @@ class Group extends React.PureComponent<Props, State> {
265279
id: comment.id,
266280
username: comment.user.name || '?',
267281
body: comment.body,
268-
onLongPress: canRemoveComment
269-
? () => this.showBlockActionSheet(comment.id)
270-
: undefined
282+
onLongPress: () =>
283+
this.showBlockActionSheet(
284+
comment.id,
285+
canRemoveComment,
286+
true,
287+
comment.body
288+
)
271289
}
272290
}
273291
)
@@ -312,7 +330,7 @@ class Group extends React.PureComponent<Props, State> {
312330
pinchHeight={pinchHeight}
313331
onLongPress={
314332
canRemove
315-
? () => this.showBlockActionSheet(item.block)
333+
? () => this.showBlockActionSheet(item.block, true, false)
316334
: undefined
317335
}
318336
/>
@@ -341,8 +359,14 @@ class Group extends React.PureComponent<Props, State> {
341359
const avatar = isSameUser ? undefined : user.avatar
342360
return (
343361
<TouchableWithoutFeedback
344-
disabled={!canRemove}
345-
onLongPress={() => this.showBlockActionSheet(item.block)}
362+
onLongPress={() =>
363+
this.showBlockActionSheet(
364+
item.block,
365+
canRemove,
366+
true,
367+
item.value.body
368+
)
369+
}
346370
>
347371
<View>
348372
<Message
@@ -399,9 +423,19 @@ class Group extends React.PureComponent<Props, State> {
399423
this.threadActionSheet.show()
400424
}
401425

402-
showBlockActionSheet = (blockId: string) => {
426+
showBlockActionSheet = (
427+
blockId: string,
428+
canRemove: boolean,
429+
isCopyable: boolean,
430+
text?: string
431+
) => {
403432
this.setState({
404-
selectedBlockId: blockId
433+
selected: {
434+
blockId,
435+
canRemove,
436+
isCopyable,
437+
text
438+
}
405439
})
406440
this.blockActionSheet.show()
407441
}
@@ -421,9 +455,25 @@ class Group extends React.PureComponent<Props, State> {
421455

422456
handleBlockActionSheetResponse = (index: number) => {
423457
const actions = [
424-
() =>
425-
this.state.selectedBlockId &&
426-
this.props.remove(this.state.selectedBlockId)
458+
...(this.state.selected && this.state.selected.canRemove
459+
? [
460+
() =>
461+
this.state.selected &&
462+
this.props.remove(this.state.selected.blockId)
463+
]
464+
: []),
465+
...(this.state.selected && this.state.selected.isCopyable
466+
? [
467+
() => {
468+
if (
469+
this.state.selected &&
470+
this.state.selected.text !== undefined
471+
) {
472+
Clipboard.setString(this.state.selected.text)
473+
}
474+
}
475+
]
476+
: [])
427477
]
428478
if (index < actions.length) {
429479
actions[index]()

0 commit comments

Comments
 (0)