Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@react-navigation/material-bottom-tabs": "^6.2.12",
"@react-navigation/native": "^6.0.8",
"@react-navigation/stack": "^6.2.0",
"@realm/react": "^0.6.1",
"react-native-sqlite-storage": "^6.0.1",
"typeorm": "^0.3.26",
"buffer": "^6.0.3",
"deepmerge": "^3.3.0",
"i18next": "^21.8.9",
Expand All @@ -60,7 +61,6 @@
"react-native-svg": "^13.9.0",
"react-native-vector-icons": "^9.1.0",
"react-native-webview": "^12.0.2",
"realm": "^12.2.1",
"solution-analyzer": "^0.1.6",
"uuid": "^9.0.0"
},
Expand Down Expand Up @@ -101,7 +101,7 @@
"react-native-flipper": "0.163.0",
"react-native-svg-app-icon": "^0.6.1",
"react-test-renderer": "18.2.0",
"realm-flipper-plugin-device": "^1.1.0",
"@types/react-native-sqlite-storage": "^6.0.4",
"rn-flipper-async-storage-advanced": "^1.0.5",
"typescript": "^4.8.4",
"yarn-deduplicate": "^6.0.1"
Expand Down
13 changes: 7 additions & 6 deletions src/persistence/hooks/useAttemptCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { useRealm } from '../realmdb';
import { useDatabase } from '../sqlitedb/DatabaseProvider';
import { AttemptSchema, attemptToRow, bumpAttemptsVersion } from '../sqlitedb';
import { STIF } from '../../lib/stif';

export function useAttemptCreator() {
const realm = useRealm();
return (attempt: STIF.Attempt) => {
realm.write(() => {
realm.create('Attempt', attempt);
});
const db = useDatabase();
return async (attempt: STIF.Attempt) => {
const repo = db.getRepository(AttemptSchema);
await repo.save(attemptToRow(attempt));
bumpAttemptsVersion();
};
}
37 changes: 7 additions & 30 deletions src/persistence/hooks/useAttemptDeletor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { useQuery, useRealm } from '../realmdb';

import { RealmAttempt } from '../realmdb/schema';
import { useDatabase } from '../sqlitedb/DatabaseProvider';
import { AttemptSchema, bumpAttemptsVersion } from '../sqlitedb';
import { UUID } from '../../lib/stif';

export function useAttemptDeletor() {
const realm = useRealm();
const query = useQuery(RealmAttempt);
return (attempt_id: UUID) => {
const toDelete = query.filtered(`id = '${attempt_id}'`);
realm.write(() => {
// Cascade delete all Realm Objects included in the Attempt

// Realm supports automatic cascade deletions, but only for
// "Embedded Objects" which cannot be directly queried.
// https://www.mongodb.com/docs/realm/sdk/react-native/model-data/relationships-and-embedded-objects/#embedded-objects

// Since we need to support queries for sub-objects of Attempt, we
// have to run the cascade deletion manually.
for(let attempt of toDelete) {
realm.delete(attempt.infractions);
realm.delete(attempt.event);
for(let solution of attempt.solutions) {
for (let phase of solution.reconstruction) {
realm.delete(phase.moves);
realm.delete(phase);
}
realm.delete(solution.reconstruction);
}
realm.delete(attempt.solutions);
realm.delete(attempt);
}
});
const db = useDatabase();
return async (attempt_id: UUID) => {
const repo = db.getRepository(AttemptSchema);
await repo.delete({ id: attempt_id });
bumpAttemptsVersion();
};
}
19 changes: 9 additions & 10 deletions src/persistence/hooks/useAttemptRestoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { useRealm, useQuery } from '../realmdb';
import { useDatabase } from '../sqlitedb/DatabaseProvider';
import { AttemptSchema, attemptToRow, bumpAttemptsVersion } from '../sqlitedb';
import { STIF } from '../../lib/stif';
import { RealmAttempt } from '../realmdb/schema';

export function useAttemptRestoration() {
const realm = useRealm();
const query = useQuery(RealmAttempt);
return (attempts: STIF.Attempt[]) => {
realm.write(() => {
realm.delete(query)
for(const attempt of attempts) {
realm.create('Attempt', attempt);
}
const db = useDatabase();
return async (attempts: STIF.Attempt[]) => {
const rows = attempts.map(attemptToRow);
await db.transaction(async manager => {
await manager.getRepository(AttemptSchema).clear();
await manager.getRepository(AttemptSchema).save(rows);
});
bumpAttemptsVersion();
};
}
40 changes: 23 additions & 17 deletions src/persistence/hooks/useAttempts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,42 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { useEffect, useState } from 'react';
import { useQuery } from '../realmdb';
import { RealmAttempt } from '../realmdb/schema';
import { useDatabase } from '../sqlitedb/DatabaseProvider';
import { useDbVersion, AttemptSchema, rowToAttempt } from '../sqlitedb';
import type { AttemptRow } from '../sqlitedb';
import { STIF } from '../../lib/stif';
import { IterableArrayLike } from '../types';

interface useAttemptsParams {
event?: STIF.CompetitiveEvent;
sortDirection?: 'ascending' | 'descending';
}
const sortDirections = {
ascending: false,
descending: true,
};

export function useAttempts({
event,
sortDirection = 'ascending',
}: useAttemptsParams): IterableArrayLike<STIF.Attempt> {
const db = useDatabase();
const [attemptsVersion] = useDbVersion('attemptsVersion');
const [attempts, setAttempts] = useState<IterableArrayLike<STIF.Attempt>>([]);
const dataset = useQuery<RealmAttempt>(RealmAttempt);

const eventId = event?.id;

useEffect(() => {
if (event !== undefined) {
const importantAttempts = dataset.filtered(`event.id = "${event.id}"`);
const sorted = importantAttempts.sorted(
'inspectionStart',
sortDirections[sortDirection],
);
setAttempts(sorted);
} else {
setAttempts(dataset);
const order = sortDirection === 'ascending' ? 'ASC' : 'DESC';
const repo = db.getRepository<AttemptRow>(AttemptSchema);
const query = repo.createQueryBuilder('attempt').orderBy(
'attempt.inspectionStart',
order,
);
if (eventId !== undefined) {
query.where('attempt.eventId = :eventId', { eventId });
}
}, [event, dataset]);
query
.getMany()
.then(rows => setAttempts(rows.map(rowToAttempt)))
.catch(console.error);
}, [db, eventId, sortDirection, attemptsVersion]);

return attempts;
}
12 changes: 6 additions & 6 deletions src/persistence/hooks/useSolveRecordingCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { useRealm } from '../realmdb';
import { useDatabase } from '../sqlitedb/DatabaseProvider';
import { SolveRecordingSchema, solveRecordingToRow } from '../sqlitedb';
import { STIF, UUID } from '../../lib/stif';

export function useSolveRecordingCreator() {
const realm = useRealm();
return (solutionId: UUID, recording: STIF.SolveRecording) => {
realm.write(() => {
realm.create('SolveRecording', { solutionId: solutionId, ...recording });
});
const db = useDatabase();
return async (solutionId: UUID, recording: STIF.SolveRecording) => {
const repo = db.getRepository(SolveRecordingSchema);
await repo.save(solveRecordingToRow(solutionId, recording));
};
}
40 changes: 0 additions & 40 deletions src/persistence/realmdb/index.ts

This file was deleted.

Loading