-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add save all transactions functionality (#1214)
* Add save-to-file functionality for transactions * Impacted screen: Main transaction list * New UI elements: Save buttons (grouped, separated from share buttons with the divider) * Save options: Text file, HAR file * Internal: Create FileSaver object for file writing * Add test for FileSaver.saveFile method * Add kotlinx-coroutines-test dependency * Test verifies correct file content is written using the provided URI * Update CHANGELOG.md Add a line to the unreleased block about save all transactions to the file * Remove unnecessary private saveToFile method from FileSaver * Add "empty content test" for FileSaverTest * Document FileSaver class * Small refactor
- Loading branch information
Showing
9 changed files
with
272 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
library/src/main/kotlin/com/chuckerteam/chucker/internal/support/FileSaver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import android.content.ContentResolver | ||
import android.net.Uri | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import okio.Source | ||
import okio.buffer | ||
import okio.sink | ||
|
||
/** | ||
* Utility class to save a file from a [Source] to a [Uri]. | ||
*/ | ||
public object FileSaver { | ||
/** | ||
* Saves the data from the [source] to the file at the [uri] using the [contentResolver]. | ||
* | ||
* @param source The source of the data to save. | ||
* @param uri The URI of the file to save the data to. | ||
* @param contentResolver The content resolver to use to save the data. | ||
* @return `true` if the data was saved successfully, `false` otherwise. | ||
*/ | ||
public suspend fun saveFile( | ||
source: Source, | ||
uri: Uri, | ||
contentResolver: ContentResolver, | ||
): Boolean = | ||
withContext(Dispatchers.IO) { | ||
runCatching { | ||
contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
outputStream.sink().buffer().use { sink -> | ||
sink.writeAll(source) | ||
} | ||
} | ||
}.onFailure { | ||
Logger.error("Failed to save data to a file", it) | ||
return@withContext false | ||
} | ||
return@withContext true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.