https://kotlinlang.org logo
#android
Title
# android
j

Joey

01/17/2020, 4:35 AM
What layer should i call my service in an MVP architecture? In the view? presenter? or model?
t

Tim Malseed

01/17/2020, 4:38 AM
The Presenter
👍 1
j

Joey

01/17/2020, 4:38 AM
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

Tim Malseed

01/17/2020, 5:03 AM
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

Joey

01/17/2020, 5:28 AM
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

Tim Malseed

01/17/2020, 6:29 AM
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

Joey

01/17/2020, 6:39 AM
Thanks a lot! @Tim Malseed appreciate the help! B-)
19 Views