What layer should i call my service in an MVP arch...
# android
j
What layer should i call my service in an MVP architecture? In the view? presenter? or model?
t
The Presenter
👍 1
j
Thanks!
Oh wait @Tim Malseed, i should pass my context to the presenter? Passing the context to my presenter will break the mvp logic right? Sorry this man is a bit confused 🤯
t
You’re right that it’s not a great idea - you want to keep the Presenter as dumb as possible
If your service requires a context, then you should initialise your service in your view (with the context), and then pass it into your presenter.
Extra points if you use Dependency Injection to solve this problem
j
So what i did is this
Copy code
interface IHomeView {
    fun onServiceStart()
    fun onServiceStop()
}
Copy code
class HomePresenterImpl(internal var iHomeView: IHomeView):
    IHomePresenter {
    override fun serviceStart() {
        iHomeView.onServiceStart()
    }
and in my activity
Copy code
override fun onServiceStart() {
        ForegroundHeartbeatService.startService(context!!, Constants.FOREGROUND_SERVICE_MESSAGE)
    }
Is this right @Tim Malseed
t
Oh, you’re talking about an Android
Service
, ha, sorry I thought you were referring to an Api Service of some kind
😄 1
Yep, that’s how I would do it
By the way, I would probably call this
startService()
rather than
onServiceStart()
.
onServiceStart()
implies the service was started and this is a callback.
startService()
sounds more like an action.
j
Thanks a lot! @Tim Malseed appreciate the help! B-)