Skip to content

Commit

Permalink
Feature/demo and examples (#28)
Browse files Browse the repository at this point in the history
* fix device frame deps

* adjust package names

* add more example for easier getting started example

* update readme

* fix demo app path in workflow

* fix some deps tree
  • Loading branch information
esafirm authored Dec 18, 2022
1 parent c05a5ad commit 224d1b6
Show file tree
Hide file tree
Showing 105 changed files with 1,034 additions and 158 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: echo "$SECRET_PROPS" >> secret.properties

- name: Build Android book
run: ./gradlew :sample:assembleDebug --stacktrace
run: ./gradlew :examples:demo:assembleDebug --stacktrace

- name: Build desktop book
run: ./gradlew :sample:desktopJar --stacktrace
run: ./gradlew :examples:demo:desktopJar --stacktrace
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Compose UI Book

Simple and extensible UI component explorer for Jetpack Compose and Android View

<p float="middle">
<img src=".github/art/ss1.png"/>
<img src=".github/art/ss2.png"/>
</p>

<details>
<summary>You can check the demo video here</summary>
<a href="https://www.youtube.com/watch?v=aB2cBjLuYHA">
<img src="https://img.youtube.com/vi/aB2cBjLuYHA/0.jpg" />
</a>
</details>

## Getting Started

Usually the UI component explorer is separated from the main app despite you have Android only or KMP project.
So the first thing you should do is to create a module with this `build.gradle.kts` (or `build.gradle` if you're using
Groovy)

### Setup Module - KMP

```kotlin
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.multiplatform")
id("org.jetbrains.compose")
id("com.google.devtools.ksp")
}

// This could be in settings.gradle or root build.gradle
repositories {
mavenCentral()
}

kotlin {
jvm("desktop")
android()

sourceSets {
named("commonMain") {
dependencies {
implementation("io.github.esafirm.uibook:browser-app:x.y.z")
}
}
named("androidMain") {
dependencies {
// This is optional. If you use app compat then include this
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("com.google.android.material:material:1.4.0")
}
}
named("desktopMain") {
dependencies {
// This is mandatory as we current OS is not packaged in POM file
implementation(compose.desktop.currentOs)
}
}
}
}

android {
// Insert Android related configuration in here
}

compose.desktop {
// Insert compose desktop related configuration in here
}

dependencies {
val processorDep = "io.github.esafirm.uibook:annotations-processor:x.y.z"
add("kspAndroid", processorDep)
add("kspDesktop", processorDep)
}
```

> :heavy_exclamation_mark: **Info**: Please check the [minimum KMP example](/examples/minimum-kmp) for more complete
> setup.
### Setup Module - Android Only

```kotlin
plugins {
id("com.android.application")
id("org.jetbrains.compose")
id("com.google.devtools.ksp")
kotlin("android")
}

android {
// Insert Android related configuration in here
}

dependencies {
implementation("io.github.esafirm.uibook:browser-app:x.y.z")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("com.google.android.material:material:1.4.0")

ksp("io.github.esafirm.uibook:annotations-processor:x:y:z")
}

```

> :heavy_exclamation_mark: **Info**: Please check the [minimum android example](/examples/minimum-android) for more
> complete setup.
change `x.y.z` to version in the [release page](https://github.com/esafirm/android-ui-book/releases)

### Create First Book

To create book simple create an extension function that extend `BookHost` and give the function `@UIBook`
annotation.
In this case I will create a Kotlin file with name `Catalogue.kt` and then create this function:

```kotlin
@UIBook(name = "Compose Button")
@Composable
fun BookHost.SimpleCouter() {
val (count, setCount) = remember { mutableStateOf(0) }
Button(onClick = { setCount(count + 1) }) {
Text(text = "Counter $count")
}
}
```

That's it now run the new module that you just created.

### Android View Support

For Android target, it supports the Android `View`, just add the return type to the function

```kotlin
@UIBook(name = "Android TextView")
fun BookHost.SimpleTextView(): TextView {
/**
* This will draw text
*/
return TextView(context).apply {
text = "Hello World"
}
}
```

## Advanced

For now, you can check the [sample module](/sample) for complete features.

## Development

To develop the project, you need to use Android Studio Dolphin above.

It have some strange issue with IntelliJ Idea 2022.3 EAP.

### Running Sample

To run the Android sample run this command:

```bash
./gradlew :sample:installDebug
```

To run the Desktop sample run this command:

```bash
./gradlew :sample:run
```

## Support

<a href='https://ko-fi.com/M4M41RRE0' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi4.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

## License

Esa @ MIT
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class UIBookGenerator(
private val defaultInputClass = ClassName("nolambda.uibook.browser.form", "DefaultInputCreator")

// Android Specific
private val androidContainerClass = ClassName("nolambda.uibook.components.bookform", "AndroidContainer")
private val androidContainerClass = ClassName("nolambda.uibook.browser.app", "AndroidContainer")
private val androidBookHost = ClassName("nolambda.uibook.browser", "AndroidBookHost")

// Functions
Expand Down
9 changes: 6 additions & 3 deletions browser-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ kotlin {
jvm("desktop")

sourceSets {
named("androidMain") {
named("commonMain") {
dependencies {
api(project(":annotations"))
api(project(":browser-core"))
}
}
named("androidMain") {
dependencies {

implementation("androidx.core:core-ktx:1.7.0")

Expand All @@ -41,8 +45,7 @@ kotlin {
}
named("desktopMain") {
dependencies {
implementation(compose.desktop.currentOs)
implementation(project(":browser-core"))
api(compose.desktop.currentOs)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions browser-app/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nolambda.uibook.browser">
package="nolambda.uibook.browser.app">

<application android:name=".BrowserApp">
<activity
Expand All @@ -20,4 +20,4 @@
android:windowSoftInputMode="adjustPan" />
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser
package nolambda.uibook.browser.app

import android.app.Activity
import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.components.bookform
package nolambda.uibook.browser.app

import android.view.Gravity
import android.view.View
Expand All @@ -14,8 +14,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import nolambda.uibook.browser.form.ComponentCreator
import nolambda.uibook.browser.measurement.MeasurementHelperImpl
import nolambda.uibook.browser.app.form.ComponentCreator
import nolambda.uibook.browser.app.measurement.MeasurementHelperImpl
import nolambda.uibook.components.bookform.GlobalState

/**
* A container to bridge pure compose with Android's View
Expand Down Expand Up @@ -90,4 +91,4 @@ private fun ViewGroup.wrapSize(): FrameLayout.LayoutParams {
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package nolambda.uibook.browser
package nolambda.uibook.browser.app

import android.app.Application
import nolambda.uibook.browser.AndroidBookHost
import nolambda.uibook.browser.BookHost
import nolambda.uibook.browser.config.AppBrowserConfig
import nolambda.uibook.browser.config.BrowserConfig
import nolambda.uibook.browser.config.ResourceLoader
import nolambda.uibook.browser.resourceloader.AndroidResourceLoader
import nolambda.uibook.browser.app.resourceloader.AndroidResourceLoader
import nolambda.uibook.clipboard.AndroidClipboardManager
import nolambda.uibook.clipboard.ClipboardManager
import nolambda.uibook.factory.AndroidLibraryLoader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser
package nolambda.uibook.browser.app

import android.content.Context
import android.content.Intent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser
package nolambda.uibook.browser.app

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser
package nolambda.uibook.browser.app

import android.view.View

Expand Down Expand Up @@ -31,4 +31,4 @@ fun View.setViewState(enabled: Boolean) {
val alpha = if (enabled) 1F else 0.5F
isEnabled = enabled
setAlpha(alpha)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package nolambda.uibook.browser.form
package nolambda.uibook.browser.app.form

import android.content.Context
import nolambda.uibook.browser.measurement.MeasurementHelper
import nolambda.uibook.browser.measurement.MeasurementOverlayView
import nolambda.uibook.browser.app.measurement.MeasurementHelper
import nolambda.uibook.browser.app.measurement.MeasurementOverlayView

internal class ComponentCreator(
private val context: Context
Expand All @@ -13,4 +13,4 @@ internal class ComponentCreator(
setMeasurementHelper(helper)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.measurement
package nolambda.uibook.browser.app.measurement

import android.graphics.Rect
import android.view.View
Expand All @@ -9,4 +9,4 @@ interface MeasurementHelper {
fun getContentRootLocation(view: View, rect: Rect)

fun toDp(px: Int): Int
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.measurement
package nolambda.uibook.browser.app.measurement

import android.graphics.Rect
import android.util.DisplayMetrics
Expand Down Expand Up @@ -31,4 +31,4 @@ class MeasurementHelperImpl(private val root: ViewGroup) : MeasurementHelper {
override fun toDp(px: Int): Int {
return (px / (displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.measurement
package nolambda.uibook.browser.app.measurement

import android.content.Context
import android.graphics.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.resourceloader
package nolambda.uibook.browser.app.resourceloader

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.painter.Painter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.desktop
package nolambda.uibook.browser.app

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -38,4 +38,4 @@ fun EmptyContent() {
fontSize = 22.sp
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.desktop
package nolambda.uibook.browser.app

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.painter.Painter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nolambda.uibook.browser.desktop
package nolambda.uibook.browser.app

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.FastOutSlowInEasing
Expand Down
2 changes: 1 addition & 1 deletion browser-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

android {
namespace = "nolambda.uibook.browser"
namespace = "nolambda.uibook"
}

kotlin {
Expand Down
Loading

0 comments on commit 224d1b6

Please sign in to comment.