Created
October 30, 2023 16:19
-
-
Save vengateshm/7a5791ef54662d96bff2da8623dbf092 to your computer and use it in GitHub Desktop.
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
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.saveable.Saver | |
import androidx.compose.runtime.saveable.SaverScope | |
import androidx.compose.runtime.saveable.rememberSaveable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import com.google.gson.Gson | |
class CustomSaverActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
MaterialTheme { | |
BookItem() | |
} | |
} | |
} | |
} | |
@Composable | |
fun BookItem() { | |
val book = rememberSaveable(stateSaver = BookSaverMapApproach) { | |
mutableStateOf(Book("A123", "Title 1", 4.6)) | |
} | |
Column(modifier = Modifier.fillMaxWidth()) { | |
Text(text = book.value.isbn) | |
Text(text = book.value.title) | |
Text(text = book.value.rating.toString()) | |
} | |
} | |
// Custom object | |
data class Book(val isbn: String, val title: String, val rating: Double) | |
// Custom saver implementing Saver interface | |
// Saving data into map | |
object BookSaverMapApproach : Saver<Book, Map<String, Any>> { | |
override fun restore(value: Map<String, Any>): Book { | |
return Book( | |
isbn = value["isbn"] as String, | |
title = value["title"] as String, | |
rating = value["rating"] as Double | |
) | |
} | |
override fun SaverScope.save(value: Book): Map<String, Any> { | |
return mapOf( | |
"isbn" to value.isbn, | |
"title" to value.title, | |
"rating" to value.rating | |
) | |
} | |
} | |
// Custom saver implementing Saver interface | |
// Saving data as string using gson | |
object BookSaverGsonApproach : Saver<Book, String> { | |
override fun restore(value: String): Book? { | |
return Gson().fromJson(value, Book::class.java) | |
} | |
override fun SaverScope.save(value: Book): String { | |
return Gson().toJson(value) | |
} | |
} | |
@Preview | |
@Composable | |
fun BookItemPreview() { | |
BookItem() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment