Skip to content

Commit

Permalink
add backup and export sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 5, 2024
1 parent 901a7e8 commit 4020609
Show file tree
Hide file tree
Showing 28 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import com.example.util.simpletimetracker.core.extension.allowDiskRead
import com.example.util.simpletimetracker.core.provider.ApplicationDataProvider
import com.example.util.simpletimetracker.navigation.params.action.ActionParams
import com.example.util.simpletimetracker.navigation.params.action.CreateFileParams
Expand All @@ -19,7 +20,7 @@ import com.example.util.simpletimetracker.navigation.params.action.OpenMarketPar
import com.example.util.simpletimetracker.navigation.params.action.OpenSystemSettings
import com.example.util.simpletimetracker.navigation.params.action.RequestPermissionParams
import com.example.util.simpletimetracker.navigation.params.action.SendEmailParams
import com.example.util.simpletimetracker.navigation.params.action.ShareImageParams
import com.example.util.simpletimetracker.navigation.params.action.ShareFileParams
import javax.inject.Inject

class ActionResolverImpl @Inject constructor(
Expand All @@ -44,7 +45,7 @@ class ActionResolverImpl @Inject constructor(
is CreateFileParams -> createFile(activity, data)
is OpenFileParams -> openFile(activity, data)
is OpenSystemSettings -> openSystemSettings(activity, data)
is ShareImageParams -> shareImage(activity, data)
is ShareFileParams -> shareFile(activity, data)
is RequestPermissionParams -> requestPermission(data)
is OpenLinkParams -> openLink(activity, data)
}
Expand Down Expand Up @@ -148,11 +149,12 @@ class ActionResolverImpl @Inject constructor(
}
}

private fun shareImage(activity: Activity?, data: ShareImageParams) {
private fun shareFile(activity: Activity?, data: ShareFileParams) {
try {
val uri = Uri.parse(data.uriString)
val type = allowDiskRead { data.type ?: activity?.contentResolver?.getType(uri) }
val intent = Intent(Intent.ACTION_SEND).apply {
setDataAndType(uri, activity?.contentResolver?.getType(uri))
setDataAndType(uri, type)
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.example.util.simpletimetracker.domain.resolver.SharingRepo
import com.example.util.simpletimetracker.feature_views.extension.getBitmapFromView
import com.example.util.simpletimetracker.feature_views.extension.measureForSharing
import com.example.util.simpletimetracker.navigation.Router
import com.example.util.simpletimetracker.navigation.params.action.ShareImageParams
import com.example.util.simpletimetracker.navigation.params.action.ShareFileParams
import com.example.util.simpletimetracker.navigation.params.notification.SnackBarParams
import javax.inject.Inject

Expand All @@ -29,8 +29,9 @@ class SharingInteractor @Inject constructor(

when (result) {
is SharingRepo.Result.Success -> {
ShareImageParams(
ShareFileParams(
uriString = result.uriString,
type = null,
notHandledCallback = { R.string.message_app_not_found.let(::showMessage) },
).let(router::execute)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.example.util.simpletimetracker.navigation.Router
import com.example.util.simpletimetracker.navigation.params.action.ActionParams
import com.example.util.simpletimetracker.navigation.params.action.CreateFileParams
import com.example.util.simpletimetracker.navigation.params.action.OpenFileParams
import com.example.util.simpletimetracker.navigation.params.action.ShareFileParams
import com.example.util.simpletimetracker.navigation.params.notification.SnackBarParams
import com.example.util.simpletimetracker.navigation.params.screen.DataExportSettingDialogParams
import com.example.util.simpletimetracker.navigation.params.screen.DataExportSettingsResult
Expand Down Expand Up @@ -250,7 +251,7 @@ class SettingsFileWorkDelegate @Inject constructor(
private fun onSaveBackup(uriString: String?) {
if (uriString == null) return
val params = saveOptionsData ?: return
executeFileWork {
executeFileWork(shareUriString = uriString) {
backupInteractor.saveBackupFile(uriString, params)
}
}
Expand Down Expand Up @@ -353,7 +354,7 @@ class SettingsFileWorkDelegate @Inject constructor(

private fun onSaveCsvFile(uriString: String?) {
if (uriString == null) return
executeFileWork {
executeFileWork(shareUriString = uriString) {
csvExportInteractor.saveCsvFile(
uriString = uriString,
range = getRange(),
Expand All @@ -370,7 +371,7 @@ class SettingsFileWorkDelegate @Inject constructor(

private fun onSaveIcsFile(uriString: String?) {
if (uriString == null) return
executeFileWork {
executeFileWork(shareUriString = uriString) {
icsExportInteractor.saveIcsFile(
uriString = uriString,
range = getRange(),
Expand All @@ -392,13 +393,20 @@ class SettingsFileWorkDelegate @Inject constructor(
// Need global scope or not cancelable scope.
// Otherwise process will be stopped on navigation.
private fun executeFileWork(
shareUriString: String? = null,
doAfter: suspend () -> Unit = {},
doWork: suspend () -> ResultCode,
) = delegateScope.launch {
fileWorkRepo.inProgress.set(true)

val resultCode = doWork()
resultCode.message?.let(::showMessage)
val isSuccessful = resultCode is ResultCode.Success
resultCode.message?.let {
showMessage(
string = it,
shareUriString = shareUriString.takeIf { isSuccessful },
)
}

fileWorkRepo.inProgress.set(false)

Expand All @@ -413,11 +421,38 @@ class SettingsFileWorkDelegate @Inject constructor(
showMessage(resourceRepo.getString(R.string.settings_file_create_error))
}

private fun showMessage(string: String) {
val params = SnackBarParams(message = string)
private fun showMessage(
string: String,
shareUriString: String? = null,
) {
val isForSharing = shareUriString != null
val actionText = if (isForSharing) {
resourceRepo.getString(R.string.message_action_share)
} else {
""
}
val params = SnackBarParams(
message = string,
actionText = actionText,
actionListener = { onShareClicked(shareUriString) },
)
router.show(params)
}

private fun onShareClicked(
shareUriString: String?,
) {
val shareData = ShareFileParams(
uriString = shareUriString.orEmpty(),
type = FILE_TYPE_BIN_OPEN,
notHandledCallback = {
resourceRepo.getString(R.string.message_app_not_found)
.let(::showMessage)
},
)
router.execute(shareData)
}

private fun getFileNameTimeStamp(): String {
return SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.util.simpletimetracker.navigation.params.action

data class ShareImageParams(
data class ShareFileParams(
val uriString: String,
val type: String?,
val notHandledCallback: (() -> Unit),
) : ActionParams
1 change: 1 addition & 0 deletions resources/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
<string name="message_import_complete">اكتمل الاستيراد</string>
<string name="message_import_complete_hint">التتبعات المضافة: %s</string>
<string name="message_import_error">خطأ في الاستيراد</string>
<string name="message_action_share">يشارك</string>
<string name="message_automatic_backup_error">حدث خطأ في النسخ الاحتياطي التلقائي.</string>
<string name="message_automatic_export_error">حدث خطأ في التصدير التلقائي.</string>
<string name="message_automatic_error_hint">حاول تحديد مكان مختلف</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Exemple:<br/>
<string name="message_import_complete">Importació completada</string>
<string name="message_import_complete_hint">Registres afegits: %s</string>
<string name="message_import_error">Error d\'importació</string>
<string name="message_action_share">Comparteix</string>
<string name="message_automatic_backup_error">Hi ha hagut un error amb la còpia de seguretat automàtica.</string>
<string name="message_automatic_export_error">Hi ha hagut un error amb l\'exportació automàtica.</string>
<string name="message_automatic_error_hint">Proveu de seleccionar una ubicació diferent</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Beispiel:<br/>
<string name="message_import_complete">Import abgeschlossen</string>
<string name="message_import_complete_hint">Hinzugefügte Datensätze: %s</string>
<string name="message_import_error">Importfehler</string>
<string name="message_action_share">Aktie</string>
<string name="message_automatic_backup_error">Bei der automatischen Sicherung ist ein Fehler aufgetreten.</string>
<string name="message_automatic_export_error">Beim automatischen Export ist ein Fehler aufgetreten.</string>
<string name="message_automatic_error_hint">Versuchen Sie, einen anderen Ort auszuwählen</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Ejemplo:<br/>
<string name="message_import_complete">Importación completada</string>
<string name="message_import_complete_hint">Registros agregados: %s</string>
<string name="message_import_error">Error de importación</string>
<string name="message_action_share">Compartir</string>
<string name="message_automatic_backup_error">Hubo un error con la copia de seguridad automática.</string>
<string name="message_automatic_export_error">Hubo un error con la exportación automática.</string>
<string name="message_automatic_error_hint">Intenta seleccionar una ubicación diferente</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
<string name="message_import_complete">واردات انجام شد</string>
<string name="message_import_complete_hint">سوابق اضافه شده: %s</string>
<string name="message_import_error">خطای واردات</string>
<string name="message_action_share">به اشتراک بگذارید</string>
<string name="message_automatic_backup_error">خطا در پشتیبانی خودکار.</string>
<string name="message_automatic_export_error">خطا در خروجی خودکار.</string>
<string name="message_automatic_error_hint">سعی کنید محل ذخیره دیگیری را انتخاب کنید.</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Exemple:<br/>
<string name="message_import_complete">Importation terminée</string>
<string name="message_import_complete_hint">Enregistrements ajoutés : %s</string>
<string name="message_import_error">Erreur d\'importation</string>
<string name="message_action_share">Partager</string>
<string name="message_automatic_backup_error">Une erreur s\'est produite lors de la sauvegarde automatique.</string>
<string name="message_automatic_export_error">Une erreur s\'est produite lors de l\'exportation automatique.</string>
<string name="message_automatic_error_hint">Essayez de sélectionner un emplacement différent</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-hi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ csv फ़ाइल में कॉमा से अलग किए गए य
<string name="message_import_complete">आयात पूरा हुआ</string>
<string name="message_import_complete_hint">रिकॉर्ड जोड़े गए: %s</string>
<string name="message_import_error">आयात त्रुटि</string>
<string name="message_action_share">शेयर करना</string>
<string name="message_automatic_backup_error">स्वचालित बैकअप के साथ एक त्रुटि हुई थी।</string>
<string name="message_automatic_export_error">स्वचालित निर्यात में त्रुटि हुई थी।</string>
<string name="message_automatic_error_hint">अलग स्थान का चयन करने का प्रयास करें</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Contoh:<br/>
<string name="message_import_complete">Impor selesai</string>
<string name="message_import_complete_hint">Catatan ditambahkan: %s</string>
<string name="message_import_error">Kesalahan impor</string>
<string name="message_action_share">Membagikan</string>
<string name="message_automatic_backup_error">Terjadi kesalahan dengan pencadangan otomatis.</string>
<string name="message_automatic_export_error">Terjadi kesalahan dengan ekspor otomatis.</string>
<string name="message_automatic_error_hint">Cobalah untuk memilih lokasi yang berbeda</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Esempio:<br/>
<string name="message_import_complete">Importazione completata</string>
<string name="message_import_complete_hint">Record aggiunti: %s</string>
<string name="message_import_error">Errore di importazione</string>
<string name="message_action_share">Condividere</string>
<string name="message_automatic_backup_error">Si è verificato un errore con il backup automatico.</string>
<string name="message_automatic_export_error">Si è verificato un errore con l\'esportazione automatica.</string>
<string name="message_automatic_error_hint">Prova a selezionare una posizione diversa</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ CSV ファイルには、カンマで区切られた次の列が含まれてい
<string name="message_import_complete">インポートが完了しました</string>
<string name="message_import_complete_hint">追加されたレコード: %s</string>
<string name="message_import_error">インポートエラー</string>
<string name="message_action_share">共有</string>
<string name="message_automatic_backup_error">自動バックアップでエラーが発生しました。</string>
<string name="message_automatic_export_error">自動エクスポートでエラーが発生しました。</string>
<string name="message_automatic_error_hint">別の場所を選択してみてください</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-ko/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ csv 파일은 다음과 같은 열(column)들을 가져야합니다:<br/>
<string name="message_import_complete">가져오기 완료</string>
<string name="message_import_complete_hint">추가된 기록 수: %s</string>
<string name="message_import_error">가져오기 오류</string>
<string name="message_action_share">공유하다</string>
<string name="message_automatic_backup_error">자동 백업 오류 발생</string>
<string name="message_automatic_export_error">자동 내보내기 오류 발생</string>
<string name="message_automatic_error_hint">다른 위치를 선택해 보세요</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Voorbeeld:<br/>
<string name="message_import_complete">Importeren voltooid</string>
<string name="message_import_complete_hint">Records toegevoegd: %s</string>
<string name="message_import_error">Fout importeren</string>
<string name="message_action_share">Deel</string>
<string name="message_automatic_backup_error">Er is een fout opgetreden bij de automatische back-up.</string>
<string name="message_automatic_export_error">Er is een fout opgetreden bij het automatisch exporteren.</string>
<string name="message_automatic_error_hint">Probeer een andere locatie te selecteren</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Przykład:<br/>
<string name="message_import_complete">Import zakończony</string>
<string name="message_import_complete_hint">Dodano rekordy: %s</string>
<string name="message_import_error">Błąd importu</string>
<string name="message_action_share">Udział</string>
<string name="message_automatic_backup_error">Wystąpił błąd podczas automatycznego tworzenia kopii zapasowych.</string>
<string name="message_automatic_export_error">Wystąpił błąd podczas automatycznego eksportu.</string>
<string name="message_automatic_error_hint">Spróbuj wybrać inną lokalizację</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-pt-rPT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Exemplo:<br/>
<string name="message_import_complete">Importação concluída</string>
<string name="message_import_complete_hint">Registos adicionados: %s</string>
<string name="message_import_error">Erro de importação</string>
<string name="message_action_share">Compartilhar</string>
<string name="message_automatic_backup_error">Ocorreu um erro com a cópia de segurança automática.</string>
<string name="message_automatic_export_error">Ocorreu um erro com a exportação automática.</string>
<string name="message_automatic_error_hint">Selecionar uma pasta diferente</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Exemplo:<br/>
<string name="message_import_complete">Importação concluída</string>
<string name="message_import_complete_hint">Registros adicionados: %s</string>
<string name="message_import_error">Erro de importação</string>
<string name="message_action_share">Compartilhar</string>
<string name="message_automatic_backup_error">Ocorreu um erro com o backup automático.</string>
<string name="message_automatic_export_error">Ocorreu um erro com a exportação automática.</string>
<string name="message_automatic_error_hint">Tente selecionar um local diferente</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-ro/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Examplu:<br/>
<string name="message_import_complete">Import completat</string>
<string name="message_import_complete_hint">Înregistrări adăugate %s</string>
<string name="message_import_error">Eroare la import</string>
<string name="message_action_share">Distribuie</string>
<string name="message_automatic_backup_error">A fost o eroare la backup-ul automat.</string>
<string name="message_automatic_export_error">A fost o eroare la exportul automat.</string>
<string name="message_automatic_error_hint">Încearcă să alegi o altă locație</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ CSV-файл должен содержать следующие столбцы,
<string name="message_import_complete">Импорт завершен</string>
<string name="message_import_complete_hint">Добавлено записей: %s</string>
<string name="message_import_error">Ошибка импорта</string>
<string name="message_action_share">Поделиться</string>
<string name="message_automatic_backup_error">Произошла ошибка с автоматическим резервным копированием.</string>
<string name="message_automatic_export_error">Произошла ошибка при автоматическом экспорте.</string>
<string name="message_automatic_error_hint">Попробуйте выбрать другое место</string>
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/res/values-sv/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Exempel:<br/>
<string name="message_import_complete">Importen slutförd</string>
<string name="message_import_complete_hint">Poster tillagda: %s</string>
<string name="message_import_error">Importfel</string>
<string name="message_action_share">Dela</string>
<string name="message_automatic_backup_error">Det uppstod ett fel med automatisk säkerhetskopiering.</string>
<string name="message_automatic_export_error">Det uppstod ett fel med automatisk export.</string>
<string name="message_automatic_error_hint">Försök att välja en annan plats</string>
Expand Down
Loading

0 comments on commit 4020609

Please sign in to comment.