refactor(GoogleDrive): Use gson to encode the syncData.

Same as before. OOM (Out of Memory) issue.

Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
KaiserBh 2024-01-10 09:50:06 +11:00
parent b23c100fab
commit 5b2bbb1123
No known key found for this signature in database
GPG Key ID: 14D73B142042BBA9

View File

@ -106,14 +106,12 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
override suspend fun pullSyncData(): SyncData? { override suspend fun pullSyncData(): SyncData? {
val drive = googleDriveService.driveService val drive = googleDriveService.driveService
// Check if the Google Drive service is initialized
if (drive == null) { if (drive == null) {
logcat(LogPriority.ERROR) { "Google Drive service not initialized" } logcat(LogPriority.ERROR) { "Google Drive service not initialized" }
throw Exception(context.getString(R.string.google_drive_not_signed_in)) throw Exception(context.stringResource(MR.strings.google_drive_not_signed_in))
} }
val fileList = getFileList(drive) val fileList = getAppDataFileList(drive)
if (fileList.isEmpty()) { if (fileList.isEmpty()) {
return null return null
} }
@ -121,12 +119,12 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
val outputStream = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
drive.files().get(gdriveFileId).executeMediaAndDownloadTo(outputStream) drive.files().get(gdriveFileId).executeMediaAndDownloadTo(outputStream)
val jsonString = withContext(Dispatchers.IO) {
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray()))
gzipInputStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
}
return json.decodeFromString(SyncData.serializer(), jsonString) return withContext(Dispatchers.IO) {
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray()))
val gson = Gson()
gson.fromJson(gzipInputStream.reader(Charsets.UTF_8), SyncData::class.java)
}
} }
override suspend fun pushSyncData(syncData: SyncData) { override suspend fun pushSyncData(syncData: SyncData) {