@@ -23,15 +23,14 @@ import android.content.pm.PackageManager
2323import android.media.MediaScannerConnection
2424import android.net.Uri
2525import android.os.Build
26- import android.os.Bundle
2726import android.os.Environment
2827import android.os.Environment.DIRECTORY_DOWNLOADS
28+ import android.os.Parcelable
2929import android.provider.MediaStore
3030import android.provider.MediaStore.Files.FileColumns
3131import android.util.Log
3232import androidx.annotation.RequiresApi
3333import androidx.core.content.ContextCompat
34- import androidx.core.os.bundleOf
3534import androidx.lifecycle.AndroidViewModel
3635import androidx.lifecycle.LiveData
3736import androidx.lifecycle.MutableLiveData
@@ -41,6 +40,7 @@ import com.samples.storage.data.SampleFiles
4140import kotlinx.coroutines.Dispatchers
4241import kotlinx.coroutines.launch
4342import kotlinx.coroutines.withContext
43+ import kotlinx.parcelize.Parcelize
4444import okhttp3.OkHttpClient
4545import okhttp3.Request
4646import okhttp3.ResponseBody
@@ -51,7 +51,7 @@ private const val TAG = "AddDocumentViewModel"
5151
5252class AddDocumentViewModel (
5353 application : Application ,
54- savedStateHandle : SavedStateHandle
54+ private val savedStateHandle : SavedStateHandle
5555) : AndroidViewModel(application) {
5656 private val context: Context
5757 get() = getApplication()
@@ -79,22 +79,7 @@ class AddDocumentViewModel(
7979 * We keep the current [FileEntry] in the savedStateHandle to re-render it if there is a
8080 * configuration change and we expose it as a [LiveData] to the UI
8181 */
82- private var _currentFileEntry : MutableLiveData <FileEntry > = MutableLiveData (null )
83- val currentFileEntry: LiveData <FileEntry > = _currentFileEntry
84-
85- init {
86- val fileEntryBundle = savedStateHandle.get<Bundle >(" current_file" )
87- if (fileEntryBundle != null ) {
88- _currentFileEntry .value = FileEntry .fromBundle(fileEntryBundle)
89- }
90- savedStateHandle.setSavedStateProvider(" current_file" ) { // saveState()
91- if (_currentFileEntry .value != null ) {
92- _currentFileEntry .value!! .toBundle()
93- } else {
94- Bundle ()
95- }
96- }
97- }
82+ val currentFileEntry = savedStateHandle.getLiveData<FileEntry >(" current_file" )
9883
9984 /* *
10085 * Generate random filename when saving a new file
@@ -162,7 +147,7 @@ class AddDocumentViewModel(
162147 val fileDetails = getFileDetails(uri)
163148 Log .d(TAG , " New file: $fileDetails " )
164149
165- _currentFileEntry .postValue( fileDetails)
150+ savedStateHandle[ " current_file " ] = fileDetails
166151 _isDownloading .postValue(false )
167152 }
168153 }
@@ -196,7 +181,7 @@ class AddDocumentViewModel(
196181 val fileDetails = getFileDetails(uri)
197182 Log .d(TAG , " New file: $fileDetails " )
198183
199- _currentFileEntry .postValue( fileDetails)
184+ savedStateHandle[ " current_file " ] = fileDetails
200185 _isDownloading .postValue(false )
201186 }
202187 }
@@ -369,45 +354,11 @@ class AddDocumentViewModel(
369354 }
370355}
371356
357+ @Parcelize
372358data class FileEntry (
373359 val filename : String ,
374360 val size : Long ,
375361 val mimeType : String ,
376362 val addedAt : Long ,
377363 val path : String
378- ) {
379- companion object {
380- /* *
381- * Create a [FileEntry] from a [Bundle] when loading [SavedStateHandle]
382- */
383- fun fromBundle (bundle : Bundle ): FileEntry ? {
384- return if (bundle.containsKey(" filename" ) &&
385- bundle.containsKey(" size" ) &&
386- bundle.containsKey(" mimeType" ) &&
387- bundle.containsKey(" addedAt" ) &&
388- bundle.containsKey(" path" )
389- ) {
390- FileEntry (
391- filename = bundle.getString(" filename" )!! ,
392- size = bundle.getLong(" size" ),
393- mimeType = bundle.getString(" mimeType" )!! ,
394- addedAt = bundle.getLong(" addedAt" ),
395- path = bundle.getString(" path" )!!
396- )
397- } else {
398- null
399- }
400- }
401- }
402-
403- /* *
404- * Export [FileEntry] as a [Bundle] when saving [SavedStateHandle]
405- */
406- fun toBundle () = bundleOf(
407- " filename" to filename,
408- " size" to size,
409- " mimeType" to mimeType,
410- " addedAt" to addedAt,
411- " path" to path
412- )
413- }
364+ ) : Parcelable
0 commit comments