-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Kotlin Extensions for Toothpick #324
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,4 @@ protected void onDestroy() { | |
Toothpick.closeScope(this); | ||
super.onDestroy(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.example.smoothie.kotlin | ||
|
||
import android.accounts.AccountManager | ||
import android.app.Activity | ||
import android.app.AlarmManager | ||
import android.app.Application | ||
import android.content.SharedPreferences | ||
import android.os.Bundle | ||
import android.widget.TextView | ||
import androidx.fragment.app.FragmentManager | ||
import butterknife.ButterKnife | ||
import com.example.smoothie.R | ||
import com.example.smoothie.kotlin.deps.ContextNamer | ||
import toothpick.Toothpick | ||
import toothpick.kotlin.androidx.inject | ||
import toothpick.kotlin.module | ||
import toothpick.kotlin.providedBy | ||
import toothpick.smoothie.module.SmoothieActivityModule | ||
|
||
class LessSimpleActivity : Activity() { | ||
// inject() will attempt to find a scope, then default to this activity instance | ||
private val injectedApplication: Application by inject() | ||
private val accountManager: AccountManager by inject() | ||
private val sharedPreferences: SharedPreferences by inject() | ||
private val alarmManager: AlarmManager by inject() | ||
private val injectFragmentManager: FragmentManager by inject() | ||
private val activity: Activity by inject() | ||
private val contextNamer: ContextNamer by inject() | ||
private val specialString: String by inject(name = "SpecialString") | ||
|
||
private val title by lazy { findViewById<TextView>(R.id.title) } | ||
private val subTitle by lazy { findViewById<TextView>(R.id.subtitle) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
val scope = Toothpick.openScopes(application, this) | ||
scope.installModules(SmoothieActivityModule(this)) | ||
scope.installModules(module { String::class named "SpecialString" providedBy "My Special String" }) | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.simple_activity) | ||
ButterKnife.bind(this) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need butterknife ? It seems to me we're using a simple lazy to assign the views. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, was just copying the Java examples over for parity. |
||
title.text = "${contextNamer.applicationName} with $specialString" | ||
subTitle.text = contextNamer.activityName | ||
} | ||
|
||
override fun onDestroy() { | ||
Toothpick.closeScope(this) | ||
super.onDestroy() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.smoothie.kotlin | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.Button | ||
import android.widget.TextView | ||
import butterknife.ButterKnife | ||
import com.example.smoothie.R | ||
import com.example.smoothie.kotlin.deps.PresenterContextNamer | ||
import toothpick.Scope | ||
import toothpick.Toothpick | ||
import toothpick.kotlin.HasScope | ||
import toothpick.kotlin.androidx.inject | ||
import toothpick.kotlin.injectLazy | ||
import toothpick.smoothie.module.SmoothieActivityModule | ||
|
||
class PersistActivity : Activity(), HasScope { | ||
override lateinit var scope: Scope | ||
|
||
// inject() will use HasScope interface to use to correct scope with presenter scope | ||
private val contextNamer: PresenterContextNamer by inject() | ||
|
||
private val title by lazy { findViewById<TextView>(R.id.title) } | ||
private val subTitle by lazy { findViewById<TextView>(R.id.subtitle) } | ||
private val button by lazy { findViewById<Button>(R.id.hello) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
scope = Toothpick.openScopes(application, PRESENTER_SCOPE, this) | ||
scope.installModules(SmoothieActivityModule(this)) | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(R.layout.simple_activity) | ||
ButterKnife.bind(this) | ||
title.text = "MVP" | ||
subTitle.text = contextNamer.instanceCount | ||
button.visibility = View.GONE | ||
} | ||
|
||
override fun onDestroy() { | ||
Toothpick.closeScope(this) | ||
super.onDestroy() | ||
} | ||
|
||
override fun onBackPressed() { | ||
//when we leave the presenter flow, | ||
//we close its scope | ||
Toothpick.closeScope(PRESENTER_SCOPE) | ||
super.onBackPressed() | ||
} | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@MustBeDocumented | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@javax.inject.Scope | ||
annotation class Presenter | ||
|
||
companion object { | ||
val PRESENTER_SCOPE: Class<*> = Presenter::class.java | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.smoothie.kotlin | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.Button | ||
import android.widget.TextView | ||
import com.example.smoothie.R | ||
import com.example.smoothie.kotlin.deps.RxPresenter | ||
import rx.Subscription | ||
import rx.functions.Action1 | ||
import toothpick.Scope | ||
import toothpick.Toothpick | ||
import toothpick.kotlin.injectLazy | ||
import toothpick.smoothie.module.SmoothieActivityModule | ||
|
||
class RxMVPActivity : Activity() { | ||
private lateinit var activityScope: Scope | ||
|
||
// Uses lazy assign of scope via InjectConfig block and default TP Kotlin Extension | ||
private val rxPresenter: RxPresenter by injectLazy { scope = activityScope } | ||
|
||
private val title by lazy { findViewById<TextView>(R.id.title) } | ||
private val subTitle by lazy { findViewById<TextView>(R.id.subtitle) } | ||
private val button by lazy { findViewById<Button>(R.id.hello) } | ||
|
||
private lateinit var subscription: Subscription | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
activityScope = Toothpick.openScopes(application, PRESENTER_SCOPE, this) | ||
activityScope.installModules(SmoothieActivityModule(this)) | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(R.layout.simple_activity) | ||
title.text = "MVP" | ||
subscription = rxPresenter.subscribe(Action1 { aLong -> subTitle.text = aLong.toString() } ) | ||
button.visibility = View.GONE | ||
} | ||
|
||
override fun onDestroy() { | ||
Toothpick.closeScope(this) | ||
subscription.unsubscribe() | ||
if (isFinishing) { | ||
//when we leave the presenter flow, | ||
//we close its scope | ||
rxPresenter.stop() | ||
Toothpick.closeScope(PRESENTER_SCOPE) | ||
} | ||
super.onDestroy() | ||
} | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@MustBeDocumented | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@javax.inject.Scope | ||
annotation class Presenter | ||
|
||
companion object { | ||
val PRESENTER_SCOPE: Class<*> = Presenter::class.java | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.smoothie.kotlin | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Button | ||
import android.widget.TextView | ||
import androidx.fragment.app.FragmentActivity | ||
import com.example.smoothie.R | ||
import com.example.smoothie.kotlin.deps.ContextNamer | ||
import toothpick.Toothpick | ||
import toothpick.kotlin.androidx.inject | ||
import toothpick.smoothie.module.SmoothieAndroidXActivityModule | ||
|
||
class SimpleActivity : FragmentActivity() { | ||
// Will use activity instance as scope | ||
private val contextNamer: ContextNamer by inject() | ||
|
||
private val title by lazy { findViewById<TextView>(R.id.title) } | ||
private val subTitle by lazy { findViewById<TextView>(R.id.subtitle) } | ||
private val button by lazy { findViewById<Button>(R.id.hello) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
val scope = Toothpick.openScopes(application, this) | ||
scope.installModules(SmoothieAndroidXActivityModule(this)) | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.simple_activity) | ||
|
||
title.text = contextNamer.applicationName | ||
subTitle.text = contextNamer.activityName | ||
button.apply { | ||
text = "click me !" | ||
setOnClickListener { startActivity(Intent(this@SimpleActivity, RxMVPActivity::class.java)) } | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
Toothpick.closeScope(this) | ||
super.onDestroy() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.smoothie.kotlin.deps | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import javax.inject.Inject | ||
|
||
class ContextNamer @Inject constructor( | ||
application: Application, | ||
activity: Activity | ||
) { | ||
val applicationName = application::class.simpleName ?: "" | ||
val activityName = activity::class.simpleName ?: "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.smoothie.kotlin.deps | ||
|
||
import com.example.smoothie.kotlin.PersistActivity | ||
import javax.inject.Inject | ||
|
||
@PersistActivity.Presenter | ||
class PresenterContextNamer @Inject constructor() { | ||
|
||
init { | ||
countInstances++ | ||
} | ||
|
||
val instanceCount: String | ||
get() = "Instance# $countInstances" | ||
|
||
companion object { | ||
private var countInstances = 0 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use Task Configuration Avoidance.