-
Notifications
You must be signed in to change notification settings - Fork 920
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
albodelu
wants to merge
1
commit into
android10:master
from
albodelu:pr-add-base-presenter-and-alternative-disposable-observer
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/com/fernandocejas/sample/features/base/BasePresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
override fun takeView(view: V) = super.takeView(view) | ||
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. Same here 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. 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) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/kotlin/com/fernandocejas/sample/features/base/BaseView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/kotlin/com/fernandocejas/sample/features/base/Presenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
app/src/main/kotlin/com/fernandocejas/sample/features/movies/MoviesPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...rc/main/kotlin/com/fernandocejas/sample/framework/interactor/UsecaseDisposableObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
7 changes: 3 additions & 4 deletions
7
app/src/main/kotlin/com/fernandocejas/sample/framework/view/LoadingView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What's the reason for overriding just to call super?
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.
My app is not a sample and I have unrelated code here, I only deleted that line.
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.
got it