Skip to content

Commit

Permalink
Merge pull request #14 from rommansabbir/dev
Browse files Browse the repository at this point in the history
Fixed issue (feature update request) (#13)
  • Loading branch information
rommansabbir authored Jun 26, 2022
2 parents ff789c5 + 8cdd204 commit 30d2d35
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 48 deletions.
4 changes: 2 additions & 2 deletions NetworkX/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
group = 'rommansabbir'
version '3.1.0'
version '3.4.1'

android {
compileSdkVersion 31
Expand Down Expand Up @@ -62,7 +62,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.rommansabbir'
artifactId = 'NetworkX'
version = '3.1.0'
version = '3.4.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object NetworkXProvider {
* @param value, new connection status
*/
internal fun setConnection(value: Boolean) {
synchronized(value) {
synchronized(Any()) {
Handler(Looper.getMainLooper()).post {
connected = value
isInternetConnectedMutableLiveData.value = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.rommansabbir.networkx.exceptions.NoInternetDialogException
import com.rommansabbir.networkx.extension.getDialogInstance
import java.lang.ref.WeakReference

@Deprecated("Use new API - NoInternetDialogV2")
class NoInternetDialog private constructor(private var builder: Builder) {
companion object {
private var isDialogVisible: Boolean = false
Expand All @@ -16,6 +17,13 @@ class NoInternetDialog private constructor(private var builder: Builder) {
*
* @return [Boolean]
*/
@Deprecated(
"Use NoInternetDialogV2",
ReplaceWith(
"NoInternetDialogV2.isVisible",
imports = arrayOf("com.rommansabbir.networkx.dialog.NoInternetDialogV2")
)
)
fun isDialogVisible(): Boolean = isDialogVisible

class Builder {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.rommansabbir.networkx.dialog

import android.app.Activity
import android.app.Dialog
import com.rommansabbir.networkx.R
import com.rommansabbir.networkx.databinding.ContentDialogNoInternetBinding
import com.rommansabbir.networkx.extension.getDialogInstance
import java.lang.ref.WeakReference

class NoInternetDialogV2 constructor(
private val activity: WeakReference<Activity>,
private val title: String,
private val message: String,
private val buttonTitle: String,
private val isCancelable: Boolean,
private val callback: () -> Unit
) {
init {
activity.get()?.let {
getDialogInstance<ContentDialogNoInternetBinding>(
it,
R.layout.content_dialog_no_internet,
R.style.my_dialog,
isCancelable
) { dialog, binding ->
NoInternetDialogV2.dialog = dialog
binding.cdniBtnRetry.text = buttonTitle
binding.cdniTvTitle.text = title
binding.cdniTvMessage.text = message
NoInternetDialogV2.dialog?.setOnDismissListener {
NoInternetDialogV2.dialog = null
}
binding.cdniBtnRetry.setOnClickListener { callback.invoke();NoInternetDialogV2.dialog?.cancel() }
NoInternetDialogV2.dialog?.show()
}
} ?: kotlin.run { dialog = null }
}

companion object {
private var dialog: Dialog? = null
val isVisible: Boolean = dialog != null && dialog!!.isShowing
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rommansabbir.networkx.exceptions

@Deprecated("Deprecated. Use NetworkXProvider instead")
class NoInternetDialogException :
Exception("Activity reference is required to build an instance of NoInternetDialog") {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ inline fun <V : ViewDataBinding> getDialogInstance(
dialog.setContentView(layout.root)
dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(setCancelable)
dialog.setCancelable(false)
dialog.setCancelable(setCancelable)
onSuccess.invoke(dialog, layout)
}
11 changes: 7 additions & 4 deletions NetworkX/src/main/res/layout/content_dialog_no_internet.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
Expand Down Expand Up @@ -101,15 +102,17 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:gravity="center"
android:justificationMode="inter_word"
android:text="@string/device_is_not_connected_to_the_internet"
android:textAlignment="center"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />
app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
tools:targetApi="o" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Expand Down
2 changes: 1 addition & 1 deletion NetworkX/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="netwrokx_cancel_observing"></string>
<string name="netwrokx_cancel_observing" />
<string name="retry">Retry</string>
<string name="device_is_not_connected_to_the_internet">Device is not connected to the internet!</string>
<string name="no_internet">No internet!</string>
Expand Down
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Step 2. Add the dependency.

| Releases
| ------------- |
| 3.4.0 |
| 3.4.1 |


# Usages
Expand Down Expand Up @@ -78,27 +78,19 @@ lastKnownSpeed?.let {

---

### How to show the **`NoInternetDialog`**?
### How to show the **`NoInternetDialogV2`**?

```kotlin
NoInternetDialog
.Companion
.Builder()
// Provide activity reference
.withActivity(this)
// Provide custom title
.withTitle("No internet!")
// Provide custom mesage
.withMessage("Your device is not connected to the internet!")
// Register for callback
.withActionCallback {
// User clicked `Retry` button
}
.build()
.show()
NoInternetDialogV2(
activity = WeakReference(this@MainActivity),
title = "No Internet Bro",
message = "This is just a dummy message",
buttonTitle = "Okay",
isCancelable = true
) { /* Button Presses */ }
```

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

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package com.rommansabbir.networkobserverexample

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.rommansabbir.networkx.NetworkXProvider
import com.rommansabbir.networkx.NetworkXProvider.isInternetConnectedFlow
import com.rommansabbir.networkx.NetworkXProvider.lastKnownSpeedFlow
import com.rommansabbir.networkx.dialog.NoInternetDialogV2
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference

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

/* //
NetworkXProvider.isInternetConnectedLiveData.observe(this) { status ->
status?.let {
//
NetworkXProvider.isInternetConnectedLiveData.observe(this) {
it?.let {
textView.text = "Internet connection status: $it"
}
}*/
}

/* NetworkXProvider.lastKnownSpeed.let {
textView2.text =
Expand All @@ -51,27 +55,28 @@ class MainActivity : AppCompatActivity() {
textView.text = "Internet connection status: $it"
}
}
}
catch (e : Exception){
} catch (e: Exception) {
e.printStackTrace()
}
}
/*
NetworkXProvider.lastKnownSpeedLiveData.observe(this) {
it?.let {
textView2.text =
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
}
}
*/

/* lifecycleScope.launchWhenCreated {
NetworkXProvider.isInternetConnectedFlow.collectLatest {
lifecycleScope.launch {
textView.text = "Internet connection status: $it"
lifecycleScope.launch {
delay(5000)
if (!NoInternetDialogV2.isVisible) {
NoInternetDialogV2(
activity = WeakReference(this@MainActivity),
title = "No Internet Bro",
message = "This is just a dummy message",
buttonTitle = "Okay",
isCancelable = true
) {
Toast.makeText(
this@MainActivity,
"Is dialog cancelled? : ${!NoInternetDialogV2.isVisible}",
Toast.LENGTH_SHORT
).show()
}
}
}*/

}
}
}

0 comments on commit 30d2d35

Please sign in to comment.