Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8297a2a0 authored by Sayantan Roychowdhury's avatar Sayantan Roychowdhury
Browse files

use retrofit api to get age groups

parent 29d72686
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
/*
 *  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 <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.data.ageRating

import foundation.e.apps.data.blockedApps.ContentRatingGroup
import retrofit2.Response
import retrofit2.http.GET

interface AgeGroupApi {

    companion object {
        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<List<ContentRatingGroup>>

}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -19,11 +19,11 @@

package foundation.e.apps.data.blockedApps

import com.google.gson.annotations.SerializedName
import com.squareup.moshi.Json

data class ContentRatingGroup(
    val id: String,
    @SerializedName("age_group")
    @Json(name = "age_group")
    val ageGroup: String,
    var ratings: List<String>
)
+6 −25
Original line number Diff line number Diff line
@@ -19,42 +19,23 @@

package foundation.e.apps.data.blockedApps

import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import foundation.e.apps.data.DownloadManager
import foundation.e.apps.data.install.FileManager
import timber.log.Timber
import java.io.File
import foundation.e.apps.data.ageRating.AgeGroupApi
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

@Singleton
class ContentRatingsRepository @Inject constructor(
    private val downloadManager: DownloadManager,
    private val contentRatingParser: ContentRatingParser
    private val ageGroupApi: AgeGroupApi,
) {

    private var _contentRatingGroups = listOf<ContentRatingGroup>()
    val contentRatingGroups: List<ContentRatingGroup>
        get() = _contentRatingGroups

    companion object {
        private const val CONTENT_RATINGS_FILE_URL =
            "https://gitlab.e.foundation/e/os/app-lounge-content-ratings/-/raw/main/" +
                    "content_ratings.json?ref_type=heads&inline=false"
        private const val CONTENT_RATINGS_FILE_NAME = "content_ratings.json"
    }

    fun fetchContentRatingData() {
        downloadManager.downloadFileInCache(
            CONTENT_RATINGS_FILE_URL,
            fileName = CONTENT_RATINGS_FILE_NAME
        ) { success, _ ->
            if (success) {
                _contentRatingGroups = contentRatingParser.parseContentRatingData()
            }
    suspend fun fetchContentRatingData() {
        val response = ageGroupApi.getDefinedAgeGroups()
        if (response.isSuccessful) {
            _contentRatingGroups = response.body() ?: emptyList()
        }
    }
}
+12 −6
Original line number Diff line number Diff line
@@ -29,23 +29,18 @@ 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.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import timber.log.Timber
import java.net.ConnectException
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.inject.Named
@@ -131,6 +126,17 @@ object RetrofitModule {
            .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 getMoshi(): Moshi {