Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
refactor: new snjs (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Apr 11, 2022
1 parent bb6e4e5 commit 51fd22a
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 338 deletions.
14 changes: 6 additions & 8 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "standardnotes-mobile",
"version": "3.14.1",
"user-version": "3.14.1",
"version": "3.15.0",
"user-version": "3.15.0",
"private": true,
"license": "AGPL-3.0-or-later",
"scripts": {
Expand Down Expand Up @@ -32,15 +32,13 @@
"@react-navigation/native": "^6.0.10",
"@react-navigation/stack": "^6.2.1",
"@standardnotes/components": "^1.7.14",
"@standardnotes/features": "^1.37.1",
"@standardnotes/filepicker": "^1.10.4",
"@standardnotes/payloads": "^1.5.1",
"@standardnotes/filepicker": "^1.10.5",
"@standardnotes/react-native-aes": "^1.4.3",
"@standardnotes/react-native-textview": "1.0.2",
"@standardnotes/react-native-utils": "1.0.1",
"@standardnotes/sncrypto-common": "1.7.4",
"@standardnotes/snjs": "2.93.4",
"@standardnotes/stylekit": "5.20.0",
"@standardnotes/sncrypto-common": "1.7.5",
"@standardnotes/snjs": "2.94.3",
"@standardnotes/stylekit": "5.21.3",
"@types/styled-components-react-native": "5.1.3",
"js-base64": "^3.7.2",
"moment": "^2.29.2",
Expand Down
1 change: 0 additions & 1 deletion src/lib/application.ts
Expand Up @@ -104,7 +104,6 @@ export class MobileApplication extends SNApplication {
}
this.MobileServices = {} as MobileServices;
this.editorGroup.deinit();
this.iconsController.deinit();
super.deinit(source);
}

Expand Down
76 changes: 43 additions & 33 deletions src/lib/application_state.ts
Expand Up @@ -18,6 +18,7 @@ import {
StorageKey,
StorageValueModes,
SystemViewId,
Uuid,
} from '@standardnotes/snjs';
import {
AppState,
Expand Down Expand Up @@ -312,7 +313,7 @@ export class ApplicationState extends ApplicationService {

if (note && note.conflictOf) {
InteractionManager.runAfterInteractions(() => {
this.application?.mutator.changeAndSaveItem(note.uuid, mutator => {
this.application?.mutator.changeAndSaveItem(note, mutator => {
mutator.conflictOf = undefined;
});
});
Expand Down Expand Up @@ -342,9 +343,9 @@ export class ApplicationState extends ApplicationService {
this.application.editorGroup.closeAllNoteViews();
}

editorForNote(note: SNNote) {
editorForNote(uuid: Uuid) {
for (const editor of this.getEditors()) {
if (editor.note?.uuid === note.uuid) {
if (editor.note?.uuid === uuid) {
return editor;
}
}
Expand All @@ -371,42 +372,53 @@ export class ApplicationState extends ApplicationService {
* Reacts to @SNNote and @SNTag Changes
*/
private handleItemsChanges() {
this.removeItemChangesListener = this.application!.streamItems(
this.removeItemChangesListener = this.application.streamItems<
SNNote | SNTag
>(
[ContentType.Note, ContentType.Tag],
async (items, source) => {
/** Close any editors for deleted/trashed/archived notes */
if (source === PayloadSource.PreSyncSave) {
const notes = items.filter(
async ({ changed, inserted, removed, source }) => {
if (
source === PayloadSource.PreSyncSave ||
source === PayloadSource.RemoteRetrieved
) {
const removedNotes = removed.filter(
i => i.content_type === ContentType.Note
);
for (const removedNote of removedNotes) {
const editor = this.editorForNote(removedNote.uuid);
if (editor) {
this.closeEditor(editor);
}
}

const notes = [...changed, ...inserted].filter(
candidate => candidate.content_type === ContentType.Note
) as SNNote[];
);

const isBrowswingTrashedNotes =
this.selectedTag instanceof SmartView &&
this.selectedTag.uuid === SystemViewId.TrashedNotes;

const isBrowsingArchivedNotes =
this.selectedTag instanceof SmartView &&
this.selectedTag.uuid === SystemViewId.ArchivedNotes;

for (const note of notes) {
const editor = this.editorForNote(note);
const editor = this.editorForNote(note.uuid);
if (!editor) {
continue;
}
if (note.deleted) {
this.closeEditor(editor);
} else if (
note.trashed &&
!(
this.selectedTag instanceof SmartView &&
this.selectedTag.uuid === SystemViewId.TrashedNotes
)
) {

if (note.trashed && !isBrowswingTrashedNotes) {
this.closeEditor(editor);
} else if (
note.archived &&
!(
this.selectedTag instanceof SmartView &&
this.selectedTag.uuid === SystemViewId.ArchivedNotes
)
) {
} else if (note.archived && !isBrowsingArchivedNotes) {
this.closeEditor(editor);
}
}
}

if (this.selectedTag) {
const matchingTag = items.find(
const matchingTag = [...changed, ...inserted].find(
candidate => candidate.uuid === this.selectedTag!.uuid
);
if (matchingTag) {
Expand Down Expand Up @@ -469,11 +481,9 @@ export class ApplicationState extends ApplicationService {
* @returns tags that are referencing note
*/
public getNoteTags(note: SNNote) {
return this.application.items
.itemsReferencingItem(note.uuid)
.filter(ref => {
return ref.content_type === ContentType.Tag;
}) as SNTag[];
return this.application.items.itemsReferencingItem(note).filter(ref => {
return ref.content_type === ContentType.Tag;
}) as SNTag[];
}

/**
Expand All @@ -483,7 +493,7 @@ export class ApplicationState extends ApplicationService {
if (tag instanceof SmartView) {
return this.application.items.notesMatchingSmartView(tag);
} else {
return this.application.items.referencesForItem(tag.uuid).filter(ref => {
return this.application.items.referencesForItem(tag).filter(ref => {
return ref.content_type === ContentType.Note;
}) as SNNote[];
}
Expand Down
9 changes: 4 additions & 5 deletions src/lib/component_manager.ts
Expand Up @@ -4,7 +4,6 @@ import {
GetFeatures,
} from '@standardnotes/features';
import {
ComponentContent,
ComponentMutator,
EncryptionService,
isRightVersionGreaterThanLeft,
Expand All @@ -17,11 +16,12 @@ import {
SNNote,
} from '@standardnotes/snjs';
import { objectToCss } from '@Style/css_parser';
import { MobileTheme } from '@Style/theme_service';
import { MobileTheme } from '@Style/MobileTheme';
import { Base64 } from 'js-base64';
import RNFS, { DocumentDirectoryPath } from 'react-native-fs';
import StaticServer from 'react-native-static-server';
import { unzip } from 'react-native-zip-archive';
import { MobileThemeContent } from './../style/MobileTheme';

export enum ComponentLoadingError {
FailedDownload = 'FailedDownload',
Expand Down Expand Up @@ -333,8 +333,7 @@ export class ComponentManager extends SNComponentManager {
urlForComponent(component: SNComponent) {
if (
component.isTheme() &&
(component.safeContent as ComponentContent & { isSystemTheme: boolean })
.isSystemTheme
(component.content as MobileThemeContent).isSystemTheme
) {
const theme = component as MobileTheme;
const cssData = objectToCss(theme.mobileContent.variables);
Expand Down Expand Up @@ -381,7 +380,7 @@ export async function associateComponentWithNote(
note: SNNote
) {
return application.mutator.changeItem<ComponentMutator>(
component.uuid,
component,
mutator => {
mutator.removeDisassociatedItemId(note.uuid);
mutator.associateWithItem(note.uuid);
Expand Down
20 changes: 13 additions & 7 deletions src/lib/interface.ts
@@ -1,6 +1,9 @@
import AsyncStorage from '@react-native-community/async-storage';
import { RawPayload } from '@standardnotes/payloads';
import { AbstractDevice, ApplicationIdentifier } from '@standardnotes/snjs';
import {
AbstractDevice,
ApplicationIdentifier,
TransferPayload,
} from '@standardnotes/snjs';
import { Alert, Linking, Platform } from 'react-native';
import FingerprintScanner from 'react-native-fingerprint-scanner';
import Keychain from './keychain';
Expand Down Expand Up @@ -101,15 +104,16 @@ export class MobileDeviceInterface extends AbstractDevice {
}

private async getDatabaseKeyValues(keys: string[]) {
const results: (RawPayload | unknown)[] = [];
const results: (TransferPayload | unknown)[] = [];

if (Platform.OS === 'android') {
const failedItemIds: string[] = [];
for (const key of keys) {
try {
const item = await AsyncStorage.getItem(key);
if (item) {
try {
results.push(JSON.parse(item) as RawPayload);
results.push(JSON.parse(item) as TransferPayload);
} catch (e) {
results.push(item);
}
Expand Down Expand Up @@ -168,18 +172,20 @@ export class MobileDeviceInterface extends AbstractDevice {
return Promise.resolve({ isNewDatabase: false });
}

async getAllRawDatabasePayloads(
async getAllRawDatabasePayloads<T extends TransferPayload = TransferPayload>(
identifier: ApplicationIdentifier
): Promise<RawPayload[]> {
): Promise<T[]> {
const keys = await this.getAllDatabaseKeys(identifier);
return this.getDatabaseKeyValues(keys) as Promise<RawPayload[]>;
return this.getDatabaseKeyValues(keys) as Promise<T[]>;
}

saveRawDatabasePayload(
payload: any,
identifier: ApplicationIdentifier
): Promise<void> {
return this.saveRawDatabasePayloads([payload], identifier);
}

async saveRawDatabasePayloads(
payloads: any[],
identifier: ApplicationIdentifier
Expand Down
9 changes: 1 addition & 8 deletions src/lib/snjs_helper_hooks.ts
Expand Up @@ -416,13 +416,6 @@ export const useChangeNoteChecks = (
return false;
}

if (note.deleted) {
application.alertService?.alert(
'The note you are attempting to edit has been deleted, and is awaiting sync. Changes you make will be disregarded.'
);
return false;
}

if (editor && editor.isTemplateNote) {
await editor.insertTemplatedNote();
}
Expand Down Expand Up @@ -455,7 +448,7 @@ export const useChangeNote = (
) => {
if (await canChangeNote()) {
await application?.mutator.changeAndSaveItem(
note!.uuid,
note!,
mutator => {
const noteMutator = mutator as NoteMutator;
mutate(noteMutator);
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Compose/ComponentView.tsx
Expand Up @@ -218,7 +218,7 @@ export const ComponentView = ({
if (didLoadRootUrl.current === true || !SupportsShouldLoadRequestHandler) {
log('Setting component viewer webview');
timeoutRef.current = setTimeout(() => {
componentViewer?.setWindow(webViewRef.current);
componentViewer?.setWindow(webViewRef.current as unknown as Window);
}, 1);
/**
* The parent will remove their loading screen on load end. We want to
Expand Down
31 changes: 13 additions & 18 deletions src/screens/Compose/Compose.tsx
Expand Up @@ -126,10 +126,11 @@ export class Compose extends React.Component<{}, State> {

this.removeStreamComponents = this.context?.streamItems(
ContentType.Component,
async (_items, source) => {
if (isPayloadSourceInternalChange(source!)) {
async ({ source }) => {
if (isPayloadSourceInternalChange(source)) {
return;
}

if (!this.note) {
return;
}
Expand Down Expand Up @@ -214,7 +215,7 @@ export class Compose extends React.Component<{}, State> {
* on a deleted note.
*/
get noteLocked() {
if (!this.note || this.note.deleted) {
if (!this.note) {
return false;
}
return this.note.locked;
Expand Down Expand Up @@ -278,14 +279,11 @@ export class Compose extends React.Component<{}, State> {
if (!note) {
return;
}
return this.context?.mutator.changeItem(
component.uuid,
(m: ItemMutator) => {
const mutator = m as ComponentMutator;
mutator.removeDisassociatedItemId(note.uuid);
mutator.associateWithItem(note.uuid);
}
);
return this.context?.mutator.changeItem(component, (m: ItemMutator) => {
const mutator = m as ComponentMutator;
mutator.removeDisassociatedItemId(note.uuid);
mutator.associateWithItem(note.uuid);
});
}

reloadComponentEditorState = async () => {
Expand Down Expand Up @@ -368,23 +366,20 @@ export class Compose extends React.Component<{}, State> {
if (!note) {
return;
}
if (note?.deleted) {
this.context!.alertService!.alert(
'Attempting to save this note has failed. The note has previously been deleted.'
);
return;
}

if (editor?.isTemplateNote) {
await editor?.insertTemplatedNote();
}

if (!this.context?.items.findItem(note!.uuid)) {
this.context?.alertService!.alert(
'Attempting to save this note has failed. The note cannot be found.'
);
return;
}

await this.context!.mutator.changeItem(
note!.uuid,
note,
mutator => {
const noteMutator = mutator as NoteMutator;
noteMutator.title = title!;
Expand Down
4 changes: 2 additions & 2 deletions src/screens/InputModal/TagInputModal.tsx
Expand Up @@ -51,7 +51,7 @@ export const TagInputModal = (props: Props) => {
const tag = application?.items.findItem(
props.route.params.tagUuid
) as SNTag;
await application?.mutator.changeItem(tag.uuid, mutator => {
await application?.mutator.changeItem(tag, mutator => {
const tagMutator = mutator as TagMutator;
tagMutator.title = text;
if (props.route.params.noteUuid) {
Expand All @@ -64,7 +64,7 @@ export const TagInputModal = (props: Props) => {
} else {
const tag = await application!.mutator.findOrCreateTag(text);
if (props.route.params.noteUuid) {
await application?.mutator.changeItem(tag.uuid, mutator => {
await application?.mutator.changeItem(tag, mutator => {
const tagMutator = mutator as TagMutator;
const note = application.items.findItem(props.route.params.noteUuid!);
if (note) {
Expand Down

0 comments on commit 51fd22a

Please sign in to comment.