File tree Expand file tree Collapse file tree 5 files changed +74
-0
lines changed
java/org/buffer/android/utils Expand file tree Collapse file tree 5 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org.buffer.android.utils
2
+
3
+ import android.annotation.SuppressLint
4
+ import android.content.Context
5
+ import android.net.ConnectivityManager
6
+
7
+ object ConnectivityUtil {
8
+
9
+ @SuppressLint(" MissingPermission" )
10
+ fun checkConnectivity (context : Context ): Boolean {
11
+ val cm = context.getSystemService(Context .CONNECTIVITY_SERVICE ) as ConnectivityManager
12
+ val activeNetwork = cm.activeNetworkInfo
13
+ return activeNetwork != null && activeNetwork.isConnectedOrConnecting
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package org.buffer.android.utils
2
+
3
+ import android.content.Context
4
+ import org.buffer.android.utils.model.ErrorResponse
5
+
6
+ open class ErrorUtil {
7
+
8
+ fun extractErrorMessage (
9
+ context : Context ,
10
+ throwable : Throwable ? ,
11
+ fallback : String
12
+ ): String {
13
+ return if (throwable is ErrorResponse ) {
14
+ return throwable.getErrorMessage()?.let {
15
+ if (it.isNotEmpty()) {
16
+ it
17
+ } else {
18
+ handleGenericMessage(context, fallback)
19
+ }
20
+ } ? : run {
21
+ handleGenericMessage(context, fallback)
22
+ }
23
+ } else {
24
+ handleGenericMessage(context, fallback)
25
+ }
26
+ }
27
+
28
+ private fun handleGenericMessage (
29
+ context : Context ,
30
+ fallback : String
31
+ ): String {
32
+ return if (ConnectivityUtil .checkConnectivity(context)) {
33
+ fallback
34
+ } else {
35
+ context.getString(R .string.error_message_internet_connect)
36
+ }
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ package org.buffer.android.utils.model
2
+
3
+ class ErrorResponse @JvmOverloads constructor(
4
+ var code : String? = null ,
5
+ var error : String? = null ,
6
+ override var message : String? = null
7
+ ) : Throwable() {
8
+
9
+ fun getErrorMessage (): String? {
10
+ return if (! error.isNullOrEmpty()) {
11
+ error
12
+ } else {
13
+ message
14
+ }
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ <resources >
2
+ <string name =" error_message_internet_connect" >The internet connection appears to be offline.</string >
3
+ </resources >
Original file line number Diff line number Diff line change 1
1
<resources >
2
2
<string name =" app_name" >Android Utils</string >
3
+
4
+ <string name =" error_message_internet_connect" >The internet connection appears to be offline.</string >
3
5
</resources >
You can’t perform that action at this time.
0 commit comments