Skip to content
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

Add base presenter and alternative disposable observer #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.runner.RunWith
abstract class AcceptanceTest<T : Activity>(clazz: Class<T>) {

@Rule @JvmField
val testRule: ActivityTestRule<T> = IntentsTestRule(clazz)
val testRule = IntentsTestRule(clazz)

val checkThat: Matchers = Matchers()
val events: Events = Events()
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/kotlin/com/fernandocejas/sample/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.ViewGroup
import com.fernandocejas.sample.di.ApplicationComponent
import com.fernandocejas.sample.features.base.BaseView

abstract class BaseFragment : Fragment() {
abstract class BaseFragment : Fragment(), BaseView {
init {
retainInstance = true
}
Expand All @@ -19,4 +20,20 @@ abstract class BaseFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?) =
inflater?.inflate(layoutId(), container, false)

override fun showLoading() {
//TODO: implement method
}

override fun hideLoading() {
//TODO: implement method
}

override fun showError(e: Throwable) {
TODO("not implemented")
}

override fun dispose() {
//TODO: implement method
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fernandocejas.sample.features.base

abstract class BasePresenter<V : BaseView> : Presenter<V>() {

override fun disposeView() = view.dispose()

override fun dropView() = super.dropView()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for overriding just to call super?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My app is not a sample and I have unrelated code here, I only deleted that line.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it


override fun takeView(view: V) = super.takeView(view)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


fun initialize(view: V) {
takeView(view)
view.hideLoading()
}

fun finalize() = dropView()

open fun onComplete() = onCompleteBase()
open fun onNext(response: Any) = onNextBase(response)
open fun onError(e: Throwable) = onErrorBase(e)

private fun onCompleteBase() = view.hideLoading()
private fun onNextBase(response: Any) = view.hideLoading()
private fun onErrorBase(e: Throwable) = view.apply {
hideLoading()
showError(e)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fernandocejas.sample.features.base

interface BaseView {
fun showLoading()
fun hideLoading()
fun showError(e: Throwable)
fun dispose()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.fernandocejas.sample.features.base

abstract class Presenter<View> {

/**
* Returns a current view attached to the presenter.
*
* @return a current attached view.
*/
abstract var view: View

/**
* This method is being called when a view gets attached to it.
*
* This method is intended for overriding.
*
* @param view a view that should be taken.
*/
protected open fun onTakeView(view: View) {
}

/**
* This method is being called when a view gets detached from the presenter.
*
* This method is intended for overriding.
*/
protected open fun onDropView() {
}

/**
* Attaches a view to the presenter.
*
* @param view a view to attach.
*/
open fun takeView(view: View) {
this.view = view
onTakeView(view)
}

/**
* Detaches the presenter from a view.
*/
open fun dropView() {
onDropView()
disposeView()
}

/**
* Dispose a view.
*/
abstract fun disposeView()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class MoviesFragment : BaseFragment(), MoviesView {
@Inject lateinit var moviesPresenter: MoviesPresenter
@Inject lateinit var moviesAdapter: MoviesAdapter

override fun layoutId(): Int {
return R.layout.fragment_movies
}
override fun layoutId(): Int = R.layout.fragment_movies

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -29,8 +27,8 @@ class MoviesFragment : BaseFragment(), MoviesView {
}

override fun onDestroy() {
super.onDestroy()
moviesPresenter.destroy()
super.onDestroy()
}

override fun renderList(movies: List<MovieViewModel>) {
Expand All @@ -41,22 +39,10 @@ class MoviesFragment : BaseFragment(), MoviesView {
TODO()
}

override fun showLoading() {
//TODO: implement method
}

override fun hideLoading() {
//TODO: implement method
}

override fun dispose() {
//TODO: dispose view resources
}

private fun initializeView() {
rv_movies.layoutManager = LinearLayoutManager(this.activity.application)
rv_movies.adapter = moviesAdapter
moviesPresenter.moviesView = this
moviesPresenter.initialize(this)
}

private fun loadMovies() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.fernandocejas.sample.features.movies

import com.fernandocejas.sample.framework.interactor.UseCaseObserver
import com.fernandocejas.sample.features.base.BasePresenter
import com.fernandocejas.sample.framework.interactor.UsecaseDisposableObserver
import javax.inject.Inject

class MoviesPresenter
@Inject constructor(private val getMovies: GetMovies) {
@Inject constructor(private val getMovies: GetMovies) : BasePresenter<MoviesView>() {

internal lateinit var moviesView: MoviesView
override lateinit var view: MoviesView

fun destroy() {
getMovies.dispose()
moviesView.dispose()
finalize()
}

fun loadMovies() {
moviesView.showLoading()
getMovies.execute(MoviesObserver())
view.showLoading()
getMovies.execute(UsecaseDisposableObserver(
{ onComplete() },
{ onGetMoviesNext(it) },
{ onError(it) }))
}

private inner class MoviesObserver : UseCaseObserver<List<Movie>>() {
override fun onComplete() = moviesView.hideLoading()
override fun onNext(value: List<Movie>) = moviesView.renderList(value.map(::MovieViewModel))
override fun onError(e: Throwable) = TODO()
}
fun onGetMoviesNext(value: List<Movie>) = view.renderList(value.map(::MovieViewModel))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import com.fernandocejas.sample.framework.view.LoadingView
interface MoviesView : LoadingView {
fun renderList(movies: List<MovieViewModel>)
fun displayDetails(movie: MovieViewModel)
fun dispose()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.fernandocejas.sample.framework.interactor
import com.fernandocejas.sample.framework.executor.ExecutionScheduler
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.observers.DisposableObserver
import javax.inject.Inject

abstract class UseCase<T, in P> where P : Any {
Expand All @@ -11,7 +12,7 @@ abstract class UseCase<T, in P> where P : Any {

abstract fun buildObservable(params: P?): Observable<T>

fun execute(observer: UseCaseObserver<T>, params: P? = null) {
fun execute(observer: DisposableObserver<T>, params: P? = null) {
disposables
.add(buildObservable(params)
.subscribeWith(observer))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fernandocejas.sample.framework.interactor

import io.reactivex.observers.DisposableObserver

open class UsecaseDisposableObserver<T>(
private val onUseCaseComplete: () -> Unit = { },
private val onUseCaseNext: (T) -> Unit = { },
private val onUseCaseError: (Throwable) -> Unit = { })
: DisposableObserver<T>() {

override fun onComplete() = onUseCaseComplete()
override fun onNext(response: T) = onUseCaseNext(response)
override fun onError(e: Throwable) = onUseCaseError(e)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fernandocejas.sample.framework.view

interface LoadingView {
fun showLoading()
fun hideLoading()
}
import com.fernandocejas.sample.features.base.BaseView

interface LoadingView : BaseView