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