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