ui/common/src/commonMain/kotlin/OpenSourceLicensesViewModel.kt
author Da Risk <da_risk@geekorum.com>
Wed, 07 May 2025 14:35:58 -0400
changeset 100 2d2243ac089e
parent 80 2474591aa280
permissions -rw-r--r--
ui-material2: use collectAsStateWithLifecycle()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     1
/*
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * AboutOss is an utility library to retrieve and display
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 * opensource licenses in Android applications.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     4
 *
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 * Copyright (C) 2023-2025 by Frederic-Charles Barthelery.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 *
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 * This file is part of AboutOss.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 *
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * AboutOss is free software: you can redistribute it and/or modify
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * it under the terms of the GNU General Public License as published by
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * the Free Software Foundation, either version 3 of the License, or
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 * (at your option) any later version.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 *
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * AboutOss is distributed in the hope that it will be useful,
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 * GNU General Public License for more details.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 *
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * You should have received a copy of the GNU General Public License
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
 */
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
package com.geekorum.aboutoss.ui.common
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import androidx.lifecycle.ViewModel
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import androidx.lifecycle.viewModelScope
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import com.geekorum.aboutoss.core.LicenseInfoRepository
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import kotlinx.coroutines.flow.SharingStarted
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
import kotlinx.coroutines.flow.flow
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
import kotlinx.coroutines.flow.map
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    30
import kotlinx.coroutines.flow.stateIn
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
/**
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    33
 * Manage opensource license information and allow to display them in an UI
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
 */
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    35
class OpenSourceLicensesViewModel(
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
    private val licenseInfoRepository: LicenseInfoRepository,
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
) : ViewModel() {
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    38
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
    private val licensesInfo = flow {
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
        emit(licenseInfoRepository.getLicensesInfo())
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
    }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyMap())
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
80
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    43
    /**
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    44
     * List of dependencies
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    45
     */
36
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
    val dependenciesList = licensesInfo.map { licensesInfo ->
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
        licensesInfo.keys.sortedBy { it.lowercase() }
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    48
    }
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    49
80
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    50
    /**
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    51
     * Obtain the license for [dependency]
2474591aa280 ui:common: add more kdocs
Da Risk <da_risk@geekorum.com>
parents: 51
diff changeset
    52
     */
36
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
    fun getLicenseDependency(dependency: String) = flow {
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
        emit(licenseInfoRepository.getLicenseFor(dependency))
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
    }
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
    companion object
a8cfcfe9a6ed make :ui:common multiplatform
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
}