sample/src/androidMain/kotlin/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt
changeset 44 7732a7112b93
parent 34 ce299aacc068
child 47 246422783c0c
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.activity.viewModels
       
    25 import androidx.lifecycle.ViewModelProvider
       
    26 import androidx.lifecycle.viewmodel.CreationExtras
       
    27 import androidx.lifecycle.viewmodel.initializer
       
    28 import androidx.lifecycle.viewmodel.viewModelFactory
       
    29 import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository
       
    30 import com.geekorum.aboutoss.ui.common.AndroidBrowserLauncher
       
    31 import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel
       
    32 import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity
       
    33 import com.geekorum.geekdroid.network.BrowserLauncher
       
    34 import kotlinx.coroutines.Dispatchers
       
    35 import com.geekorum.aboutoss.ui.material.OpenSourceLicensesActivity as Material2OpenSourceLicensesActivity
       
    36 
       
    37 /**
       
    38  * Custom activity needed to load resources from another set of files than default generated by
       
    39  * OSS Licenses Gradle Plugin.
       
    40  *
       
    41  * This is necessary because OSS License gradle plugin generates stub resources on debug builds.
       
    42  */
       
    43 class PrebuiltLicencesMaterial2Activity : Material2OpenSourceLicensesActivity() {
       
    44     override val viewModel: OpenSourceLicensesViewModel by viewModels(
       
    45         factoryProducer = {
       
    46             viewModelFactory {
       
    47                 initializer {
       
    48                     createPrebuildOpenSourceLicensesViewModel()
       
    49                 }
       
    50             }
       
    51         }
       
    52     )
       
    53 }
       
    54 
       
    55 
       
    56 /**
       
    57  * Custom activity needed to load resources from another set of files than default generated by
       
    58  * OSS Licenses Gradle Plugin.
       
    59  *
       
    60  * This is necessary because OSS License gradle plugin generates stub resources on debug builds.
       
    61  */
       
    62 class PrebuiltLicencesMaterial3Activity : OpenSourceLicensesActivity() {
       
    63     override val viewModel: OpenSourceLicensesViewModel by viewModels(
       
    64         factoryProducer = {
       
    65             viewModelFactory {
       
    66                 initializer {
       
    67                     createPrebuildOpenSourceLicensesViewModel()
       
    68                 }
       
    69             }
       
    70         }
       
    71     )
       
    72 }
       
    73 
       
    74 fun CreationExtras.createPrebuildOpenSourceLicensesViewModel(): OpenSourceLicensesViewModel {
       
    75     val application =
       
    76         this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]!!
       
    77     val licenseInfoRepository = GmsLicenseInfoRepository(
       
    78         appContext = application,
       
    79         mainCoroutineDispatcher = Dispatchers.Main,
       
    80         ioCoroutineDispatcher = Dispatchers.IO,
       
    81         thirdPartyLicensesResourceName = "prebuilt_third_party_licenses",
       
    82         thirdPartyLicenseMetadataResourceName = "prebuilt_third_party_license_metadata"
       
    83     )
       
    84     val browserLauncher = BrowserLauncher(application, application.packageManager)
       
    85     return OpenSourceLicensesViewModel(
       
    86         licenseInfoRepository,
       
    87         AndroidBrowserLauncher(application, browserLauncher)
       
    88     )
       
    89 }