Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #79 from 47deg/+core-add-contexts-view
Browse files Browse the repository at this point in the history
Adding implicit ContextWrapper for Views
  • Loading branch information
stanch committed Mar 3, 2016
2 parents fa33d05 + 09b8b5c commit 2a2b71c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 20 additions & 2 deletions macroid-core/src/main/scala/macroid/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package macroid

import android.app.{ Service, Activity, Application }
import android.content.Context
import android.view.View
import scala.ref.WeakReference
import scala.annotation.implicitNotFound
import macroid.support.{ Fragment, FragmentApi }
Expand All @@ -12,8 +13,8 @@ import macroid.support.{ Fragment, FragmentApi }
* (which is more specific, but may die and is stored as a weak reference)
*/
@implicitNotFound("""Could not find a `ContextWrapper`.
If you are inside Activity, Fragment or Service, extend Contexts[Activity], Contexts[Fragment] or Contexts[Service],
otherwise pass an instance of `ContextWrapper` from outside.""")
If you are inside Activity, Fragment or Service, extend Contexts[Activity], Contexts[Fragment], Contexts[Service] or
Contexts[View], otherwise pass an instance of `ContextWrapper` from outside.""")
sealed trait ContextWrapper {
type C <: Context

Expand All @@ -33,6 +34,9 @@ case class ServiceContextWrapper(original: WeakReference[Service], application:
case class ApplicationContextWrapper(original: WeakReference[Application], application: Context)
extends ContextWrapper { type C = Application }

case class GenericContextWrapper(original: WeakReference[Context], application: Context)
extends ContextWrapper { type C = Context }

object ContextWrapper {
def apply(activity: Activity): ActivityContextWrapper =
ActivityContextWrapper(WeakReference(activity), activity.getApplicationContext)
Expand All @@ -45,6 +49,17 @@ object ContextWrapper {

def apply[F](fragment: F)(implicit fragmentImpl: Fragment[F]): ActivityContextWrapper =
ContextWrapper(fragmentImpl.activity(fragment))

def apply(context: Context): GenericContextWrapper =
GenericContextWrapper(WeakReference(context), context.getApplicationContext)

def apply(view: View): ContextWrapper = view.getContext match {
case activity: Activity ContextWrapper(activity)
case service: Service ContextWrapper(service)
case app: Application ContextWrapper(app)
case context ContextWrapper(context)
}

}

/** FragmentManager context, used to manipulate fragments within an Activity or another Fragment
Expand All @@ -67,6 +82,9 @@ trait Contexts[X] { self: X ⇒
implicit def serviceContextWrapper(implicit service: X <:< Service): ServiceContextWrapper =
ContextWrapper(service(self))

implicit def viewContextWrapper(implicit view: X <:< View): ContextWrapper =
ContextWrapper(view(self))

implicit def activityManagerContext[M, F, A >: X](implicit fragmentApi: FragmentApi[F, M, A]) =
FragmentManagerContext[F, M](fragmentApi.activityManager(self))

Expand Down
14 changes: 11 additions & 3 deletions macroid-docs/guide/Contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ This is done with the `ContextWrapper` trait, which has four methods:
* `contextWrapper.bestAvailable` will return the original context if it’s available,
otherwise — the application context.

There are a few specialized cases of `ContextWrapper`: `ActivityContextWrapper`, `ServiceContextWrapper`
and `ApplicationContextWrapper`.
There are a few specialized cases of `ContextWrapper`: `ActivityContextWrapper`, `ServiceContextWrapper`,
`ApplicationContextWrapper` and `GenericContextWrapper`.

## Including

Expand All @@ -39,14 +39,22 @@ class MyActivity extends FragmentActivity with Contexts[FragmentActivity] {
}
```

Finally, in a fragment (either `android.app.Fragment` or `android.support.v4.app.Fragment`):
In a fragment (either `android.app.Fragment` or `android.support.v4.app.Fragment`):

```scala
class MyFragment extends Fragment with Contexts[Fragment] {
...
}
```

Finally, if you want to include the implicit context in your views:

```scala
class MyFrameLayout extends FrameLayout with Contexts[View] {
...
}
```

You can also construct a `ContextWrapper` directly:

```scala
Expand Down

0 comments on commit 2a2b71c

Please sign in to comment.