Skip to content

Instantly share code, notes, and snippets.

@alaershov
Last active May 21, 2018 09:30
Show Gist options
  • Save alaershov/5b4e12c944aba6031c08b277860e3198 to your computer and use it in GitHub Desktop.
Save alaershov/5b4e12c944aba6031c08b277860e3198 to your computer and use it in GitHub Desktop.
Moxy + Toothpick
package ru.improvegroup.bookahut.presentation.ui.book
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import butterknife.BindView
import butterknife.ButterKnife
import com.arellomobile.mvp.presenter.InjectPresenter
import com.arellomobile.mvp.presenter.ProvidePresenter
import ru.improvegroup.bookahut.R
import ru.improvegroup.bookahut.di.Scopes
import ru.improvegroup.bookahut.domain.entity.Book
import ru.improvegroup.bookahut.domain.entity.LongId
import ru.improvegroup.bookahut.presentation.common.mvp.MoxyActivity
import toothpick.Toothpick
import toothpick.config.Module
import javax.inject.Inject
import javax.inject.Provider
/**
* Single book view.
*/
class BookActivity : MoxyActivity(), BookView {
@BindView(R.id.text_title)
lateinit var titleTextView: TextView
@BindView(R.id.text_author)
lateinit var authorTextView: TextView
@BindView(R.id.text_section)
lateinit var sectionTextView: TextView
@InjectPresenter
lateinit var presenter: BookPresenter
@Inject
lateinit var presenterProvider: Provider<BookPresenter>
companion object {
private val ARG_BOOK_ID = "ARG_BOOK_ID"
fun buildArgs(bookId: Long): Bundle {
return Bundle().also {
it.putLong(ARG_BOOK_ID, bookId)
}
}
}
@ProvidePresenter
fun providePresenter(): BookPresenter = presenterProvider.get()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_book)
ButterKnife.bind(this)
}
override fun openScope() {
val bookId = intent.getLongExtra(ARG_BOOK_ID, 0)
val bookScope = Toothpick.openScopes(Scopes.APP_SCOPE, Scopes.BOOK_SCOPE)
bookScope.installModules(object : Module() {
init {
bind(BookPresenter::class.java)
bind(LongId::class.java).toInstance(LongId(bookId))
}
})
Toothpick.inject(this, bookScope)
}
override fun closeScope() {
Toothpick.closeScope(Scopes.BOOK_SCOPE)
}
override fun showError(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
override fun showBook(book: Book) {
titleTextView.text = book.title
authorTextView.text = book.author
sectionTextView.text = book.section.name
}
}
package ru.improvegroup.bookahut.presentation.common.mvp
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.arellomobile.mvp.MvpDelegate
abstract class MoxyActivity : AppCompatActivity() {
/**
* @return The [MvpDelegate] used by this Activity.
*/
val mvpDelegate: MvpDelegate<*> by lazy {
MvpDelegate(this)
}
protected abstract fun openScope()
protected open fun closeScope() {}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
openScope()
mvpDelegate.onCreate(savedInstanceState)
}
override fun onStart() {
super.onStart()
mvpDelegate.onAttach()
}
override fun onResume() {
super.onResume()
mvpDelegate.onAttach()
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
mvpDelegate.onSaveInstanceState(outState!!)
mvpDelegate.onDetach()
}
override fun onStop() {
super.onStop()
mvpDelegate.onDetach()
}
override fun onDestroy() {
super.onDestroy()
mvpDelegate.onDestroyView()
if (isFinishing) {
closeScope()
mvpDelegate.onDestroy()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment