Skip to content
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

refactor: Convert everything to TypeScript #121

Merged
merged 43 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4385d36
Start ts
amaury1093 Jul 1, 2019
2ba71ab
Convert all files to TS
amaury1093 Jul 1, 2019
c911747
Make search work
amaury1093 Jul 6, 2019
1a02aaf
Make png import work
amaury1093 Jul 6, 2019
4805930
Add retry
amaury1093 Jul 6, 2019
e41774b
Make api call work
amaury1093 Jul 7, 2019
957ebaa
Add long waiting on Loading
amaury1093 Jul 7, 2019
8f8abe1
Make screens work
amaury1093 Jul 7, 2019
156a810
Make cigarettes work
amaury1093 Jul 7, 2019
3fa011d
Make header work
amaury1093 Jul 7, 2019
7d1db03
Reverse geocoding
amaury1093 Jul 7, 2019
7e3b3b8
Make details work
amaury1093 Jul 7, 2019
e137e0b
Make search work
amaury1093 Jul 7, 2019
4bcc494
Fix reload app
amaury1093 Jul 7, 2019
8f992fe
Make tests pass
amaury1093 Jul 7, 2019
0fcf5e3
Run eslint
amaury1093 Jul 7, 2019
5edd500
AqiHistory
amaury1093 Jul 9, 2019
f05a927
Add History manager back
amaury1093 Jul 9, 2019
704bb62
Start getting background location
amaury1093 Jul 9, 2019
9784aa1
Remove circular dependency
amaury1093 Jul 10, 2019
28b334c
Fix isSaveNeeded
amaury1093 Jul 10, 2019
2e54f5e
Add getData for testing
amaury1093 Jul 10, 2019
8462eeb
Add clearTable
amaury1093 Jul 10, 2019
60874bd
Fix tests
amaury1093 Jul 10, 2019
50af1ec
Update README
amaury1093 Jul 10, 2019
2a03a7f
Remove vs code settings
amaury1093 Jul 10, 2019
6135652
Fix too many requests
amaury1093 Jul 12, 2019
9886b4f
Add task to save to AsyncStorage
amaury1093 Jul 13, 2019
9206856
Return correct background fetch response
amaury1093 Jul 13, 2019
cf203ab
Refactor a bit the components
amaury1093 Jul 13, 2019
b1eed90
Make scroll work nicely
amaury1093 Jul 13, 2019
5fd5ccb
Add shadow on buttons
amaury1093 Jul 13, 2019
40f8848
Make stuff work
amaury1093 Jul 13, 2019
a516044
Weekly monthly
amaury1093 Jul 13, 2019
86d52a0
Calculate cigarettes
amaury1093 Jul 13, 2019
c1fed0b
Small fixes
amaury1093 Jul 13, 2019
bce6ecb
Small fixes
amaury1093 Jul 13, 2019
b12ee31
Small tweaks with icons
amaury1093 Jul 14, 2019
eb862cb
Add more dev info
amaury1093 Jul 16, 2019
050761c
Reverse geocode in search
amaury1093 Jul 16, 2019
d7ba1fd
Small tweaks
amaury1093 Jul 16, 2019
8c029ed
Small tweaks
amaury1093 Jul 17, 2019
d44d40b
Update icons
amaury1093 Jul 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add clearTable
  • Loading branch information
amaury1093 committed Jul 10, 2019
commit 8462eebabd54682bbe9504ceee47f2d8968ac4a6
2 changes: 2 additions & 0 deletions App/Screens/About/About.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { NavigationInjectedProps } from 'react-navigation';

import { Box } from './Box';
import { BackButton } from '../../components/BackButton';
import { Dev } from './Dev';
import { Language } from './Language';
import { i18n } from '../../localization';
import * as theme from '../../util/theme';
Expand Down Expand Up @@ -152,6 +153,7 @@ export function About (props: AboutProps) {
<Language />
</View>
</View>
<Dev />
</ScrollView>
);
}
Expand Down
72 changes: 72 additions & 0 deletions App/Screens/About/Dev/Dev.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Sh**t! I Smoke
// Copyright (C) 2018-2019 Marcelo S. Coelho, Amaury Martiny

// Sh**t! I Smoke is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Sh**t! I Smoke is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Sh**t! I Smoke. If not, see <http:https://www.gnu.org/licenses/>.

import { pipe } from 'fp-ts/lib/pipeable';
import * as T from 'fp-ts/lib/Task';
import * as TE from 'fp-ts/lib/TaskEither';
import React, { useEffect, useState } from 'react';
import { Text, View } from 'react-native';

import {
AqiHistoryItem,
getAveragePm25,
getData
} from '../../../managers/AqiHistoryDb';
import * as theme from '../../../util/theme';

export function Dev () {
const [allData, setAllData] = useState<AqiHistoryItem[]>([]);
const [average, setAverage] = useState(0);

useEffect(() => {
const oneWeekAgo = new Date();
oneWeekAgo.setHours(oneWeekAgo.getHours() - 24 * 7);
pipe(
getAveragePm25(oneWeekAgo),
TE.fold(
err => {
console.log(err);
return T.of(undefined);
},
avg => {
setAverage(avg);
return T.of(undefined);
}
)
)();

pipe(
getData(10),
TE.fold(
err => {
console.log(err);
return T.of(undefined);
},
data => {
setAllData(data);
return T.of(undefined);
}
)
)();
}, []);

return (
<View>
<Text style={theme.text}>Average: {average}</Text>
<Text style={theme.text}>All data: {JSON.stringify(allData)}</Text>
</View>
);
}
17 changes: 17 additions & 0 deletions App/Screens/About/Dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Sh**t! I Smoke
// Copyright (C) 2018-2019 Marcelo S. Coelho, Amaury Martiny

// Sh**t! I Smoke is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Sh**t! I Smoke is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Sh**t! I Smoke. If not, see <http:https://www.gnu.org/licenses/>.

export * from './Dev';
56 changes: 52 additions & 4 deletions App/managers/AqiHistoryDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ interface AqiHistoryItemInput extends LatLng {
rawPm25: number;
}

interface AqiHistoryItem extends AqiHistoryItemInput {
export interface AqiHistoryItem extends AqiHistoryItemInput {
creationTime: Date;
id: number;
}

export const SAVE_DATA_INTERVAL = 360000 * 1000; // 1 hour
export const SAVE_DATA_INTERVAL = 3600 * 1000; // 1 hour
const AQI_HISTORY_DB = 'AQI_HISTORY_DB';
const AQI_HISTORY_TABLE = 'AqiHistory';

/**
* Open database and create (if not exists) the AqiHistory table.
*/
function initDb () {
return pipe(
TE.rightTask(T.of(SQLite.openDatabase(AQI_HISTORY_DB) as Database)),
Expand Down Expand Up @@ -70,11 +73,17 @@ function initDb () {
);
}

/**
* Fetch the db
*/
function getDb () {
// TODO Only do initDb once (although that operation is idempotent)
return initDb();
}

/**
* Check if we need to save a new entry (every `SAVE_DATA_INTERVAL`)
*/
function isSaveNeeded () {
return pipe(
getDb(),
Expand All @@ -98,7 +107,7 @@ function isSaveNeeded () {
if (resultSet.rows.length > 0) {
try {
console.log(
`<AqiHistoryDb> - isSaveNeeded - Last save happened at ${
`<AqiHistoryDb> - isSaveNeeded - false: last save happened at ${
resultSet.rows.item(0).creationTime
}`
);
Expand All @@ -120,6 +129,11 @@ function isSaveNeeded () {
);
}

/**
* Add a new row in the table
*
* @param value - The entry to add.
*/
export function saveData (value: AqiHistoryItemInput) {
return pipe(
isSaveNeeded(),
Expand Down Expand Up @@ -160,6 +174,11 @@ export function saveData (value: AqiHistoryItemInput) {
);
}

/**
* Get the average PM25 since `date`.
*
* @param date - The date to start calculating the average PM25.
*/
export function getAveragePm25 (date: Date) {
return pipe(
getDb(),
Expand All @@ -179,7 +198,7 @@ export function getAveragePm25 (date: Date) {
`,
[date.getTime() / 1000],
(_transaction: Transaction, resultSet: ResultSet) =>
resolve(resultSet.rows.item(0)),
resolve(resultSet.rows.item(0)['AVG(rawPm25)']),
(_transaction: Transaction, error: Error) => reject(error)
);
});
Expand Down Expand Up @@ -207,6 +226,7 @@ export function getData (limit: number) {
tx.executeSql(
`
SELECT * FROM ${AQI_HISTORY_TABLE}
ORDER BY id DESC
LIMIT ?
`,
[limit],
Expand All @@ -221,3 +241,31 @@ export function getData (limit: number) {
)
);
}

/**
* Clears the whole db. DANGEROUS.
*/
export function clearTable () {
return pipe(
getDb(),
TE.chain(db =>
TE.tryCatch(
() =>
new Promise((resolve, reject) => {
db.readTransaction((tx: Transaction) => {
tx.executeSql(
`DROP TABLE ${AQI_HISTORY_TABLE}`,
[],
() => resolve(undefined as void),
(_transaction: Transaction, error: Error) => reject(error)
);
});
}) as Promise<void>,
toError
)
)
);
}

// Uncomment this to clear the table
// clearTable()();