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

Commit 7e95f775 authored by Nishant D's avatar Nishant D
Browse files

Merge branch '1401-refactor-network-layer' into 'epic103-improve_app_lounge'

1401 refactor network layer

See merge request !331
parents 71f0b0f7 d3400d52
Loading
Loading
Loading
Loading
Loading
+43 −1
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * 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/>.
 */


plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id("com.google.dagger.hilt.android")
}

ext {
@@ -8,7 +29,14 @@ ext {
    android_target_sdk_version = 33
    core_version = '1.10.1'
    gson_version = '2.9.0'
    kotlin_reflection = '1.8.10'
    kotlin_reflection = '1.8.20'
    retrofit_version = '2.9.0'
    retrofit_interceptor_version = '5.0.0-alpha.2'
    kotlin_coroutines_core = '1.7.2'
    google_play_api = '3.0.1'
    protobuf_java = '3.19.3'
    javax_version = '1'
    dagger_hilt_version = '2.46.1'
}

android {
@@ -43,6 +71,20 @@ dependencies {
    implementation "androidx.core:core-ktx:$core_version"
    implementation "com.google.code.gson:gson:$gson_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_reflection"
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_core")

    implementation("com.squareup.retrofit2:retrofit:$retrofit_version")
    implementation("com.squareup.retrofit2:converter-gson:$retrofit_version")
    implementation("com.squareup.retrofit2:converter-scalars:$retrofit_version")
    implementation("com.squareup.okhttp3:logging-interceptor:$retrofit_interceptor_version")

    implementation("foundation.e:gplayapi:$google_play_api")
    implementation("com.google.protobuf:protobuf-java:$protobuf_java")

    implementation("javax.inject:javax.inject:$javax_version")

    implementation("com.google.dagger:hilt-android:$dagger_hilt_version")
    kapt("com.google.dagger:hilt-android-compiler:$dagger_hilt_version")

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
+2 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>
 No newline at end of file
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * 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 app.lounge.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal object NetworkModule {

    private const val HTTP_TIMEOUT = 10L

    @Provides
    @Singleton
    internal fun providesRetrofit(
        okHttpClient: OkHttpClient,
        baseUrl: String
    ) : Retrofit {
        return Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build()
    }

    @Provides
    @Singleton
    @Named("privateOkHttpClient")
    internal fun providesOkHttpClient(
        httpLogger: HttpLoggingInterceptor
    ): OkHttpClient {
        return OkHttpClient.Builder()
            .addNetworkInterceptor(httpLogger)
            .callTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
            .build()
    }

    @Provides
    @Singleton
    internal fun providesHttpLogger() : HttpLoggingInterceptor {
        return run {
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.apply {
                httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            }
        }
    }

}
 No newline at end of file
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * 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 app.lounge.networking

import retrofit2.Response

sealed interface NetworkResult<T> {
    data class Success<T>(val data: T) : NetworkResult<T>
    data class Error<T>(
        val exception: Throwable,
        val code: Int,
        val errorMessage: String,
    ) : NetworkResult<T>
}

suspend fun <T> fetch(call: suspend () -> Response<T>): NetworkResult<T> {
    try {
        val response = call()
        if (response.isSuccessful) {
            response.body()?.let { result ->
                return NetworkResult.Success(result)
            }
        }

        return NetworkResult.Error(
            exception = Exception(response.message()),
            code = response.code(),
            errorMessage = " ${response.code()} ${response.message()}"
        )
    } catch (exception: Exception) {
        return NetworkResult.Error(
            exception = exception,
            code = exception.hashCode(),
            errorMessage = exception.toString()
        )
    }
}