Skip to content

Commit

Permalink
refactor(storage): use the new Kotlin Destructuring Declarations (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatfiredev authored Sep 24, 2020
1 parent 2fc7c6b commit d20b5b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.firebase.ktx.Firebase
import com.google.firebase.quickstart.firebasestorage.R
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.ktx.component1
import com.google.firebase.storage.ktx.component2
import com.google.firebase.storage.ktx.storage

class MyDownloadService : MyBaseTaskService() {
Expand Down Expand Up @@ -46,8 +48,7 @@ class MyDownloadService : MyBaseTaskService() {
showProgressNotification(getString(R.string.progress_downloading), 0, 0)

// Download and get total bytes
storageRef.child(downloadPath).getStream { taskSnapshot, inputStream ->
val totalBytes = taskSnapshot.totalByteCount
storageRef.child(downloadPath).getStream { (_, totalBytes), inputStream ->
var bytesDownloaded: Long = 0

val buffer = ByteArray(1024)
Expand All @@ -63,12 +64,12 @@ class MyDownloadService : MyBaseTaskService() {

// Close the stream at the end of the Task
inputStream.close()
}.addOnSuccessListener { taskSnapshot ->
}.addOnSuccessListener { (_, totalBytes) ->
Log.d(TAG, "download:SUCCESS")

// Send success broadcast with number of bytes downloaded
broadcastDownloadFinished(downloadPath, taskSnapshot.totalByteCount)
showDownloadFinishedNotification(downloadPath, taskSnapshot.totalByteCount.toInt())
broadcastDownloadFinished(downloadPath, totalBytes)
showDownloadFinishedNotification(downloadPath, totalBytes.toInt())

// Mark task completed
taskCompleted()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.google.firebase.quickstart.firebasestorage.kotlin

import android.app.Service
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
Expand All @@ -11,6 +10,8 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.firebase.ktx.Firebase
import com.google.firebase.quickstart.firebasestorage.R
import com.google.firebase.storage.StorageReference
import com.google.firebase.storage.ktx.component1
import com.google.firebase.storage.ktx.component2
import com.google.firebase.storage.ktx.storage

/**
Expand Down Expand Up @@ -49,12 +50,12 @@ class MyUploadService : MyBaseTaskService() {
uploadFromUri(fileUri)
}

return Service.START_REDELIVER_INTENT
return START_REDELIVER_INTENT
}

// [START upload_from_uri]
private fun uploadFromUri(fileUri: Uri) {
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString())
Log.d(TAG, "uploadFromUri:src:$fileUri")

// [START_EXCLUDE]
taskStarted()
Expand All @@ -70,10 +71,10 @@ class MyUploadService : MyBaseTaskService() {

// Upload file to Firebase Storage
Log.d(TAG, "uploadFromUri:dst:" + photoRef.path)
photoRef.putFile(fileUri).addOnProgressListener { taskSnapshot ->
photoRef.putFile(fileUri).addOnProgressListener { (bytesTransferred, totalByteCount) ->
showProgressNotification(getString(R.string.progress_uploading),
taskSnapshot.bytesTransferred,
taskSnapshot.totalByteCount)
bytesTransferred,
totalByteCount)
}.continueWithTask { task ->
// Forward any exceptions
if (!task.isSuccessful) {
Expand Down

0 comments on commit d20b5b4

Please sign in to comment.