package com.example.littlelemonandroid
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class MenuNetwork(
@SerialName("menu")
val menu: List
)
@Serializable
data class MenuItemNetwork(
@SerialName("id")
val id: Int,
@SerialName("title")
val title: String,
@SerialName("description")
val description: String,
@SerialName("price")
val price: Double,
@SerialName("image")
val image: String,
@SerialName("category")
val category: String,
) {
fun toMenuItemRoom() = MenuItemRoom(id, title, description, price, image, category)
}
{
"menu": [
{
"id": 1,
"title": "Greek Salad",
"description": "The famous greek salad of crispy lettuce, peppers, olives, our Chicago.",
"price": "10",
"image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/greekSalad.jpg?raw=true",
"category": "starters"
},
{
"id": 2,
"title": "Lemon Desert",
"description": "Traditional homemade Italian Lemon Ricotta Cake.",
"price": "10",
"image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/lemonDessert%202.jpg?raw=true",
"category": "desserts"
},
{
"id": 3,
"title": "Grilled Fish",
"description": "Our Bruschetta is made from grilled bread that has been smeared with garlic and seasoned with salt and olive oil.",
"price": "10",
"image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/grilledFish.jpg?raw=true",
"category": "mains"
},
{
"id": 4,
"title": "Pasta",
"description": "Penne with fried aubergines, cherry tomatoes, tomato sauce, fresh chili, garlic, basil & salted ricotta cheese.",
"price": "10",
"image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/pasta.jpg?raw=true",
"category": "mains"
},
{
"id": 5,
"title": "Bruschetta",
"description": "Oven-baked bruschetta stuffed with tomatoes and herbs.",
"price": "10",
"image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/bruschetta.jpg?raw=true",
"category": "starters"
}
]
}
Попробовал запустить fetchMenu в runBlocking и LaunchedEffect, но это не работает. В основном мне нужно иметь возможность делать следующее: 1.) Вызывать функцию и 2.) Сохранять данные в базе данных и отображать результат. Я новичок в Jetpack Compose, если у вас есть другие предложения, скажите об этом.
//@Preview(showBackground = true) @Composable fun Home(navController: NavHostController) {
val searchPhrase = remember { mutableStateOf("") }
val focusManager: FocusManager = LocalFocusManager.current var isAnyTextFieldFocused by remember { mutableStateOf(false) } var isFocused by remember { mutableStateOf(false) } val focusRequester = remember { FocusRequester() }
val context = LocalContext.current val database by lazy { Room.databaseBuilder(context, AppDatabase::class.java, "database").build() }
suspend fun fetchMenu() : List { val httpClient = HttpClient(Android) { install(ContentNegotiation) { json() } }
val dataURL = "https://raw.githubusercontent.com/Meta-Mobile-Developer-PC/Working-With-Data-API/main/menu.json" val menuNetwork : MenuNetwork = httpClient.get(dataURL).body()
return menuNetwork.menu } [/code] Это MainActivity.kt [code]package com.example.littlelemonandroid
@Entity data class MenuItemRoom ( @PrimaryKey val id: Int, val title: String, val description: String, val price: Double, val image: String, val category: String, )
@Dao interface MenuItemDao { @Query("SELECT * FROM MenuItemRoom") fun getAll(): LiveData
@Insert fun insertAll(vararg menuItems: MenuItemRoom)
@Query("SELECT (SELECT COUNT(*) FROM MenuItemRoom) == 0") fun isEmpty(): Boolean }
@Database(entities = [MenuItemRoom::class], version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun menuItemDao(): MenuItemDao } [/code] Как отобразить результат fetchMenu, как показано ниже Изображение приложения Это то, что дает вызов API, выполненный в функции fetchMenu : [code]{ "menu": [ { "id": 1, "title": "Greek Salad", "description": "The famous greek salad of crispy lettuce, peppers, olives, our Chicago.", "price": "10", "image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/greekSalad.jpg?raw=true", "category": "starters" }, { "id": 2, "title": "Lemon Desert", "description": "Traditional homemade Italian Lemon Ricotta Cake.", "price": "10", "image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/lemonDessert%202.jpg?raw=true", "category": "desserts" }, { "id": 3, "title": "Grilled Fish", "description": "Our Bruschetta is made from grilled bread that has been smeared with garlic and seasoned with salt and olive oil.", "price": "10", "image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/grilledFish.jpg?raw=true", "category": "mains" }, { "id": 4, "title": "Pasta", "description": "Penne with fried aubergines, cherry tomatoes, tomato sauce, fresh chili, garlic, basil & salted ricotta cheese.", "price": "10", "image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/pasta.jpg?raw=true", "category": "mains" }, { "id": 5, "title": "Bruschetta", "description": "Oven-baked bruschetta stuffed with tomatoes and herbs.", "price": "10", "image": "https://github.com/Meta-Mobile-Developer-PC/Working-With-Data-API/blob/main/images/bruschetta.jpg?raw=true", "category": "starters" } ] } [/code] Попробовал запустить fetchMenu в runBlocking и LaunchedEffect, но это не работает. В основном мне нужно иметь возможность делать следующее: 1.) Вызывать функцию и 2.) Сохранять данные в базе данных и отображать результат. Я новичок в Jetpack Compose, если у вас есть другие предложения, скажите об этом.