Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty transaction export #5

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Exiting the wallet after accepting an identity verification error
- Incorrect environment name in a private key export file for Mainnet
- Improper handling of rejected identity verification when setting up a new wallet
- An issue where exporting transaction logs for an account without any transactions would be stuck at 0%

### Changed
- Suggest running a recovery when facing account or identity creation errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ExportTransactionLogActivity : BaseActivity(
viewModel.onIdleRequested()

hideActionBarBack(isVisible = true) {
viewModel.onIdleRequested()
finish()
}
}
Expand Down Expand Up @@ -86,6 +85,7 @@ class ExportTransactionLogActivity : BaseActivity(

binding.successLayout.isVisible = downloadState is FileDownloadScreenState.Downloaded
binding.failedLayout.isVisible = downloadState is FileDownloadScreenState.Failed
binding.noContentLayout.isVisible = downloadState is FileDownloadScreenState.NoContent

binding.generate.isVisible = downloadState is FileDownloadScreenState.Idle
binding.cancel.isVisible = downloadState is FileDownloadScreenState.Downloading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.http.Streaming
Expand All @@ -31,20 +32,24 @@ import java.util.concurrent.TimeUnit
interface FileDownloadApi {
@Streaming
@GET
suspend fun downloadFile(@Url url: String?): ResponseBody
suspend fun downloadFile(@Url url: String?): Response<ResponseBody>
}

sealed class FileDownloadScreenState {
object Idle : FileDownloadScreenState()
data class Downloading(val progress: Int) : FileDownloadScreenState()
object Failed : FileDownloadScreenState()
object Downloaded : FileDownloadScreenState()
object NoContent : FileDownloadScreenState()
}

class ExportTransactionLogViewModel(application: Application) : AndroidViewModel(application) {
lateinit var account: Account
private lateinit var api: FileDownloadApi

val HTTP_OK = 200
val HTTP_NO_CONTENT = 204

val textResourceInt: MutableLiveData<Int> by lazy { MutableLiveData<Int>() }

private sealed class DownloadState {
Expand Down Expand Up @@ -78,8 +83,17 @@ class ExportTransactionLogViewModel(application: Application) : AndroidViewModel
)
)

api.downloadFile(downloadFile)
.saveFile(destinationFolder)
val response = api.downloadFile(downloadFile)
val statusCode = response.code()
if (statusCode == HTTP_NO_CONTENT) {
[email protected](FileDownloadScreenState.NoContent)
return@launch
} else if (statusCode != HTTP_OK || response.body() == null) {
[email protected](FileDownloadScreenState.Failed)
return@launch
}

response.body()!!.saveFile(destinationFolder)
.collect { downloadState ->
// Add visual delay and ensure the coroutine is active.
delay(300)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_export_transaction_log.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
style="@style/CryptoX_Container_Error.TextView"
android:text="@string/export_transaction_log_failed" />

<TextView
android:id="@+id/no_content_layout"
style="@style/CryptoX_Container_Information.TextView"
android:text="@string/export_transaction_log_no_content" />

<Space
android:layout_width="0dp"
android:layout_height="20dp" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@
<string name="export_transaction_log_cancel">Cancel</string>
<string name="export_transaction_log_saved">The transaction logs have been saved</string>
<string name="export_transaction_log_failed">Failed to download transaction logs</string>
<string name="export_transaction_log_no_content">There are no transaction logs available for this account</string>
<string name="export_transaction_log_progress">%1$d%%</string> <!-- 45% -->
<string name="export_transaction_log_downloading">Downloading transaction logs…</string>

Expand Down
Loading