/**
* This Source Code Form is subject to the terms of the Mozilla Public
* 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 { useFocusEffect, useNavigation } from '@react-navigation/native';
import * as FileSystem from 'expo-file-system';
import { toJS, values } from 'mobx';
import { observer } from 'mobx-react-lite';
import React, { useCallback, useContext, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, FlatList, StyleSheet } from 'react-native';
import { Button, ThemeContext } from 'react-native-elements';
import { SafeAreaView } from 'react-native-safe-area-context';
import DownloadListItem from '../components/DownloadListItem';
import MediaTypes from '../constants/MediaTypes';
import { useStores } from '../hooks/useStores';
const DownloadScreen = observer(() => {
const navigation = useNavigation();
const { rootStore } = useStores();
const { t } = useTranslation();
const { theme } = useContext(ThemeContext);
const [ isEditMode, setIsEditMode ] = useState(false);
const [ selectedItems, setSelectedItems ] = useState([]);
function exitEditMode() {
setIsEditMode(false);
setSelectedItems([]);
}
React.useLayoutEffect(() => {
async function deleteItem(download) {
// TODO: Add user messaging on errors
try {
await FileSystem.deleteAsync(download.localPath);
rootStore.downloadStore.downloads.delete(download.key);
console.log('[DownloadScreen] download "%s" deleted', download.title);
} catch (e) {
console.error('[DownloadScreen] Failed to delete download', e);
}
}
function onDeleteItems(downloads) {
Alert.alert(
'Delete Downloads',
'These items will be permanently deleted from this device.',
[
{
text: t('common.cancel'),
onPress: exitEditMode
},
{
text: `Delete ${downloads.length} Downloads`,
onPress: async () => {
await Promise.all(downloads.map(deleteItem));
exitEditMode();
},
style: 'destructive'
}
]
);
}
navigation.setOptions({
headerLeft: () => (
isEditMode ?
:
null
),
headerRight: () => (
isEditMode ?