Skip to content

Commit

Permalink
fix: Fix backup files not getting saved in some cases (#134)
Browse files Browse the repository at this point in the history
For example, when saving the backup file via file managers like Solid Explorer or Google Drive.
---------
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored May 13, 2024
1 parent c421c6d commit a2ad63b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/com/starry/greenstash/backup/BackupManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import java.time.LocalDateTime
/**
* Handles all backup & restore related functionalities.
* Note: Access this class using DI instead of manually initialising.
*
* @param context [Context] instance.
* @param goalDao [GoalDao] instance.
*/
class BackupManager(private val context: Context, private val goalDao: GoalDao) {

Expand All @@ -66,11 +69,18 @@ class BackupManager(private val context: Context, private val goalDao: GoalDao)

/** An ISO-8601 date format for Gson */
private const val ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

/** Backup folder name inside cache directory. */
private const val BACKUP_FOLDER_NAME = "backups"
}

/**
* Model for backup json data, containing current schema version
* and timestamp when backup was created.
*
* @param version backup schema version.
* @param timestamp timestamp when backup was created.
* @param data list of [GoalWithTransactions] to be backed up.
*/
@Keep
data class BackupJsonModel(
Expand Down Expand Up @@ -104,8 +114,8 @@ class BackupManager(private val context: Context, private val goalDao: GoalDao)
)

log("Creating backup json file inside cache directory...")
val fileName = "GreenStash-Backup (${LocalDateTime.now()}).json"
val file = File(context.cacheDir, fileName)
val fileName = "GreenStash-Backup(${System.currentTimeMillis()}).json"
val file = File(File(context.cacheDir, BACKUP_FOLDER_NAME).apply { mkdir() }, fileName)
file.updateText(jsonString)
val uri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, file)

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<paths>
<cache-path
name="backup_and_restore"
path="/" />
path="backups/" />
</paths>

0 comments on commit a2ad63b

Please sign in to comment.