Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit f65eb3f

Browse files
committed
Replace custom savedState logic by @parcelize annotation
1 parent 6b916ba commit f65eb3f

3 files changed

Lines changed: 10 additions & 57 deletions

File tree

ScopedStorage/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
plugins {
1818
id 'com.android.application'
1919
id 'kotlin-android'
20+
id 'kotlin-parcelize'
2021
}
2122

2223
android {

ScopedStorage/app/src/main/java/com/samples/storage/mediastore/AddDocumentViewModel.kt

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ import android.content.pm.PackageManager
2323
import android.media.MediaScannerConnection
2424
import android.net.Uri
2525
import android.os.Build
26-
import android.os.Bundle
2726
import android.os.Environment
2827
import android.os.Environment.DIRECTORY_DOWNLOADS
28+
import android.os.Parcelable
2929
import android.provider.MediaStore
3030
import android.provider.MediaStore.Files.FileColumns
3131
import android.util.Log
3232
import androidx.annotation.RequiresApi
3333
import androidx.core.content.ContextCompat
34-
import androidx.core.os.bundleOf
3534
import androidx.lifecycle.AndroidViewModel
3635
import androidx.lifecycle.LiveData
3736
import androidx.lifecycle.MutableLiveData
@@ -41,6 +40,7 @@ import com.samples.storage.data.SampleFiles
4140
import kotlinx.coroutines.Dispatchers
4241
import kotlinx.coroutines.launch
4342
import kotlinx.coroutines.withContext
43+
import kotlinx.parcelize.Parcelize
4444
import okhttp3.OkHttpClient
4545
import okhttp3.Request
4646
import okhttp3.ResponseBody
@@ -51,7 +51,7 @@ private const val TAG = "AddDocumentViewModel"
5151

5252
class 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
372358
data 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

ScopedStorage/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ buildscript {
3232

3333
plugins {
3434
id 'com.diffplug.spotless' version '5.12.5'
35+
id 'org.jetbrains.kotlin.android' version '1.4.20' apply false
3536
}
3637

3738
subprojects {

0 commit comments

Comments
 (0)