0

I want to use this single function only, but instead of UserAPIService, there may be ProductAPIService on runtime based on what user is clicking. Is that possible?

Code:

@Provides
@Singleton
fun provideRetrofitService(retrofit: Retrofit): UserAPIService = retrofit.create(UserAPIService::class.java)

UserAPIService:

    interface UserAPIService {
    @GET(URLConstants.API_CUSTOMER_REVIEWS_BY_ID + "{customerId}")
    suspend fun getReviewData(@Path("customerId") value: String): Response<ReviewResponse>
}

FLOW:

Activity/Fragment - Viewmodel - repo - remotedatasource

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • I don't see the point here . you will have two `@provides` for both and you will inject both and use any one of them upon your condition . – ADM Jun 14 '22 at 11:00
  • @ADM In entire project will it be single instance of retrofit, or will it create new one? BTW, I use single app module – Shyneil Singh Jun 14 '22 at 11:03
  • It will be same Since `Retrofit` is another object you must be providing as `@Sigleton` i suppose . Do u need diffrent instance of Retrofit ? Alos u might wanna add some more context to your question right its not much clear what you want to do and why . – ADM Jun 14 '22 at 11:05
  • @ADM Basically I want single instance, but it should create based on user selection. Let's say if user is opening product related page, then ProductAPIService should use here: retrofit.create(UserAPIService::class.java). .....like this – Shyneil Singh Jun 14 '22 at 11:10
  • @ADM see this accepted answer, in that 2 fun are written for diff service, but I want single fun with diff service: https://stackoverflow.com/a/68666959/18865388 – Shyneil Singh Jun 14 '22 at 11:15
  • I stand with my previous comment then . provide and inject both(both will use same retrofit instance) . if you need multiple instances of same object type you can always go with [@Named](https://stackoverflow.com/a/35884040/4168607) – ADM Jun 14 '22 at 11:16
  • I need single instance with diff obj based on user action. IS that possible with single provide function? Is there any trick? @ADM – Shyneil Singh Jun 14 '22 at 11:21

0 Answers0