-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
276 lines (226 loc) · 6.67 KB
/
main.js
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
import Immutable from 'immutable';
import _ from 'lodash';
import {openDb as open, storage, getOrientation, getDeviceSize} from '../helpers';
const MERGE_SETTINGS = 'MAIN::MERGE_SETTINGS';
const SET_BACKGROUND_INDEX = 'MAIN::SET_BACKGROUND_INDEX';
const SET_DB = 'MAIN::SET_DB';
const SET_DB_ERROR = 'MAIN::SET_DB_ERROR';
const SET_FONT_SIZE = 'MAIN::SET_FONT_SIZE';
const SET_KEYBOARD_HEIGHT = 'MAIN::SET_KEYBOARD_HEIGHT';
const SET_LINE_HEIGHT = 'MAIN::SET_LINE_HEIGHT';
const SET_LOADING = 'MAIN::SET_LOADING';
const SET_SELECTED_TAB = 'MAIN::SET_SELECTED_TAB';
const SET_SIDE_MENU_STATUS = 'MAIN::SET_SIDE_MENU_STATUS';
const SET_SUTRA_MAP = 'MAIN::SET_SUTRA_MAP';
const SET_WYLIE_STATUS = 'MAIN::SET_WYLIE_STATUS';
const SET_ORIENTATION = 'MAIN::SET_ORIENTATION';
const SET_DEVICE_WIDTH = 'MAIN::SET_DEVICE_WIDTH';
const SET_DEVICE_HEIGHT = 'MAIN::SET_DEVICE_HEIGHT';
const defaultReaderSettings = {
backgroundIndex: 1,
fontSize: 16,
lineHeight: 2,
wylieOn: false
};
const initialState = Immutable.Map(Object.assign({
db: null,
dbError: null,
isLoading: false,
isSideMenuOpen: false,
keyboardHeight: 0,
keyboardOn: false,
selectedTab: 'category',
orientation: 'PROTRAIT', // PROTRAIT or LANDSCAPE
deviceWidth: 0,
deviceHeight: 0
}, defaultReaderSettings));
const actionsMap = {
[MERGE_SETTINGS]: (state, action) => state.merge(action.settings),
[SET_BACKGROUND_INDEX]: (state, action) => state.set('backgroundIndex', action.backgroundIndex),
[SET_DB]: (state, action) => state.set('db', action.db),
[SET_DB_ERROR]: (state, action) => state.set('dbError', action.err),
[SET_FONT_SIZE]: (state, action) => state.set('fontSize', action.fontSize),
[SET_LINE_HEIGHT]: (state, action) => state.set('lineHeight', action.lineHeight),
[SET_LOADING]: (state, action) => state.set('isLoading', action.isLoading),
[SET_SELECTED_TAB]: (state, action) => state.set('selectedTab', action.selectedTab),
[SET_SIDE_MENU_STATUS]: (state, action) => state.set('isSideMenuOpen', action.isSideMenuOpen),
[SET_WYLIE_STATUS]: (state, action) => state.set('wylieOn', action.wylieStatus),
[SET_KEYBOARD_HEIGHT]: (state, action) => state.set('keyboardHeight', action.keyboardHeight),
[SET_ORIENTATION]: (state, action) => state.set('orientation', action.orientation),
[SET_DEVICE_WIDTH]: (state, action) => state.set('deviceWidth', action.width),
[SET_DEVICE_HEIGHT]: (state, action) => state.set('deviceHeight', action.height)
};
export default function reducer(state = initialState, action) {
const reduceFn = actionsMap[action.type];
return reduceFn ? reduceFn(state, action) : state;
}
export function loadStorage() {
return async dispatch => {
const existedReaderSettings = await storage.get('readerSettings');
const readerSettings = Object.assign({}, defaultReaderSettings, existedReaderSettings);
dispatch(mergeSettings(readerSettings));
};
}
export async function setReaderSettings(data) {
let existedReaderSettings = await storage.get('readerSettings') || {};
await storage.set('readerSettings', Object.assign(existedReaderSettings, data));
}
export function openDb() {
return dispatch => {
return open()
.then(db => {
dispatch(setDb(db));
return db;
})
.catch(err => {
dispatch(setDbError(err));
return err;
});
};
}
export function mergeSettings(settings) {
return {
type: MERGE_SETTINGS,
settings
};
}
export function setDb(db) {
return {
type: SET_DB,
db
};
}
export function setDbError(err) {
return {
type: SET_DB_ERROR,
err
};
}
export function setLoading(isLoading) {
return {
type: SET_LOADING,
isLoading
};
}
export function setSelectedTab(selectedTab) {
return {
type: SET_SELECTED_TAB,
selectedTab
};
}
export function setSideMenuStatus(isSideMenuOpen) {
return {
type: SET_SIDE_MENU_STATUS,
isSideMenuOpen
};
}
export function setFontSize(fontSize) {
return {
type: SET_FONT_SIZE,
fontSize
};
}
export function setLineHeight(lineHeight) {
return {
type: SET_LINE_HEIGHT,
lineHeight
};
}
export function setWylieStatus(wylieStatus) {
return {
type: SET_WYLIE_STATUS,
wylieStatus
};
}
export function setBackgroundIndex(backgroundIndex) {
return {
type: SET_BACKGROUND_INDEX,
backgroundIndex
};
}
export function setKeyboardHeight(keyboardHeight) {
return {
type: SET_KEYBOARD_HEIGHT,
keyboardHeight
};
}
export function setOrientation(orientation) {
return {
type: SET_ORIENTATION,
orientation
};
}
export function setDeviceWidth(width) {
return {
type: SET_DEVICE_WIDTH,
width
};
}
export function setDeviceHeight(height) {
return {
type: SET_DEVICE_HEIGHT,
height
};
}
export function setDeviceOrientation() {
return async (dispatch) => {
let orientation = await getOrientation();
dispatch(setOrientation(orientation));
};
}
export function setDeviceSize() {
let {width, height} = getDeviceSize();
return async dispatch => {
dispatch(setDeviceWidth(width));
dispatch(setDeviceHeight(height));
};
}
export function increaseFontSize() {
return async (dispatch, getState) => {
let newFontSize = getState().main.get('fontSize') + 1;
if (newFontSize < 30) {
await setReaderSettings({fontSize: newFontSize});
dispatch(setFontSize(newFontSize));
}
};
}
export function decreaseFontSize() {
return async (dispatch, getState) => {
let newFontSize = getState().main.get('fontSize') - 1;
if (newFontSize > 0) {
await setReaderSettings({fontSize: newFontSize});
dispatch(setFontSize(newFontSize));
}
};
}
export function increaseLineHeight() {
return async (dispatch, getState) => {
let newLineHeight = parseFloat((getState().main.get('lineHeight') + 0.1).toFixed(1));
if (newLineHeight < 30) {
await setReaderSettings({lineHeight: newLineHeight});
dispatch(setLineHeight(newLineHeight));
}
};
}
export function decreaseLineHeight() {
return async (dispatch, getState) => {
let newLineHeight = parseFloat((getState().main.get('lineHeight') - 0.1).toFixed(1));
if (newLineHeight > 0) {
await setReaderSettings({lineHeight: newLineHeight});
dispatch(setLineHeight(newLineHeight));
}
};
}
export function toggleWylieStatus() {
return async (dispatch, getState) => {
let newWylieStatus = ! getState().main.get('wylieOn');
await setReaderSettings({toggleWylieStatus: newWylieStatus});
dispatch(setWylieStatus(newWylieStatus));
};
}
export function setBackground(backgroundIndex) {
return async (dispatch, getState) => {
await setReaderSettings({backgroundIndex});
dispatch(setBackgroundIndex(backgroundIndex));
};
}