Skip to content

Commit

Permalink
Add option to delete files after export #247 (#259)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
BIBLETUM authored Feb 22, 2024
1 parent 166c742 commit c4b1e96
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +41,9 @@ class GalleryFragment : Fragment() {
@Inject
lateinit var navigator: GalleryNavigator

@Inject
lateinit var config: Config

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -59,7 +63,7 @@ class GalleryFragment : Fragment() {

launchLifecycleAwareJob {
viewModel.eventsFlow.collect { event ->
navigator.navigate(event, findNavController(), this)
navigator.navigate(event, findNavController(), this, config.deleteExportedFiles)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -50,20 +51,24 @@ class GalleryNavigator @Inject constructor() {
is GalleryNavigationEvent.StartExportDialog -> navigateStartExportDialog(
fragment.requireContext(),
event.photosToExport,
fragment.childFragmentManager
fragment.childFragmentManager,
deleteExportedFiles
)
}
}

private fun navigateStartExportDialog(
context: Context,
photos: List<Photo>,
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class GalleryFragment : BindableFragment<FragmentGalleryBinding>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
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
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
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,20 @@ class ImageViewerFragment : BindableFragment<FragmentImageViewerBinding>(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))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/dev/leonlatsch/photok/settings/data/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<!-- EXPORT -->
<string name="export_are_you_sure">هل أنت متأكد أنك تريد <b>تصدير</b> %1$d file(s),إلى معرض الصور الخاص بك؟</string>
<string name="export_are_you_sure_this">هل أنت متأكد أنك تريد <b>تصدير</b> هذا الملف إلى معرض الصور الخاص بك؟ </string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_exporting">جارٍ تصدير الملف (الملفات) ... </string>
<string name="export_permission_rationale">يحتاج Photok للوصول إلى التخزين الخاص بك ، لتصدير الملفات. </string>
<string name="export_finished">تم التصدير</string>
Expand Down Expand Up @@ -156,6 +159,8 @@
<string name="settings_advanced_backup_summary">قم بإنشاء نسخة احتياطية من خزنتك </string>
<string name="settings_advanced_delete_imported_title">احذف الملفات المستوردة</string>
<string name="settings_advanced_delete_imported_summary">احذف الملفات بعد استيرادها</string>
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">آخر</string>
<string name="settings_other_feedback_title">تعليق</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<!-- EXPORT -->
<string name="export_are_you_sure">Bist du sucher, dass du %1$d Dateien, zurück in deine Galerie, <b>exportieren</b> willst?</string>
<string name="export_are_you_sure_this">Bist du sicher, dass du diese Datei, zurück in deine Galerie, <b>exportieren</b> willst?</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_exporting">Exportiere Dateien…</string>
<string name="export_permission_rationale">Photok benötigt Zugang zu deinem Speicher, um Fotos zu exportieren.</string>
<string name="export_finished">Exportiert</string>
Expand Down Expand Up @@ -158,6 +161,8 @@
<string name="settings_advanced_backup_summary">Erstelle ein Backup von deinem Safe</string>
<string name="settings_advanced_delete_imported_title">Lösche importierte Dateien</string>
<string name="settings_advanced_delete_imported_summary">Lösche Dateien nach dem Importieren</string>
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">Andere</string>
<string name="settings_other_feedback_title">Feedback</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<!-- EXPORT -->
<string name="export_are_you_sure">¿Estás seguro de que quieres <b>exportar</b> %1$d foto(s), en tu galería?</string>
<string name="export_are_you_sure_this">¿Estás seguro de que quieres <b>exportar</b> esta foto en tu galería?</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_exporting">Exportando foto(s)…</string>
<string name="export_permission_rationale">Photok necesita acceder a tu almacenamiento interno para exportar las fotos</string>
<string name="export_finished">Exportado</string>
Expand Down Expand Up @@ -156,6 +159,8 @@
<string name="settings_advanced_backup_summary">Crea una copia de seguridad de tu baúl</string>
<string name="settings_advanced_delete_imported_title">Delete imported files</string> <!-- TODO -->
<string name="settings_advanced_delete_imported_summary">Delete files after importing them</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">Otros</string>
<string name="settings_other_feedback_title">Sugerencias</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<!-- EXPORT -->
<string name="export_are_you_sure">Voulez-vous vraiment <b>exporter</b> %1$d fichier(s) vers votre galerie ?</string>
<string name="export_are_you_sure_this">Voulez-vous vraiment <b>exporter</b> ce fichier vers votre galerie ?</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_exporting">Exportation en cours…</string>
<string name="export_permission_rationale">Photok a besoin d\'accéder à votre espace de stockage pour exporter des fichiers.</string>
<string name="export_finished">Exporté</string>
Expand Down Expand Up @@ -158,6 +161,8 @@
<string name="settings_advanced_backup_summary">Créer une sauvegarde de votre coffre-fort</string>
<string name="settings_advanced_delete_imported_title">Delete imported files</string> <!-- TODO -->
<string name="settings_advanced_delete_imported_summary">Delete files after importing them</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">Autre</string>
<string name="settings_other_feedback_title">Feedback</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<!-- EXPORT -->
<string name="export_are_you_sure">Weet je zeker dat je %1$d bestand(en) wilt <b>exporteren</b>, terug naar uw galerij?</string>
<string name="export_are_you_sure_this">Weet je zeker dat je dit bestand wilt <b>exporteren</b> naar je galerij?</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_exporting">Bestand(en) exporteren…</string>
<string name="export_permission_rationale">Photok heeft toegang nodig tot je opslag om bestanden te exporteren.</string>
<string name="export_finished">Geëxporteerd</string>
Expand Down Expand Up @@ -139,6 +142,8 @@
<string name="settings_advanced_backup_summary">Maak een back-up van uw kluis</string>
<string name="settings_advanced_delete_imported_title">Verwijder geïmporteerde bestanden</string>
<string name="settings_advanced_delete_imported_summary">Verwijder bestanden na het importeren</string>
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">Andere</string>
<string name="settings_other_feedback_title">Feedback</string>
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@
<!-- EXPORT -->
<string name="export_are_you_sure">Tem certeza que quer <b>exportar</b> %1$d o(s) arquivo(s), de volta à sua galeria?</string>
<string name="export_are_you_sure_this">Tem certeza que quer <b>exportar</b> este arquivo, de volta à sua galeria?</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_exporting">Exportando o(s) arquivo(s)…</string>
<string name="export_permission_rationale">Photok precisa acessar seu armazenamento, para poder exportar os arquivos.</string>
<string name="export_finished">Exportado</string>
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->

<!-- ABOUT -->
<string name="about_version">Versão:</string>
Expand Down Expand Up @@ -248,5 +251,7 @@
<!-- Recovery Menu -->
<string name="recovery_subtitle">Recovery Menu</string> <!-- TODO -->
<string name="recovery_menu_item_open_photok">Open Photok</string> <!-- TODO -->
<string name="recovery_subtitle_reset_hide_app">Reset Hide App Setting</string> <!-- TODO -->
<string name="recovery_subtitle_reset_hide_app">Reset Hide App Setting</string>
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<string name="export_are_you_sure">Вы уверены, что хотите <b>экспортировать</b> %1$d файл(ов) обратно в галерею?</string>
<string name="export_are_you_sure_this">Вы уверены, что хотите <b>экспортировать</b> этот файл в галерею?</string>
<string name="export_exporting">Экспортирование файла(ов)…</string>
<string name="export_and_delete_are_you_sure">Are you sure, you want to <b>export</b> %1$d file(s), back to your gallery and delete them?</string> <!-- TODO -->
<string name="export_and_delete_finished">Exported and deleted</string> <!-- TODO -->
<string name="export_and_delete_are_you_sure_this">Are you sure, you want to <b>export</b> this file to your gallery and delete it?</string> <!-- TODO -->
<string name="export_permission_rationale">Photok требует доступ к вашему хранилищу для экспорта файлов.</string>
<string name="export_finished">Экспортировано</string>

Expand Down Expand Up @@ -156,6 +159,8 @@
<string name="settings_advanced_backup_summary">Сделать резервную копию вашего сейфа</string>
<string name="settings_advanced_delete_imported_title">Удалять импортированные файлы</string>
<string name="settings_advanced_delete_imported_summary">Удалять файлы после их импорта в сейф</string>
<string name="settings_advanced_delete_exported_title">Delete exported files</string> <!-- TODO -->
<string name="settings_advanced_delete_exported_summary">Delete files after exporting them</string> <!-- TODO -->

<string name="settings_other_title">Прочее</string>
<string name="settings_other_feedback_title">Обратная связь</string>
Expand Down
Loading

0 comments on commit c4b1e96

Please sign in to comment.