Skip to content

Commit 05ba1c0

Browse files
authored
Add required utils for auth lib (#3)
1 parent b5abe4c commit 05ba1c0

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="error_message_internet_connect">The internet connection appears to be offline.</string>
3+
</resources>

lib/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<resources>
22
<string name="app_name">Android Utils</string>
3+
4+
<string name="error_message_internet_connect">The internet connection appears to be offline.</string>
35
</resources>

0 commit comments

Comments
 (0)