Skip to content

Commit

Permalink
Refactor out protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvik committed Mar 12, 2011
1 parent cb9b849 commit 5060420
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 40 deletions.
35 changes: 35 additions & 0 deletions src/clojure/neko/_protocols/resolvable.clj
@@ -0,0 +1,35 @@
; Copyright © 2011 Sattvik Software & Technology Resources, Ltd. Co.
; All rights reserved.
;
; This program and the accompanying materials are made available under the
; terms of the Eclipse Public License v1.0 which accompanies this distribution,
; and is available at <http://www.eclipse.org/legal/epl-v10.html>.
;
; By using this software in any fashion, you are agreeing to be bound by the
; terms of this license. You must not remove this notice, or any other, from
; this software.

(ns neko.-protocols.resolvable
"Home of the Resolvable protocol and related utilities."
{:author "Daniel Solano Gómez"})

(defprotocol Resolvable
"Protocol for resolving a resource given some sort of id, a context, and a
type."
(resolve-it [id context type])
(resolve-id [id context])
(resolve-string [id context])
(resolve-layout [id context]))

(extend-type Integer
Resolvable
(resolve-it [id _1 _2] id)
(resolve-id [id _] id)
(resolve-string [id _] id)
(resolve-layout [id _] id))

(defn resolvable?
"Determines whether the argument represents an argument that can be resolved
as a resource."
[x]
(satisfies? Resolvable x))
18 changes: 18 additions & 0 deletions src/clojure/neko/_protocols/view_finder.clj
@@ -0,0 +1,18 @@
; Copyright © 2011 Sattvik Software & Technology Resources, Ltd. Co.
; All rights reserved.
;
; This program and the accompanying materials are made available under the
; terms of the Eclipse Public License v1.0 which accompanies this distribution,
; and is available at <http://www.eclipse.org/legal/epl-v10.html>.
;
; By using this software in any fashion, you are agreeing to be bound by the
; terms of this license. You must not remove this notice, or any other, from
; this software.

(ns neko.-protocols.view-finder
"Home of the ViewFinder protocol and related utilities."
{:author "Daniel Solano Gómez"})

(defprotocol ViewFinder
"Protocol for types that support finding child views by an ID."
(-find-view [finder id]))
66 changes: 50 additions & 16 deletions src/clojure/neko/activity.clj
Expand Up @@ -12,9 +12,11 @@
(ns neko.activity
"Utilities to aid in working with an activity."
{:author "Daniel Solano Gómez"}
(:import android.app.Activity)
(:import android.app.Activity
android.view.View)
(:require [neko.context :as context])
(:use neko.-utils))
(:use [neko.-protocols resolvable view-finder]
neko.-utils))

(def
^{:doc "The current activity to operate on."}
Expand All @@ -28,6 +30,22 @@
*activity* activity#]
~@body)))

(defn- activity?
"Determines whether the argument is an instance of Activity."
[x]
(instance? Activity x))

(defn- within-with-activity?
"Ensures that the function is within a valid with-activity form."
[]
(and (bound? #'*activity*)
(activity? *activity*)))

(extend-type Activity
ViewFinder
(-find-view [activity id]
(.findViewById activity (resolve-id id activity))))

(defn find-view
"Finds a view that identified by the given ID. Returns the view if found or
nil otherwise. The id-or-name argument may either be the integer ID or a
Expand All @@ -36,9 +54,17 @@
If processed within a with-activity form, the activity argument may be
omitted."
([id-or-name]
(find-view *activity* id-or-name))
([^Activity activity id-or-name]
(.findViewById activity (context/resolve-resource activity :id id-or-name))))
{:pre [(resolvable? id-or-name)
(within-with-activity?)]
:post [(or (nil? %)
(instance? View %))]}
(-find-view *activity* id-or-name))
([activity id-or-name]
{:pre [(activity? activity)
(resolvable? id-or-name)]
:post [(or (nil? %)
(instance? View %))]}
(-find-view activity id-or-name)))

(defn set-content-view!
"Sets the content for the activity. The view may be one of:
Expand All @@ -48,18 +74,20 @@
+ A keyword used to resolve to a layout ID using
(neko.context/resolve-resource)"
([view]
{:pre [(or (instance? View view)
(resolvable? view))]}
(set-content-view! *activity* view))
([^Activity activity view]
{:pre [(or (instance? android.view.View view)
(integer? view)
(keyword? view))]}
{:pre [(activity? activity)
(or (instance? View view)
(resolvable? view))]}
(cond
(instance? android.view.View view)
(.setContentView activity ^android.view.View view)
(instance? View view)
(.setContentView activity ^View view)
(integer? view)
(.setContentView activity ^Integer view)
:else
(.setContentView activity ^Integer (context/resolve-resource activity :layout view)))))
(.setContentView activity ^Integer (resolve-layout view activity)))))

(defn request-window-features!
"Requests the given features for the activity. The features should be
Expand All @@ -72,12 +100,18 @@
is not necessary.
This function should be called before set-content-view!."
[activity? & features]

{:arglists '([& features] [activity & features])}
[activity & features]
{:pre [(or (activity? activity)
(and (keyword? activity)
(within-with-activity?)))
(every? keyword? features)]
:post [%
(every? (fn [x] (instance? Boolean x)) %)]}
(let [[^Activity activity features]
(if (instance? Activity activity?)
[activity? features]
[*activity* (cons activity? features)])
(if (instance? Activity activity)
[activity features]
[*activity* (cons activity features)])
keyword->int (fn [k]
(static-field-value android.view.Window
k
Expand Down
31 changes: 7 additions & 24 deletions src/clojure/neko/context.clj
Expand Up @@ -13,7 +13,8 @@
"Utilities to aid in working with a context."
{:author "Daniel Solano Gómez"}
(:import android.content.Context)
(:use neko.-utils))
(:use neko.-protocols.resolvable
neko.-utils))

(def
^{:doc "The current context object to operate on."}
Expand Down Expand Up @@ -62,34 +63,16 @@
type
name)))))))

(defprotocol Resolvable
"Protocol for resolving a resource given some sort of id, a context, and a
type."
(resolve-it [id context type])
(resolve-string [id context])
(resolve-layout [id context]))

(extend-protocol Resolvable
Integer
(resolve-it [id _1 _2] id)
(resolve-string [id _] id)
(resolve-layout [id _] id)


clojure.lang.Keyword
(extend-type clojure.lang.Keyword
Resolvable
(resolve-it
[name context type] (resolve-from-keyword context type name))
(resolve-id
[name context] (resolve-from-keyword context :id name))
(resolve-string
[name context] (resolve-from-keyword context :string name))
(resolve-layout
[name context] (resolve-from-keyword context :layout name))
)

(defn- resolvable?
"Determines whether the argument represents an argument that can be resolved
as a resource."
[x]
(satisfies? Resolvable x))
[name context] (resolve-from-keyword context :layout name)))

(defn resolve-resource
"Resolves the resource ID of a given type with the given name. For example,
Expand Down

0 comments on commit 5060420

Please sign in to comment.