Skip to content

Commit bad1a50

Browse files
committed
Add httpProfiler
1 parent 49ecb32 commit bad1a50

File tree

10 files changed

+41
-33
lines changed

10 files changed

+41
-33
lines changed

app/build.gradle.kts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ android {
2020
applicationId = "com.example.moviedb"
2121
buildToolsVersion("30.0.3")
2222
minSdkVersion(23)
23-
compileSdkVersion(30)
24-
targetSdkVersion(30)
23+
compileSdkVersion(31)
24+
targetSdkVersion(31)
2525
multiDexEnabled = true
2626
vectorDrawables {
2727
useSupportLibrary = true
@@ -192,6 +192,10 @@ dependencies {
192192
implementation("com.squareup.retrofit2:converter-moshi:2.9.0")
193193
implementation("com.squareup.okhttp3:logging-interceptor:4.8.0")
194194

195+
// OkHttpProfiler
196+
// https://github.com/itkacher/OkHttpProfiler
197+
implementation("com.localebro:okhttpprofiler:1.0.8")
198+
195199
// stetho
196200
// http://facebook.github.io/stetho/
197201
implementation("com.facebook.stetho:stetho:1.5.1")

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<activity
3030
android:name="com.example.moviedb.ui.screen.main.MainActivity"
3131
android:configChanges="orientation|screenSize"
32+
android:exported="true"
3233
android:theme="@style/Theme.MyApplication.NoActionBar">
3334
<intent-filter>
3435
<action android:name="android.intent.action.MAIN" />
@@ -38,7 +39,9 @@
3839
</intent-filter>
3940
</activity>
4041

41-
<activity android:name=".ui.screen.permission.PermissionActivity">
42+
<activity
43+
android:name=".ui.screen.permission.PermissionActivity"
44+
android:exported="false">
4245
<!--<intent-filter>
4346
<action android:name="android.intent.action.MAIN" />
4447
<action android:name="android.intent.action.VIEW" />

app/src/main/java/com/example/moviedb/MainApplication.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.os.Looper
66
import android.widget.Toast
77
import androidx.multidex.MultiDex
88
import com.example.moviedb.ui.screen.main.MainActivity
9+
import com.facebook.stetho.Stetho
910
import dagger.hilt.android.HiltAndroidApp
1011
import kotlinx.coroutines.CoroutineScope
1112
import kotlinx.coroutines.SupervisorJob
@@ -30,9 +31,9 @@ class MainApplication : Application() {
3031
Timber.plant(Timber.DebugTree())
3132

3233
// init stetho
33-
// Stetho.initializeWithDefaults(this)
34+
Stetho.initializeWithDefaults(this)
3435
} else {
35-
handleUncaughtException()
36+
// handleUncaughtException()
3637
}
3738
}
3839

app/src/main/java/com/example/moviedb/data/remote/ErrorHandlingFactory.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.example.moviedb.data.remote
22

3-
import com.example.moviedb.utils.safeLog
43
import com.squareup.moshi.Moshi
54
import retrofit2.HttpException
65
import retrofit2.Response
6+
import timber.log.Timber
77
import java.io.IOException
88

99
/*
@@ -102,7 +102,7 @@ fun Throwable.toBaseException(): BaseException {
102102
val serverErrorResponseBody = try {
103103
response.errorBody()?.string() ?: ""
104104
} catch (e: Exception) {
105-
e.safeLog()
105+
Timber.e(e)
106106
""
107107
}
108108

@@ -111,7 +111,7 @@ fun Throwable.toBaseException(): BaseException {
111111
Moshi.Builder().build().adapter(ServerErrorResponse::class.java)
112112
.fromJson(serverErrorResponseBody)
113113
} catch (e: Exception) {
114-
e.safeLog()
114+
Timber.e(e)
115115
ServerErrorResponse()
116116
}
117117

app/src/main/java/com/example/moviedb/di/NetworkModule.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import com.example.moviedb.BuildConfig
66
import com.example.moviedb.data.remote.ApiService
77
import com.example.moviedb.data.remote.api.MockInterceptor
88
import com.example.moviedb.enableLogging
9+
import com.facebook.stetho.okhttp3.StethoInterceptor
10+
import com.localebro.okhttpprofiler.OkHttpProfilerInterceptor
911
import com.squareup.moshi.Moshi
1012
import dagger.Module
1113
import dagger.Provides
1214
import dagger.hilt.InstallIn
1315
import dagger.hilt.components.SingletonComponent
16+
import okhttp3.Authenticator
1417
import okhttp3.Cache
1518
import okhttp3.Interceptor
1619
import okhttp3.OkHttpClient
@@ -66,21 +69,21 @@ class NetworkModule {
6669
@Singleton
6770
@Provides
6871
fun provideOkHttpClient(
72+
@Named("logging") logging: Interceptor,
6973
@Named("header") header: Interceptor,
74+
// authenticator: Authenticator,
7075
@Named("mock") mockInterceptor: MockInterceptor
7176
): OkHttpClient =
7277
OkHttpClient.Builder()
7378
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
7479
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
7580
.writeTimeout(TIMEOUT, TimeUnit.SECONDS)
76-
.addInterceptor(header)
7781
.apply {
78-
// .addNetworkInterceptor(StethoInterceptor())
79-
if (enableLogging()) {
80-
val loggingInterceptor = HttpLoggingInterceptor()
81-
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
82-
addInterceptor(loggingInterceptor)
83-
}
82+
addInterceptor(logging)
83+
if (BuildConfig.DEBUG) addNetworkInterceptor(StethoInterceptor())
84+
if (BuildConfig.DEBUG) addInterceptor(OkHttpProfilerInterceptor())
85+
addInterceptor(header)
86+
// authenticator(authenticator)
8487
if (BuildConfig.DEBUG && BuildConfig.MOCK_DATA) addInterceptor(mockInterceptor)
8588
}
8689
.build()

app/src/main/java/com/example/moviedb/ui/base/BaseViewModel.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import androidx.lifecycle.MutableLiveData
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import com.example.moviedb.data.remote.toBaseException
7-
import com.example.moviedb.utils.SingleLiveEvent
7+
import com.example.moviedb.utils.SingleLiveData
88
import kotlinx.coroutines.CoroutineExceptionHandler
99
import kotlinx.coroutines.launch
1010
import kotlinx.coroutines.plus
@@ -19,14 +19,14 @@ open class BaseViewModel : ViewModel() {
1919
val isLoading by lazy { MutableLiveData(false) }
2020

2121
// error message
22-
val errorMessage by lazy { SingleLiveEvent<String>() }
22+
val errorMessage by lazy { SingleLiveData<String>() }
2323

2424
// optional flags
25-
val noInternetConnectionEvent by lazy { SingleLiveEvent<Unit>() }
26-
val connectTimeoutEvent by lazy { SingleLiveEvent<Unit>() }
27-
val forceUpdateAppEvent by lazy { SingleLiveEvent<Unit>() }
28-
val serverMaintainEvent by lazy { SingleLiveEvent<Unit>() }
29-
val unknownErrorEvent by lazy { SingleLiveEvent<Unit>() }
25+
val noInternetConnectionEvent by lazy { SingleLiveData<Unit>() }
26+
val connectTimeoutEvent by lazy { SingleLiveData<Unit>() }
27+
val forceUpdateAppEvent by lazy { SingleLiveData<Unit>() }
28+
val serverMaintainEvent by lazy { SingleLiveData<Unit>() }
29+
val unknownErrorEvent by lazy { SingleLiveData<Unit>() }
3030

3131
// exception handler for coroutine
3232
private val exceptionHandler by lazy {

app/src/main/java/com/example/moviedb/ui/widgets/EndlessRecyclerOnScrollListener.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import androidx.recyclerview.widget.GridLayoutManager
44
import androidx.recyclerview.widget.LinearLayoutManager
55
import androidx.recyclerview.widget.RecyclerView
66
import com.example.moviedb.data.constants.Constants
7-
import com.example.moviedb.utils.safeLog
7+
import timber.log.Timber
88

99
abstract class EndlessRecyclerOnScrollListener(
1010
threshold: Int = Constants.DEFAULT_NUM_VISIBLE_THRESHOLD
@@ -34,7 +34,7 @@ abstract class EndlessRecyclerOnScrollListener(
3434
firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
3535
}
3636
else -> {
37-
Exception("Unsupported LayoutManage").safeLog()
37+
Timber.e("Unsupported LayoutManage")
3838
}
3939
}
4040

app/src/main/java/com/example/moviedb/utils/DateUtils.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.moviedb.utils
22

3+
import timber.log.Timber
34
import java.text.SimpleDateFormat
45
import java.util.*
56

@@ -16,7 +17,7 @@ fun String.toDate(
1617
return try {
1718
SimpleDateFormat(format, locale).parse(this)
1819
} catch (e: Exception) {
19-
e.safeLog()
20+
Timber.e(e)
2021
null
2122
}
2223
}
@@ -42,7 +43,7 @@ fun Long.toTimeString(
4243
return try {
4344
SimpleDateFormat(format, locale).format(Date(this))
4445
} catch (e: Exception) {
45-
e.safeLog()
46+
Timber.e(e)
4647
null
4748
}
4849
}
@@ -64,7 +65,7 @@ fun String.changeTimeFormat(
6465
if (date != null) simpleDateFormat.format(date)
6566
else null
6667
} catch (e: Exception) {
67-
e.safeLog()
68+
Timber.e(e)
6869
null
6970
}
7071
}
@@ -79,7 +80,7 @@ fun Date.toTimeString(format: String, locale: Locale = Locale.getDefault()): Str
7980
else try {
8081
SimpleDateFormat(format, locale).format(this)
8182
} catch (e: Exception) {
82-
e.safeLog()
83+
Timber.e(e)
8384
null
8485
}
8586
}

app/src/main/java/com/example/moviedb/utils/Extensions.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import java.io.IOException
2222

2323
// ## View
2424

25-
fun Exception.safeLog() {
26-
if (enableLogging()) printStackTrace()
27-
}
28-
2925
/**
3026
* Extension method to provide simpler access to {@link View#getResources()#getString(int)}.
3127
*/

app/src/main/java/com/example/moviedb/utils/SingleLiveEvent.kt renamed to app/src/main/java/com/example/moviedb/utils/SingleLiveData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
1010
* Description: Custom mutable live data that used for single event
1111
* such as navigation (for configuration change), show toast..
1212
*/
13-
class SingleLiveEvent<T> : MutableLiveData<T>() {
13+
class SingleLiveData<T> : MutableLiveData<T>() {
1414

1515
private val pending = AtomicBoolean(false)
1616

0 commit comments

Comments
 (0)