Skip to content

Commit

Permalink
Merge pull request #55 from RaghavJit/main
Browse files Browse the repository at this point in the history
issue 52 fixed
  • Loading branch information
RaghavJit authored Apr 9, 2023
2 parents 8704c35 + 08e05e1 commit cc0429a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 26 deletions.
34 changes: 31 additions & 3 deletions code/MyComp/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import QuickTasker from './QuickTasker';
import MyProfile from './MyProfile';
import {resetAIjson} from '../brain/testing'
import {resetHIjson} from '../brain/testing'
import {resetJson} from '../brain/QuickTasker'
const RNFS = require('react-native-fs')


Expand All @@ -31,7 +32,9 @@ if(colorscheme === 'dark'){
colors[4] = 'black'
}
const packageName = NativeModules?.AppInfo?.packageName ?? '';
const filePath = `${RNFS.DocumentDirectoryPath}/${packageName}/hi.json`;
const filePath1 = `${RNFS.DocumentDirectoryPath}/${packageName}/hi.json`;
const filePath2 = `${RNFS.DocumentDirectoryPath}/${packageName}/Avalible_ID.json`;
const filePath3 = `${RNFS.DocumentDirectoryPath}/${packageName}/QuickTasks.json`;

const Home = ({navigation}) => {

Expand All @@ -53,9 +56,22 @@ const Home = ({navigation}) => {
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the storage');
console.log('You can use the storage')

RNFS.exists(filePath)
RNFS.exists(filePath1)
.then((exists) => {
if (exists) {
console.log('File exists');
} else {
resetHIjson()
resetAIjson()
}
})
.catch((error) => {
console.log(error);
});

RNFS.exists(filePath2)
.then((exists) => {
if (exists) {
console.log('File exists');
Expand All @@ -68,6 +84,18 @@ const Home = ({navigation}) => {
console.log(error);
});

RNFS.exists(filePath3)
.then((exists) => {
if (exists) {
console.log('File exists');
} else {
resetJson()
}
})
.catch((error) => {
console.log(error);
});

} else {
console.log('Storage permission denied');
}
Expand Down
12 changes: 11 additions & 1 deletion code/MyComp/QuickTasker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, TextInput, Button, TouchableOpacity, Alert } from 'react-native';
import { addQTask, getList } from '../brain/QuickTasker'

export default function QuickTasker() {
const [task, setTask] = useState('');
Expand All @@ -14,17 +15,26 @@ export default function QuickTasker() {
}
};

useEffect(() => {
getList().then(data => {
setTaskList(data);
console.log(data)
})
}, []);

const addTask = () => {
setPrevTaskList(taskList);
setTaskList([...taskList, task]);
setTask('');
addQTask([...taskList, task])
};

const deleteTask = (index) => {
setPrevTaskList(taskList);
const newTaskList = [...taskList];
newTaskList.splice(index, 1);
setTaskList(newTaskList);
addQTask(newTaskList)
};

const undo = () => {
Expand Down
49 changes: 49 additions & 0 deletions code/brain/QuickTasker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const RNFS = require('react-native-fs')
import { NativeModules } from 'react-native';

const packageName = NativeModules?.AppInfo?.packageName ?? '';
const path = `${RNFS.DocumentDirectoryPath}/${packageName}/QuickTasks.json`;

exports.resetJson = () => {
let QuickTasks = {
"Tasks":
[
"sample"
]
}
RNFS.writeFile(path, JSON.stringify(QuickTasks));
console.log(JSON.stringify(QuickTasks))
}

exports.addQTask = (Tlist) => {

RNFS.readFile(path)
.then(value => {
// Parse the JSON data
const jsonData = JSON.parse(value);

// Use the JSON data as needed
jsonData.Tasks = Tlist

RNFS.writeFile(path, JSON.stringify(jsonData));

console.log(JSON.stringify(jsonData))
})
.catch(error => {
console.error(error);
});

}

exports.getList = () => {
return RNFS.readFile(path)
.then(value => {
// Parse the JSON data
const jsonData = JSON.parse(value);
console.log(jsonData)
return Promise.resolve(jsonData.Tasks);
})
.catch(error => {
console.error(error);
});
}
44 changes: 22 additions & 22 deletions code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc0429a

Please sign in to comment.