Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
update: FrogoApiClient.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
amirisback committed Jul 26, 2021
1 parent 81158b1 commit 2c8af8d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

implementation 'com.google.code.gson:gson:2.8.6'

implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation "com.squareup.retrofit2:adapter-rxjava:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.9.0"

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.19'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/com/frogobox/frogosdk/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.frogobox.frogosdk

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.frogobox.frogosdk.core.FrogoActivity
import com.frogobox.frogosdk.databinding.ActivityMainBinding

class MainActivity : FrogoActivity<ActivityMainBinding>() {

override fun setupViewBinding(): ActivityMainBinding {
return ActivityMainBinding.inflate(layoutInflater)
}

override fun setupViewModel() {
}

override fun setupUI(savedInstanceState: Bundle?) {

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
Expand Down
3 changes: 1 addition & 2 deletions frogosdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frogobox.frogosdk">

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

</manifest>
40 changes: 25 additions & 15 deletions frogosdk/src/main/java/com/frogobox/frogosdk/core/FrogoApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.frogobox.frogosdk.core

import com.frogobox.frogosdk.FrogoApplication
import android.content.Context
import com.readystatesoftware.chuck.ChuckInterceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -24,30 +24,40 @@ import java.util.concurrent.TimeUnit

object FrogoApiClient {

inline fun <reified T> create(url: String, debug: Boolean): T {
inline fun <reified T> create(url: String): T {
val mClient = OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build()

return Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(mClient)
.build().create(T::class.java)

}

inline fun <reified T> create(url: String, context: Context): T {

val mLoggingInterceptor = HttpLoggingInterceptor()
mLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

val mClient = if (debug) {
OkHttpClient.Builder()
.addInterceptor(mLoggingInterceptor)
.addInterceptor(ChuckInterceptor(FrogoApplication.getContext()))
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build()
} else {
OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build()
}
val mClient = OkHttpClient.Builder()
.addInterceptor(mLoggingInterceptor)
.addInterceptor(ChuckInterceptor(context))
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build()

return Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(mClient)
.build().create(T::class.java)

}

}

0 comments on commit 2c8af8d

Please sign in to comment.