Permalink
Browse files

Introduce `defapplication` macro

It is convenient to start nREPL in the application's onCreate so it is
not restarted when the Activity is recreated (when device is rotated,
for example)
  • Loading branch information...
1 parent b02ef39 commit c0754c2a44c149b7e0bdded89b89c38645880704 @alexander-yakushev alexander-yakushev committed Jun 9, 2012
Showing with 36 additions and 0 deletions.
  1. +5 −0 src/clojure/neko/_utils.clj
  2. +31 −0 src/clojure/neko/application.clj
@@ -33,3 +33,8 @@
[x]
(or (integer? x)
(keyword? x)))
+
+(defn simple-name [full-class-name]
+ "Takes a possibly package-qualified class name symbol and returns a
+ simple class name from it."
+ (nth (re-find #"(.*\.)?(.+)" (str full-class-name)) 2))
@@ -0,0 +1,31 @@
+(ns neko.application
+ "Contains the tools to create and manipulate Application instances."
+ (:use [neko.-utils :only [simple-name]])
+ (:import android.app.Application))
+
+(defmacro defapplication
+ "Creates an application class with the given full package-qualified
+ name. Optional arguments should be provided in a key-value fashion.
+
+ Available optional arguments:
+
+ :extends, :prefix - same as for `gen-class`.
+
+ :create - takes a one-argument function. Generates a handler for
+ application's `onCreate` event which automatically calls the
+ superOnCreate method and then calls the provided function onto the
+ Application object."
+ [name &
+ {:keys [extends prefix create]}]
+ (let [prefix (or prefix (str (simple-name name) "-"))]
+ `(do
+ (gen-class
+ :name ~name
+ :main false
+ :prefix ~prefix
+ :extends ~(or extends android.app.Application)
+ :exposes-methods {~'onCreate ~'superOnCreate})
+ ~(when create
+ `(defn ~(symbol (str prefix "onCreate")) [~'this]
+ (.superOnCreate ~'this)
+ (~create ~'this))))))

0 comments on commit c0754c2

Please sign in to comment.