Skip to content

Commit

Permalink
New storage: mmkv
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgustavo committed Aug 15, 2024
1 parent e7c94ac commit 66c9b86
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 144 deletions.
6 changes: 6 additions & 0 deletions android/app/src/main/java/com/worknotes/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.worknotes

import android.os.Bundle;

import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate

class MainActivity : ReactActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null)
}

/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
Expand Down
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/**
* @format
*/

import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);
import getStore from './src/store';
import {PersistGate} from 'redux-persist/integration/react';
import {Provider} from 'react-redux';

const {store, persistor} = getStore();

const ReduxProvider = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
);
};

AppRegistry.registerComponent(appName, () => ReduxProvider);
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
"test": "jest"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.23.1",
"@react-native-community/masked-view": "^0.1.11",
"@react-navigation/native": "^6.1.17",
"@react-navigation/native-stack": "^6.9.26",
"@react-navigation/native": "^6.1.18",
"@react-navigation/native-stack": "^6.11.0",
"moment": "^2.30.1",
"react": "18.2.0",
"react-native": "0.73.6",
"react-native-gesture-handler": "^2.16.0",
"react-native-mmkv": "^2.12.2",
"react-native-reanimated": "^3.8.1",
"react-native-safe-area-context": "^4.9.0",
"react-native-screens": "^3.30.1",
"react-native-safe-area-context": "^4.10.8",
"react-native-screens": "^3.34.0",
"react-native-svg": "^15.1.0",
"react-redux": "^9.1.0",
"redux": "^5.0.1",
"redux-persist": "^6.0.0",
"redux-thunk": "^3.1.0",
"reselect": "^5.1.1",
"styled-components": "^6.1.8"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/components/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const List = ({notes, navigation}: Props) => {
style={[styles.noteContainer, {backgroundColor: colors.card}]}
onPress={() => {
navigation.push('ViewNote', {
notes: notes,
note: item.note,
id: item.id,
text: item.text,
date: item.date,
});
}}>
Expand All @@ -31,7 +31,7 @@ const List = ({notes, navigation}: Props) => {
{moment(item.date).format('dddd, MMMM Do YYYY')}
</Text>
<Text style={[styles.noteContent, {color: colors.text}]}>
{item.note}
{item.text}
</Text>
<Text style={[styles.noteDate, {color: colors.text}]}>
{moment(item.date).fromNow()}
Expand Down
20 changes: 15 additions & 5 deletions src/screens/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import {
} from 'react-native';
import {useTheme} from '@react-navigation/native';

import {storeData} from '../services/storage';
import {useAppDispatch} from '../store';
import {createNote} from '../store/app';
import styles from '../styles';

const AddNote = ({route, navigation}) => {
const getUniqueId = () => {
return Math.random().toString(36).substr(2, 9);
};

const AddNote = ({navigation}) => {
const dispatch = useAppDispatch();
const {colors} = useTheme();
const scheme = useColorScheme();
let {notes} = route.params ? route.params.notes : {notes: []};
const [textAreaValue, setTextAreaValue] = useState('');
const today = Date.now();

const addNote = (text: string) => {
notes.push({note: text, date: today});
storeData(notes);
dispatch(
createNote({
id: getUniqueId(),
text: text,
date: Date.now(),
}),
);
};

return (
Expand Down
38 changes: 9 additions & 29 deletions src/screens/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,28 @@ import {
} from 'react-native';
import {useTheme} from '@react-navigation/native';

import {getData} from '../services/storage';
import {useAppDispatch, useAppSelector, RootState} from '../store';
import {notesInitialize} from '../store/app';
import {NoteObject} from '../store/app/app.types';

import Welcome from '../components/welcome';
import List from '../components/list';
import Error from '../components/error';
import styles from '../styles';

const Home = ({navigation}) => {
const dispatch = useAppDispatch();
const {colors} = useTheme();
const [notes, setNotes] = useState([]);
const _notes = useAppSelector(({APP}: RootState) => APP.notes);
const [notes, setNotes] = useState<NoteObject[]>(_notes);

const [error, setError] = useState(false);
const [loaded, setLoaded] = useState(false);

const readNotesFromStorage = async () => {
const data = (await getData()) || [];
setNotes(data);
};

useEffect(() => {
readNotesFromStorage()
.catch(() => {
setError(true);
})
.finally(() => {
setLoaded(true);
});
}, [notes]);

useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => navigation.navigate('AddNote', {notes: {notes}})}>
<TouchableOpacity onPress={() => navigation.navigate('AddNote')}>
<Text style={{color: colors.text}}>New Note</Text>
</TouchableOpacity>
),
Expand All @@ -50,16 +38,8 @@ const Home = ({navigation}) => {
return (
<SafeAreaView
style={[styles.globalContainer, {backgroundColor: colors.background}]}>
{notes.length === 0 && loaded ? (
<Welcome navigation={navigation} />
) : null}
{loaded && !error && <List notes={notes} navigation={navigation} />}
{!loaded ? (
<ActivityIndicator size="large" style={styles.welcomeContainer} />
) : null}
{error && (
<Error errorText1={'Error'} errorText2={'Could not load the page'} />
)}
{notes.length === 0 ? <Welcome navigation={navigation} /> : null}
{!error && <List notes={notes} navigation={navigation} />}
</SafeAreaView>
);
};
Expand Down
15 changes: 8 additions & 7 deletions src/screens/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import React, {useLayoutEffect} from 'react';
import moment from 'moment';
import {View, Text, TouchableOpacity} from 'react-native';
import {useTheme} from '@react-navigation/native';
import {storeData} from '../services/storage';
import {useAppDispatch} from '../store';
import {deleteNote} from '../store/app';

import styles from '../styles';

const ViewNote = ({route, navigation}) => {
const dispatch = useAppDispatch();
const {colors} = useTheme();
let {notes, note, date} = route.params;
const deleteNote = () => {
notes.splice(notes.indexOf(date), 1);
storeData(notes);
let {id, text, date} = route.params;
const _delete = () => {
dispatch(deleteNote(id));
navigation.goBack();
};
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity onPress={() => deleteNote()}>
<TouchableOpacity onPress={() => _delete()}>
<Text style={{color: colors.notification}}>Delete</Text>
</TouchableOpacity>
),
Expand All @@ -28,7 +29,7 @@ const ViewNote = ({route, navigation}) => {
<Text style={[styles.title, {color: colors.text}]}>
{moment(date).format('dddd, MMMM Do YYYY')}
</Text>
<Text style={[styles.noteMainContent, {color: colors.text}]}>{note}</Text>
<Text style={[styles.noteMainContent, {color: colors.text}]}>{text}</Text>
</View>
);
};
Expand Down
40 changes: 0 additions & 40 deletions src/services/storage.tsx

This file was deleted.

29 changes: 29 additions & 0 deletions src/store/app/app.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {AppActionType, AppActionTypes, NoteObject} from './app.types';

export const notesPending = (): AppActionType => ({
type: AppActionTypes.NOTES_PENDING,
});

export const notesSuccess = (notes: NoteObject[]): AppActionType => ({
type: AppActionTypes.NOTES_SUCCESS,
payload: notes,
});

export const notesFailed = (): AppActionType => ({
type: AppActionTypes.NOTES_FAILED,
});

export const createNote = (note: NoteObject): AppActionType => ({
type: AppActionTypes.CREATE,
payload: note,
});

export const deleteNote = (id: string): AppActionType => ({
type: AppActionTypes.DELETE,
payload: id,
});

export const updateNote = (note: NoteObject): AppActionType => ({
type: AppActionTypes.UPDATE,
payload: note,
});
41 changes: 41 additions & 0 deletions src/store/app/app.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Effect} from '../index';
import {
notesFailed,
notesSuccess,
notesPending,
createNote,
deleteNote,
updateNote,
} from './index';

import {NoteObject} from './app.types';

export const initialize =
(): Effect<Promise<any>> => async (dispatch, getState) => {
const {APP} = getState();
dispatch(notesPending());
try {
const notes = APP.notes;
dispatch(notesSuccess(notes));
} catch (error: any) {
dispatch(notesFailed());
}
};

export const create =
(note: NoteObject): Effect<void> =>
async (dispatch, getState) => {
dispatch(createNote(note));
};

export const remove =
(id: string): Effect<void> =>
async (dispatch, getState) => {
dispatch(deleteNote(id));
};

export const update =
(note: NoteObject): Effect<void> =>
async (dispatch, getState) => {
dispatch(updateNote(note));
};
Loading

0 comments on commit 66c9b86

Please sign in to comment.