Repository navigation
Expand file tree
/
Copy pathdown.js
More file actions
177 lines (171 loc) · 4.4 KB
/
Copy pathdown.js
File metadata and controls
177 lines (171 loc) · 4.4 KB
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
import { fromJS } from 'immutable'
import { handleActions } from 'redux-actions'
import RNFS from 'react-native-fs'
// Constants
// --------------------------------------------------
export const DOWN_SET = 'DOWN_SET'
export const DOWNLOADING_FILE = 'DOWNLOADING_FILE'
export const DOWNLOADED_FILE_DELETE = 'DOWNLOADED_FILE_DELETE'
// Actions
// ---------------------------------.3-----------------
export const setDownInformation = (
params = {},
cb = () => {},
) => async dispatch => {
await dispatch({
type: DOWN_SET,
payload: params,
})
cb && cb()
}
export const downloadFile = (params = {}) => async (dispatch, getState) => {
let value = 0
let timer = setInterval(async () => {
let shouldUpdate = false,
isExist = false
let downloads = getState().down.toJS().downloads
for (let index = 0; index < downloads.length; index++) {
const element = downloads[index]
if (element.id === params.key) {
isExist = true
shouldUpdate = value > downloads[index].progress
break
}
}
if (!isExist) {
shouldUpdate = true
}
if (shouldUpdate && value !== 100) {
const data = {
id: params.key,
progress: value,
params: params,
}
await dispatch({
type: DOWNLOADING_FILE,
payload: data,
})
}
if (value === 100) {
clearInterval(timer)
timer = null
}
}, 3000)
params.progress = async res => {
value = res.progress >= 0 ? ~~res.progress.toFixed(0) : 0
if (value === 100) {
const data = {
id: params.key,
progress: value,
params: params,
}
await dispatch({
type: DOWNLOADING_FILE,
payload: data,
})
}
}
let result = RNFS.downloadFile(params)
return result.promise
}
export const deleteDownloadFile = (params = {}) => async dispatch => {
await dispatch({
type: DOWNLOADED_FILE_DELETE,
payload: params,
})
}
const initialState = fromJS({
downList: [
{
isShowProgressView: false,
progress: '',
disabled: false,
index: 0,
},
{
isShowProgressView: false,
progress: '',
disabled: false,
index: 1,
},
{
isShowProgressView: false,
progress: '',
disabled: false,
index: 2,
},
{
isShowProgressView: false,
progress: '',
disabled: false,
index: 3,
},
],
downloads: [],
})
export default handleActions(
{
[`${DOWN_SET}`]: (state, { payload }) => {
let downList = state.toJS().downList
if (payload.index) {
let index = payload.index
downList[index].index = payload.index
if (payload.isShowProgressView) {
downList[index].isShowProgressView = payload.isShowProgressView
}
if (payload.progress) {
downList[index].progress = payload.progress
}
if (payload.disabled) {
downList[index].disabled = payload.disabled
}
}
return state.setIn(['downList'], fromJS(downList))
},
[`${DOWNLOADING_FILE}`]: (state, { payload }) => {
let downloads = state.toJS().downloads
if (payload.id) {
if (downloads.length > 0) {
let isItem = false
for (let index = 0; index < downloads.length; index++) {
const element = downloads[index]
if (
element.id === payload.id &&
payload.progress !== downloads[index].progress
) {
isItem = true
downloads[index] = payload
break
}
}
if (!isItem) {
downloads.push(payload)
}
} else {
downloads.push(payload)
}
}
return state.setIn(['downloads'], fromJS(downloads))
},
[`${DOWNLOADED_FILE_DELETE}`]: (state, { payload }) => {
let downloads = state.toJS().downloads
if (payload.id) {
if (downloads.length > 0) {
for (let index = 0; index < downloads.length; index++) {
const element = downloads[index]
if (element.id === payload.id) {
downloads.splice(index, 1)
break
}
}
}
}
return state.setIn(['downloads'], fromJS(downloads))
},
// [REHYDRATE]: () => {
// // return payload && payload.down ? fromJS(payload.down) : state
// return initialState
// },
},
initialState,
)