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

Commit

Permalink
refactor: remove if (application) checks when application is initia…
Browse files Browse the repository at this point in the history
…lized for sure (#583)
  • Loading branch information
vardan-arm committed Apr 5, 2022
1 parent b2ecd5f commit bb6e4e5
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 197 deletions.
15 changes: 8 additions & 7 deletions src/App.tsx
Expand Up @@ -145,15 +145,16 @@ export const App = (props: { env: TEnvironment }) => {
return removeAppChangeObserver;
}, [applicationGroupRef.current.primaryApplication]);

if (!application) {
return null;
}
return (
<ApplicationContext.Provider value={application}>
{application && (
<AppComponent
env={props.env}
key={application.Uuid}
application={application}
/>
)}
<AppComponent
env={props.env}
key={application.Uuid}
application={application}
/>
</ApplicationContext.Provider>
);
};
43 changes: 4 additions & 39 deletions src/hooks/useFiles.ts
@@ -1,6 +1,6 @@
import { ToastType } from '@Lib/types';
import { useNavigation } from '@react-navigation/native';
import { ApplicationContext } from '@Root/ApplicationContext';
import { useSafeApplicationContext } from '@Root/hooks/useSafeApplicationContext';
import { SCREEN_INPUT_MODAL_FILE_NAME } from '@Screens/screens';
import { TAppStackNavigationProp } from '@Screens/UploadedFilesList/UploadedFileItem';
import {
Expand All @@ -15,7 +15,7 @@ import {
SNNote,
} from '@standardnotes/snjs';
import { useCustomActionSheet } from '@Style/custom_action_sheet';
import { useCallback, useContext, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { Platform } from 'react-native';
import FileViewer from 'react-native-file-viewer';
import RNFS, { exists } from 'react-native-fs';
Expand All @@ -41,7 +41,7 @@ export const isFileTypePreviewable = (fileType: string) => {
};

export const useFiles = ({ note }: Props) => {
const application = useContext(ApplicationContext);
const application = useSafeApplicationContext();

const { showActionSheet } = useCustomActionSheet();
const navigation = useNavigation<TAppStackNavigationProp>();
Expand All @@ -53,9 +53,6 @@ export const useFiles = ({ note }: Props) => {
const { Success, Info, Error } = ToastType;

const reloadAttachedFiles = useCallback(() => {
if (!application || !note) {
return [];
}
setAttachedFiles(
application.items
.getFilesForNote(note)
Expand All @@ -64,9 +61,6 @@ export const useFiles = ({ note }: Props) => {
}, [application, note]);

const reloadAllFiles = useCallback(() => {
if (!application) {
return [];
}
setAllFiles(
application.items
.getItems(ContentType.File)
Expand All @@ -87,7 +81,7 @@ export const useFiles = ({ note }: Props) => {
file,
saveInTempLocation = false,
}: TDownloadFileAndReturnLocalPathParams): Promise<string | undefined> => {
if (isDownloading || !application) {
if (isDownloading) {
return;
}
const filesService = application.getFilesService();
Expand Down Expand Up @@ -148,9 +142,6 @@ export const useFiles = ({ note }: Props) => {

const shareFile = useCallback(
async (file: SNFile) => {
if (!application) {
return;
}
const downloadedFilePath = await downloadFileAndReturnLocalPath({
file,
saveInTempLocation: true,
Expand Down Expand Up @@ -201,9 +192,6 @@ export const useFiles = ({ note }: Props) => {

const attachFileToNote = useCallback(
async (file: SNFile) => {
if (!application || !note) {
return;
}
await application.items.associateFileWithNote(file, note);
Toast.show({
type: Success,
Expand All @@ -216,9 +204,6 @@ export const useFiles = ({ note }: Props) => {

const detachFileFromNote = useCallback(
async (file: SNFile) => {
if (!application || !note) {
return;
}
await application.items.disassociateFileWithNote(file, note);
Toast.show({
type: Success,
Expand All @@ -232,9 +217,6 @@ export const useFiles = ({ note }: Props) => {
const toggleFileProtection = useCallback(
async (file: SNFile) => {
try {
if (!application) {
return file.protected;
}
let result: SNFile | undefined;
if (file.protected) {
result = await application.mutator.unprotectFile(file);
Expand All @@ -253,9 +235,6 @@ export const useFiles = ({ note }: Props) => {

const authorizeProtectedActionForFile = useCallback(
async (file: SNFile, challengeReason: ChallengeReason) => {
if (!application) {
return false;
}
const authorizedFiles =
await application.protections.authorizeProtectedActionForFiles(
[file],
Expand All @@ -268,20 +247,13 @@ export const useFiles = ({ note }: Props) => {

const renameFile = useCallback(
async (file: SNFile, fileName: string) => {
if (!application) {
return;
}
await application.items.renameFile(file, fileName);
},
[application]
);

const previewFile = useCallback(
async (file: SNFile) => {
if (!application) {
return;
}

let downloadedFilePath: string | undefined = '';
try {
const isPreviewable = isFileTypePreviewable(file.mimeType);
Expand Down Expand Up @@ -327,10 +299,6 @@ export const useFiles = ({ note }: Props) => {
);
const handleFileAction = useCallback(
async (action: UploadedFileItemAction) => {
if (!application) {
return false;
}

const file =
action.type !== UploadedFileItemActionType.RenameFile
? action.payload
Expand Down Expand Up @@ -395,9 +363,6 @@ export const useFiles = ({ note }: Props) => {
);

useEffect(() => {
if (!application) {
return;
}
const unregisterFileStream = application.streamItems(
ContentType.File,
() => {
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useSafeApplicationContext.ts
@@ -0,0 +1,8 @@
import { MobileApplication } from '@Lib/application';
import { ApplicationContext } from '@Root/ApplicationContext';
import { useContext } from 'react';

export const useSafeApplicationContext = () => {
const application = useContext(ApplicationContext) as MobileApplication;
return application;
};
55 changes: 26 additions & 29 deletions src/lib/snjs_helper_hooks.ts
@@ -1,4 +1,5 @@
import { ApplicationContext } from '@Root/ApplicationContext';
import { useSafeApplicationContext } from '@Root/hooks/useSafeApplicationContext';
import { SCREEN_NOTES } from '@Screens/screens';
import {
ApplicationEvent,
Expand All @@ -17,7 +18,7 @@ export const useSignedIn = (
signedOutCallback?: () => void
) => {
// Context
const application = React.useContext(ApplicationContext);
const application = useSafeApplicationContext();

const [isLocked] = useIsLocked();

Expand All @@ -28,24 +29,22 @@ export const useSignedIn = (
let mounted = true;
const getSignedIn = async () => {
if (mounted && !isLocked) {
setSignedIn(!application?.noAccount());
setSignedIn(!application.noAccount());
}
};
getSignedIn();
const removeSignedInObserver = application?.addEventObserver(
async event => {
if (event === ApplicationEvent.Launched) {
getSignedIn();
}
if (event === ApplicationEvent.SignedIn) {
setSignedIn(true);
signedInCallback && signedInCallback();
} else if (event === ApplicationEvent.SignedOut) {
setSignedIn(false);
signedOutCallback && signedOutCallback();
}
const removeSignedInObserver = application.addEventObserver(async event => {
if (event === ApplicationEvent.Launched) {
getSignedIn();
}
);
if (event === ApplicationEvent.SignedIn) {
setSignedIn(true);
signedInCallback && signedInCallback();
} else if (event === ApplicationEvent.SignedOut) {
setSignedIn(false);
signedOutCallback && signedOutCallback();
}
});

return () => {
mounted = false;
Expand All @@ -58,15 +57,15 @@ export const useSignedIn = (

export const useOutOfSync = () => {
// Context
const application = React.useContext(ApplicationContext);
const application = useSafeApplicationContext();

// State
const [outOfSync, setOutOfSync] = React.useState<boolean>(false);

React.useEffect(() => {
let isMounted = true;
const getOutOfSync = async () => {
const outOfSyncInitial = await application?.sync.isOutOfSync();
const outOfSyncInitial = await application.sync.isOutOfSync();
if (isMounted) {
setOutOfSync(Boolean(outOfSyncInitial));
}
Expand All @@ -78,15 +77,13 @@ export const useOutOfSync = () => {
}, [application]);

React.useEffect(() => {
const removeSignedInObserver = application?.addEventObserver(
async event => {
if (event === ApplicationEvent.EnteredOutOfSync) {
setOutOfSync(true);
} else if (event === ApplicationEvent.ExitedOutOfSync) {
setOutOfSync(false);
}
const removeSignedInObserver = application.addEventObserver(async event => {
if (event === ApplicationEvent.EnteredOutOfSync) {
setOutOfSync(true);
} else if (event === ApplicationEvent.ExitedOutOfSync) {
setOutOfSync(false);
}
);
});

return removeSignedInObserver;
}, [application]);
Expand Down Expand Up @@ -412,15 +409,15 @@ export const useChangeNoteChecks = (
editor: NoteViewController | undefined = undefined
) => {
// Context
const application = React.useContext(ApplicationContext);
const application = useSafeApplicationContext();

const canChangeNote = useCallback(async () => {
if (!note) {
return false;
}

if (note.deleted) {
application?.alertService?.alert(
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;
Expand All @@ -430,8 +427,8 @@ export const useChangeNoteChecks = (
await editor.insertTemplatedNote();
}

if (!application?.items.findItem(note.uuid)) {
application?.alertService!.alert(
if (!application.items.findItem(note.uuid)) {
application.alertService!.alert(
"The note you are attempting to save can not be found or has been deleted. Changes you make will not be synced. Please copy this note's text and start a new note."
);
return false;
Expand Down
7 changes: 2 additions & 5 deletions src/screens/InputModal/FileInputModal.tsx
@@ -1,7 +1,7 @@
import { ButtonCell } from '@Components/ButtonCell';
import { SectionedTableCell } from '@Components/SectionedTableCell';
import { TableSection } from '@Components/TableSection';
import { ApplicationContext } from '@Root/ApplicationContext';
import { useSafeApplicationContext } from '@Root/hooks/useSafeApplicationContext';
import { ModalStackNavigationProp } from '@Root/ModalStack';
import { SCREEN_INPUT_MODAL_FILE_NAME } from '@Screens/screens';
import { UploadedFileItemActionType } from '@Screens/UploadedFilesList/UploadedFileItemAction';
Expand All @@ -15,16 +15,13 @@ type Props = ModalStackNavigationProp<typeof SCREEN_INPUT_MODAL_FILE_NAME>;
export const FileInputModal: FC<Props> = props => {
const { file, handleFileAction } = props.route.params;
const themeService = useContext(ThemeServiceContext);
const application = useContext(ApplicationContext);
const application = useSafeApplicationContext();

const fileNameInputRef = useRef<TextInput>(null);

const [fileName, setFileName] = useState(file.name);

const onSubmit = async () => {
if (!application) {
return;
}
const trimmedFileName = fileName.trim();
if (trimmedFileName === '') {
setFileName(file.name);
Expand Down

0 comments on commit bb6e4e5

Please sign in to comment.