Skip to content

Commit a398880

Browse files
committed
Added network error view
1 parent 13d3ba4 commit a398880

File tree

7 files changed

+150
-14
lines changed

7 files changed

+150
-14
lines changed

demo-app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111

1212
defaultConfig {
1313
applicationId "co.thepeer"
14-
minSdkVersion 21
14+
minSdkVersion 23
1515
targetSdkVersion 33
1616
versionCode 1
1717
versionName "1.0"

thepeer-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ android {
1313
compileSdkVersion 33
1414

1515
defaultConfig {
16-
minSdkVersion 21
16+
minSdkVersion 23
1717
targetSdkVersion 33
1818
versionCode 1
1919
versionName "1.0"

thepeer-android/src/main/java/co/thepeer/sdk/ui/activity/ThepeerSDKActivity.kt

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@ import android.annotation.SuppressLint
44
import android.content.Intent
55
import android.os.Build
66
import android.os.Bundle
7+
import android.webkit.WebResourceError
8+
import android.webkit.WebResourceRequest
79
import android.webkit.WebView
810
import android.webkit.WebViewClient
11+
import android.widget.Toast
912
import androidx.activity.OnBackPressedCallback
1013
import androidx.appcompat.app.AppCompatActivity
1114
import androidx.core.view.isVisible
15+
import co.thepeer.sdk.R
1216
import co.thepeer.sdk.databinding.SdkActivityBinding
1317
import co.thepeer.sdk.model.ThepeerParam
1418
import co.thepeer.sdk.model.ThepeerResult
1519
import co.thepeer.sdk.ui.fragments.ThepeerFragment
20+
import co.thepeer.sdk.utils.*
1621
import co.thepeer.sdk.utils.Logger
17-
import co.thepeer.sdk.utils.ThepeerConstants
1822
import co.thepeer.sdk.utils.Urls
1923
import co.thepeer.sdk.utils.WebInterface
2024

2125
class ThepeerSDKActivity : AppCompatActivity() {
2226
private var params: ThepeerParam? = null
27+
private var url: String = ""
2328
private lateinit var binding: SdkActivityBinding
29+
private var isLoaded = false
2430
override fun onCreate(savedInstanceState: Bundle?) {
2531
super.onCreate(savedInstanceState)
2632
binding = SdkActivityBinding.inflate(layoutInflater)
@@ -36,19 +42,17 @@ class ThepeerSDKActivity : AppCompatActivity() {
3642
intent.getParcelableExtra<ThepeerParam>(ThepeerConstants.THE_PEER_PARAMS)
3743
}
3844

39-
4045
if (params == null) {
4146
throw IllegalStateException("Params should not be null. Initialize thePeer using this function Thepeer.Builder(...)")
4247
}
4348

49+
4450
params?.let {
45-
val url = Urls.createTransactionUrl(it)
46-
setupWebView(url)
51+
url = Urls.createTransactionUrl(it)
52+
showWebView()
4753
Logger.log(this, url)
4854
}
4955

50-
51-
5256
}
5357

5458
@SuppressLint("SetJavaScriptEnabled")
@@ -63,8 +67,20 @@ class ThepeerSDKActivity : AppCompatActivity() {
6367
binding.webViewPeer.webViewClient = object : WebViewClient() {
6468
override fun onPageFinished(view: WebView, url: String) {
6569
super.onPageFinished(view, url)
70+
isLoaded = true
6671
binding.webViewPeer.isVisible = true
6772
}
73+
74+
override fun onReceivedError(
75+
view: WebView?,
76+
request: WebResourceRequest?,
77+
error: WebResourceError?
78+
) {
79+
if (error != null) {
80+
showNetworkErrorRetryView()
81+
}
82+
}
83+
6884
}
6985

7086

@@ -80,10 +96,56 @@ class ThepeerSDKActivity : AppCompatActivity() {
8096

8197
override fun onDestroy() {
8298
super.onDestroy()
99+
clearWebViewValues()
100+
redirectWithResult(ThepeerResult.Cancelled)
101+
}
102+
103+
private fun clearWebViewValues() {
83104
params = null
84105
binding.webViewPeer.loadUrl("about:blank")
85106
binding.webViewPeer.clearHistory()
86-
redirectWithResult(ThepeerResult.Cancelled)
107+
}
108+
109+
private fun showNetworkErrorRetryView() {
110+
if (!isNetworkConnectionAvailable) {
111+
if (!isLoaded) {
112+
hideWebView()
113+
binding.includeRetryView.retryView.isVisible = true
114+
} else {
115+
Toast.makeText(this, getString(R.string.retry_text), Toast.LENGTH_LONG)
116+
.show()
117+
}
118+
119+
} else {
120+
Toast.makeText(this, "Something went wrong. Contact thePeer support", Toast.LENGTH_LONG)
121+
.show()
122+
}
123+
124+
binding.includeRetryView.retryButton.setOnClickListener {
125+
if (isNetworkConnectionAvailable && url.isNotBlank()) {
126+
hideRetryView()
127+
setupWebView(url)
128+
}
129+
}
130+
}
131+
132+
private fun hideRetryView(){
133+
binding.includeRetryView.retryView.isVisible = false
134+
binding.loading.isVisible = true
135+
}
136+
137+
private fun showWebView() {
138+
binding.loading.isVisible = true
139+
if (isNetworkConnectionAvailable) {
140+
setupWebView(url)
141+
} else {
142+
showNetworkErrorRetryView()
143+
}
144+
}
145+
146+
private fun hideWebView() {
147+
binding.webViewPeer.isVisible = false
148+
binding.loading.isVisible = false
87149
}
88150

89151
private fun redirectWithResult(result: ThepeerResult) {
@@ -97,6 +159,9 @@ class ThepeerSDKActivity : AppCompatActivity() {
97159
object : OnBackPressedCallback(true) {
98160
override fun handleOnBackPressed() {
99161
params = null
162+
if(!isNetworkConnectionAvailable){
163+
finish()
164+
}
100165
}
101166
}
102167

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
package co.thepeer.sdk.utils
22

3+
import android.content.Context
4+
import android.net.ConnectivityManager
5+
import android.net.NetworkCapabilities
6+
37
fun String.getLastPart() = this.split(".")[1]
48
fun String.getFirstPart() = this.split(".")[0]
9+
10+
/**
11+
* [returns] true if the device is connected to the internet. False otherwise.
12+
*/
13+
val Context.isNetworkConnectionAvailable: Boolean
14+
get() {
15+
val connectivityManager =
16+
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
17+
val nw = connectivityManager.activeNetwork ?: return false
18+
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false
19+
return when {
20+
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
21+
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
22+
else -> false
23+
}
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/retry_view"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent">
8+
9+
<Button
10+
android:id="@+id/retry_button"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_marginTop="16dp"
14+
android:text="@string/retry"
15+
android:background="@color/colorPrimary"
16+
android:textColor="@color/unclearWhite"
17+
app:layout_constraintEnd_toEndOf="@+id/textView"
18+
app:layout_constraintStart_toStartOf="@+id/textView"
19+
app:layout_constraintTop_toBottomOf="@+id/textView" />
20+
21+
<TextView
22+
android:id="@+id/textView"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_marginStart="24dp"
26+
android:layout_marginEnd="24dp"
27+
android:text="@string/retry_text"
28+
app:layout_constraintBottom_toBottomOf="parent"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintStart_toStartOf="parent"
31+
app:layout_constraintTop_toTopOf="parent"
32+
app:layout_constraintVertical_bias="0.5" />
33+
34+
</androidx.constraintlayout.widget.ConstraintLayout>

thepeer-android/src/main/res/layout/sdk_activity.xml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,38 @@
1717
android:layout_width="24dp"
1818
android:layout_height="24dp"
1919
android:background="@null"
20-
app:layout_constraintTop_toTopOf="parent"
20+
android:visibility="gone"
2121
app:layout_constraintBottom_toBottomOf="parent"
22+
app:layout_constraintEnd_toEndOf="parent"
2223
app:layout_constraintStart_toStartOf="parent"
23-
app:layout_constraintEnd_toEndOf="parent" />
24+
app:layout_constraintTop_toTopOf="parent" />
2425

2526
<WebView
2627
android:id="@+id/webViewPeer"
2728
android:layout_width="0dp"
2829
android:layout_height="0dp"
2930
android:layout_marginTop="10dp"
3031
android:layout_marginBottom="10dp"
31-
app:layout_constraintTop_toTopOf="parent"
32+
android:visibility="gone"
3233
app:layout_constraintBottom_toBottomOf="parent"
33-
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintEnd_toEndOf="parent"
3435
app:layout_constraintHorizontal_bias="0.5"
36+
app:layout_constraintStart_toStartOf="parent"
37+
app:layout_constraintTop_toTopOf="parent" />
38+
39+
<include
40+
android:id="@+id/include_retry_view"
41+
layout="@layout/retry_view"
42+
android:layout_width="0dp"
43+
android:layout_height="0dp"
44+
android:layout_marginTop="10dp"
45+
android:layout_marginBottom="10dp"
46+
android:visibility="gone"
47+
app:layout_constraintBottom_toBottomOf="parent"
3548
app:layout_constraintEnd_toEndOf="parent"
36-
android:visibility="gone" />
49+
app:layout_constraintHorizontal_bias="0.5"
50+
app:layout_constraintStart_toStartOf="parent"
51+
app:layout_constraintTop_toTopOf="parent" />
3752

3853
</androidx.constraintlayout.widget.ConstraintLayout>
3954
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
<string name="retry_text">Oops, Check your internet connection and Retry</string>
4+
<string name="retry">Retry</string>
35
</resources>

0 commit comments

Comments
 (0)