-
Notifications
You must be signed in to change notification settings - Fork 3
Create an abstraction layer of async storage #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @format | ||
| * @flow | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const RNTesterList = require('./RNTesterList'); | ||
|
|
||
| export type RNTesterNavigationState = {openExample: ?string, ...}; | ||
|
|
||
| function RNTesterNavigationReducer( | ||
| state: ?RNTesterNavigationState, | ||
| action: any, | ||
| ): RNTesterNavigationState { | ||
| if ( | ||
| // Default value is to see example list | ||
| !state || | ||
| // Handle the explicit list action | ||
| action.type === 'RNTesterListAction' || | ||
| // Handle requests to go back to the list when an example is open | ||
| (state.openExample && action.type === 'RNTesterBackAction') | ||
| ) { | ||
| return { | ||
| // A null openExample will cause the views to display the RNTester example list | ||
| openExample: null, | ||
| }; | ||
| } | ||
|
|
||
| if (action.type === 'RNTesterExampleAction') { | ||
| // Make sure we see the module before returning the new state | ||
| const ExampleModule = RNTesterList.Modules[action.openExample]; | ||
|
|
||
| if (ExampleModule) { | ||
| return { | ||
| openExample: action.openExample, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return state; | ||
| } | ||
|
|
||
| module.exports = RNTesterNavigationReducer; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @format | ||
| * @flow | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import {Platform, Linking} from 'react-native'; | ||
| import AsyncStorage from '@react-native-community/async-storage'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be removed and the new native modules need to be imported here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @manyaagarwal , can't do that for the time being. We will have to create our local state management solution before that which a different issue for this sprint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what you mean by local state management solution? From what I understand @chirag-singhal has already implemented the native modules required for async storage in #162
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @manyaagarwal , I was referring to Chirag's PR only. Will only need to change the import statement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we can change it after #162 gets merged, but we should do that before merging this PR. |
||
| const RNTesterNavigationReducer = require('./RNTesterNavigationReducer'); | ||
| const URIActionMap = require('./URIActionMap'); | ||
|
|
||
| const APP_STATE_KEY = 'RNTesterAppState.v2'; | ||
|
|
||
| export const initializeInitialState = async (context) => { | ||
| const url = await Linking.getInitialURL(); | ||
| const [err, storedString] = await AsyncStorage.getItem(APP_STATE_KEY); | ||
| const exampleAction = URIActionMap(context.props.exampleFromAppetizeParams); | ||
| const urlAction = URIActionMap(url); | ||
| const launchAction = exampleAction || urlAction; | ||
|
|
||
| if (err || !storedString) { | ||
| const initialAction = launchAction || {type: 'InitialAction'}; | ||
| context.setState(RNTesterNavigationReducer(null, initialAction)); | ||
| return; | ||
| } | ||
| const storedState = JSON.parse(storedString); | ||
| if (launchAction) { | ||
| context.setState(RNTesterNavigationReducer(storedState, launchAction)); | ||
| return; | ||
| } | ||
| context.setState(storedState); | ||
|
|
||
| if (Platform.OS === 'ios') { | ||
| Linking.addEventListener('url', (url) => { | ||
| handleNavigation(URIActionMap(url)); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const handleNavigation = (context, action) => { | ||
| if (Platform.OS === 'android') { | ||
| context.drawer && context.drawer.closeDrawer(); // will need to purge this once the new design kicks in or is it ubiquitous | ||
| } | ||
|
|
||
| if (!action) { | ||
| return false; | ||
| } | ||
| const newState = RNTesterNavigationReducer(context.state, action); | ||
| if (context.state !== newState) { | ||
| context.setState(newState, () => | ||
| AsyncStorage.setItem(APP_STATE_KEY, JSON.stringify(context.state)), | ||
| ); | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason why this was moved from RNTesterNavigationReducer to RNTesterNavigationContext? @stealthanthrax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manyaagarwal , what do you mean by
this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the code in this file
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manyaagarwal , so that all the AsyncStorage interaction takes place from one file/location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stealthanthrax This is the same code from
RNTesterNavigationReducer, there has been no addition. Is there something else also that you intend to add to this file?