1
- import { Switch , Text , TouchableOpacity , View } from "react-native"
1
+ import { Switch , Text , TouchableOpacity , useWindowDimensions , View } from "react-native"
2
2
import Ionicons from '@expo/vector-icons/Ionicons'
3
3
import styles from "./styles/taskStyle"
4
4
import TaskEntity from '../../../entities/task'
5
- import { useContext } from "react"
5
+ import React , { useContext } from "react"
6
6
import taskContext from "../../../dataManager/contexts/taskContext"
7
7
import { markTask } from "../../../dataManager/data/actions"
8
- import Animated , { Easing , Layout , SlideInRight , SlideOutLeft , SlideOutRight } from "react-native-reanimated"
8
+ import Animated , {
9
+ cond ,
10
+ Easing ,
11
+ interpolate ,
12
+ interpolateColor ,
13
+ interpolateColors ,
14
+ Layout ,
15
+ SlideInRight ,
16
+ SlideOutLeft ,
17
+ SlideOutRight ,
18
+ useAnimatedGestureHandler ,
19
+ useAnimatedStyle ,
20
+ useSharedValue ,
21
+ withSpring ,
22
+ withTiming
23
+ } from "react-native-reanimated"
9
24
import navigationContext from "../../../dataManager/contexts/navigationContext"
25
+ import { PanGestureHandler } from "react-native-gesture-handler"
10
26
11
27
type TaskPropType = ( { task} : { task : TaskEntity } ) => JSX . Element
28
+ type ContextType = {
29
+ x : number
30
+ }
12
31
13
32
const Task : TaskPropType = ( { task } ) => {
14
33
// Get data from the global state
15
34
const { dispatch, selectTask } = useContext ( taskContext )
16
35
const { changeModalVisible } = useContext ( navigationContext )
17
36
37
+ // use Dimensions
38
+ const { width : windowWidth } = useWindowDimensions ( )
39
+
40
+ // Some constants
41
+ const WINDOWWIDTH = windowWidth
42
+ const MINIMUMWIDTH = 100
43
+
44
+ // Set Shared value
45
+ const translationX = useSharedValue ( 0 )
46
+
18
47
const handleMarkTask = ( ) => {
19
48
// Change the state of the task
20
49
dispatch ( markTask ( task . getId , ! task . getMarked ) )
@@ -28,36 +57,125 @@ const Task: TaskPropType = ({ task }) => {
28
57
changeModalVisible ( )
29
58
}
30
59
60
+ const eventHandler = useAnimatedGestureHandler ( {
61
+ onStart : ( _ , context : ContextType ) => {
62
+ context . x = translationX . value
63
+ } ,
64
+ onActive : ( event , context ) => {
65
+ console . log ( event . translationX )
66
+ if ( event . translationX > 0 ) {
67
+ translationX . value = context . x + event . translationX
68
+ }
69
+ } ,
70
+ onEnd : ( ) => {
71
+ translationX . value = withTiming ( 0 , { duration : 300 } )
72
+ }
73
+ } )
74
+
75
+ // Define some animated styles
76
+ const translationStyle = useAnimatedStyle ( ( ) => {
77
+ return ( {
78
+ transform : [
79
+ {
80
+ translateX : interpolate (
81
+ translationX . value ,
82
+ [ 0 , MINIMUMWIDTH ] ,
83
+ [ 0 , MINIMUMWIDTH ] ,
84
+ "clamp"
85
+ )
86
+ }
87
+ ]
88
+ } )
89
+ } )
90
+
91
+ const taskBackgroundStyle = useAnimatedStyle ( ( ) => {
92
+ return ( {
93
+ position : "absolute" ,
94
+ top : 0 ,
95
+ left : 0 ,
96
+ width : "100%" ,
97
+ height : "100%" ,
98
+ backgroundColor : interpolateColor (
99
+ translationX . value ,
100
+ [ 0 , MINIMUMWIDTH ] ,
101
+ [ "#fff" , "#ef233c" ]
102
+ ) as string
103
+ } )
104
+ } )
105
+
106
+ const taskBackgroundIconStyle = useAnimatedStyle ( ( ) => {
107
+ return ( {
108
+ transform : [
109
+ {
110
+ scale : interpolate (
111
+ translationX . value ,
112
+ [ 0 , MINIMUMWIDTH ] ,
113
+ [ .5 , 1.2 ] ,
114
+ "clamp"
115
+ )
116
+ } ,
117
+ {
118
+ translateX : interpolate (
119
+ translationX . value ,
120
+ [ 0 , MINIMUMWIDTH ] ,
121
+ [ - 40 , 0 ] ,
122
+ "clamp"
123
+ )
124
+ }
125
+ ] ,
126
+ opacity : interpolate (
127
+ translationX . value ,
128
+ [ 0 , MINIMUMWIDTH ] ,
129
+ [ .4 , 1 ] ,
130
+ "clamp"
131
+ )
132
+ } )
133
+ } )
134
+
31
135
return (
32
136
< Animated . View
33
137
style = { styles . container }
34
138
entering = { SlideInRight }
35
139
layout = { Layout . springify ( ) }
36
- exiting = { SlideOutRight . duration ( 100 ) }
140
+ exiting = { SlideOutRight . duration ( 100 ) }
37
141
>
38
- < View style = { styles . leftSection } >
39
- < Switch
40
- value = { task . getMarked }
41
- thumbColor = { task . getMarked ? "#3e4bff" : "#eee" }
42
- trackColor = { {
43
- false : "#ced4da" ,
44
- true : "#48cae4"
45
- } }
46
- onValueChange = { handleMarkTask }
47
- />
48
- < Text style = { [ styles . taskText , task . getMarked && styles . taskMaked ] } > { task . getValue } </ Text >
49
- </ View >
50
-
51
- < TouchableOpacity
52
- style = { styles . taskMenu }
53
- onPress = { handleSelectTask }
54
- activeOpacity = { .6 }
142
+ < Animated . View
143
+ style = { [ taskBackgroundStyle ] }
55
144
>
56
- < Ionicons
57
- style = { styles . taskMenuIcon }
58
- name = "ellipsis-vertical"
59
- />
60
- </ TouchableOpacity >
145
+ < Animated . View style = { [ styles . containerBackgroundIcon , taskBackgroundIconStyle ] } >
146
+ < Ionicons name = "trash" size = { 25 } color = "#fff" />
147
+ </ Animated . View >
148
+ </ Animated . View >
149
+
150
+ < PanGestureHandler onGestureEvent = { eventHandler } >
151
+ < Animated . View
152
+ style = { [ styles . containerIn , translationStyle ] }
153
+ >
154
+ < View style = { styles . leftSection } >
155
+ < Switch
156
+ value = { task . getMarked }
157
+ thumbColor = { task . getMarked ? "#3e4bff" : "#eee" }
158
+ trackColor = { {
159
+ false : "#ced4da" ,
160
+ true : "#90e0ef"
161
+ } }
162
+ onValueChange = { handleMarkTask }
163
+ />
164
+ < Text style = { [ styles . taskText , task . getMarked && styles . taskMaked ] } > { task . getValue } </ Text >
165
+ </ View >
166
+
167
+ < TouchableOpacity
168
+ style = { styles . taskMenu }
169
+ onPress = { handleSelectTask }
170
+ activeOpacity = { .6 }
171
+ >
172
+ < Ionicons
173
+ style = { styles . taskMenuIcon }
174
+ name = "ellipsis-vertical"
175
+ />
176
+ </ TouchableOpacity >
177
+ </ Animated . View >
178
+ </ PanGestureHandler >
61
179
</ Animated . View >
62
180
)
63
181
}
0 commit comments