From c4b1e9649fe72dc13224f301cef6e5aa3bdcde7f Mon Sep 17 00:00:00 2001 From: Pavel Remizov <84866852+BIBLETUM@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:47:11 +0300 Subject: [PATCH] Add option to delete files after export #247 (#259) **Description:** *Add option to delete files after export* #247 **Tasks:** - [ ] Add option to delete files after export to settings layout - [ ] Add option to delete files after export on ImageViewFragment - [ ] Add option to delete files after export on GalleryFragment - [ ] Translate all new strings --- .../photok/cgallery/ui/GalleryFragment.kt | 6 +++++- .../cgallery/ui/navigation/GalleryNavigator.kt | 11 ++++++++--- .../photok/gallery/ui/GalleryFragment.kt | 2 +- .../photok/gallery/ui/GalleryViewModel.kt | 13 +++++++++++++ .../photok/imageviewer/ui/ImageViewerFragment.kt | 15 +++++++++++++-- .../photok/imageviewer/ui/ImageViewerViewModel.kt | 7 +++++-- .../photok/model/repositories/PhotoRepository.kt | 9 +++++++-- .../dev/leonlatsch/photok/settings/data/Config.kt | 10 ++++++++++ app/src/main/res/values-ar/strings.xml | 5 +++++ app/src/main/res/values-de/strings.xml | 5 +++++ app/src/main/res/values-es/strings.xml | 5 +++++ app/src/main/res/values-fr/strings.xml | 5 +++++ app/src/main/res/values-nl/strings.xml | 5 +++++ app/src/main/res/values-pt-rBR/strings.xml | 7 ++++++- app/src/main/res/values-ru/strings.xml | 5 +++++ app/src/main/res/values-zh-rCN/strings.xml | 7 ++++++- app/src/main/res/values/strings.xml | 5 +++++ app/src/main/res/xml/settings.xml | 6 ++++++ 18 files changed, 115 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/GalleryFragment.kt b/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/GalleryFragment.kt index 0ab58838..9b41fd67 100644 --- a/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/GalleryFragment.kt +++ b/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/GalleryFragment.kt @@ -30,6 +30,7 @@ import dev.leonlatsch.photok.cgallery.ui.compose.GalleryScreen import dev.leonlatsch.photok.cgallery.ui.navigation.GalleryNavigator import dev.leonlatsch.photok.imageloading.compose.LocalEncryptedImageLoader import dev.leonlatsch.photok.other.extensions.launchLifecycleAwareJob +import dev.leonlatsch.photok.settings.data.Config import javax.inject.Inject @AndroidEntryPoint @@ -40,6 +41,9 @@ class GalleryFragment : Fragment() { @Inject lateinit var navigator: GalleryNavigator + @Inject + lateinit var config: Config + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -59,7 +63,7 @@ class GalleryFragment : Fragment() { launchLifecycleAwareJob { viewModel.eventsFlow.collect { event -> - navigator.navigate(event, findNavController(), this) + navigator.navigate(event, findNavController(), this, config.deleteExportedFiles) } } } diff --git a/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/navigation/GalleryNavigator.kt b/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/navigation/GalleryNavigator.kt index 628b664c..9a68d49f 100644 --- a/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/navigation/GalleryNavigator.kt +++ b/app/src/main/java/dev/leonlatsch/photok/cgallery/ui/navigation/GalleryNavigator.kt @@ -37,6 +37,7 @@ class GalleryNavigator @Inject constructor() { event: GalleryNavigationEvent, navController: NavController, fragment: Fragment, + deleteExportedFiles: Boolean ) { when (event) { is GalleryNavigationEvent.OpenPhoto -> navigateOpenPhoto(event.photoUUID, navController) @@ -50,7 +51,8 @@ class GalleryNavigator @Inject constructor() { is GalleryNavigationEvent.StartExportDialog -> navigateStartExportDialog( fragment.requireContext(), event.photosToExport, - fragment.childFragmentManager + fragment.childFragmentManager, + deleteExportedFiles ) } } @@ -58,12 +60,15 @@ class GalleryNavigator @Inject constructor() { private fun navigateStartExportDialog( context: Context, photos: List, - fragmentManager: FragmentManager + fragmentManager: FragmentManager, + deleteExportedFiles: Boolean ) { + var conformationText = context.getString(R.string.export_are_you_sure) + if (deleteExportedFiles) conformationText = context.getString(R.string.export_and_delete_are_you_sure) Dialogs.showConfirmDialog( context, String.format( - context.getString(R.string.export_are_you_sure), + conformationText, photos.size ) ) { _, _ -> // On positive button clicked diff --git a/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryFragment.kt b/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryFragment.kt index 644252e5..80aa1250 100644 --- a/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryFragment.kt +++ b/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryFragment.kt @@ -233,7 +233,7 @@ class GalleryFragment : BindableFragment(R.layout.fragme Dialogs.showConfirmDialog( requireContext(), String.format( - getString(R.string.export_are_you_sure), + viewModel.getAreYouSureExportString(requireContext()), adapter.selectedItems.size ) ) { _, _ -> // On positive button clicked diff --git a/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryViewModel.kt b/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryViewModel.kt index 49a980d8..d451ce81 100644 --- a/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryViewModel.kt +++ b/app/src/main/java/dev/leonlatsch/photok/gallery/ui/GalleryViewModel.kt @@ -17,6 +17,7 @@ package dev.leonlatsch.photok.gallery.ui import android.app.Application +import android.content.Context import android.view.View import androidx.databinding.Bindable import androidx.lifecycle.viewModelScope @@ -24,6 +25,7 @@ import androidx.paging.Pager import androidx.paging.PagingConfig import dagger.hilt.android.lifecycle.HiltViewModel import dev.leonlatsch.photok.BR +import dev.leonlatsch.photok.R import dev.leonlatsch.photok.model.repositories.PhotoRepository import dev.leonlatsch.photok.news.newfeatures.ui.FEATURE_VERSION_CODE import dev.leonlatsch.photok.other.onMain @@ -82,6 +84,17 @@ class GalleryViewModel @Inject constructor( } } + /** + * Change the dialog string accordingly to settings. + */ + fun getAreYouSureExportString(context: Context): String{ + return if (this.config.deleteExportedFiles) { + context.getString(R.string.export_and_delete_are_you_sure) + } else { + context.getString(R.string.export_are_you_sure) + } + } + /** * Run the [onNewsPresent] if the app was just updated. */ diff --git a/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerFragment.kt b/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerFragment.kt index 1340b851..4c657b1c 100644 --- a/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerFragment.kt +++ b/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerFragment.kt @@ -129,9 +129,20 @@ class ImageViewerFragment : BindableFragment(R.layou Manifest.permission.READ_EXTERNAL_STORAGE ) || Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ) { - Dialogs.showConfirmDialog(requireContext(), getString(R.string.export_are_you_sure_this)) { _, _ -> + + var toastStringAreYouSure = getString(R.string.export_are_you_sure_this) + var toastStringFinishedExport = getString(R.string.export_finished) + if (config.deleteExportedFiles) { + toastStringAreYouSure = getString(R.string.export_and_delete_are_you_sure_this) + toastStringFinishedExport = getString(R.string.export_and_delete_finished) + } + + Dialogs.showConfirmDialog(requireContext(), toastStringAreYouSure) { _, _ -> viewModel.exportPhoto({ // onSuccess - Dialogs.showShortToast(requireContext(), getString(R.string.export_finished)) + if (config.deleteExportedFiles) { // close current photo if deleteExportedFiles is true + requireActivity().onBackPressed() + } + Dialogs.showShortToast(requireContext(), toastStringFinishedExport) }, { // onError Dialogs.showLongToast(requireContext(), getString(R.string.common_error)) }) diff --git a/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerViewModel.kt b/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerViewModel.kt index e55900a8..61822047 100644 --- a/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerViewModel.kt +++ b/app/src/main/java/dev/leonlatsch/photok/imageviewer/ui/ImageViewerViewModel.kt @@ -98,7 +98,10 @@ class ImageViewerViewModel @Inject constructor( currentPhoto ?: return@launch currentPhoto!!.id ?: return@launch - val success = photoRepository.exportPhoto(currentPhoto!!) - if (success) onSuccess() else onError() + photoRepository.exportPhoto(currentPhoto!!).let { success -> + onMain { + if (success) onSuccess() else onError() + } + } } } \ No newline at end of file diff --git a/app/src/main/java/dev/leonlatsch/photok/model/repositories/PhotoRepository.kt b/app/src/main/java/dev/leonlatsch/photok/model/repositories/PhotoRepository.kt index 57a00e8a..445ebd82 100644 --- a/app/src/main/java/dev/leonlatsch/photok/model/repositories/PhotoRepository.kt +++ b/app/src/main/java/dev/leonlatsch/photok/model/repositories/PhotoRepository.kt @@ -308,7 +308,7 @@ class PhotoRepository @Inject constructor( * * @param photo The Photo to be saved */ - fun exportPhoto(photo: Photo): Boolean { + suspend fun exportPhoto(photo: Photo): Boolean { return try { val inputStream = encryptedStorageManager.internalOpenEncryptedFileInput(photo.internalFileName) @@ -320,7 +320,12 @@ class PhotoRepository @Inject constructor( val wrote = inputStream.copyTo(outputStream) outputStream.lazyClose() - wrote != -1L + var deleted = true + if (config.deleteExportedFiles) { + deleted = safeDeletePhoto(photo) + } + + wrote != -1L && deleted } catch (e: IOException) { Timber.d("Error exporting file: ${photo.fileName}") false diff --git a/app/src/main/java/dev/leonlatsch/photok/settings/data/Config.kt b/app/src/main/java/dev/leonlatsch/photok/settings/data/Config.kt index fee02676..f31680c8 100644 --- a/app/src/main/java/dev/leonlatsch/photok/settings/data/Config.kt +++ b/app/src/main/java/dev/leonlatsch/photok/settings/data/Config.kt @@ -93,6 +93,13 @@ class Config(context: Context) { get() = getBoolean(ADVANCED_DELETE_IMPORTED_FILES, ADVANCED_DELETE_IMPORTED_FILES_DEFAULT) set(value) = putBoolean(ADVANCED_DELETE_IMPORTED_FILES, value) + /** + * Determines if files should be deleted after exporting them. + */ + var deleteExportedFiles: Boolean + get() = getBoolean(ADVANCED_DELETE_EXPORTED_FILES, ADVANCED_DELETE_EXPORTED_FILES_DEFAULT) + set(value) = putBoolean(ADVANCED_DELETE_EXPORTED_FILES, value) + var timestampLastRecoveryStart: Long get() = getLong(TIMESTAMP_LAST_RECOVERY_START, TIMESTAMP_LAST_RECOVERY_START_DEFAULT) set(value) = putLong(TIMESTAMP_LAST_RECOVERY_START, value) @@ -176,6 +183,9 @@ class Config(context: Context) { const val ADVANCED_DELETE_IMPORTED_FILES = "advanced^deleteImportedFiles" const val ADVANCED_DELETE_IMPORTED_FILES_DEFAULT = false + const val ADVANCED_DELETE_EXPORTED_FILES = "advanced^deleteExportedFiles" + const val ADVANCED_DELETE_EXPORTED_FILES_DEFAULT = false + const val TIMESTAMP_LAST_RECOVERY_START = "internal^timestampLastRecoveryStart" const val TIMESTAMP_LAST_RECOVERY_START_DEFAULT = 0L } diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index 6dc61b4a..73559336 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -99,6 +99,9 @@ هل أنت متأكد أنك تريد تصدير %1$d file(s),إلى معرض الصور الخاص بك؟ هل أنت متأكد أنك تريد تصدير هذا الملف إلى معرض الصور الخاص بك؟ + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? + Exported and deleted جارٍ تصدير الملف (الملفات) ... يحتاج Photok للوصول إلى التخزين الخاص بك ، لتصدير الملفات. تم التصدير @@ -156,6 +159,8 @@ قم بإنشاء نسخة احتياطية من خزنتك احذف الملفات المستوردة احذف الملفات بعد استيرادها + Delete exported files + Delete files after exporting them آخر تعليق diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index ecc98fab..90bd8c00 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -100,6 +100,9 @@ Bist du sucher, dass du %1$d Dateien, zurück in deine Galerie, exportieren willst? Bist du sicher, dass du diese Datei, zurück in deine Galerie, exportieren willst? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? + Exported and deleted Exportiere Dateien… Photok benötigt Zugang zu deinem Speicher, um Fotos zu exportieren. Exportiert @@ -158,6 +161,8 @@ Erstelle ein Backup von deinem Safe Lösche importierte Dateien Lösche Dateien nach dem Importieren + Delete exported files + Delete files after exporting them Andere Feedback diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 34755838..97509e39 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -100,6 +100,9 @@ ¿Estás seguro de que quieres exportar %1$d foto(s), en tu galería? ¿Estás seguro de que quieres exportar esta foto en tu galería? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? + Exported and deleted Exportando foto(s)… Photok necesita acceder a tu almacenamiento interno para exportar las fotos Exportado @@ -156,6 +159,8 @@ Crea una copia de seguridad de tu baúl Delete imported files Delete files after importing them + Delete exported files + Delete files after exporting them Otros Sugerencias diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 0accc200..56780e15 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -100,6 +100,9 @@ Voulez-vous vraiment exporter %1$d fichier(s) vers votre galerie ? Voulez-vous vraiment exporter ce fichier vers votre galerie ? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? + Exported and deleted Exportation en cours… Photok a besoin d\'accéder à votre espace de stockage pour exporter des fichiers. Exporté @@ -158,6 +161,8 @@ Créer une sauvegarde de votre coffre-fort Delete imported files Delete files after importing them + Delete exported files + Delete files after exporting them Autre Feedback diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 884e161e..d3279249 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -82,6 +82,9 @@ Weet je zeker dat je %1$d bestand(en) wilt exporteren, terug naar uw galerij? Weet je zeker dat je dit bestand wilt exporteren naar je galerij? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? + Exported and deleted Bestand(en) exporteren… Photok heeft toegang nodig tot je opslag om bestanden te exporteren. Geëxporteerd @@ -139,6 +142,8 @@ Maak een back-up van uw kluis Verwijder geïmporteerde bestanden Verwijder bestanden na het importeren + Delete exported files + Delete files after exporting them Andere Feedback diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 278be1d7..b9bd3c02 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -100,9 +100,12 @@ Tem certeza que quer exportar %1$d o(s) arquivo(s), de volta à sua galeria? Tem certeza que quer exportar este arquivo, de volta à sua galeria? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? Exportando o(s) arquivo(s)… Photok precisa acessar seu armazenamento, para poder exportar os arquivos. Exportado + Exported and deleted Versão: @@ -248,5 +251,7 @@ Recovery Menu Open Photok - Reset Hide App Setting + Reset Hide App Setting + Delete exported files + Delete files after exporting them diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index c195b3c3..6ad9908d 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -100,6 +100,9 @@ Вы уверены, что хотите экспортировать %1$d файл(ов) обратно в галерею? Вы уверены, что хотите экспортировать этот файл в галерею? Экспортирование файла(ов)… + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Exported and deleted + Are you sure, you want to export this file to your gallery and delete it? Photok требует доступ к вашему хранилищу для экспорта файлов. Экспортировано @@ -156,6 +159,8 @@ Сделать резервную копию вашего сейфа Удалять импортированные файлы Удалять файлы после их импорта в сейф + Delete exported files + Delete files after exporting them Прочее Обратная связь diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index abfa7274..6f409fe1 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -99,7 +99,10 @@ 你确定要把 %1$d 个文件 回到系统图库吗? 你确定要把这个文件 回到系统图库吗? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? + Are you sure, you want to export this file to your gallery and delete it? 导出文件中…… + Exported and deleted Photok 需要访问你的存储来导出文件。 已导出 @@ -153,7 +156,9 @@ 备份 创建文件保险库的备份 删除导入的文件 - 导入文件后删除它们 + 导入文件后删除它们 + Delete exported files + Delete files after exporting them 其他 反馈 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 45978960..a9eb29c6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -98,10 +98,13 @@ Are you sure, you want to export %1$d file(s), back to your gallery? + Are you sure, you want to export %1$d file(s), back to your gallery and delete them? Are you sure, you want to export this file to your gallery? + Are you sure, you want to export this file to your gallery and delete it? Exporting file(s)… Photok needs to access your storage, to export files. Exported + Exported and deleted Version: @@ -156,6 +159,8 @@ Create a backup of your safe Delete imported files Delete files after importing them + Delete exported files + Delete files after exporting them Other Feedback diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 6c222496..6bb4067e 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -102,6 +102,12 @@ android:summary="@string/settings_advanced_delete_imported_summary" android:title="@string/settings_advanced_delete_imported_title" /> + +