This repository was archived by the owner on Feb 21, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
380 lines (348 loc) Β· 10.7 KB
/
Copy pathApp.js
File metadata and controls
380 lines (348 loc) Β· 10.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import React, {Component} from 'react'
import {
Platform,
View,
TouchableOpacity
} from 'react-native'
import {
Container,
Header,
Title,
Content,
Button,
Left,
Right,
Body,
Icon,
Drawer
} from 'native-base'
import DropboxNoteList from './views/DropboxNoteList'
import SideBar from './components/SideBar'
import NoteModal from './views/note/NoteModal'
import NoteListItem from './components/NoteList/NoteListItem'
import styles from './AppStyle'
import AwsMobileAnalyticsConfig from './lib/AwsMobileAnalytics'
import { makeRandomHex } from './lib/Strings'
import RNFetchBlob from 'react-native-fetch-blob'
const fs = RNFetchBlob.fs
const dirs = RNFetchBlob.fs.dirs
const MARKDOWN_NOTE = 'MARKDOWN_NOTE'
// const SNIPPET_NOTE = 'SNIPPET_NOTE'
const DEFAULT_FOLDER = 'DEFAULT_FOLDER'
const HeaderLeft = ({openDrawer}) => (
<Left>
<View>
<Button transparent onPress={openDrawer}>
<Icon name='md-reorder' style={styles.headerMenuButton} />
</Button>
</View>
</Left>
)
const HeaderBody = ({mode}) => (
<Body>
<View>
<Title style={Platform.OS === 'android' ? styles.androidAppName : styles.iOsAppName}>
{
mode === 0
? 'All Notes'
: 'Dropbox'
}
</Title>
</View>
</Body>
)
const HeaderRight = ({onFilterFavorites, filterFavorites}) => (
<Right>
<View>
<TouchableOpacity onPress={onFilterFavorites}>
<Icon name={filterFavorites ? 'md-star' : 'md-star-outline'}
style={styles.headerRightMenuButton} />
</TouchableOpacity>
</View>
</Right>
)
const NoteList = ({noteList, filterFavorites, onStarPress, setNoteModalIsOpen}) => (
<Content contentContainerStyle={{ display: 'flex' }}>
{
noteList.map((note) => {
if (filterFavorites && !note.isStarred) return null
return <NoteListItem
note={note}
onStarPress={onStarPress}
onNotePress={setNoteModalIsOpen}
key={note.fileName} />
})
}
</Content>
)
const CreateNewNoteButton = ({onPressActionButton}) => (
<Button
transparent
onPress={() => onPressActionButton()}
style={styles.newPostButtonWrap}>
<View style={styles.newPostButton}>
<Icon name='md-add' style={{color: '#fff'}} />
</View>
</Button>
)
export default class App extends Component {
constructor () {
super()
this.state = {
isNoteOpen: false,
mode: 0, // 0: 'AllNote', 1: 'Dropbox'
noteList: [],
fileName: '',
content: '',
filterFavorites: false,
isConnectedToDropbox: false
}
// Init AwsMobileAnalytics
AwsMobileAnalyticsConfig.initAwsMobileAnalytics()
this.openDrawer = this.openDrawer.bind(this)
this.closeDrawer = this.closeDrawer.bind(this)
this.setNoteModalIsOpen = this.setNoteModalIsOpen.bind(this)
this.onStarPress = this.onStarPress.bind(this)
this.listDir = this.listDir.bind(this)
this.listFiles = this.listFiles.bind(this)
this.listFilesAndSetState = this.listFilesAndSetState.bind(this)
this.createDir = this.createDir.bind(this)
this.createNewNote = this.createNewNote.bind(this)
this.changeMode = this.changeMode.bind(this)
this.onFilterFavorites = this.onFilterFavorites.bind(this)
this.onPressActionButton = this.onPressActionButton.bind(this)
}
componentWillMount () {
this.listDir(dirs.DocumentDir)
.then((files) => {
// Check whether the 'Boostnote' folder exist or not.
const filteredFiles = files.filter((name) => {
return name === 'Boostnote'
})
// If not, create.
if (filteredFiles.length === 0) {
this.createDir()
}
return this.listFiles()
})
.then((files) => {
const filteredFiles = files.filter((name) => {
return name === 'boostnote.json'
})
// Check whether the folder has a setting file or not.
if (filteredFiles.length === 0) {
// If not, create.
const defaultJson = {
note: []
}
fs.createFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, JSON.stringify(defaultJson), 'utf8')
.catch(err => console.log(err))
}
return this.listFiles()
})
.then((files) => {
const filteredFiles = files.filter((name) => {
return name.endsWith('.md')
})
// Check whether the folder has any note files or not.
if (filteredFiles.length === 0) {
// If not, create.
this.createNewNote(`${makeRandomHex()}.md`)
}
return this.listFilesAndSetState()
})
.catch((err) => {
console.log(err)
})
}
openDrawer () {
this._drawer._root.open()
}
closeDrawer () {
this._drawer._root.close()
}
setNoteModalIsOpen (fileName, isOpen) {
if (isOpen) {
fs.readFile(`${dirs.DocumentDir}/Boostnote/${fileName}`, 'utf8')
.then((content) => {
this.setState({
fileName: fileName,
content: content,
isNoteOpen: true
})
})
} else {
AwsMobileAnalyticsConfig.recordDynamicCustomEvent('EDIT_NOTE')
this.listFilesAndSetState()
this.setState({
isNoteOpen: false
})
}
}
onStarPress (fileName) {
fs.readFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, 'utf8')
.then((content) => {
const contentObject = JSON.parse(content)
const newNotes = []
for (let i in contentObject.note) {
const newNote = {...contentObject.note[i]}
if (newNote.name === fileName) {
newNote.isStarred = !newNote.isStarred
}
newNotes.push(newNote)
}
contentObject.note = newNotes
fs.writeFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, JSON.stringify(contentObject), 'utf8')
.then(this.listFilesAndSetState)
.catch(err => console.log(err))
})
.catch((err) => {
console.log(err)
})
}
onFilterFavorites () {
this.setState((prevState, props) => {
return {filterFavorites: !prevState.filterFavorites}
})
}
listDir () {
return RNFetchBlob.fs.ls(`${dirs.DocumentDir}`)
}
listFiles () {
return RNFetchBlob.fs.ls(`${dirs.DocumentDir}/Boostnote`)
}
async listFilesAndSetState () {
const files = await this.listFiles()
const filteredFiles = files.filter((name) => {
return name.endsWith('.md')
})
const settingJsonFile = await fs.readFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, 'utf8')
// Change file name to object of file name and one liner content
const fileList = []
for (let i = 0; i < filteredFiles.length; i++) {
const fileName = filteredFiles[i]
const content = await fs.readFile(`${dirs.DocumentDir}/Boostnote/${fileName}`, 'utf8')
const filteredSettingFile = JSON.parse(settingJsonFile).note.filter(setting => {
return setting.name === fileName
})[0]
fileList.push({
fileName: fileName,
content: content === '' ? 'Tap here and write something!' : content.split(/\r\n|\r|\n/)[0],
createdAt: filteredSettingFile.createdAt,
isStarred: filteredSettingFile.isStarred,
updatedAt: filteredSettingFile.updatedAt,
})
}
fileList.sort((a, b) => {
return a.createdAt < b.createdAt ? 1 : -1
})
this.setState({
noteList: fileList
})
}
createDir () {
RNFetchBlob.fs.mkdir(`${dirs.DocumentDir}/Boostnote`)
.then(() => {
console.log('OK')
})
.catch(() => {
console.log('NG')
})
}
createNewNote (fileName, isOpen) {
const newFileName = fileName === '' ? `${makeRandomHex()}.md` : fileName
// Create a real file
fs.createFile(`${dirs.DocumentDir}/Boostnote/${newFileName}`, '', 'utf8')
.then((file) => {
this.setState({
isNoteOpen: isOpen,
fileName: newFileName,
content: ''
})
// Update setting file
return fs.readFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, 'utf8')
})
.then((content) => {
const contentObject = JSON.parse(content)
const date = new Date()
const thisNote = {
'type': MARKDOWN_NOTE,
'folder': DEFAULT_FOLDER,
'title': '',
'name': newFileName,
'isStarred': false,
'createdAt': date,
'updatedAt': date
}
contentObject.note.push(thisNote)
fs.writeFile(`${dirs.DocumentDir}/Boostnote/boostnote.json`, JSON.stringify(contentObject), 'utf8')
.catch(err => console.log(err))
AwsMobileAnalyticsConfig.recordDynamicCustomEvent('CREATE_NOTE')
})
.catch((err) => {
console.log(err)
})
}
changeMode (mode) {
this.setState({
mode: mode
})
}
onPressActionButton () {
if (this.state.mode === 0) {
this.createNewNote('', true)
} else if (this.state.mode === 1) {
this.refs.dropboxNoteList.createNewNote()
}
}
setIsConnectedToDropbox (value) {
this.setState({
isConnectedToDropbox: value
})
}
render () {
const { noteList, mode, filterFavorites, isNoteOpen, fileName, content, isConnectedToDropbox } = this.state
return (
<Drawer
ref={(ref) => {
this._drawer = ref
}}
content={
<SideBar
changeMode={this.changeMode}
onClose={() => this.closeDrawer()} />
}
panOpenMask={0.05}>
<Container>
<Header style={Platform.OS === 'android' ? styles.androidHeader : styles.iosHeader} androidStatusBarColor='#239F85'>
<HeaderLeft openDrawer={this.openDrawer} />
<HeaderBody mode={mode} />
<HeaderRight onFilterFavorites={this.onFilterFavorites}
filterFavorites={filterFavorites} />
</Header>
{
mode === 0
? <NoteList noteList={noteList}
filterFavorite={filterFavorites}
onStarPress={this.onStarPress}
setNoteModalIsOpen={this.setNoteModalIsOpen} />
: <DropboxNoteList ref='dropboxNoteList'
isConnectedToDropbox={isConnectedToDropbox}
setIsConnectedToDropbox={this.setIsConnectedToDropbox.bind(this)} />
}
</Container>
<View>
{
mode === 1 && !isConnectedToDropbox
? null
: <CreateNewNoteButton onPressActionButton={this.onPressActionButton} />
}
<NoteModal setIsOpen={this.setNoteModalIsOpen}
isNoteOpen={isNoteOpen}
fileName={fileName}
content={content} />
</View>
</Drawer>
)
}
}