Skip to content

Commit

Permalink
Create a compose-based Step
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmoelter committed May 2, 2021
1 parent b4a4d94 commit c9f26a2
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ android:
components:
- tools
- platform-tools
- build-tools-29.0.2
- android-29
- build-tools-30.0.2
- android-30
- extra

before_install:
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'org.jetbrains.kotlin.jvm' version '1.4.30'
}

repositories {
Expand All @@ -9,7 +9,7 @@ repositories {
}

dependencies {
implementation "com.android.tools.build:gradle:4.1.1"
implementation "com.android.tools.build:gradle:7.0.0-alpha15"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

Expand Down
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Versions.archVersion
import Versions.butterKnifeVersion
import Versions.composeVersion
import Versions.coroutinesVersion
import Versions.daggerVersion
import Versions.espressoVersion
Expand Down Expand Up @@ -52,6 +53,11 @@ object Dependencies {
const val coroutinesAndroid = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
const val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"

const val composeUi = "androidx.compose.ui:ui:$composeVersion"
const val composeUiTooling = "androidx.compose.ui:ui-tooling:$composeVersion"
const val composeFoundation = "androidx.compose.foundation:foundation:$composeVersion"
const val composeMaterial = "androidx.compose.material:material:$composeVersion"

const val glide = "com.github.bumptech.glide:glide:$glideVersion"
const val retrofit = "com.squareup.retrofit2:retrofit:$retrofitVersion"
const val rxjava = "io.reactivex:rxjava:$rxjavaVersion"
Expand Down
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
object Versions {
const val compileSdkVersion = 30
const val minSdkVersion = 15
const val minSdkVersion = 21
const val targetSdkVersion = 30

const val kotlinVersion = "1.4.30"
const val kotlinterVersion = "3.4.0"
const val buildToolsVersion = "29.0.2"
const val buildToolsVersion = "30.0.2"
const val detektVersion = "1.5.1"
const val supportLibVersion = "1.1.0"
const val robolectricVersion = "4.3.1"
Expand All @@ -25,6 +25,7 @@ object Versions {
const val javaInjectVersion = "1"
const val materialVersion = "1.1.0"
const val coroutinesVersion = "1.4.3"
const val composeVersion = "1.0.0-beta01"

const val testCoreVersion = "1.2.0"
const val junitVersion = "4.13"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip
1 change: 1 addition & 0 deletions magellan-compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
69 changes: 69 additions & 0 deletions magellan-compose/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

group = GROUP
version = VERSION_NAME

android {
compileSdkVersion Versions.compileSdkVersion
buildToolsVersion Versions.buildToolsVersion

resourcePrefix 'magellan_'

defaultConfig {
minSdkVersion Versions.minSdkVersion
targetSdkVersion Versions.targetSdkVersion
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
compose true
}

composeOptions {
kotlinCompilerExtensionVersion Versions.composeVersion
}

compileOptions {
setSourceCompatibility(JavaVersion.VERSION_1_8)
setTargetCompatibility(JavaVersion.VERSION_1_8)
}

buildTypes {
release {
minifyEnabled false
}
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
if (!name.contains("UnitTest")) {
kotlinOptions.freeCompilerArgs = ['-Xjvm-default=compatibility', '-Xexplicit-api=strict', '-Xopt-in=kotlin.RequiresOptIn']
}
kotlinOptions.allWarningsAsErrors = true
kotlinOptions.jvmTarget = "1.8"
}

dependencies {
implementation project(':magellan-library')

implementation Dependencies.appCompat
implementation Dependencies.kotlinStdLib
implementation Dependencies.inject
implementation Dependencies.coroutines
implementation Dependencies.coroutinesAndroid
implementation Dependencies.composeUi
implementation Dependencies.composeUiTooling
implementation Dependencies.composeFoundation

testImplementation Dependencies.testCore
testImplementation Dependencies.junit
testImplementation Dependencies.truth
testImplementation Dependencies.mockito
testImplementation Dependencies.robolectric
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
4 changes: 4 additions & 0 deletions magellan-compose/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POM_ARTIFACT_ID=magellan-compose
POM_NAME=Magellan Compose
POM_DESCRIPTION=Compose support for Magellan
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.magellan.compose

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.magellan.compose.test", appContext.packageName)
}
}
7 changes: 7 additions & 0 deletions magellan-compose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.magellan.compose"
>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.magellan.compose

import androidx.compose.runtime.Composable
import com.wealthfront.magellan.core.Displayable

@Composable
public fun Displayable(displayable: Displayable<@Composable () -> Unit>) {
displayable.view!!()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.magellan.compose

import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.viewinterop.AndroidView
import com.wealthfront.magellan.core.Displayable
import com.wealthfront.magellan.core.Navigable
import com.wealthfront.magellan.lifecycle.LifecycleAwareComponent
import com.wealthfront.magellan.lifecycle.lifecycle
import com.wealthfront.magellan.lifecycle.lifecycleWithContext

@Composable
public fun Displayable(displayable: Displayable<View>, modifier: Modifier = Modifier) {
AndroidView(modifier = modifier, factory = {
if (displayable.view == null) {
throw IllegalStateException(
"View does not exist on ${displayable::class.java.simpleName}. " +
"Is it attached to the lifecycle?"
)
}
displayable.view!!
})
}

public class ComposeStepWrapper(
composeStep: Navigable<@Composable () -> Unit>
) : LifecycleAwareComponent(), Navigable<View> {

public val composeStep: Navigable<@Composable () -> Unit> by lifecycle(composeStep)

override val view: View? by lifecycleWithContext { context ->
ComposeView(context).apply {
setContent @Composable { composeStep.view!!() }
}
}
}

public class ViewStepComposer(
viewStep: Navigable<View>
) : LifecycleAwareComponent(), Navigable<@Composable () -> Unit> {

public val viewStep: Navigable<View> by lifecycle(viewStep)

override val view: @Composable () -> Unit = {
Displayable(viewStep)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.magellan.compose

import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import com.wealthfront.magellan.core.Navigable
import com.wealthfront.magellan.coroutines.ShownLifecycleScope
import com.wealthfront.magellan.lifecycle.LifecycleAwareComponent
import com.wealthfront.magellan.lifecycle.lifecycle
import kotlinx.coroutines.CoroutineScope

public abstract class ComposeStep : LifecycleAwareComponent(), Navigable<@Composable () -> Unit> {

override val view: (@Composable () -> Unit)?
get() = { Compose() }

public var shownScope: CoroutineScope by lifecycle(ShownLifecycleScope()) { it }
@VisibleForTesting set

@Composable
public abstract fun Compose()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.magellan.compose

import androidx.compose.runtime.Composable

public class SimpleComposeStep(public val Content: @Composable SimpleComposeStep.() -> Unit) : ComposeStep() {

@Composable
override fun Compose() {
this.Content()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.magellan.compose

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {

@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ include ':magellan-rx2'
include ':magellan-sample'
include ':magellan-sample-advanced'
include ':magellan-support'
include ':magellan-compose'

0 comments on commit c9f26a2

Please sign in to comment.