2018-03-11 16:32:13 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { Provider, connect } from 'react-redux';
|
|
|
|
import DiscoverPage from './page/discover';
|
2018-04-24 21:32:17 +02:00
|
|
|
import {
|
|
|
|
AppRegistry,
|
|
|
|
AppState,
|
|
|
|
AsyncStorage,
|
|
|
|
Text,
|
|
|
|
View,
|
|
|
|
NativeModules
|
|
|
|
} from 'react-native';
|
2018-03-11 16:32:13 +01:00
|
|
|
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
|
|
|
|
import {
|
|
|
|
StackNavigator, addNavigationHelpers
|
|
|
|
} from 'react-navigation';
|
|
|
|
import { AppNavigator } from './component/AppNavigator';
|
|
|
|
import AppWithNavigationState from './component/AppNavigator';
|
|
|
|
import { persistStore, autoRehydrate } from 'redux-persist';
|
|
|
|
import createCompressor from 'redux-persist-transform-compress';
|
|
|
|
import createFilter from 'redux-persist-transform-filter';
|
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import {
|
|
|
|
Lbry,
|
|
|
|
claimsReducer,
|
|
|
|
costInfoReducer,
|
|
|
|
fileInfoReducer,
|
2018-05-03 20:58:31 +02:00
|
|
|
notificationsReducer,
|
2018-03-11 16:32:13 +01:00
|
|
|
searchReducer,
|
|
|
|
walletReducer
|
|
|
|
} from 'lbry-redux';
|
2018-03-23 00:03:05 +01:00
|
|
|
import settingsReducer from './redux/reducers/settings';
|
2018-04-24 21:32:17 +02:00
|
|
|
import moment from 'moment';
|
2018-03-11 16:32:13 +01:00
|
|
|
import { reactNavigationMiddleware } from './utils/redux';
|
|
|
|
|
|
|
|
function isFunction(object) {
|
|
|
|
return typeof object === 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNotFunction(object) {
|
|
|
|
return !isFunction(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBulkThunkMiddleware() {
|
|
|
|
return ({ dispatch, getState }) => next => action => {
|
|
|
|
if (action.type === 'BATCH_ACTIONS') {
|
|
|
|
action.actions.filter(isFunction).map(actionFn => actionFn(dispatch, getState));
|
|
|
|
}
|
|
|
|
return next(action);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableBatching(reducer) {
|
|
|
|
return function batchingReducer(state, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'BATCH_ACTIONS':
|
|
|
|
return action.actions.filter(isNotFunction).reduce(batchingReducer, state);
|
|
|
|
default:
|
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const router = AppNavigator.router;
|
|
|
|
const navAction = router.getActionForPathAndParams('Splash');
|
|
|
|
const initialNavState = router.getStateForAction(navAction);
|
|
|
|
const navigatorReducer = (state = initialNavState, action) => {
|
|
|
|
const nextState = AppNavigator.router.getStateForAction(action, state);
|
|
|
|
return nextState || state;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reducers = combineReducers({
|
|
|
|
claims: claimsReducer,
|
|
|
|
costInfo: costInfoReducer,
|
|
|
|
fileInfo: fileInfoReducer,
|
2018-05-03 20:58:31 +02:00
|
|
|
notifications: notificationsReducer,
|
2018-03-11 16:32:13 +01:00
|
|
|
search: searchReducer,
|
|
|
|
wallet: walletReducer,
|
2018-03-23 00:03:05 +01:00
|
|
|
nav: navigatorReducer,
|
|
|
|
settings: settingsReducer
|
2018-03-11 16:32:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const bulkThunk = createBulkThunkMiddleware();
|
|
|
|
const middleware = [thunk, bulkThunk, reactNavigationMiddleware];
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
const composeEnhancers = compose;
|
|
|
|
|
|
|
|
const store = createStore(
|
|
|
|
enableBatching(reducers),
|
|
|
|
{}, // initial state,
|
|
|
|
composeEnhancers(
|
|
|
|
autoRehydrate(),
|
|
|
|
applyMiddleware(...middleware)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const compressor = createCompressor();
|
|
|
|
const saveClaimsFilter = createFilter('claims', ['byId', 'claimsByUri']);
|
|
|
|
const subscriptionsFilter = createFilter('subscriptions', ['subscriptions']);
|
2018-03-23 08:09:01 +01:00
|
|
|
const settingsFilter = createFilter('settings', ['clientSettings']);
|
2018-05-03 20:58:31 +02:00
|
|
|
const walletFilter = createFilter('wallet', ['receiveAddress']);
|
2018-03-11 16:32:13 +01:00
|
|
|
|
|
|
|
const persistOptions = {
|
2018-05-03 20:58:31 +02:00
|
|
|
whitelist: ['claims', 'subscriptions', 'settings', 'wallet'],
|
2018-03-11 16:32:13 +01:00
|
|
|
// Order is important. Needs to be compressed last or other transforms can't
|
|
|
|
// read the data
|
2018-05-03 20:58:31 +02:00
|
|
|
transforms: [saveClaimsFilter, subscriptionsFilter, settingsFilter, walletFilter, compressor],
|
2018-03-11 16:32:13 +01:00
|
|
|
debounce: 10000,
|
|
|
|
storage: AsyncStorage
|
|
|
|
};
|
|
|
|
|
|
|
|
persistStore(store, persistOptions, err => {
|
|
|
|
if (err) {
|
|
|
|
console.log('Unable to load saved SETTINGS');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class LBRYApp extends React.Component {
|
2018-04-24 21:32:17 +02:00
|
|
|
componentDidMount() {
|
|
|
|
AsyncStorage.getItem('hasLaunched').then(value => {
|
|
|
|
if (value == null || value !== 'true') {
|
|
|
|
AsyncStorage.setItem('hasLaunched', 'true');
|
|
|
|
// only set firstLaunchTime since we've determined that this is the first app launch ever
|
|
|
|
AsyncStorage.setItem('firstLaunchTime', String(moment().unix()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-11 16:32:13 +01:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Provider store={store}>
|
|
|
|
<AppWithNavigationState />
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AppRegistry.registerComponent('LBRYApp', () => LBRYApp);
|
|
|
|
|
|
|
|
export default LBRYApp;
|