Repository: Automattic/pocket-casts-android
File: modules/services/servers/src/main/java/au/com/shiftyjelly/pocketcasts/servers/di/ServersModule.kt (Kotlin)

Code before:
            return Retrofit.Builder()
                .addConverterFactory(ProtoConverterFactory.create())
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build()
        }
    }

    @Provides
    @Singleton
    fun provideMoshi(): Moshi {
        return Moshi.Builder()
            .add(InstantAdapter())
            .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
            .add(SyncUpdateResponse::class.java, SyncUpdateResponseParser())
            .add(EpisodePlayingStatus::class.java, EpisodePlayingStatusMoshiAdapter())
            .add(PodcastsSortType::class.java, PodcastsSortTypeMoshiAdapter())
            .add(AccessToken::class.java, AccessToken.Adapter)
            .add(RefreshToken::class.java, RefreshToken.Adapter)
            .add(BlazeAdLocation::class.java, BlazeAdLocationMoshiAdapter())
            .add(
                PolymorphicJsonAdapterFactory.of(AutoCompleteResult::class.java, "type")
                    .withSubtype(AutoCompleteResult.TermResult::class.java, "term")
                    .withSubtype(AutoCompleteResult.PodcastResult::class.java, "podcast")
            )
            .add(AnonymousBumpStat.Adapter)
            .add(LoginIdentity.Adapter)
            .add(ListTypeMoshiAdapter())
            .add(DisplayStyleMoshiAdapter())
            .add(ExpandedStyleMoshiAdapter())
            .build()
    }

    @Provides
    @Singleton
    @Raw
    fun provideRawClient(): OkHttpClient {
        return OkHttpClient()
    }

    @Provides
    @Cached
    fun provideCachedCache(@ApplicationContext context: Context): Cache {
        return createCache(folder = "HttpCache", context = context, cacheSizeInMB = 10)
    }

    @Provides
    @Singleton
    @Cached

Review comment (suggestion):
💡 I would consider putting it as a property of a companion object for `AutoCompleteResult`. This way it will be more visible if any changes are needed due to new search result types, etc.

Alternatively we could consider adding `moshi-sealed` to automate adapter generation - https://github.com/ZacSweers/MoshiX/tree/main/moshi-sealed. I used it in the past for some projects and it provides some nice safety features.

Code after:
                .addConverterFactory(ProtoConverterFactory.create())
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build()
        }
    }

    @Provides
    @Singleton
    fun provideMoshi(): Moshi {
        return Moshi.Builder()
            .add(InstantAdapter())
            .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
            .add(SyncUpdateResponse::class.java, SyncUpdateResponseParser())
            .add(EpisodePlayingStatus::class.java, EpisodePlayingStatusMoshiAdapter())
            .add(PodcastsSortType::class.java, PodcastsSortTypeMoshiAdapter())
            .add(AccessToken::class.java, AccessToken.Adapter)
            .add(RefreshToken::class.java, RefreshToken.Adapter)
            .add(BlazeAdLocation::class.java, BlazeAdLocationMoshiAdapter())
            .add(AutoCompleteResult.jsonAdapter)
            .add(AnonymousBumpStat.Adapter)
            .add(LoginIdentity.Adapter)
            .add(ListTypeMoshiAdapter())
            .add(DisplayStyleMoshiAdapter())
            .add(ExpandedStyleMoshiAdapter())
            .build()
    }

    @Provides
    @Singleton
    @Raw
    fun provideRawClient(): OkHttpClient {
        return OkHttpClient()
    }

    @Provides
    @Cached
    fun provideCachedCache(@ApplicationContext context: Context): Cache {
        return createCache(folder = "HttpCache", context = context, cacheSizeInMB = 10)
    }

    @Provides
    @Singleton
    @Cached
    fun provideCachedClient(
        @Raw client: OkHttpClient,
        @Cached cache: Cache,
        @Cached interceptors: List<@JvmSuppressWildcards OkHttpInterceptor>,
    ): OkHttpClient {