ui/material3/src/androidMain/kotlin/OpenSourceLicensesActivity.kt
changeset 42 d59fc19f19fa
parent 34 ce299aacc068
child 43 b144901856dc
equal deleted inserted replaced
41:59b612ba3644 42:d59fc19f19fa
       
     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.ui.material3
       
    23 
       
    24 import android.app.Activity
       
    25 import android.net.Uri
       
    26 import android.os.Bundle
       
    27 import androidx.activity.compose.setContent
       
    28 import androidx.activity.viewModels
       
    29 import androidx.compose.foundation.isSystemInDarkTheme
       
    30 import androidx.compose.material3.MaterialTheme
       
    31 import androidx.compose.runtime.Composable
       
    32 import androidx.compose.runtime.SideEffect
       
    33 import androidx.compose.ui.graphics.toArgb
       
    34 import androidx.compose.ui.platform.LocalView
       
    35 import androidx.core.view.WindowCompat
       
    36 import androidx.navigation.compose.NavHost
       
    37 import androidx.navigation.compose.composable
       
    38 import androidx.navigation.compose.rememberNavController
       
    39 import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository
       
    40 import com.geekorum.aboutoss.ui.common.BaseOpensourceLicenseActivity
       
    41 import com.geekorum.aboutoss.ui.common.Factory
       
    42 import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel
       
    43 import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity.Companion.themeProvider
       
    44 import kotlinx.coroutines.Dispatchers
       
    45 
       
    46 /**
       
    47  * Activity to display opensource license information
       
    48  *
       
    49  * This activity use Material compose to create the UI.
       
    50  * You can specify the Material theme to use by setting [themeProvider]
       
    51  * before launching the activity
       
    52  */
       
    53 open class OpenSourceLicensesActivity : BaseOpensourceLicenseActivity() {
       
    54     override val viewModel: OpenSourceLicensesViewModel by viewModels(
       
    55         factoryProducer = {
       
    56             val gmsLicenseInfoRepository = GmsLicenseInfoRepository(
       
    57                 appContext = applicationContext,
       
    58                 mainCoroutineDispatcher = Dispatchers.Main,
       
    59                 ioCoroutineDispatcher = Dispatchers.IO,
       
    60             )
       
    61             OpenSourceLicensesViewModel.Factory(gmsLicenseInfoRepository)
       
    62         }
       
    63     )
       
    64 
       
    65     override fun onCreate(savedInstanceState: Bundle?) {
       
    66         super.onCreate(savedInstanceState)
       
    67         WindowCompat.setDecorFitsSystemWindows(window, false)
       
    68         setContent {
       
    69             themeProvider {
       
    70                 DependencyNavHost(
       
    71                     openSourceLicensesViewModel = viewModel,
       
    72                     navigateUp = {
       
    73                         if (!onNavigateUp()) {
       
    74                             finish()
       
    75                         }
       
    76                     }
       
    77                 )
       
    78             }
       
    79         }
       
    80     }
       
    81 
       
    82     companion object {
       
    83         /**
       
    84          * The composable Theme function to set the theme of the UI in [OpenSourceLicensesActivity]
       
    85          * Default to base material theme [MaterialTheme]
       
    86          */
       
    87         var themeProvider: @Composable (@Composable () -> Unit) -> Unit = { content ->
       
    88             val darkTheme: Boolean = isSystemInDarkTheme()
       
    89             val colorScheme = MaterialTheme.colorScheme
       
    90             val view = LocalView.current
       
    91             if (!view.isInEditMode) {
       
    92                 SideEffect {
       
    93                     val window = (view.context as Activity).window
       
    94                     window.statusBarColor = colorScheme.primary.toArgb()
       
    95                     WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
       
    96                 }
       
    97             }
       
    98             MaterialTheme(content = content)
       
    99         }
       
   100     }
       
   101 }
       
   102 
       
   103 @Composable
       
   104 fun DependencyNavHost(
       
   105     openSourceLicensesViewModel: OpenSourceLicensesViewModel,
       
   106     navigateUp: () -> Unit
       
   107 ) {
       
   108     val navController = rememberNavController()
       
   109     NavHost(navController, startDestination = "dependencies") {
       
   110         composable("dependencies") {
       
   111             OpenSourceDependenciesListScreen(
       
   112                 viewModel = openSourceLicensesViewModel,
       
   113                 onDependencyClick = {
       
   114                     navController.navigate("dependency_license/${Uri.encode(it)}")
       
   115                 },
       
   116                 onUpClick = navigateUp
       
   117             )
       
   118         }
       
   119         composable("dependency_license/{dependency}") {
       
   120             val dependency = requireNotNull(it.arguments?.getString("dependency"))
       
   121             OpenSourceLicenseScreen(
       
   122                 viewModel = openSourceLicensesViewModel,
       
   123                 dependency = dependency,
       
   124                 onUpClick = {
       
   125                     navController.popBackStack()
       
   126                 },
       
   127             )
       
   128         }
       
   129     }
       
   130 }