Skip to content

Commit

Permalink
sample: complete sample application
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarthelery committed May 2, 2023
1 parent 09dbd4b commit d5f4778
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 19 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core",
geekdroid = { module = "com.geekorum.geekdroid:geekdroid", version.ref = "geekdroid" }

androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref="androidx-lifecycle" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }
androidx-activity = { module = "androidx.activity:activity-ktx", version.ref="androidx-activity" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref="androidx-activity" }

Expand All @@ -67,6 +68,7 @@ androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-te
com-android-application = { id = "com.android.application", version.ref = "com-android-application" }
com-android-library = { id = "com.android.library", version.ref = "com-android-library" }
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
google-gms-oss-license = { id = "com.google.android.gms.oss-licenses-plugin", version = "0.10.6" }

[bundles]

19 changes: 19 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.geekorum.build.source-license-checker")
alias(libs.plugins.google.gms.oss.license)
}

// workaround bug https://issuetracker.google.com/issues/275534543
buildscript {
dependencies {
classpath("com.android.tools.build:gradle:8.0.0")
}
}

android {
Expand Down Expand Up @@ -61,6 +69,7 @@ android {
}
buildFeatures {
compose = true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.androidx.compose.compiler.get()
Expand All @@ -73,9 +82,19 @@ android {
}

dependencies {
implementation(project(":core"))
implementation(project(":ui:common"))
implementation(project(":ui:material2"))
implementation(project(":ui:material3"))

implementation(libs.geekdroid) {
//TODO get rid of dagger platform in geekdroid
exclude("com.google.dagger", "dagger-platform")
}


implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
Expand Down
9 changes: 9 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".PrebuiltLicencesMaterial2Activity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:exported="false" />

<activity android:name=".PrebuiltLicencesMaterial3Activity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:exported="false" />

</application>

</manifest>
113 changes: 113 additions & 0 deletions sample/src/main/java/com/geekorum/aboutoss/sampleapp/CustomViewer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* AboutOss is an utility library to retrieve and display
* opensource licenses in Android applications.
*
* Copyright (C) 2023 by Frederic-Charles Barthelery.
*
* This file is part of AboutOss.
*
* AboutOss is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AboutOss is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AboutOss. If not, see <http://www.gnu.org/licenses/>.
*/
package com.geekorum.aboutoss.sampleapp

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel

@Composable
fun CustomViewer(
viewModel: OpenSourceLicensesViewModel = viewModel(
initializer = {
createPrebuildOpenSourceLicensesViewModel()
}
),
modifier: Modifier = Modifier
) {
Column(modifier = modifier) {
Text("This section shows our you can use a custom ui to display licenses")
DependenciesGrid(viewModel, Modifier.padding(top = 16.dp))
}
}

@Composable
private fun DependenciesGrid(
viewModel: OpenSourceLicensesViewModel,
modifier: Modifier = Modifier
) {
val dependencies by viewModel.dependenciesList.collectAsState(initial = emptyList())
var selected by remember { mutableStateOf(-1) }
LazyVerticalGrid(
GridCells.Adaptive(150.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = modifier
) {
itemsIndexed(dependencies) { idx, dependency ->
if (idx == selected) {
val license by viewModel.getLicenseDependency(dependency)
.collectAsState(initial = "")
LicenseCard(license, onClick = {
selected = -1
})
} else {
DependencyCard(dependency, onClick = {
selected = idx
})
}
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun LicenseCard(license: String, onClick: () -> Unit) {
Card(modifier = Modifier.size(150.dp), onClick = onClick,
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primary)
) {
Text(
license, style = MaterialTheme.typography.bodyMedium,
modifier = Modifier
.padding(16.dp)
.fillMaxSize()
.wrapContentSize(
Alignment.Center
)
)
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun DependencyCard(dependency: String, onClick: () -> Unit) {
Card(modifier = Modifier.size(150.dp), onClick = onClick) {
Text(
dependency,
style = MaterialTheme.typography.titleLarge,
modifier = Modifier
.padding(16.dp)
.fillMaxSize()
.wrapContentSize(
Alignment.Center
)
)
}
}
121 changes: 103 additions & 18 deletions sample/src/main/java/com/geekorum/aboutoss/sampleapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,132 @@
*/
package com.geekorum.aboutoss.sampleapp

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.geekorum.aboutoss.sampleapp.ui.theme.AboutOssTheme
import com.geekorum.aboutoss.sampleapp.ui.theme.OpenSourceLicenseTheme
import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity
import com.geekorum.aboutoss.ui.material.OpenSourceLicensesActivity as Material2OpenSourceLicensesActivity

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AboutOssTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
Sample(
onMaterial2Click = {
startMaterial2LicenseActivity()
},
onMaterial3Click = {
startMaterial3LicenseActivity()
}
)
}
}
}

private fun startMaterial2LicenseActivity() {
val intent = if (BuildConfig.DEBUG) {
// we launch a custom activity in debug to display some fake licenses
// see Material2AcPrebuiltLicencesMaterial2Activitytivity for more info
Intent(this, PrebuiltLicencesMaterial2Activity::class.java)
} else {
Intent(this, Material2OpenSourceLicensesActivity::class.java)
}
startActivity(intent)
}

private fun startMaterial3LicenseActivity() {
// Don't use default MaterialTheme but supply our own
OpenSourceLicensesActivity.themeProvider = { content ->
OpenSourceLicenseTheme(content)
}
val intent = if (BuildConfig.DEBUG) {
// we launch a custom activity in debug to display some fake licenses
// see PrebuiltLicencesMaterial3Activity for more info
Intent(this, PrebuiltLicencesMaterial3Activity::class.java)
} else {
Intent(this, OpenSourceLicensesActivity::class.java)
}
startActivity(intent)
}
}

@Composable
private fun Sample(
onMaterial2Click: () -> Unit,
onMaterial3Click: () -> Unit,
) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(Modifier.fillMaxSize()) {
LaunchActivitySection(onMaterial2Click, onMaterial3Click)
CustomViewer(modifier = Modifier.padding(horizontal = 16.dp))
}
}
}

@Composable
private fun LaunchActivitySection(
onMaterial2Click: () -> Unit,
onMaterial3Click: () -> Unit,
modifier: Modifier = Modifier
) {
Column(modifier.padding(16.dp)) {
Text("This section launch a new activity to display licences information")
Row(
horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier
.fillMaxWidth()
.padding(32.dp)
) {
Material2Card(onClick = onMaterial2Click)
Material3Card(onClick = onMaterial3Click)
}
}
}


@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
@OptIn(ExperimentalMaterial3Api::class)
private fun Material2Card(
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
Card(modifier = modifier, onClick = onClick) {
Column(Modifier.padding(16.dp)) {
Text("Material2 UI", style = MaterialTheme.typography.labelLarge)
}
}
}

@Composable
@OptIn(ExperimentalMaterial3Api::class)
private fun Material3Card(
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
Card(modifier = modifier, onClick = onClick) {
Column(Modifier.padding(16.dp)) {
Text("Material3 UI", style = MaterialTheme.typography.labelLarge)
}
}
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
fun LauncherActivitySectionPreview() {
AboutOssTheme {
Greeting("Android")
LaunchActivitySection(onMaterial2Click = {}, onMaterial3Click = {})
}
}
Loading

0 comments on commit d5f4778

Please sign in to comment.