Skip to content

Commit

Permalink
Add option to omit course name in filenames (close #1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertying committed Sep 14, 2021
1 parent b983a41 commit a6a455f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/assets/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default {
clearFileCacheSucceeded: 'Successfully cleared file cache',
clearFileCacheFailed: 'Failed to clear file cache',
fileUseDocumentDir: 'Save Downloaded Files to Documents',
fileOmitCourseName: 'Omit Course Name in Filenames',
fileDownloadFailed: 'Failed to download the file',
openFileFailed:
'Failed to open the file. Re-download the file or make sure there is some app on system that can handle the file type.',
Expand Down
1 change: 1 addition & 0 deletions src/assets/translations/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default {
clearFileCacheSucceeded: '清空文件缓存成功',
clearFileCacheFailed: '清空文件缓存失败:',
fileUseDocumentDir: '保存打开的文件到“文档”',
fileOmitCourseName: '文件名不包含课程名',
fileDownloadFailed: '文件下载失败',
openFileFailed:
'文件打开失败。请重新下载文件或确保存在可打开此文件类型的应用。',
Expand Down
1 change: 1 addition & 0 deletions src/data/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const mockStore: PersistAppState = {
newUpdate: false,
graduate: false,
fileUseDocumentDir: false,
fileOmitCourseName: false,
courseInformationSharing: false,
courseInformationSharingBadgeShown: false,
tabFilterSelections: {
Expand Down
1 change: 1 addition & 0 deletions src/data/reducers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function settings(
newUpdate: false,
graduate: false,
fileUseDocumentDir: false,
fileOmitCourseName: false,
tabFilterSelections: {
notice: 'all',
assignment: 'unfinished',
Expand Down
1 change: 1 addition & 0 deletions src/data/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SettingsState {
alarms: AlarmSettings;
graduate: boolean;
fileUseDocumentDir: boolean;
fileOmitCourseName: boolean;
newUpdate: boolean;
courseInformationSharing: boolean;
courseInformationSharingBadgeShown: boolean;
Expand Down
12 changes: 8 additions & 4 deletions src/helpers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export const downloadFile = async (
refresh?: boolean,
onProgress?: (progress: number) => void,
) => {
const settings = store.getState().settings;

let dir = `${
store.getState().settings.fileUseDocumentDir
settings.fileUseDocumentDir
? ExpoFileSystem.documentDirectory
: ExpoFileSystem.cacheDirectory
}learnX-files/${file.courseName}/${file.id}`;
Expand All @@ -23,9 +25,11 @@ export const downloadFile = async (
}
await fs.mkdir(dir);

let path = `${dir}/${file.courseName}-${file.title}${
file.fileType ? `.${file.fileType}` : ''
}`;
let path = settings.fileOmitCourseName
? `${dir}/${file.title}${file.fileType ? `.${file.fileType}` : ''}`
: `${dir}/${file.courseName}-${file.title}${
file.fileType ? `.${file.fileType}` : ''
}`;

if (Platform.OS === 'android') {
path = encodeURI(path);
Expand Down
22 changes: 22 additions & 0 deletions src/screens/FileCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const FileCache: React.FC<NativeStackScreenProps<ScreenParams, 'FileCache'>> =
const fileUseDocumentDir = useTypedSelector(
state => state.settings.fileUseDocumentDir,
);
const fileOmitCourseName = useTypedSelector(
state => state.settings.fileOmitCourseName,
);

const handleClearCache = () => {
Alert.alert(
Expand Down Expand Up @@ -72,6 +75,25 @@ const FileCache: React.FC<NativeStackScreenProps<ScreenParams, 'FileCache'>> =
? 'Files are saved in App Document folder and will only be deleted along with the App.'
: 'Files are saved in App Cache Folder and will be deleted by the system on demand.'}
</Caption>
<TableCell
style={styles.marginTop}
iconName="drive-file-rename-outline"
primaryText={t('fileOmitCourseName')}
switchValue={fileOmitCourseName}
onSwitchValueChange={value =>
dispatch(setSetting('fileOmitCourseName', value))
}
type="switch"
/>
<Caption style={styles.caption}>
{getLocale().startsWith('zh')
? fileOmitCourseName
? '文件以“文件名”形式保存。'
: '文件以“课程名-文件名”形式保存。'
: fileOmitCourseName
? 'Files are saved as "filename".'
: 'Files are saved as "coursename-filename".'}
</Caption>
<TableCell
style={styles.marginTop}
iconName="delete"
Expand Down

0 comments on commit a6a455f

Please sign in to comment.