From 4c9d895546ccb3389cefe76fb4e65e163e28be50 Mon Sep 17 00:00:00 2001 From: Fahim Masud Choudhury Date: Tue, 25 Jun 2024 17:29:32 +0600 Subject: [PATCH 1/3] feat: add network module and update retrofit module This commit includes the addition of a network module and updates to the retrofit module. The newly added network module includes the configuration for OkHttpClient and different deserializers. Some of the deserializers added include Moshi, Gson, and JacksonConverterFactory for YAML files. In addition to this, the retrofit module has been updated to include different APIs such as CleanApkRetrofit, CleanApkAppDetailsRetrofit, ExodusTrackerApi, FdroidApiInterface, EcloudApiInterface, AgeGroupApi, and FDroidMonitorApi. Furthermore, there are also some changes in the directory structure of the codebase. The ageRating package has been renamed to parentalcontrol, and the RetrofitModule has been moved to the network package. Lastly, the commit includes the addition of the okhttp3 logging interceptor in the build.gradle file. --- app/build.gradle | 1 + .../e/apps/data/ageRating/AgeGroupApi.kt | 35 -- .../e/apps/data/ageRating/FDroidMonitorApi.kt | 34 -- .../apps/data/ageRating/FDroidMonitorData.kt | 36 -- .../blockedApps/ContentRatingsRepository.kt | 29 +- .../apps/data/parentalcontrol/AgeGroupApi.kt | 34 ++ .../data/parentalcontrol/FDroidMonitorApi.kt | 33 ++ .../data/parentalcontrol/FDroidMonitorData.kt | 35 ++ .../foundation/e/apps/di/AgeRatingModule.kt | 59 ---- .../e/apps/di/network/InterceptorModule.kt | 73 ++++ .../e/apps/di/network/NetworkModule.kt | 91 +++++ .../network/RetrofitApiModule.kt} | 323 ++++++++---------- 12 files changed, 422 insertions(+), 361 deletions(-) delete mode 100644 app/src/main/java/foundation/e/apps/data/ageRating/AgeGroupApi.kt delete mode 100644 app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorApi.kt delete mode 100644 app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorData.kt create mode 100644 app/src/main/java/foundation/e/apps/data/parentalcontrol/AgeGroupApi.kt create mode 100644 app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorApi.kt create mode 100644 app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorData.kt delete mode 100644 app/src/main/java/foundation/e/apps/di/AgeRatingModule.kt create mode 100644 app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt create mode 100644 app/src/main/java/foundation/e/apps/di/network/NetworkModule.kt rename app/src/main/java/foundation/e/apps/{data/cleanapk/RetrofitModule.kt => di/network/RetrofitApiModule.kt} (62%) diff --git a/app/build.gradle b/app/build.gradle index dc93b5e1f..50b88b167 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -219,6 +219,7 @@ dependencies { implementation "com.squareup.moshi:moshi-kotlin:1.13.0" // implementation "com.squareup.moshi:moshi-adapters:1.5.0" implementation "com.squareup.okhttp3:okhttp:4.9.2" + implementation "com.squareup.okhttp3:logging-interceptor:4.9.2" // JSON Converter implementation 'com.squareup.retrofit2:converter-gson:2.5.0' diff --git a/app/src/main/java/foundation/e/apps/data/ageRating/AgeGroupApi.kt b/app/src/main/java/foundation/e/apps/data/ageRating/AgeGroupApi.kt deleted file mode 100644 index 9a3d04694..000000000 --- a/app/src/main/java/foundation/e/apps/data/ageRating/AgeGroupApi.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright MURENA SAS 2024 - * Apps Quickly and easily install Android apps onto your device! - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package foundation.e.apps.data.ageRating - -import foundation.e.apps.data.blockedApps.ContentRatingGroup -import retrofit2.Response -import retrofit2.http.GET - -interface AgeGroupApi { - - companion object { - const val BASE_URL = "https://gitlab.e.foundation/e/os/app-lounge-content-ratings/-/raw/main/" - } - - @GET("content_ratings.json?ref_type=heads") - suspend fun getDefinedAgeGroups(): Response> - -} diff --git a/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorApi.kt b/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorApi.kt deleted file mode 100644 index ed9de2185..000000000 --- a/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorApi.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright MURENA SAS 2024 - * Apps Quickly and easily install Android apps onto your device! - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package foundation.e.apps.data.ageRating - -import retrofit2.Response -import retrofit2.http.GET - -interface FDroidMonitorApi { - - companion object { - const val BASE_URL = "https://f-droid.org/repo/" - } - - @GET("status/update.json") - suspend fun getMonitorData(): Response - -} diff --git a/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorData.kt b/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorData.kt deleted file mode 100644 index 762e60213..000000000 --- a/app/src/main/java/foundation/e/apps/data/ageRating/FDroidMonitorData.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright MURENA SAS 2024 - * Apps Quickly and easily install Android apps onto your device! - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package foundation.e.apps.data.ageRating - -import com.squareup.moshi.Json - -data class FDroidMonitorData( - val antiFeatures: AntiFeatures -) { - fun getNSFWApps() = antiFeatures.nsfw.apps -} - -data class AntiFeatures( - @Json(name = "NSFW") val nsfw: NSFW -) - -data class NSFW( - val apps: List -) diff --git a/app/src/main/java/foundation/e/apps/data/blockedApps/ContentRatingsRepository.kt b/app/src/main/java/foundation/e/apps/data/blockedApps/ContentRatingsRepository.kt index 7804d0948..b09fbb43c 100644 --- a/app/src/main/java/foundation/e/apps/data/blockedApps/ContentRatingsRepository.kt +++ b/app/src/main/java/foundation/e/apps/data/blockedApps/ContentRatingsRepository.kt @@ -1,30 +1,27 @@ /* - * Copyright MURENA SAS 2024 - * Apps Quickly and easily install Android apps onto your device! + * Copyright (C) 2024 MURENA SAS * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * */ package foundation.e.apps.data.blockedApps import com.aurora.gplayapi.data.models.ContentRating -import com.aurora.gplayapi.helpers.ContentRatingHelper -import foundation.e.apps.data.ageRating.AgeGroupApi -import foundation.e.apps.data.ageRating.FDroidMonitorApi +import foundation.e.apps.data.parentalcontrol.AgeGroupApi +import foundation.e.apps.data.parentalcontrol.FDroidMonitorApi import foundation.e.apps.data.handleNetworkResult -import foundation.e.apps.data.login.AuthenticatorRepository import foundation.e.apps.data.playstore.PlayStoreRepository import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/foundation/e/apps/data/parentalcontrol/AgeGroupApi.kt b/app/src/main/java/foundation/e/apps/data/parentalcontrol/AgeGroupApi.kt new file mode 100644 index 000000000..7ccc00550 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/data/parentalcontrol/AgeGroupApi.kt @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.data.parentalcontrol + +import foundation.e.apps.data.blockedApps.ContentRatingGroup +import retrofit2.Response +import retrofit2.http.GET + +interface AgeGroupApi { + + companion object { + const val BASE_URL = "https://gitlab.e.foundation/e/os/app-lounge-content-ratings/-/raw/main/" + } + + @GET("content_ratings.json?ref_type=heads") + suspend fun getDefinedAgeGroups(): Response> + +} diff --git a/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorApi.kt b/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorApi.kt new file mode 100644 index 000000000..2d36c5d05 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorApi.kt @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.data.parentalcontrol + +import retrofit2.Response +import retrofit2.http.GET + +interface FDroidMonitorApi { + + companion object { + const val BASE_URL = "https://f-droid.org/repo/" + } + + @GET("status/update.json") + suspend fun getMonitorData(): Response + +} diff --git a/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorData.kt b/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorData.kt new file mode 100644 index 000000000..b30c61794 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/data/parentalcontrol/FDroidMonitorData.kt @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.data.parentalcontrol + +import com.squareup.moshi.Json + +data class FDroidMonitorData( + val antiFeatures: AntiFeatures +) { + fun getNSFWApps() = antiFeatures.nsfw.apps +} + +data class AntiFeatures( + @Json(name = "NSFW") val nsfw: NSFW +) + +data class NSFW( + val apps: List +) diff --git a/app/src/main/java/foundation/e/apps/di/AgeRatingModule.kt b/app/src/main/java/foundation/e/apps/di/AgeRatingModule.kt deleted file mode 100644 index b47981ec9..000000000 --- a/app/src/main/java/foundation/e/apps/di/AgeRatingModule.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright MURENA SAS 2024 - * Apps Quickly and easily install Android apps onto your device! - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package foundation.e.apps.di - -import com.squareup.moshi.Moshi -import dagger.Module -import dagger.Provides -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent -import foundation.e.apps.data.ageRating.AgeGroupApi -import foundation.e.apps.data.ageRating.FDroidMonitorApi -import javax.inject.Singleton -import okhttp3.OkHttpClient -import retrofit2.Retrofit -import retrofit2.converter.moshi.MoshiConverterFactory - -@Module -@InstallIn(SingletonComponent::class) -object AgeRatingModule { - - @Singleton - @Provides - fun provideAgeGroupApi(okHttpClient: OkHttpClient, moshi: Moshi): AgeGroupApi { - return Retrofit.Builder() - .baseUrl(AgeGroupApi.BASE_URL) - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - .create(AgeGroupApi::class.java) - } - - @Singleton - @Provides - fun provideFDroidMonitorApi(okHttpClient: OkHttpClient, moshi: Moshi): FDroidMonitorApi { - return Retrofit.Builder() - .baseUrl(FDroidMonitorApi.BASE_URL) - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - .create(FDroidMonitorApi::class.java) - } -} diff --git a/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt b/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt new file mode 100644 index 000000000..b504c7a26 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.di.network + +import android.os.Build +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import foundation.e.apps.BuildConfig +import java.util.Locale +import javax.inject.Singleton +import okhttp3.Interceptor +import okhttp3.logging.HttpLoggingInterceptor + +@Module +@InstallIn(SingletonComponent::class) +class InterceptorModule { + + companion object { + private const val HEADER_USER_AGENT = "User-Agent" + private const val HEADER_ACCEPT_LANGUAGE = "Accept-Language" + } + + @Singleton + @Provides + fun provideInterceptor(): Interceptor { + return Interceptor { chain -> + val builder = + chain + .request() + .newBuilder() + .header( + HEADER_USER_AGENT, + "Dalvik/2.1.0 (Linux; U; Android ${Build.VERSION.RELEASE};)") + .header(HEADER_ACCEPT_LANGUAGE, Locale.getDefault().language) + + val response = chain.proceed(builder.build()) + + return@Interceptor response + } + } + + @Provides + @Singleton + fun provideLoggingInterceptor(): HttpLoggingInterceptor { + val interceptor = HttpLoggingInterceptor() + + interceptor.level = + when { + BuildConfig.DEBUG -> HttpLoggingInterceptor.Level.BODY + else -> HttpLoggingInterceptor.Level.NONE + } + + return interceptor + } +} diff --git a/app/src/main/java/foundation/e/apps/di/network/NetworkModule.kt b/app/src/main/java/foundation/e/apps/di/network/NetworkModule.kt new file mode 100644 index 000000000..6fbf75e99 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/di/network/NetworkModule.kt @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021-2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.di.network + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import foundation.e.apps.data.cleanapk.ApplicationDeserializer +import foundation.e.apps.data.cleanapk.data.app.Application +import okhttp3.Cache +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import retrofit2.converter.jackson.JacksonConverterFactory +import java.util.concurrent.TimeUnit +import javax.inject.Named +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +object NetworkModule { + + private const val HTTP_TIMEOUT_IN_SECOND = 10L + + @Singleton + @Provides + fun getMoshi(): Moshi { + return Moshi.Builder() + .add(KotlinJsonAdapterFactory()) + .build() + } + + @Singleton + @Provides + @Named("gsonCustomAdapter") + fun getGson(): Gson { + return GsonBuilder() + .registerTypeAdapter(Application::class.java, ApplicationDeserializer()) + .enableComplexMapKeySerialization() + .create() + } + + /** + * Used in [RetrofitApiModule.provideFdroidApi]. + * Reference: https://stackoverflow.com/a/69859687 + */ + @Singleton + @Provides + @Named("yamlFactory") + fun getYamlFactory(): JacksonConverterFactory { + return JacksonConverterFactory.create(ObjectMapper(YAMLFactory())) + } + + @Singleton + @Provides + fun provideOkHttpClient( + cache: Cache, + interceptor: Interceptor, + httpLoggingInterceptor: HttpLoggingInterceptor + ): OkHttpClient { + return OkHttpClient.Builder() + .addInterceptor(interceptor) + .addInterceptor(httpLoggingInterceptor) // Put logging interceptor last + .callTimeout(HTTP_TIMEOUT_IN_SECOND, TimeUnit.SECONDS) + .cache(cache) + .build() + } +} diff --git a/app/src/main/java/foundation/e/apps/data/cleanapk/RetrofitModule.kt b/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt similarity index 62% rename from app/src/main/java/foundation/e/apps/data/cleanapk/RetrofitModule.kt rename to app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt index 3a2d832d8..bf3f19999 100644 --- a/app/src/main/java/foundation/e/apps/data/cleanapk/RetrofitModule.kt +++ b/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt @@ -1,181 +1,142 @@ -/* - * Apps Quickly and easily install Android apps onto your device! - * Copyright (C) 2021 E FOUNDATION - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package foundation.e.apps.data.cleanapk - -import android.os.Build -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory -import com.google.gson.Gson -import com.google.gson.GsonBuilder -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory -import dagger.Module -import dagger.Provides -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent -import foundation.e.apps.data.ageRating.AgeGroupApi -import foundation.e.apps.data.cleanapk.data.app.Application -import foundation.e.apps.data.ecloud.EcloudApiInterface -import foundation.e.apps.data.exodus.ExodusTrackerApi -import foundation.e.apps.data.fdroid.FdroidApiInterface -import okhttp3.Cache -import okhttp3.Interceptor -import okhttp3.OkHttpClient -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory -import retrofit2.converter.jackson.JacksonConverterFactory -import retrofit2.converter.moshi.MoshiConverterFactory -import java.util.Locale -import java.util.concurrent.TimeUnit -import javax.inject.Named -import javax.inject.Singleton - -@Module -@InstallIn(SingletonComponent::class) -object RetrofitModule { - - private const val HTTP_TIMEOUT_IN_SECOND = 10L - - /** - * Provides an instance of Retrofit to work with CleanAPK API - * @return instance of [CleanApkRetrofit] - */ - @Singleton - @Provides - fun provideCleanAPKInterface(okHttpClient: OkHttpClient, moshi: Moshi): CleanApkRetrofit { - return Retrofit.Builder() - .baseUrl(CleanApkRetrofit.BASE_URL) - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - .create(CleanApkRetrofit::class.java) - } - - /** - * Provides an instance of Retrofit to work with CleanAPK API - * @return instance of [CleanApkAppDetailsRetrofit] - */ - @Singleton - @Provides - fun provideCleanAPKDetailApi( - okHttpClient: OkHttpClient, - @Named("gsonCustomAdapter") gson: Gson - ): CleanApkAppDetailsRetrofit { - return Retrofit.Builder() - .baseUrl(CleanApkRetrofit.BASE_URL) - .client(okHttpClient) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() - .create(CleanApkAppDetailsRetrofit::class.java) - } - - @Singleton - @Provides - fun provideExodusApi(okHttpClient: OkHttpClient, moshi: Moshi): ExodusTrackerApi { - return Retrofit.Builder() - .baseUrl(ExodusTrackerApi.BASE_URL) - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - .create(ExodusTrackerApi::class.java) - } - - /** - * The fdroid api returns results in .yaml format. - * Hence we need a yaml convertor. - * Convertor is being provided by [getYamlFactory]. - */ - @Singleton - @Provides - fun provideFdroidApi( - okHttpClient: OkHttpClient, - @Named("yamlFactory") yamlFactory: JacksonConverterFactory - ): FdroidApiInterface { - return Retrofit.Builder() - .baseUrl(FdroidApiInterface.BASE_URL) - .client(okHttpClient) - .addConverterFactory(yamlFactory) - .build() - .create(FdroidApiInterface::class.java) - } - - @Singleton - @Provides - fun provideEcloudApi(okHttpClient: OkHttpClient, moshi: Moshi): EcloudApiInterface { - return Retrofit.Builder() - .baseUrl(EcloudApiInterface.BASE_URL) - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - .create(EcloudApiInterface::class.java) - } - - @Singleton - @Provides - fun getMoshi(): Moshi { - return Moshi.Builder() - .add(KotlinJsonAdapterFactory()) - .build() - } - - @Singleton - @Provides - @Named("gsonCustomAdapter") - fun getGson(): Gson { - return GsonBuilder() - .registerTypeAdapter(Application::class.java, ApplicationDeserializer()) - .enableComplexMapKeySerialization() - .create() - } - - /** - * Used in above [provideFdroidApi]. - * Reference: https://stackoverflow.com/a/69859687 - */ - @Singleton - @Provides - @Named("yamlFactory") - fun getYamlFactory(): JacksonConverterFactory { - return JacksonConverterFactory.create(ObjectMapper(YAMLFactory())) - } - - @Singleton - @Provides - fun provideInterceptor(): Interceptor { - return Interceptor { chain -> - val builder = chain.request().newBuilder() - builder.header( - "User-Agent", - "Dalvik/2.1.0 (Linux; U; Android ${Build.VERSION.RELEASE};)" - ).header("Accept-Language", Locale.getDefault().language) - - return@Interceptor chain.proceed(builder.build()) - } - } - - @Singleton - @Provides - fun provideOkHttpClient(cache: Cache, interceptor: Interceptor): OkHttpClient { - return OkHttpClient.Builder() - .addInterceptor(interceptor) - .callTimeout(HTTP_TIMEOUT_IN_SECOND, TimeUnit.SECONDS) - .cache(cache) - .build() - } -} +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package foundation.e.apps.di.network + +import com.google.gson.Gson +import com.squareup.moshi.Moshi +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import foundation.e.apps.data.cleanapk.CleanApkAppDetailsRetrofit +import foundation.e.apps.data.cleanapk.CleanApkRetrofit +import foundation.e.apps.data.ecloud.EcloudApiInterface +import foundation.e.apps.data.exodus.ExodusTrackerApi +import foundation.e.apps.data.fdroid.FdroidApiInterface +import foundation.e.apps.di.network.NetworkModule.getYamlFactory +import foundation.e.apps.data.parentalcontrol.AgeGroupApi +import foundation.e.apps.data.parentalcontrol.FDroidMonitorApi +import okhttp3.OkHttpClient +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.converter.jackson.JacksonConverterFactory +import retrofit2.converter.moshi.MoshiConverterFactory +import javax.inject.Named +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +class RetrofitApiModule { + /** + * Provides an instance of Retrofit to work with CleanAPK API + * @return instance of [CleanApkRetrofit] + */ + @Singleton + @Provides + fun provideCleanApkInterface(okHttpClient: OkHttpClient, moshi: Moshi): CleanApkRetrofit { + return Retrofit.Builder() + .baseUrl(CleanApkRetrofit.BASE_URL) + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + .create(CleanApkRetrofit::class.java) + } + + /** + * Provides an instance of Retrofit to work with CleanAPK API + * @return instance of [CleanApkAppDetailsRetrofit] + */ + @Singleton + @Provides + fun provideCleanApkDetailApi( + okHttpClient: OkHttpClient, + @Named("gsonCustomAdapter") gson: Gson + ): CleanApkAppDetailsRetrofit { + return Retrofit.Builder() + .baseUrl(CleanApkRetrofit.BASE_URL) + .client(okHttpClient) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(CleanApkAppDetailsRetrofit::class.java) + } + + @Singleton + @Provides + fun provideExodusApi(okHttpClient: OkHttpClient, moshi: Moshi): ExodusTrackerApi { + return Retrofit.Builder() + .baseUrl(ExodusTrackerApi.BASE_URL) + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + .create(ExodusTrackerApi::class.java) + } + + /** + * The fdroid api returns results in .yaml format. + * Hence we need a yaml convertor. + * Convertor is being provided by [getYamlFactory]. + */ + @Singleton + @Provides + fun provideFdroidApi( + okHttpClient: OkHttpClient, + @Named("yamlFactory") yamlFactory: JacksonConverterFactory + ): FdroidApiInterface { + return Retrofit.Builder() + .baseUrl(FdroidApiInterface.BASE_URL) + .client(okHttpClient) + .addConverterFactory(yamlFactory) + .build() + .create(FdroidApiInterface::class.java) + } + + @Singleton + @Provides + fun provideEcloudApi(okHttpClient: OkHttpClient, moshi: Moshi): EcloudApiInterface { + return Retrofit.Builder() + .baseUrl(EcloudApiInterface.BASE_URL) + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + .create(EcloudApiInterface::class.java) + } + + @Singleton + @Provides + fun provideAgeGroupApi(okHttpClient: OkHttpClient, moshi: Moshi): AgeGroupApi { + return Retrofit.Builder() + .baseUrl(AgeGroupApi.BASE_URL) + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + .create(AgeGroupApi::class.java) + } + + @Singleton + @Provides + fun provideFDroidMonitorApi(okHttpClient: OkHttpClient, moshi: Moshi): FDroidMonitorApi { + return Retrofit.Builder() + .baseUrl(FDroidMonitorApi.BASE_URL) + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + .create(FDroidMonitorApi::class.java) + } + +} -- GitLab From 43e315a4fa4bf154fd05a1ca00ca2ab3d1bb5271 Mon Sep 17 00:00:00 2001 From: Fahim Masud Choudhury Date: Wed, 26 Jun 2024 11:36:07 +0600 Subject: [PATCH 2/3] refactor: refactor InterceptorModule for better readability Optimized InterceptorModule.kt by reducing the lines of code and improving readability. The creation of the HttpLoggingInterceptor instance has been simplified with Kotlin's apply function. Also, imports have been re-ordered. --- .../e/apps/di/network/InterceptorModule.kt | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt b/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt index b504c7a26..476e5f7d8 100644 --- a/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt +++ b/app/src/main/java/foundation/e/apps/di/network/InterceptorModule.kt @@ -24,10 +24,10 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import foundation.e.apps.BuildConfig -import java.util.Locale -import javax.inject.Singleton import okhttp3.Interceptor import okhttp3.logging.HttpLoggingInterceptor +import java.util.Locale +import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) @@ -60,14 +60,11 @@ class InterceptorModule { @Provides @Singleton fun provideLoggingInterceptor(): HttpLoggingInterceptor { - val interceptor = HttpLoggingInterceptor() - - interceptor.level = - when { + return HttpLoggingInterceptor().apply { + level = when { BuildConfig.DEBUG -> HttpLoggingInterceptor.Level.BODY else -> HttpLoggingInterceptor.Level.NONE } - - return interceptor + } } } -- GitLab From 4d4677c25d5f49c6d67ceee7b91b64e4970fa1c4 Mon Sep 17 00:00:00 2001 From: Fahim Masud Choudhury Date: Wed, 26 Jun 2024 11:49:12 +0600 Subject: [PATCH 3/3] refactor: refactor Retrofit API module and FdroidApiInterface Refactored the FdroidApiInterface and the RetrofitApiModule in the Android project. Updated the documentation comments in the FdroidApiInterface to reflect the new method in the RetrofitApiModule that it was created from. Also cleaned up the RetrofitApiModule by removing redundant comments and renaming the methods to more accurately describe their functionality. --- .../e/apps/data/fdroid/FdroidApiInterface.kt | 20 ++++++++++++++++++- .../e/apps/di/network/RetrofitApiModule.kt | 12 ++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/fdroid/FdroidApiInterface.kt b/app/src/main/java/foundation/e/apps/data/fdroid/FdroidApiInterface.kt index 0480873f1..120064d8e 100644 --- a/app/src/main/java/foundation/e/apps/data/fdroid/FdroidApiInterface.kt +++ b/app/src/main/java/foundation/e/apps/data/fdroid/FdroidApiInterface.kt @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2024 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + package foundation.e.apps.data.fdroid import foundation.e.apps.data.fdroid.models.FdroidApiModel @@ -7,7 +25,7 @@ import retrofit2.http.Path /** * Interface for retrofit calls. - * Created from [foundation.e.apps.data.cleanapk.RetrofitModule.provideFdroidApi]. + * Created from [foundation.e.apps.di.network.RetrofitApiModule.provideFdroidApi]. */ interface FdroidApiInterface { diff --git a/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt b/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt index bf3f19999..f76699090 100644 --- a/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt +++ b/app/src/main/java/foundation/e/apps/di/network/RetrofitApiModule.kt @@ -43,13 +43,9 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) class RetrofitApiModule { - /** - * Provides an instance of Retrofit to work with CleanAPK API - * @return instance of [CleanApkRetrofit] - */ @Singleton @Provides - fun provideCleanApkInterface(okHttpClient: OkHttpClient, moshi: Moshi): CleanApkRetrofit { + fun provideCleanApkApi(okHttpClient: OkHttpClient, moshi: Moshi): CleanApkRetrofit { return Retrofit.Builder() .baseUrl(CleanApkRetrofit.BASE_URL) .client(okHttpClient) @@ -58,13 +54,9 @@ class RetrofitApiModule { .create(CleanApkRetrofit::class.java) } - /** - * Provides an instance of Retrofit to work with CleanAPK API - * @return instance of [CleanApkAppDetailsRetrofit] - */ @Singleton @Provides - fun provideCleanApkDetailApi( + fun provideCleanApkAppDetailsApi( okHttpClient: OkHttpClient, @Named("gsonCustomAdapter") gson: Gson ): CleanApkAppDetailsRetrofit { -- GitLab