From e5c92aeb426a82dd0a73a43104b9ad0e5bd58b91 Mon Sep 17 00:00:00 2001 From: Emma Date: Mon, 4 Mar 2024 16:21:57 -0500 Subject: [PATCH] Add setting for disabling automatic file copy on directory move --- .../freetube/FreeTubeJavaScriptInterface.kt | 8 ++++ .../components/data-settings/data-settings.js | 43 +++++++++++++------ .../data-settings/data-settings.vue | 7 +++ static/locales-android/en-US.yaml | 2 + 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt b/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt index fe37bbbee8c02..e7014104cf3f3 100644 --- a/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt +++ b/android/app/src/main/java/io/freetubeapp/freetube/FreeTubeJavaScriptInterface.kt @@ -512,6 +512,14 @@ class FreeTubeJavaScriptInterface { context.wakeLock.release() } + @JavascriptInterface + fun restart() { + context.finish() + context.startActivity(Intent(Intent.ACTION_MAIN) + .addCategory(Intent.CATEGORY_LAUNCHER) + .setClass(context, MainActivity::class.java)) + } + /** * @return the id of a promise on the window */ diff --git a/src/renderer/components/data-settings/data-settings.js b/src/renderer/components/data-settings/data-settings.js index 281edcc4714c2..5b1319bd8d890 100644 --- a/src/renderer/components/data-settings/data-settings.js +++ b/src/renderer/components/data-settings/data-settings.js @@ -22,6 +22,7 @@ import { import { invidiousAPICall } from '../../helpers/api/invidious' import { getLocalChannel } from '../../helpers/api/local' import { handleAmbigiousContent, initalizeDatabasesInDirectory, readFile, requestDirectory, writeFile } from '../../helpers/android' +import android from 'android' export default defineComponent({ name: 'DataSettings', @@ -44,6 +45,7 @@ export default defineComponent({ ], shouldExportPlaylistForOlderVersions: false, + shouldCopyDataFilesWhenMoving: true } }, computed: { @@ -98,12 +100,18 @@ export default defineComponent({ locationMap = locationInfo.files.map((file) => { return [file.fileName, file.uri] }) } if (locationMap.length !== 0) { - for (const [key, value] of locationMap) { - writeFile('data://', key, readFile(value)) + if (this.shouldCopyDataFilesWhenMoving) { + for (const [key, value] of locationMap) { + writeFile('data://', key, readFile(value)) + } } // clear out data-location.json writeFile('data://', 'data-location.json', '') showToast(this.$t('Data Settings.Your data directory has been moved successfully')) + if (!this.shouldCopyDataFilesWhenMoving) { + // the application must restart in order to refresh the dbs + android.restart() + } } else { showToast(this.$t('Data Settings.Nothing to change')) } @@ -117,18 +125,20 @@ export default defineComponent({ const directory = await requestDirectory() const files = await initalizeDatabasesInDirectory(directory) if (files.length > 0) { - const locationData = readFile('data://', 'data-location.json') - let locationInfo = { directory: 'data://', files: [] } - let locationMap = {} - if (locationData !== '') { - locationInfo = JSON.parse(locationData) - locationMap = Object.fromEntries(locationInfo.files.map((file) => { return [file.fileName, file.uri] })) - } - for (let i = 0; i < files.length; i++) { - const data = locationInfo.files.length === 0 - ? readFile('data://', files[i].fileName) - : readFile(locationMap[files[i].fileName], '') - writeFile(files[i].uri, '', data) + if (this.shouldCopyDataFilesWhenMoving) { + const locationData = readFile('data://', 'data-location.json') + let locationInfo = { directory: 'data://', files: [] } + let locationMap = {} + if (locationData !== '') { + locationInfo = JSON.parse(locationData) + locationMap = Object.fromEntries(locationInfo.files.map((file) => { return [file.fileName, file.uri] })) + } + for (let i = 0; i < files.length; i++) { + const data = locationInfo.files.length === 0 + ? readFile('data://', files[i].fileName) + : readFile(locationMap[files[i].fileName], '') + writeFile(files[i].uri, '', data) + } } // update the data files writeFile('data://', 'data-location.json', JSON.stringify({ @@ -136,9 +146,14 @@ export default defineComponent({ files })) showToast(this.$t('Data Settings.Your data directory has been moved successfully')) + if (!this.shouldCopyDataFilesWhenMoving) { + // the application must restart in order to refresh the dbs + android.restart() + } } } catch (exception) { showToast(this.$t('Data Settings.Error moving data directory')) + console.error(exception) } }, diff --git a/src/renderer/components/data-settings/data-settings.vue b/src/renderer/components/data-settings/data-settings.vue index 8fbcd7401ccf3..517b3fcc6bc0e 100644 --- a/src/renderer/components/data-settings/data-settings.vue +++ b/src/renderer/components/data-settings/data-settings.vue @@ -15,6 +15,13 @@ :label="$t('Data Settings.Reset Data Directory')" @click="resetDataDirectory" /> +

diff --git a/static/locales-android/en-US.yaml b/static/locales-android/en-US.yaml index fb1b6e74eeab7..3ea81aede54e1 100644 --- a/static/locales-android/en-US.yaml +++ b/static/locales-android/en-US.yaml @@ -11,3 +11,5 @@ Data Settings: Your data directory has been moved successfully: Your data directory has been moved successfully Error moving data directory: Error occured while moving data directory Nothing to change: Nothing to change + Copy Data Files When Moving: Copy data files when moving directories + Copy Data Files When Moving Tooltip: This option overwrites the db files present in the directory you have selected with the current db files. Disabling this option will force the app to restart immediately after the directory has been moved.