Coder Social home page Coder Social logo

androidtools_networkmanager's Introduction

AndroidTools_NetworkManager

Personal tools, used as a template in my personal apps to manage networks connections via Retrofit.

Implementation

Retrofit

Use the latest Retrofit version.

    //    Retrofit & Gson
    def retrofit_version = '2.9.0'
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

OkHttp

Use the latest compatible OkHttp version.

    //    OkHttp
    implementation("com.squareup.okhttp3:okhttp:4.9.1")

NetworkManager

NetworkManager contains all network instances like OkHttp client and Retrofit client. Those instances use constants values:

  • Constants.NETWORK_TIMEOUT_IN_S -> const val NETWORK_TIMEOUT_IN_S = 30L
  • Constants.GSON_DATE_FORMAT -> const val GSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
object NetworkManager {

    // TODO Add network services

    private val httpClient = OkHttpClient.Builder()
        .writeTimeout(Constants.NETWORK_TIMEOUT_IN_S, TimeUnit.SECONDS)
        .readTimeout(Constants.NETWORK_TIMEOUT_IN_S, TimeUnit.SECONDS)
        // Add network interceptor
        //.addNetworkInterceptor()
        .build()

    private val gson = GsonBuilder()
        .setPrettyPrinting()
        .setDateFormat(Constants.GSON_DATE_FORMAT)
        .create()

    private val retrofit = Retrofit.Builder()
        // TODO Set up base url
        //.baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .client(httpClient)
        .build()

}

It is necessary to implement a baseUrl variable/constant with flavor or inside a constants file.

Then, implement each service as a lazy loaded variable from this template:

    val myService: MyService by lazy {
        retrofit.create(MyService::class.java)
    }

BaseDataSource

Here, we use DataSource implementations to call them into repositories layer. They extend BaseDataSource:

abstract class BaseDataSource {

    protected suspend fun <T : Any> getResult(networkCall: suspend () -> Response<T>): Result<T> {
        try {
            val response = networkCall()
            if (response.isSuccessful) {
                val body = response.body()
                return Result.Success(body)
            }
            return error(NetworkError.fromCode(response.code()))
        } catch (e: Exception) {
            // TODO Add Timber dependency
            //Timber.e(e)
            return error(NetworkError.fromException(e))
        }
    }

    private fun error(errorType: NetworkError) : Result.Error {
        // TODO Add Timber dependency
        //Timber.w(errorType.toString())
        return Result.Error(errorType)
    }

}

We can find getResult(...) that return a sealed class Result is returned. If there is an error/exception the function return a NetworkError enum via the constructor Result.Error(...).

androidtools_networkmanager's People

Contributors

gbenoitg avatar

Stargazers

โญ ๐Ÿพ avatar Christian <kimo> B. avatar

Watchers

James Cloos avatar  avatar

Forkers

kimocoder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.