|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | + * * Licensed under the MIT License. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +package com.microsoft.device.display.samples.composesample |
| 9 | + |
| 10 | +import android.util.Log |
| 11 | +import androidx.compose.foundation.Image |
| 12 | +import androidx.compose.foundation.Text |
| 13 | +import androidx.compose.foundation.layout.Arrangement |
| 14 | +import androidx.compose.foundation.layout.Column |
| 15 | +import androidx.compose.foundation.layout.Row |
| 16 | +import androidx.compose.foundation.layout.Spacer |
| 17 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 18 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 19 | +import androidx.compose.foundation.layout.padding |
| 20 | +import androidx.compose.foundation.layout.preferredHeight |
| 21 | +import androidx.compose.foundation.layout.preferredWidth |
| 22 | +import androidx.compose.foundation.layout.wrapContentSize |
| 23 | +import androidx.compose.foundation.lazy.LazyColumnForIndexed |
| 24 | +import androidx.compose.foundation.selection.selectable |
| 25 | +import androidx.compose.material.Divider |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.runtime.livedata.observeAsState |
| 28 | +import androidx.compose.ui.Alignment |
| 29 | +import androidx.compose.ui.Modifier |
| 30 | +import androidx.compose.ui.graphics.Color |
| 31 | +import androidx.compose.ui.res.imageResource |
| 32 | +import androidx.compose.ui.text.font.FontWeight |
| 33 | +import androidx.compose.ui.unit.dp |
| 34 | +import androidx.compose.ui.unit.sp |
| 35 | +import androidx.ui.tooling.preview.Preview |
| 36 | +import com.microsoft.device.display.samples.composesample.models.DataProvider |
| 37 | +import com.microsoft.device.display.samples.composesample.models.ImageModel |
| 38 | +import com.microsoft.device.display.samples.composesample.viewModels.AppStateViewModel |
| 39 | + |
| 40 | +private lateinit var appStateViewModel: AppStateViewModel |
| 41 | +private val DEBUG_TAG = "ComposeSample" |
| 42 | + |
| 43 | +@Preview |
| 44 | +@Composable |
| 45 | +fun HomePreview() { |
| 46 | + val models = DataProvider.imageModels |
| 47 | + ShowList(models = models) |
| 48 | +} |
| 49 | + |
| 50 | +@Composable |
| 51 | +fun Home(viewModel: AppStateViewModel) { |
| 52 | + appStateViewModel = viewModel |
| 53 | + SetupUI() |
| 54 | +} |
| 55 | + |
| 56 | +@Composable |
| 57 | +fun SetupUI() { |
| 58 | + val models = DataProvider.imageModels |
| 59 | + val isScreenSpannedLiveData = appStateViewModel.getIsScreenSpannedLiveData() |
| 60 | + val isScreenSpanned = isScreenSpannedLiveData.observeAsState(initial = false).value |
| 61 | + |
| 62 | + Log.i(DEBUG_TAG, "SetupUI isScreenSpanned: $isScreenSpanned") |
| 63 | + |
| 64 | + if (isScreenSpanned) { |
| 65 | + ShowDetailWithList(models) |
| 66 | + } else { |
| 67 | + ShowList(models) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@Composable |
| 72 | +private fun ShowList(models: List<ImageModel>) { |
| 73 | + ShowListColumn(models, Modifier.fillMaxHeight() then Modifier.fillMaxWidth()) |
| 74 | +} |
| 75 | + |
| 76 | +@Composable |
| 77 | +private fun ShowListColumn(models: List<ImageModel>, modifier: Modifier) { |
| 78 | + val imageSelectionLiveData = appStateViewModel.getImageSelectionLiveData() |
| 79 | + val selectedIndex = imageSelectionLiveData.observeAsState(initial = 0).value |
| 80 | + |
| 81 | +// ScrollableColumn(modifier) { |
| 82 | +// models.forEachIndexed { index, model -> |
| 83 | + LazyColumnForIndexed( |
| 84 | + items = models, |
| 85 | + modifier = modifier |
| 86 | + ) { index, item -> |
| 87 | + Row( |
| 88 | + modifier = Modifier.selectable( |
| 89 | + selected = (index == selectedIndex), |
| 90 | + onClick = { |
| 91 | + appStateViewModel.setImageSelectionLiveData(index) |
| 92 | + } |
| 93 | + ) then Modifier.fillMaxWidth(), |
| 94 | + verticalGravity = Alignment.CenterVertically |
| 95 | + ) { |
| 96 | + Image(asset = imageResource(item.image), modifier = Modifier.preferredHeight(100.dp).preferredWidth(150.dp)) |
| 97 | + Spacer(Modifier.preferredWidth(16.dp)) |
| 98 | + Column(modifier = Modifier.fillMaxHeight() then Modifier.padding(16.dp)) { |
| 99 | + Text(item.id, modifier = Modifier.fillMaxHeight().wrapContentSize(Alignment.Center), fontSize = 20.sp, fontWeight = FontWeight.Bold) |
| 100 | + Text(item.title, modifier = Modifier.fillMaxHeight().wrapContentSize(Alignment.Center)) |
| 101 | + } |
| 102 | + } |
| 103 | + Divider(color = Color.LightGray) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +@Composable |
| 108 | +fun ShowDetailWithList(models: List<ImageModel>) { |
| 109 | + val imageSelectionLiveData = appStateViewModel.getImageSelectionLiveData() |
| 110 | + val selectedIndex = imageSelectionLiveData.observeAsState(initial = 0).value |
| 111 | + val selectedImageModel = models[selectedIndex] |
| 112 | + |
| 113 | + Row( |
| 114 | + modifier = Modifier.fillMaxHeight().wrapContentSize(Alignment.Center) |
| 115 | + then Modifier.fillMaxWidth().wrapContentSize(Alignment.Center) |
| 116 | + ) { |
| 117 | + ShowListColumn( |
| 118 | + models, Modifier.fillMaxHeight().wrapContentSize(Alignment.Center).weight(1f) |
| 119 | + ) |
| 120 | + Column( |
| 121 | + modifier = Modifier.fillMaxHeight().wrapContentSize(Alignment.Center).weight(1f), |
| 122 | + horizontalGravity = Alignment.CenterHorizontally, |
| 123 | + verticalArrangement = Arrangement.spacedBy(space = 40.dp) |
| 124 | + ) { |
| 125 | + Text(text = selectedImageModel.id, fontSize = 60.sp) |
| 126 | + Image(asset = imageResource(selectedImageModel.image)) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments