sample/src/main/java/com/geekorum/aboutoss/sampleapp/CustomViewer.kt
changeset 44 7732a7112b93
parent 43 b144901856dc
child 45 94ec01fee12c
equal deleted inserted replaced
43:b144901856dc 44:7732a7112b93
     1 /*
       
     2  * AboutOss is an utility library to retrieve and display
       
     3  * opensource licenses in Android applications.
       
     4  *
       
     5  * Copyright (C) 2023-2025 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of AboutOss.
       
     8  *
       
     9  * AboutOss is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * AboutOss is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.aboutoss.sampleapp
       
    23 
       
    24 import androidx.compose.foundation.layout.*
       
    25 import androidx.compose.foundation.lazy.grid.GridCells
       
    26 import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
       
    27 import androidx.compose.foundation.lazy.grid.itemsIndexed
       
    28 import androidx.compose.material3.*
       
    29 import androidx.compose.runtime.*
       
    30 import androidx.compose.ui.Alignment
       
    31 import androidx.compose.ui.Modifier
       
    32 import androidx.compose.ui.unit.dp
       
    33 import androidx.lifecycle.viewmodel.compose.viewModel
       
    34 import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel
       
    35 
       
    36 @Composable
       
    37 fun CustomViewer(
       
    38     viewModel: OpenSourceLicensesViewModel = viewModel(
       
    39         initializer = {
       
    40             createPrebuildOpenSourceLicensesViewModel()
       
    41         }
       
    42     ),
       
    43     modifier: Modifier = Modifier
       
    44 ) {
       
    45     Column(modifier = modifier) {
       
    46         Text("This section shows our you can use a custom ui to display licenses")
       
    47         DependenciesGrid(viewModel, Modifier.padding(top = 16.dp))
       
    48     }
       
    49 }
       
    50 
       
    51 @Composable
       
    52 private fun DependenciesGrid(
       
    53     viewModel: OpenSourceLicensesViewModel,
       
    54     modifier: Modifier = Modifier
       
    55 ) {
       
    56     val dependencies by viewModel.dependenciesList.collectAsState(initial = emptyList())
       
    57     var selected by remember { mutableStateOf(-1) }
       
    58     LazyVerticalGrid(
       
    59         GridCells.Adaptive(150.dp),
       
    60         horizontalArrangement = Arrangement.spacedBy(16.dp),
       
    61         verticalArrangement = Arrangement.spacedBy(16.dp),
       
    62         modifier = modifier
       
    63     ) {
       
    64         itemsIndexed(dependencies) { idx, dependency ->
       
    65             if (idx == selected) {
       
    66                 val license by viewModel.getLicenseDependency(dependency)
       
    67                     .collectAsState(initial = "")
       
    68                 LicenseCard(license, onClick = {
       
    69                     selected = -1
       
    70                 })
       
    71             } else {
       
    72                 DependencyCard(dependency, onClick = {
       
    73                     selected = idx
       
    74                 })
       
    75             }
       
    76         }
       
    77     }
       
    78 }
       
    79 
       
    80 @OptIn(ExperimentalMaterial3Api::class)
       
    81 @Composable
       
    82 private fun LicenseCard(license: String, onClick: () -> Unit) {
       
    83     Card(modifier = Modifier.size(150.dp), onClick = onClick,
       
    84         colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primary)
       
    85     ) {
       
    86         Text(
       
    87             license, style = MaterialTheme.typography.bodyMedium,
       
    88             modifier = Modifier
       
    89                 .padding(16.dp)
       
    90                 .fillMaxSize()
       
    91                 .wrapContentSize(
       
    92                     Alignment.Center
       
    93                 )
       
    94         )
       
    95     }
       
    96 }
       
    97 
       
    98 @OptIn(ExperimentalMaterial3Api::class)
       
    99 @Composable
       
   100 private fun DependencyCard(dependency: String, onClick: () -> Unit) {
       
   101     Card(modifier = Modifier.size(150.dp), onClick = onClick) {
       
   102         Text(
       
   103             dependency,
       
   104             style = MaterialTheme.typography.titleLarge,
       
   105             modifier = Modifier
       
   106                 .padding(16.dp)
       
   107                 .fillMaxSize()
       
   108                 .wrapContentSize(
       
   109                     Alignment.Center
       
   110                 )
       
   111         )
       
   112     }
       
   113 }