Skip to content

Commit

Permalink
feat(android): use handlebar templates to bind real properties/functi…
Browse files Browse the repository at this point in the history
…ons instead of using extensions (#173)
  • Loading branch information
n8chur authored and stristr committed Jun 27, 2019
1 parent 19fa10f commit 7a53dfd
Show file tree
Hide file tree
Showing 27 changed files with 567 additions and 376 deletions.
1 change: 0 additions & 1 deletion packages/compiler/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ export interface TargetOutput<
Spec = TargetComponentSpec,
> {
processedComponents: Map<PropertyType, TargetSpecLedger<Spec, Binding>>;
sources: Set<string>;
dependencies: Set<Dependency>;
assetBindings: AssetBindings;
projectName: string;
Expand Down
10 changes: 0 additions & 10 deletions packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,6 @@ export abstract class TargetCompiler<
*/
output: OutputType;

/**
* Updates a output based on the contents of bindings.
*/
protected abstract mergeBindingToOutput (binding: BindingType): void;

/**
* Creates fresh output.
*/
Expand Down Expand Up @@ -722,11 +717,6 @@ export abstract class TargetCompiler<
return false;
}

for (const {binding} of this.output.processedComponents.values()) {
if (binding) {
this.mergeBindingToOutput(binding as BindingType);
}
}
await this.writeAssets();
copySync(this.program.emitRoot, this.program.hotRoot);
return true;
Expand Down
7 changes: 0 additions & 7 deletions packages/compiler/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ export class TestTargetCompiler extends TargetCompiler<TargetOutput, TargetBindi
return 'foo.bar';
}

protected mergeBindingToOutput (binding: TargetBinding): void {
for (const source in binding.sources) {
this.output.sources.add(source);
}
}

protected createOutput (sdkRoot: string, projectName: string): TargetOutput {
return {
sdkRoot,
projectName,
processedComponents: new Map(),
sources: new Set(),
dependencies: new Set(),
assetBindings: new Map(),
};
Expand Down
26 changes: 3 additions & 23 deletions packages/targets/sources/android/android.component.handlebars
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
{{#if hasProperties}}
data class {{{componentName}}}(
{{#if singleton}}
{{#each properties}}
val {{{@key}}}: {{{this.type}}} = {{{this.initializer}}}{{#unless @last}},{{/unless}}
{{/each}}
{{else}}
{{#each properties}}
val {{{@key}}}: {{{this.type}}}{{#unless @last}},{{/unless}}
{{/each}}
{{/if}}
{{else}}
data class {{{componentName}}}(
val nonce: String = ""
{{/if}}
{{#if public}}) : StateBag {
companion object {}
override val name = "{{{componentName}}}"
package {{{packageName}}}

{{> androidDataClassStart }}
}
{{else}}
) {
companion object {}
}
{{/if}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{#if hasProperties}}
data class {{{componentName}}}(
{{#if singleton}}
{{#each properties}}
val {{{@key}}}: {{{this.type}}} = {{{this.initializer}}}{{#unless @last}},{{/unless}}
{{/each}}
{{else}}
{{#each properties}}
val {{{@key}}}: {{{this.type}}}{{#unless @last}},{{/unless}}
{{/each}}
{{/if}}
{{else}}
data class {{{componentName}}}(
val nonce: String = ""
{{/if}}
{{#if public}}) : StateBag {
companion object {}
override val name = "{{{componentName}}}"
{{else}}
) {
companion object {}
{{/if}}
25 changes: 15 additions & 10 deletions packages/targets/sources/android/bindings/Color.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package {{{packageName}}}

import android.graphics.Color as CoreColor
import android.support.v4.graphics.ColorUtils

val Color.color: Int
get() {
val rgb = ColorUtils.HSLToColor(floatArrayOf(this.h * 360, this.s, this.l))
return CoreColor.argb(
(this.a * 255).toInt(),
CoreColor.red(rgb),
CoreColor.green(rgb),
CoreColor.blue(rgb)
)
}
{{> androidDataClassStart }}

val color: Int
get() {
val rgb = ColorUtils.HSLToColor(floatArrayOf(this.h * 360, this.s, this.l))
return CoreColor.argb(
(this.a * 255).toInt(),
CoreColor.red(rgb),
CoreColor.green(rgb),
CoreColor.blue(rgb)
)
}
}
86 changes: 45 additions & 41 deletions packages/targets/sources/android/bindings/File.kt
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
package {{{packageName}}}

import android.net.Uri
import java.net.URL

private val extensionReplacer = """(.+)(_.+)""".toRegex()
private val fileReplacer = """[^a-z0-9_]""".toRegex()
{{> androidDataClassStart }}

internal val File.resourceName: String
get() {
return extensionReplacer.replace(fileReplacer.replace(src.toLowerCase(), "_"), "$1")
}

private val File.resourcePath: String
get() {
return "$type/$resourceName"
}

internal val File.resourceId: Int
get() {
return Environment.resources.getIdentifier(
resourceName,
type,
Environment.packageName
)
}

internal val File.canonicalURL: String
get() {
if (Environment.isHot) {
return "${Environment.serverUrl}/$src"
val uri: Uri
get() {
return Uri.parse(this.canonicalURL)
}

return "android.resource://${Environment.packageName}/$resourcePath"
}
val url: URL
get() {
return URL(this.canonicalURL)
}

internal val File.websafeURL: String
get() {
if (Environment.isHot) {
return canonicalURL
internal val resourceName: String
get() {
return extensionReplacer.replace(fileReplacer.replace(src.toLowerCase(), "_"), "$1")
}

return "file:///android_res/$resourcePath"
}
internal val resourceId: Int
get() {
return Environment.resources.getIdentifier(
resourceName,
type,
Environment.packageName
)
}

internal val canonicalURL: String
get() {
if (Environment.isHot) {
return "${Environment.serverUrl}/$src"
}

val File.uri: Uri
get() {
return Uri.parse(this.canonicalURL)
}
return "android.resource://${Environment.packageName}/$resourcePath"
}

internal val websafeURL: String
get() {
if (Environment.isHot) {
return canonicalURL
}

val File.url: URL
get() {
return URL(this.canonicalURL)
}
return "file:///android_res/$resourcePath"
}

private val resourcePath: String
get() {
return "$type/$resourceName"
}
}

private val extensionReplacer = """(.+)(_.+)""".toRegex()
private val fileReplacer = """[^a-z0-9_]""".toRegex()
62 changes: 33 additions & 29 deletions packages/targets/sources/android/bindings/Image.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package {{{packageName}}}

import android.graphics.BitmapFactory
import android.graphics.Shader
import android.graphics.drawable.BitmapDrawable
Expand All @@ -12,6 +14,37 @@ import com.bumptech.glide.signature.ObjectKey
import android.widget.ImageView
import android.widget.TextView

{{> androidDataClassStart }}

internal val correctDensityFile: File
get () {
return when (effectiveDensity) {
1 -> file
2 -> file2x
3 -> file3x
else -> file4x
}
}

internal val resourceId: Int
get () {
return this.correctDensityFile.resourceId
}

internal val drawableFromRawResource: Drawable?
get () {
return ResourcesCompat.getDrawable(
Environment.resources,
Environment.resources.getIdentifier(
file.resourceName,
"drawable",
Environment.packageName
),
null
)
}
}

fun ImageView.load(image: Image) {
if (Environment.isHot) {
getFromNetwork(image, this, fun(drawable) {
Expand Down Expand Up @@ -79,21 +112,6 @@ private val effectiveDensity: Int
return Math.ceil(density).toInt()
}

private val Image.correctDensityFile: File
get () {
return when (effectiveDensity) {
1 -> file
2 -> file2x
3 -> file3x
else -> file4x
}
}

private val Image.resourceId: Int
get () {
return this.correctDensityFile.resourceId
}

private fun getFromNetwork(image: Image, view: View, callback: (BitmapDrawable) -> Unit) {
val width = (image.width * Environment.resources.displayMetrics.density.toDouble()).toInt()
val height = (image.height * Environment.resources.displayMetrics.density.toDouble()).toInt()
Expand All @@ -108,17 +126,3 @@ private fun getFromNetwork(image: Image, view: View, callback: (BitmapDrawable)
}
})
}


private val Image.drawableFromRawResource: Drawable?
get () {
return ResourcesCompat.getDrawable(
Environment.resources,
Environment.resources.getIdentifier(
file.resourceName,
"drawable",
Environment.packageName
),
null
)
}
5 changes: 5 additions & 0 deletions packages/targets/sources/android/bindings/Lottie.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package {{{packageName}}}

import android.util.Log
import android.widget.FrameLayout
import com.airbnb.lottie.LottieAnimationView
Expand All @@ -6,6 +8,9 @@ import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieListener
import com.airbnb.lottie.LottieDrawable

{{> androidDataClassStart }}
}

fun LottieAnimationView.load(lottie: Lottie) {
val task = when(Environment.isHot) {
true -> LottieCompositionFactory.fromUrl(context, lottie.file.canonicalURL)
Expand Down
Loading

0 comments on commit 7a53dfd

Please sign in to comment.