Skip to content

Commit 8cdd204

Browse files
committed
Fixed issue (feature update request) (#13)
1 parent ca75a33 commit 8cdd204

File tree

10 files changed

+98
-48
lines changed

10 files changed

+98
-48
lines changed

NetworkX/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
apply plugin: 'kotlin-android'
66
apply plugin: 'kotlin-kapt'
77
group = 'rommansabbir'
8-
version '3.1.0'
8+
version '3.4.1'
99

1010
android {
1111
compileSdkVersion 31
@@ -62,7 +62,7 @@ afterEvaluate {
6262
from components.release
6363
groupId = 'com.github.rommansabbir'
6464
artifactId = 'NetworkX'
65-
version = '3.1.0'
65+
version = '3.4.1'
6666
}
6767
}
6868
}

NetworkX/src/main/java/com/rommansabbir/networkx/NetworkXProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object NetworkXProvider {
3030
* @param value, new connection status
3131
*/
3232
internal fun setConnection(value: Boolean) {
33-
synchronized(value) {
33+
synchronized(Any()) {
3434
Handler(Looper.getMainLooper()).post {
3535
connected = value
3636
isInternetConnectedMutableLiveData.value = value

NetworkX/src/main/java/com/rommansabbir/networkx/dialog/NoInternetDialog.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.rommansabbir.networkx.exceptions.NoInternetDialogException
77
import com.rommansabbir.networkx.extension.getDialogInstance
88
import java.lang.ref.WeakReference
99

10+
@Deprecated("Use new API - NoInternetDialogV2")
1011
class NoInternetDialog private constructor(private var builder: Builder) {
1112
companion object {
1213
private var isDialogVisible: Boolean = false
@@ -16,6 +17,13 @@ class NoInternetDialog private constructor(private var builder: Builder) {
1617
*
1718
* @return [Boolean]
1819
*/
20+
@Deprecated(
21+
"Use NoInternetDialogV2",
22+
ReplaceWith(
23+
"NoInternetDialogV2.isVisible",
24+
imports = arrayOf("com.rommansabbir.networkx.dialog.NoInternetDialogV2")
25+
)
26+
)
1927
fun isDialogVisible(): Boolean = isDialogVisible
2028

2129
class Builder {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.rommansabbir.networkx.dialog
2+
3+
import android.app.Activity
4+
import android.app.Dialog
5+
import com.rommansabbir.networkx.R
6+
import com.rommansabbir.networkx.databinding.ContentDialogNoInternetBinding
7+
import com.rommansabbir.networkx.extension.getDialogInstance
8+
import java.lang.ref.WeakReference
9+
10+
class NoInternetDialogV2 constructor(
11+
private val activity: WeakReference<Activity>,
12+
private val title: String,
13+
private val message: String,
14+
private val buttonTitle: String,
15+
private val isCancelable: Boolean,
16+
private val callback: () -> Unit
17+
) {
18+
init {
19+
activity.get()?.let {
20+
getDialogInstance<ContentDialogNoInternetBinding>(
21+
it,
22+
R.layout.content_dialog_no_internet,
23+
R.style.my_dialog,
24+
isCancelable
25+
) { dialog, binding ->
26+
NoInternetDialogV2.dialog = dialog
27+
binding.cdniBtnRetry.text = buttonTitle
28+
binding.cdniTvTitle.text = title
29+
binding.cdniTvMessage.text = message
30+
NoInternetDialogV2.dialog?.setOnDismissListener {
31+
NoInternetDialogV2.dialog = null
32+
}
33+
binding.cdniBtnRetry.setOnClickListener { callback.invoke();NoInternetDialogV2.dialog?.cancel() }
34+
NoInternetDialogV2.dialog?.show()
35+
}
36+
} ?: kotlin.run { dialog = null }
37+
}
38+
39+
companion object {
40+
private var dialog: Dialog? = null
41+
val isVisible: Boolean = dialog != null && dialog!!.isShowing
42+
}
43+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.rommansabbir.networkx.exceptions
22

3-
@Deprecated("Deprecated. Use NetworkXProvider instead")
43
class NoInternetDialogException :
54
Exception("Activity reference is required to build an instance of NoInternetDialog") {
65
}

NetworkX/src/main/java/com/rommansabbir/networkx/extension/ViewExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ inline fun <V : ViewDataBinding> getDialogInstance(
3030
dialog.setContentView(layout.root)
3131
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
3232
dialog.setCanceledOnTouchOutside(setCancelable)
33-
dialog.setCancelable(false)
33+
dialog.setCancelable(setCancelable)
3434
onSuccess.invoke(dialog, layout)
3535
}

NetworkX/src/main/res/layout/content_dialog_no_internet.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<layout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<layout xmlns:tools="http://schemas.android.com/tools"
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:app="http://schemas.android.com/apk/res-auto">
45

56
<data>
@@ -101,15 +102,17 @@
101102
android:layout_width="0dp"
102103
android:layout_height="wrap_content"
103104
android:layout_marginStart="32dp"
104-
android:layout_marginLeft="32dp"
105105
android:layout_marginTop="32dp"
106106
android:layout_marginEnd="32dp"
107-
android:layout_marginRight="32dp"
107+
android:gravity="center"
108+
android:justificationMode="inter_word"
108109
android:text="@string/device_is_not_connected_to_the_internet"
110+
android:textAlignment="center"
109111
android:textColor="@android:color/black"
110112
app:layout_constraintEnd_toEndOf="parent"
111113
app:layout_constraintStart_toStartOf="parent"
112-
app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />
114+
app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
115+
tools:targetApi="o" />
113116

114117
</androidx.constraintlayout.widget.ConstraintLayout>
115118
</androidx.cardview.widget.CardView>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="netwrokx_cancel_observing"></string>
3+
<string name="netwrokx_cancel_observing" />
44
<string name="retry">Retry</string>
55
<string name="device_is_not_connected_to_the_internet">Device is not connected to the internet!</string>
66
<string name="no_internet">No internet!</string>

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Step 2. Add the dependency.
3434

3535
| Releases
3636
| ------------- |
37-
| 3.4.0 |
37+
| 3.4.1 |
3838

3939

4040
# Usages
@@ -78,27 +78,19 @@ lastKnownSpeed?.let {
7878

7979
---
8080

81-
### How to show the **`NoInternetDialog`**?
81+
### How to show the **`NoInternetDialogV2`**?
8282

8383
```kotlin
84-
NoInternetDialog
85-
.Companion
86-
.Builder()
87-
// Provide activity reference
88-
.withActivity(this)
89-
// Provide custom title
90-
.withTitle("No internet!")
91-
// Provide custom mesage
92-
.withMessage("Your device is not connected to the internet!")
93-
// Register for callback
94-
.withActionCallback {
95-
// User clicked `Retry` button
96-
}
97-
.build()
98-
.show()
84+
NoInternetDialogV2(
85+
activity = WeakReference(this@MainActivity),
86+
title = "No Internet Bro",
87+
message = "This is just a dummy message",
88+
buttonTitle = "Okay",
89+
isCancelable = true
90+
) { /* Button Presses */ }
9991
```
10092

101-
* Also, you can determine if the `NoInternetDialog` is currently visible or not by calling this method `NoInternetDialog.isDialogVisible()` which return an `Boolean`.
93+
* Also, you can determine if the `NoInternetDialogV2` is currently visible or not by calling this variable `NoInternetDialogV2.isVisible` which return an `Boolean`.
10294

10395
---
10496

app/src/main/java/com/rommansabbir/networkobserverexample/MainActivity.kt

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package com.rommansabbir.networkobserverexample
22

33
import android.annotation.SuppressLint
44
import android.os.Bundle
5+
import android.widget.Toast
56
import androidx.appcompat.app.AppCompatActivity
67
import androidx.lifecycle.lifecycleScope
78
import com.rommansabbir.networkx.NetworkXProvider
89
import com.rommansabbir.networkx.NetworkXProvider.isInternetConnectedFlow
910
import com.rommansabbir.networkx.NetworkXProvider.lastKnownSpeedFlow
11+
import com.rommansabbir.networkx.dialog.NoInternetDialogV2
1012
import kotlinx.android.synthetic.main.activity_main.*
13+
import kotlinx.coroutines.delay
1114
import kotlinx.coroutines.flow.collect
1215
import kotlinx.coroutines.launch
16+
import java.lang.ref.WeakReference
1317

1418
class MainActivity : AppCompatActivity() {
1519
@SuppressLint("SetTextI18n")
@@ -20,12 +24,12 @@ class MainActivity : AppCompatActivity() {
2024
val status = NetworkXProvider.isInternetConnected
2125
textView.text = "Internet connection status: $status"
2226

23-
/* //
24-
NetworkXProvider.isInternetConnectedLiveData.observe(this) { status ->
25-
status?.let {
27+
//
28+
NetworkXProvider.isInternetConnectedLiveData.observe(this) {
29+
it?.let {
2630
textView.text = "Internet connection status: $it"
2731
}
28-
}*/
32+
}
2933

3034
/* NetworkXProvider.lastKnownSpeed.let {
3135
textView2.text =
@@ -51,27 +55,28 @@ class MainActivity : AppCompatActivity() {
5155
textView.text = "Internet connection status: $it"
5256
}
5357
}
54-
}
55-
catch (e : Exception){
58+
} catch (e: Exception) {
5659
e.printStackTrace()
5760
}
5861
}
59-
/*
60-
NetworkXProvider.lastKnownSpeedLiveData.observe(this) {
61-
it?.let {
62-
textView2.text =
63-
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
64-
}
65-
}
66-
*/
6762

68-
/* lifecycleScope.launchWhenCreated {
69-
NetworkXProvider.isInternetConnectedFlow.collectLatest {
70-
lifecycleScope.launch {
71-
textView.text = "Internet connection status: $it"
63+
lifecycleScope.launch {
64+
delay(5000)
65+
if (!NoInternetDialogV2.isVisible) {
66+
NoInternetDialogV2(
67+
activity = WeakReference(this@MainActivity),
68+
title = "No Internet Bro",
69+
message = "This is just a dummy message",
70+
buttonTitle = "Okay",
71+
isCancelable = true
72+
) {
73+
Toast.makeText(
74+
this@MainActivity,
75+
"Is dialog cancelled? : ${!NoInternetDialogV2.isVisible}",
76+
Toast.LENGTH_SHORT
77+
).show()
7278
}
7379
}
74-
}*/
75-
80+
}
7681
}
7782
}

0 commit comments

Comments
 (0)