From 803263690a33b1fce4f46f43cce710a14a7a8623 Mon Sep 17 00:00:00 2001 From: Nathan Hamblen Date: Wed, 14 Jul 2010 22:29:54 -0400 Subject: [PATCH 1/4] warn if resource id used for different types --- src/main/scala/TypedResources.scala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/scala/TypedResources.scala b/src/main/scala/TypedResources.scala index 1a064e2..be92d14 100644 --- a/src/main/scala/TypedResources.scala +++ b/src/main/scala/TypedResources.scala @@ -29,7 +29,13 @@ trait TypedResources extends AndroidProject { } } } - }.foldLeft(Map.empty[String, String]) { case (m, (k, v)) => m + (k -> v) } + }.foldLeft(Map.empty[String, String]) { + case (m, (k, v)) => + m.get(k).foreach { v0 => + if (v0 != v) log.warn("Resource id '%s' mapped to %s and %s" format (k, v0, v)) + } + m + (k -> v) + } FileUtilities.write(typedResource.asFile, """ |package %s |import android.app.Activity From 58f0fa80b04ca6737352b919185882b8d909e84e Mon Sep 17 00:00:00 2001 From: Nathan Hamblen Date: Wed, 14 Jul 2010 22:49:07 -0400 Subject: [PATCH 2/4] only consider layout dir --- src/main/scala/TypedResources.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/scala/TypedResources.scala b/src/main/scala/TypedResources.scala index be92d14..c6b739a 100644 --- a/src/main/scala/TypedResources.scala +++ b/src/main/scala/TypedResources.scala @@ -5,15 +5,15 @@ trait TypedResources extends AndroidProject { def managedScalaPath = "src_managed" / "main" / "scala" def typedResource = managedScalaPath / "TR.scala" abstract override def mainSourceRoots = super.mainSourceRoots +++ managedScalaPath - def xmlResources = mainResPath ** "*.xml" + def layoutResources = mainResPath / "layout" ** "*.xml" override def compileAction = super.compileAction dependsOn generateTypedResources override def cleanAction = super.cleanAction dependsOn cleanTask(managedScalaPath) - override def watchPaths = super.watchPaths +++ xmlResources + override def watchPaths = super.watchPaths +++ layoutResources - lazy val generateTypedResources = fileTask(typedResource from xmlResources) { + lazy val generateTypedResources = fileTask(typedResource from layoutResources) { val Id = """@\+id/(.*)""".r val androidJarLoader = ClasspathUtilities.toLoader(androidJarPath) - val resources = xmlResources.get.flatMap { path => + val resources = layoutResources.get.flatMap { path => XML.loadFile(path.asFile).descendant_or_self flatMap { node => // all nodes node.attribute("http://schemas.android.com/apk/res/android", "id") flatMap { From e3a20aab6767ec2d44f1cd2b6525cd49053c334f Mon Sep 17 00:00:00 2001 From: Nathan Hamblen Date: Wed, 14 Jul 2010 23:39:40 -0400 Subject: [PATCH 3/4] name of implicit function --- src/main/scala/TypedResources.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/TypedResources.scala b/src/main/scala/TypedResources.scala index c6b739a..835299a 100644 --- a/src/main/scala/TypedResources.scala +++ b/src/main/scala/TypedResources.scala @@ -57,7 +57,7 @@ trait TypedResources extends AndroidProject { |trait TypedActivity extends Activity with TypedActivityHolder { def activity = this } |object TypedResource { | implicit def view2typed(v: View) = new TypedViewHolder { def view = v } - | implicit def view2typed(act: Activity) = new TypedActivityHolder { def activity = act } + | implicit def activity2typed(act: Activity) = new TypedActivityHolder { def activity = act } |} |""".stripMargin.format( manifestPackage, resources map { case (id, classname) => From c66748f9bd9550a7c81ec22c85b1df8b37ed976a Mon Sep 17 00:00:00 2001 From: Nathan Hamblen Date: Thu, 15 Jul 2010 00:04:40 -0400 Subject: [PATCH 4/4] typed resource documentation --- README.markdown | 8 ++++++++ notes/0.5.0.markdown | 1 + 2 files changed, 9 insertions(+) create mode 100644 notes/0.5.0.markdown diff --git a/README.markdown b/README.markdown index 951311c..6d80155 100644 --- a/README.markdown +++ b/README.markdown @@ -59,6 +59,14 @@ Whenever you change build versions, you'll need to run `update` again to fetch d [cb]: http://code.google.com/p/simple-build-tool/wiki/CrossBuild +##Typed resources references + +As an enhancement to the Android build process, this plugin can generate typed references to application layout elements. To enable, mix the `TypedResources` trait into your sbt project definition. During compilation a file `TR.scala` will be generated under `src_managed/main/scala`. + +Typed resource references are created in an object `TR` (similar to Android's standard `R`). These are handled by the method `findView` defined in the traits `TypedView` and `TypedActivity`. There are also implicit conversions defined in the object `TypedResource`; import these to add the method on demand to any views and activities in scope. The `findView` method casts the view to the known resource type before returning it, so that application code can avoid the redundancy of casting a resource to a type it has declared in the resource definition. + +Since Android's resource IDs are scoped to the application, a warning is issued by the plugin when the same ID is used for different types of a resources; the type of resources retrieved by that ID will be unpredictable. + ##Hacking on the plugin If you need make modifications to the plugin itself, you can compile and install it locally (you need at least sbt 0.7.x to build it): diff --git a/notes/0.5.0.markdown b/notes/0.5.0.markdown new file mode 100644 index 0000000..34a0c8d --- /dev/null +++ b/notes/0.5.0.markdown @@ -0,0 +1 @@ +* `TypedResources` trait processes layout definitions to generate typed resource references in a Scala source file \ No newline at end of file